60 lines
1.6 KiB
C
60 lines
1.6 KiB
C
|
#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"
|
||
|
|
||
|
void test_evaluation()
|
||
|
{
|
||
|
printf("== test evaluation == \n");
|
||
|
|
||
|
// test int parsing
|
||
|
int resVal = 0;
|
||
|
unsigned char resType = 0;
|
||
|
|
||
|
evaluate("-4", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(-4 == resVal);
|
||
|
|
||
|
evaluate("-(4+9)+1", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(-12 == resVal);
|
||
|
|
||
|
evaluate("(-(8-9+5))+8", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(4 == resVal);
|
||
|
|
||
|
evaluate("2^6", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(64 == resVal);
|
||
|
|
||
|
evaluate("3 + 4", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(7 == resVal);
|
||
|
|
||
|
evaluate("(2*4)+0+0", &resVal, &resType);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(8 == resVal);
|
||
|
|
||
|
evaluate("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);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(50 == resVal);
|
||
|
|
||
|
evaluate("- ( 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);
|
||
|
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);
|
||
|
assert(resType == TYPE_INT);
|
||
|
assert(4 == resVal);
|
||
|
}
|