feat(Evaluator): add pow operator
feat(Evaluator): add minus sign reducing fix(Evaluator): trim spaces test: add base for unit testing
This commit is contained in:
parent
17c7c247af
commit
a62dd411aa
20 changed files with 654 additions and 137 deletions
10
tests/test.c
Normal file
10
tests/test.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include "./test_utils.h"
|
||||
#include "./test_evaluation.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("== UNIT TESTS == \n");
|
||||
test_utils();
|
||||
test_evaluation();
|
||||
}
|
||||
60
tests/test_evaluation.c
Normal file
60
tests/test_evaluation.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#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);
|
||||
}
|
||||
1
tests/test_evaluation.h
Normal file
1
tests/test_evaluation.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
void test_evaluation();
|
||||
65
tests/test_utils.c
Normal file
65
tests/test_utils.c
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "../src/number_parsing.h"
|
||||
#include "../src/utils.h"
|
||||
|
||||
void test_utils()
|
||||
{
|
||||
printf("== test utils == \n");
|
||||
|
||||
// test int parsing
|
||||
char test1[] = "151087";
|
||||
int res = 0;
|
||||
parse_clean_positive_integer(strlen(test1), test1, &res);
|
||||
assert(res == 151087);
|
||||
|
||||
char test2[] = "-428579";
|
||||
parse_int(test2, &res);
|
||||
assert(res == -428579);
|
||||
|
||||
|
||||
float res3 = 0;
|
||||
char test3[] = "3121.897";
|
||||
parse_float(test3, &res3);
|
||||
assert(float_almost_equal(3121.897, res3));
|
||||
|
||||
float res4 = 0;
|
||||
char test4[] = "-0.4397";
|
||||
parse_float(test4, &res4);
|
||||
assert(float_almost_equal(-0.4397, res4));
|
||||
|
||||
assert(integer_pow(1, 0) == 1);
|
||||
assert(integer_pow(2, 1) == 2);
|
||||
assert(integer_pow(2, 6) == 64);
|
||||
|
||||
assert(m_factorial(0) == 1);
|
||||
assert(m_factorial(1) == 1);
|
||||
assert(m_factorial(2) == 2);
|
||||
assert(m_factorial(3) == 6);
|
||||
assert(m_factorial(4) == 2*3*4);
|
||||
|
||||
assert(float_almost_equal(2.7182, m_exp(1.0)));
|
||||
assert(float_almost_equal(1, m_exp(0)));
|
||||
|
||||
assert(float_almost_equal(0, m_sin(0)));
|
||||
assert(float_almost_equal(1, m_sin(CST_PI/2)));
|
||||
assert(float_almost_equal(0, m_sin(CST_PI)));
|
||||
assert(float_almost_equal(-1, m_sin(3*CST_PI/2)));
|
||||
assert(float_almost_equal(0, m_sin(2*CST_PI)));
|
||||
assert(float_almost_equal(0, m_sin(4*CST_PI)));
|
||||
|
||||
assert(float_almost_equal(1.41421, m_sqrt(2)));
|
||||
assert(float_almost_equal(2.2360, m_sqrt(5)));
|
||||
|
||||
assert(float_almost_equal(0, m_ln(1)));
|
||||
assert(float_almost_equal(1, m_ln(2.7182818)));
|
||||
|
||||
// char src[] = " hello world ";
|
||||
// char* trimed = trim_space(src);
|
||||
// assert(strcmp("hello world", trimed) == 0);
|
||||
|
||||
// char src2[] = "hello";
|
||||
// char* trimed2 = trim_space(&src2);
|
||||
// assert(strcmp("hello", trimed2) == 0);
|
||||
}
|
||||
1
tests/test_utils.h
Normal file
1
tests/test_utils.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
void test_utils();
|
||||
Loading…
Add table
Add a link
Reference in a new issue