2022-04-30 14:16:37 +00:00
|
|
|
#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"
|
2022-05-06 09:49:09 +00:00
|
|
|
#include "../src/state.h"
|
2022-04-30 14:16:37 +00:00
|
|
|
|
|
|
|
void test_evaluation()
|
|
|
|
{
|
|
|
|
printf("== test evaluation == \n");
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
struct StateContainer* state = state_init();
|
|
|
|
|
2022-04-30 14:16:37 +00:00
|
|
|
// test int parsing
|
|
|
|
int resVal = 0;
|
|
|
|
unsigned char resType = 0;
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "-4", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(-4 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "-(4+9)+1", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(-12 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "(-(8-9+5))+8", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(4 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "2^6", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(64 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "3 + 4", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(7 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "(2*4)+0+0", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(8 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "2.5-(2+0.1)", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_FLOAT);
|
|
|
|
assert(float_almost_equal(0.4, get_float_from_int_rep(resVal)));
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "1^0 + (7*(5 +2))", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(50 == resVal);
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "- ( 0.1+ 0.3 )", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_FLOAT);
|
|
|
|
assert(float_almost_equal(-0.4, get_float_from_int_rep(resVal)));
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "sqrt(2)-1", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_FLOAT);
|
|
|
|
assert(float_almost_equal(0.41421, get_float_from_int_rep(resVal)));
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "(abs((0-1)*2)) + abs(2)", &resVal, &resType);
|
2022-04-30 14:16:37 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(4 == resVal);
|
2022-04-30 16:01:22 +00:00
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "exp(2)-1", &resVal, &resType);
|
2022-04-30 16:01:22 +00:00
|
|
|
assert(resType == TYPE_FLOAT);
|
|
|
|
assert(float_almost_equal(6.389, get_float_from_int_rep(resVal)));
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "(cos(2)^2)+(sin(2)^2)", &resVal, &resType);
|
2022-04-30 16:01:22 +00:00
|
|
|
assert(resType == TYPE_FLOAT);
|
|
|
|
assert(float_almost_equal(1, get_float_from_int_rep(resVal)));
|
|
|
|
|
2022-05-06 09:49:09 +00:00
|
|
|
evaluate(state, "random_int(1, 100)", &resVal, &resType);
|
2022-04-30 16:01:22 +00:00
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
printf(" - random int: %d \n", resVal);
|
2022-05-06 09:49:09 +00:00
|
|
|
|
|
|
|
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);
|
2022-05-06 11:13:58 +00:00
|
|
|
|
|
|
|
evaluate(state, "2 = 2", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "2 = 3", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(0 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "2 = 3.2", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(0 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "!(2 = 3)", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "!! 0", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(0 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "(! (! 1)) + 0", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "!!2", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "1 & 1", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "1 & ((1+0)-1)", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(0 == resVal);
|
|
|
|
|
|
|
|
evaluate(state, "1 & (1 | 0)", &resVal, &resType);
|
|
|
|
assert(resType == TYPE_INT);
|
|
|
|
assert(1 == resVal);
|
2022-04-30 16:01:22 +00:00
|
|
|
}
|