feat: line processing and basic variable support

This commit is contained in:
Matthieu Bessat 2022-05-06 11:49:09 +02:00
parent cb2a1df61f
commit 11fa2b1e6f
21 changed files with 893 additions and 136 deletions

View file

@ -1,5 +1,6 @@
#include "./test_utils.h"
#include "./test_evaluation.h"
#include "./test_line_processing.h"
#include <stdio.h>
int main()
@ -7,4 +8,5 @@ int main()
printf("== UNIT TESTS == \n");
test_utils();
test_evaluation();
test_line_processing();
}

View file

@ -5,68 +5,80 @@
#include "../src/number_parsing.h"
#include "../src/utils.h"
#include "../src/evaluator.h"
#include "../src/state.h"
void test_evaluation()
{
printf("== test evaluation == \n");
struct StateContainer* state = state_init();
// test int parsing
int resVal = 0;
unsigned char resType = 0;
evaluate("-4", &resVal, &resType);
evaluate(state, "-4", &resVal, &resType);
assert(resType == TYPE_INT);
assert(-4 == resVal);
evaluate("-(4+9)+1", &resVal, &resType);
evaluate(state, "-(4+9)+1", &resVal, &resType);
assert(resType == TYPE_INT);
assert(-12 == resVal);
evaluate("(-(8-9+5))+8", &resVal, &resType);
evaluate(state, "(-(8-9+5))+8", &resVal, &resType);
assert(resType == TYPE_INT);
assert(4 == resVal);
evaluate("2^6", &resVal, &resType);
evaluate(state, "2^6", &resVal, &resType);
assert(resType == TYPE_INT);
assert(64 == resVal);
evaluate("3 + 4", &resVal, &resType);
evaluate(state, "3 + 4", &resVal, &resType);
assert(resType == TYPE_INT);
assert(7 == resVal);
evaluate("(2*4)+0+0", &resVal, &resType);
evaluate(state, "(2*4)+0+0", &resVal, &resType);
assert(resType == TYPE_INT);
assert(8 == resVal);
evaluate("2.5-(2+0.1)", &resVal, &resType);
evaluate(state, "2.5-(2+0.1)", &resVal, &resType);
assert(resType == TYPE_FLOAT);
assert(float_almost_equal(0.4, get_float_from_int_rep(resVal)));
evaluate("1^0 + (7*(5 +2))", &resVal, &resType);
evaluate(state, "1^0 + (7*(5 +2))", &resVal, &resType);
assert(resType == TYPE_INT);
assert(50 == resVal);
evaluate("- ( 0.1+ 0.3 )", &resVal, &resType);
evaluate(state, "- ( 0.1+ 0.3 )", &resVal, &resType);
assert(resType == TYPE_FLOAT);
assert(float_almost_equal(-0.4, get_float_from_int_rep(resVal)));
evaluate("sqrt(2)-1", &resVal, &resType);
evaluate(state, "sqrt(2)-1", &resVal, &resType);
assert(resType == TYPE_FLOAT);
assert(float_almost_equal(0.41421, get_float_from_int_rep(resVal)));
evaluate("(abs((0-1)*2)) + abs(2)", &resVal, &resType);
evaluate(state, "(abs((0-1)*2)) + abs(2)", &resVal, &resType);
assert(resType == TYPE_INT);
assert(4 == resVal);
evaluate("exp(2)-1", &resVal, &resType);
evaluate(state, "exp(2)-1", &resVal, &resType);
assert(resType == TYPE_FLOAT);
assert(float_almost_equal(6.389, get_float_from_int_rep(resVal)));
evaluate("(cos(2)^2)+(sin(2)^2)", &resVal, &resType);
evaluate(state, "(cos(2)^2)+(sin(2)^2)", &resVal, &resType);
assert(resType == TYPE_FLOAT);
assert(float_almost_equal(1, get_float_from_int_rep(resVal)));
evaluate("random_int(1, 100)", &resVal, &resType);
evaluate(state, "random_int(1, 100)", &resVal, &resType);
assert(resType == TYPE_INT);
printf(" - random int: %d \n", resVal);
evaluate(state, "abs(2)+abs(-2)", &resVal, &resType);
assert(resType == TYPE_INT);
assert(4 == resVal);
// testing function composition is important
evaluate(state, "abs(abs(-2))", &resVal, &resType);
assert(resType == TYPE_INT);
assert(2 == resVal);
}

View file

@ -0,0 +1,32 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "../src/types.h"
#include "../src/number_parsing.h"
#include "../src/utils.h"
#include "../src/evaluator.h"
#include "../src/state.h"
#include "../src/var_store.h"
void test_line_processing()
{
printf("== test line processing == \n");
struct StateContainer* state = state_init();
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(process_line(state, "set VAR_B to 1.5"));
assert(float_almost_equal(1.5, var_store_get_float(state->varStore, "VAR_B")));
assert(process_line(state, "set VAR_C to 142"));
assert(142 == var_store_get_int(state->varStore, "VAR_C"));
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")));
}

View file

@ -0,0 +1 @@
void test_line_processing();