fix: tests and main
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "evaluator.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -9,22 +6,8 @@
|
||||
#include <cmocka.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_basic_evaluation(void** state) {
|
||||
(void) state;
|
||||
|
||||
char expr[256] = "2 + 4 * 40 / 2";
|
||||
TokenizeResult tokens = tokenize(expr);
|
||||
ParseResult result = parse(tokens);
|
||||
int64_t value = evaluate(result);
|
||||
|
||||
assert_int_equal(value, 82);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_basic_evaluation),
|
||||
};
|
||||
|
||||
cmocka_run_group_tests(tests, NULL, NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,106 +1,11 @@
|
||||
#include "arraylist.h"
|
||||
#include "lexer.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
static void test_tokenize_normal_expresion(void **state) {
|
||||
(void) state;
|
||||
|
||||
char expr[256] = "2 + 3 / 66 * 789";
|
||||
ASTNode node;
|
||||
TokenizeResult tokens = tokenize(expr);
|
||||
|
||||
assert_true(tokens.is_valid);
|
||||
assert_int_equal(arraylist_size(tokens.arr), 7);
|
||||
|
||||
arraylist_get(tokens.arr, 0, &node);
|
||||
assert_int_equal(node.type, NODE_INTEGER);
|
||||
assert_int_equal(node.data.integer, 2);
|
||||
|
||||
arraylist_get(tokens.arr, 1, &node);
|
||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
||||
assert_int_equal(node.data.binary.op, OP_ADD);
|
||||
|
||||
arraylist_get(tokens.arr, 2, &node);
|
||||
assert_int_equal(node.type, NODE_INTEGER);
|
||||
assert_int_equal(node.data.integer, 3);
|
||||
|
||||
arraylist_get(tokens.arr, 3, &node);
|
||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
||||
assert_int_equal(node.data.binary.op, OP_DIV);
|
||||
|
||||
arraylist_get(tokens.arr, 4, &node);
|
||||
assert_int_equal(node.type, NODE_INTEGER);
|
||||
assert_int_equal(node.data.integer, 66);
|
||||
|
||||
arraylist_get(tokens.arr, 5, &node);
|
||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
||||
assert_int_equal(node.data.binary.op, OP_MUL);
|
||||
|
||||
arraylist_get(tokens.arr, 6, &node);
|
||||
assert_int_equal(node.type, NODE_INTEGER);
|
||||
assert_int_equal(node.data.integer, 789);
|
||||
}
|
||||
|
||||
static void test_tokenize_unrecognized_symbol(void **state) {
|
||||
(void) state;
|
||||
|
||||
char expr[256] = " 2 j 3 / 66 } 789";
|
||||
TokenizeResult tokens = tokenize(expr);
|
||||
|
||||
assert_false(tokens.is_valid);
|
||||
assert_uint_equal(tokens.err, LEXER_NOT_RECOGNIZED_SYMBOL);
|
||||
}
|
||||
|
||||
static void test_tokenize_wrong_sintax(void **state) {
|
||||
(void) state;
|
||||
|
||||
char expr[256] = "2 3 / 66 789";
|
||||
TokenizeResult tokens = tokenize(expr);
|
||||
|
||||
assert_false(tokens.is_valid);
|
||||
assert_uint_equal(tokens.err, LEXER_WRONG_SYNTAX);
|
||||
}
|
||||
|
||||
static void test_string_to_number_normal(void **state) {
|
||||
(void) state;
|
||||
|
||||
char num[16] = "2333t55";
|
||||
size_t offset = 0;
|
||||
ASTNodeResult result = tokenize_number(num, &offset);
|
||||
|
||||
assert_true(result.is_valid);
|
||||
|
||||
assert_int_equal(offset, 4); // equal to t position in string
|
||||
assert_int_equal(result.node.type, NODE_INTEGER);
|
||||
assert_int_equal(result.node.data.integer, 2333);
|
||||
}
|
||||
|
||||
static void test_string_to_number_overflow(void **state) {
|
||||
(void) state;
|
||||
|
||||
// Number is INT64_MAX but with a extra 899 at the end
|
||||
char num[32] = "92233720368547758079";
|
||||
size_t offset = 0;
|
||||
ASTNodeResult result = tokenize_number(num, &offset);
|
||||
assert_false(result.is_valid);
|
||||
assert_uint_equal(result.err, LEXER_INT_OVERFLOW);
|
||||
// Technically it can trigger a buf overflow error but obvioulsy
|
||||
// it will trigger int overflow error first
|
||||
}
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_string_to_number_normal),
|
||||
cmocka_unit_test(test_string_to_number_overflow),
|
||||
cmocka_unit_test(test_tokenize_normal_expresion),
|
||||
cmocka_unit_test(test_tokenize_unrecognized_symbol),
|
||||
cmocka_unit_test(test_tokenize_wrong_sintax),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,81 +1,11 @@
|
||||
#include "arena.h"
|
||||
#include "arraylist.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
static void test_parsing_basic_expression(void **state) {
|
||||
(void) state;
|
||||
|
||||
char expr[256] = "2 + 3 / 66 * 789";
|
||||
TokenizeResult tokens = tokenize(expr);
|
||||
|
||||
assert_true(tokens.is_valid);
|
||||
assert_int_equal(arraylist_size(tokens.arr), 7);
|
||||
|
||||
ParseResult result = parse(tokens);
|
||||
// Assert head is +
|
||||
assert_int_equal(result.tree->type, NODE_BINARY_OP);
|
||||
assert_int_equal(result.tree->data.binary.op, OP_ADD);
|
||||
|
||||
assert_int_equal(result.tree->data.binary.left->type, NODE_INTEGER);
|
||||
assert_int_equal(result.tree->data.binary.left->data.integer, 2);
|
||||
|
||||
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->type,
|
||||
NODE_BINARY_OP
|
||||
);
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.op,
|
||||
OP_MUL
|
||||
);
|
||||
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.right->type,
|
||||
NODE_INTEGER);
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.right->data.integer,
|
||||
789);
|
||||
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->type,
|
||||
NODE_BINARY_OP
|
||||
);
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->data.binary.op,
|
||||
OP_DIV
|
||||
);
|
||||
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->data.binary.right->type,
|
||||
NODE_INTEGER
|
||||
);
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->data.binary.right->data.integer,
|
||||
66
|
||||
);
|
||||
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->data.binary.left->type,
|
||||
NODE_INTEGER
|
||||
);
|
||||
assert_int_equal(
|
||||
result.tree->data.binary.right->data.binary.left->data.binary.left->data.integer,
|
||||
3
|
||||
);
|
||||
arena_destroy(&result.arena);
|
||||
}
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests [] = {
|
||||
cmocka_unit_test(test_parsing_basic_expression),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user