Skip to content
Permalink
Browse files

add process tests

  • Loading branch information
samdoshi committed Apr 9, 2016
1 parent f524aa0 commit 61bddfb08a29fe8fe2cb155ca85ebfb5cf62aa4c
Showing with 130 additions and 3 deletions.
  1. +5 −1 tests/Makefile
  2. +36 −0 tests/main.c
  3. +2 −2 tests/parser_tests.h
  4. +79 −0 tests/process_tests.c
  5. +8 −0 tests/process_tests.h
@@ -1,7 +1,9 @@
.PHONY: clean format test
CFLAGS = -std=c99 -g -Wall -DSIM -I../src -I../libavr32/src

tests: main.o parser_tests.o ../src/teletype.o ../src/table.o ../src/euclidean/data.o ../src/euclidean/euclidean.o ../libavr32/src/util.o
tests: main.o parser_tests.o process_tests.o \
../src/teletype.o ../src/table.o ../src/euclidean/data.o ../src/euclidean/euclidean.o \
../libavr32/src/util.o
$(CC) -o $@ $^ $(CFLAGS)

test: tests
@@ -18,3 +20,5 @@ format:
clang-format -style=file -i main.c
clang-format -style=file -i parser_tests.h
clang-format -style=file -i parser_tests.c
clang-format -style=file -i process_tests.h
clang-format -style=file -i process_tests.c
@@ -1,13 +1,49 @@
#include <stdint.h>

#include "greatest/greatest.h"

#include "teletype.h"

#include "parser_tests.h"
#include "process_tests.h"

void tele_metro(int16_t m, int16_t m_act, uint8_t m_reset) {}
void tele_tr(uint8_t i, int16_t v) {}
void tele_cv(uint8_t i, int16_t v, uint8_t s) {}
void tele_cv_slew(uint8_t i, int16_t v) {}
void tele_delay(uint8_t i) {}
void tele_s(uint8_t i) {}
void tele_cv_off(uint8_t i, int16_t v) {}
void tele_ii(uint8_t i, int16_t d) {}
void tele_scene(uint8_t i) {}
void tele_pi() {}
void tele_script(uint8_t a) {}
void tele_kill() {}
void tele_mute(uint8_t i, uint8_t s) {}
void tele_input_state(uint8_t n) {}

GREATEST_MAIN_DEFS();

int main(int argc, char **argv) {
update_metro = &tele_metro;
update_tr = &tele_tr;
update_cv = &tele_cv;
update_cv_slew = &tele_cv_slew;
update_delay = &tele_delay;
update_s = &tele_s;
update_cv_off = &tele_cv_off;
update_ii = &tele_ii;
update_scene = &tele_scene;
update_pi = &tele_pi;
run_script = &tele_script;
update_kill = &tele_kill;
update_mute = &tele_mute;
update_input = &tele_input_state;

GREATEST_MAIN_BEGIN();

RUN_SUITE(parser_suite);
RUN_SUITE(process_suite);

GREATEST_MAIN_END();
}
@@ -1,5 +1,5 @@
#ifndef _PARSER_TEST_H_
#define _PARSER_TEST_H_
#ifndef _PARSER_TESTS_H_
#define _PARSER_TESTS_H_

#include "greatest/greatest.h"

@@ -0,0 +1,79 @@
#include "process_tests.h"

#include "greatest/greatest.h"

#include "teletype.h"

// runs multiple lines of commands and then asserts that the final answer is
// correct
TEST process_helper(size_t n, char* lines[], int16_t answer) {
process_result_t result = {.h = PR_EMPTY, .v = 0 };
for (size_t i = 0; i < n; i++) {
tele_command_t cmd;
error_t error = parse(lines[i], &cmd);
if (error != E_OK) { FAIL(); }
if (validate(&cmd) != E_OK) { FAIL(); }
result = process(&cmd);
}
ASSERT_EQ(PR_VALUE, result.h);
ASSERT_EQ(answer, result.v);

PASS();
}

TEST test_ADD() {
// beware of global state!!!
char* test1[1] = { "ADD 5 6" };
CHECK_CALL(process_helper(1, test1, 11));

char* test2[1] = { "ADD 2 -2" };
CHECK_CALL(process_helper(1, test2, 0));

PASS();
}

TEST test_P() {
// beware of global state!!!
char* test1[2] = { "P 0 1", "P 0" };
CHECK_CALL(process_helper(2, test1, 1));

char* test2[2] = { "P 0 2", "P 0" };
CHECK_CALL(process_helper(2, test2, 2));

PASS();
}

TEST test_PN() {
// beware of global state!!!
char* test1[2] = { "PN 0 0 1", "PN 0 0" };
CHECK_CALL(process_helper(2, test1, 1));

char* test2[2] = { "PN 0 0 2", "PN 0 0" };
CHECK_CALL(process_helper(2, test2, 2));

char* test3[3] = { "P.N 0", "P 0 3", "PN 0 0" };
CHECK_CALL(process_helper(3, test3, 3));

char* test4[3] = { "P.N 0", "PN 0 0 4", "P 0" };
CHECK_CALL(process_helper(3, test4, 4));

PASS();
}

TEST test_X() {
// beware of global state!!!
char* test1[2] = { "X 0", "X" };
CHECK_CALL(process_helper(2, test1, 0));

char* test2[2] = { "X 10", "X" };
CHECK_CALL(process_helper(2, test2, 10));

PASS();
}

SUITE(process_suite) {
RUN_TEST(test_ADD);
RUN_TEST(test_P);
RUN_TEST(test_PN);
RUN_TEST(test_X);
}
@@ -0,0 +1,8 @@
#ifndef _PROCESS_TESTS_H_
#define _PROCESS_TESTS_H_

#include "greatest/greatest.h"

SUITE_EXTERN(process_suite);

#endif

0 comments on commit 61bddfb

Please sign in to comment.