feat: add while loops
This commit is contained in:
parent
14717958c6
commit
538205e8a5
29 changed files with 1248 additions and 282 deletions
|
|
@ -1,11 +1,15 @@
|
|||
#include "./test_utils.h"
|
||||
#include "./test_evaluation.h"
|
||||
#include "./test_line_processing.h"
|
||||
#include "./test_stack.h"
|
||||
#include "./test_var_store.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("== UNIT TESTS == \n");
|
||||
test_var_store();
|
||||
test_stack();
|
||||
test_utils();
|
||||
test_evaluation();
|
||||
test_line_processing();
|
||||
|
|
|
|||
|
|
@ -7,28 +7,112 @@
|
|||
#include "../src/evaluator.h"
|
||||
#include "../src/state.h"
|
||||
#include "../src/var_store.h"
|
||||
#include "../src/line_processing.h"
|
||||
|
||||
void test_line_processing()
|
||||
{
|
||||
printf("== test line processing == \n");
|
||||
|
||||
struct StateContainer* state = state_init();
|
||||
// test string manipulation
|
||||
assert(!recognize_word("", "hello"));
|
||||
assert(!recognize_word(" hell", "hello"));
|
||||
assert(recognize_word("hello", "hello"));
|
||||
assert(recognize_word("hello ", "hello"));
|
||||
assert(recognize_word(" hello ", "hello"));
|
||||
assert(recognize_word(" hello ", "hello"));
|
||||
assert(recognize_word(" hello ", "hello"));
|
||||
|
||||
assert(process_line(state, "#"));
|
||||
assert(process_line(state, "# some random comment"));
|
||||
assert(process_line(state, "set VAR_A to 8.5"));
|
||||
printf("%d \n", var_store_get_int(state->varStore, "VAR_A"));
|
||||
assert(float_almost_equal(8.5, var_store_get_float(state->varStore, "VAR_A")));
|
||||
assert(recognize_termination_keyword("end", "01 end", 2));
|
||||
assert(!recognize_termination_keyword("end", "01 end == something end", 2));
|
||||
assert(!recognize_termination_keyword("end", "xend", 0));
|
||||
assert(!recognize_termination_keyword("end", "end", 0));
|
||||
assert(recognize_termination_keyword("end", " end", 0));
|
||||
|
||||
assert(process_line(state, "set VAR_B to 1.5"));
|
||||
assert(float_almost_equal(1.5, var_store_get_float(state->varStore, "VAR_B")));
|
||||
struct SetStatement res;
|
||||
assert(recognize_set_statement("set x to 1", &res));
|
||||
assert(recognize_set_statement("set someBigThings_yes to (wow)+yes", &res));
|
||||
assert(!recognize_set_statement("set to ", &res));
|
||||
assert(!recognize_set_statement("set thing uo to yes", &res));
|
||||
assert(recognize_set_statement("set varToThing_to to yes", &res));
|
||||
|
||||
assert(process_line(state, "set VAR_C to 142"));
|
||||
assert(142 == var_store_get_int(state->varStore, "VAR_C"));
|
||||
struct IfStatement resIf;
|
||||
assert(recognize_if_statement("if something is not cool then", &resIf));
|
||||
assert(recognize_if_statement("if some_then_var then", &resIf));
|
||||
|
||||
assert(process_line(state, "set VAR_D to VAR_A+VAR_B"));
|
||||
assert(float_almost_equal(10, var_store_get_float(state->varStore, "VAR_D")));
|
||||
struct WhileStatement resWhile;
|
||||
assert(recognize_while_statement("while i < 10 do", &resWhile));
|
||||
assert(recognize_while_statement("while !(i = 10) do", &resWhile));
|
||||
assert(!recognize_while_statement(" while 1 then", &resWhile));
|
||||
|
||||
assert(process_line(state, "set VAR_D to VAR_A+VAR_B"));
|
||||
assert(float_almost_equal(10, var_store_get_float(state->varStore, "VAR_D")));
|
||||
struct StateContainer* state1 = state_init();
|
||||
|
||||
assert(process_line(state1, "#"));
|
||||
assert(process_line(state1, "# some random comment"));
|
||||
assert(process_line(state1, "set VAR_A to 8.5"));
|
||||
printf("%d \n", var_store_get_int(state1->varStore, "VAR_A"));
|
||||
assert(float_almost_equal(8.5, var_store_get_float(state1->varStore, "VAR_A")));
|
||||
|
||||
assert(process_line(state1, "set VAR_B to 1.5"));
|
||||
assert(float_almost_equal(1.5, var_store_get_float(state1->varStore, "VAR_B")));
|
||||
|
||||
assert(process_line(state1, "set VAR_C to 142"));
|
||||
assert(142 == var_store_get_int(state1->varStore, "VAR_C"));
|
||||
|
||||
assert(process_line(state1, "set VAR_D to VAR_A+VAR_B"));
|
||||
assert(float_almost_equal(10, var_store_get_float(state1->varStore, "VAR_D")));
|
||||
|
||||
assert(process_line(state1, "set VAR_D to VAR_A+VAR_B"));
|
||||
assert(float_almost_equal(10, var_store_get_float(state1->varStore, "VAR_D")));
|
||||
|
||||
assert(process_line(state1, "if VAR_D = 19 & 1 then"));
|
||||
|
||||
|
||||
char* lines1 = "# hello world\n"
|
||||
"set x to 5\n"
|
||||
"set y to 1\n"
|
||||
"if (x+y) = 6 then\n"
|
||||
" set res to (1024)\n"
|
||||
"end\n"
|
||||
"\n";
|
||||
struct StateContainer* state2 = state_init();
|
||||
process_script(state2, lines1);
|
||||
|
||||
assert(!state2->skipping);
|
||||
assert(1024 == var_store_get_int(state2->varStore, "res"));
|
||||
assert(5 == var_store_get_int(state2->varStore, "x"));
|
||||
assert(1 == var_store_get_int(state2->varStore, "y"));
|
||||
|
||||
char* lines2 = "set x to 5\n"
|
||||
"set y to 1\n"
|
||||
"set z to -1\n"
|
||||
"if (x+y) = 6 then\n"
|
||||
" if z = -1 then\n"
|
||||
" set res to (1024)\n"
|
||||
" end\n"
|
||||
" set res to (res+1)\n"
|
||||
"end\n";
|
||||
process_script(state2, lines2);
|
||||
|
||||
assert(!state2->skipping);
|
||||
assert(1025 == var_store_get_int(state2->varStore, "res"));
|
||||
|
||||
char* lines3 = "set res to -42\n"
|
||||
"if 0 then\n"
|
||||
" set res to 42\n"
|
||||
"end\n"
|
||||
"\n";
|
||||
process_script(state2, lines3);
|
||||
|
||||
assert(!state2->skipping);
|
||||
assert(-42 == var_store_get_int(state2->varStore, "res"));
|
||||
|
||||
char* lines4 = "set i to 0\n"
|
||||
"while !(i = 9) do\n"
|
||||
" set i to i+1\n"
|
||||
"end\n";
|
||||
process_script(state2, lines4);
|
||||
|
||||
assert(!state2->skipping);
|
||||
printf("got i: %d \n", var_store_get_int(state2->varStore, "i"));
|
||||
assert(9 == var_store_get_int(state2->varStore, "i"));
|
||||
}
|
||||
|
|
|
|||
39
tests/test_stack.c
Normal file
39
tests/test_stack.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "../src/stack.h"
|
||||
#include "../src/utils.h"
|
||||
|
||||
void test_stack()
|
||||
{
|
||||
printf("== test stack == \n");
|
||||
int res;
|
||||
|
||||
struct IntStack* stack1 = int_stack_init();
|
||||
assert(int_stack_push(stack1, 255));
|
||||
assert(1 == int_stack_length(stack1));
|
||||
assert(int_stack_push(stack1, 742));
|
||||
assert(2 == int_stack_length(stack1));
|
||||
|
||||
assert(int_stack_pop(stack1, &res));
|
||||
assert(742 == res);
|
||||
assert(1 == int_stack_length(stack1));
|
||||
|
||||
assert(int_stack_push(stack1, 0));
|
||||
assert(int_stack_push(stack1, -1));
|
||||
assert(int_stack_push(stack1, -2));
|
||||
assert(int_stack_push(stack1, 854));
|
||||
assert(int_stack_push(stack1, 1024));
|
||||
assert(int_stack_push(stack1, 2024));
|
||||
assert(int_stack_push(stack1, 2025));
|
||||
assert(int_stack_push(stack1, 2026));
|
||||
assert(16 == stack1->allocated);
|
||||
printf("%d \n", int_stack_length(stack1));
|
||||
assert(9 == int_stack_length(stack1));
|
||||
|
||||
int_stack_print(stack1);
|
||||
|
||||
assert(int_stack_pop(stack1, &res));
|
||||
assert(2026 == res);
|
||||
assert(8 == int_stack_length(stack1));
|
||||
}
|
||||
1
tests/test_stack.h
Normal file
1
tests/test_stack.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
void test_stack();
|
||||
|
|
@ -62,4 +62,16 @@ void test_utils()
|
|||
// char src2[] = "hello";
|
||||
// char* trimed2 = trim_space(&src2);
|
||||
// assert(strcmp("hello", trimed2) == 0);
|
||||
|
||||
char src[] = " termination ";
|
||||
char dst[strlen(src)];
|
||||
trim_space((char*) &dst, src);
|
||||
assert(strcmp("termination", dst) == 0);
|
||||
|
||||
char src2[] = "termination";
|
||||
char dst2[strlen(src2)];
|
||||
trim_space((char*) &dst2, src2);
|
||||
assert(strcmp("termination", dst2) == 0);
|
||||
|
||||
|
||||
}
|
||||
18
tests/test_var_store.c
Normal file
18
tests/test_var_store.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "../src/var_store.h"
|
||||
|
||||
void test_var_store()
|
||||
{
|
||||
printf("== test var store == \n");
|
||||
|
||||
struct VariableStore* store = var_store_init();
|
||||
|
||||
int iDest = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
var_store_set(store, "i", TYPE_INT, &i);
|
||||
var_store_copy(store, "i", (void*) &iDest);
|
||||
assert(i == iDest);
|
||||
}
|
||||
}
|
||||
1
tests/test_var_store.h
Normal file
1
tests/test_var_store.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
void test_var_store();
|
||||
Loading…
Add table
Add a link
Reference in a new issue