Minor refactor to parsing and testing cleanup

This commit is contained in:
2022-11-15 22:57:25 -05:00
parent 086d6f9106
commit 1595a6310d
5 changed files with 81 additions and 117 deletions

View File

@@ -42,23 +42,19 @@ void tearDown(void) {
scope = NULL;
}
static struct ucl_object *eval (const char *code) {
struct ucl_object *sexp = ucl_parse(code);
return ucl_evaluate(scope, ucl_car(sexp));
}
void test_simple_add(void) {
ucl_scope_put(scope, "+", ucl_builtin_create(ucl_builtin_add));
response = eval("(+ 2 3)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 5);
TEST_ASSERT_OBJ_INT_V(response, 5);
}
void test_simple_let(void) {
response = eval("(let ((x 2)) (+ x 3))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 5);
TEST_ASSERT_OBJ_INT_V(response, 5);
}
void test_let_assignment_error(void) {
@@ -68,8 +64,7 @@ void test_let_assignment_error(void) {
void test_nested_let(void) {
response = eval("(let ((x 2)) (let ((y 5)) (+ x y)))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
TEST_ASSERT_OBJ_INT_V(response, 7);
}
void test_nested_let_scope(void) {
@@ -79,33 +74,28 @@ void test_nested_let_scope(void) {
void test_multi_let(void) {
response = eval("(let ((x 2)(y 5)) (+ x y))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
TEST_ASSERT_OBJ_INT_V(response, 7);
}
void test_let_return_sym(void) {
response = eval("(let ((x 2)) x)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_eval_int(void) {
response = eval("2");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_eval_string(void) {
response = eval("\"foo\"");
TEST_ASSERT_OBJ_STRING(response);
TEST_ASSERT_EQUAL_STRING(response->string, "foo");
TEST_ASSERT_OBJ_STRING_V(response, "foo");
}
void test_eval_sym_defined(void) {
ucl_scope_put(scope, "foo", ucl_int_create(2));
response = eval("foo");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_eval_sym_undefined(void) {
@@ -133,8 +123,7 @@ void test_eval_list(void) {
void test_eval_defun(void) {
response = eval("(defun foo (a b) (+ a b))");
TEST_ASSERT_OBJ_SYMBOL(response);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
}
void test_call_function(void) {
@@ -152,44 +141,37 @@ void test_call_function(void) {
void test_setq(void) {
response = eval("(setq bar 123)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->symbol, 123);
TEST_ASSERT_OBJ_INT_V(response, 123);
response = eval("bar");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->symbol, 123);
TEST_ASSERT_OBJ_INT_V(response, 123);
}
void test_setq_from_function(void) {
response = eval("(defun foo (a) (setq bar a))");
TEST_ASSERT_OBJ_SYMBOL(response);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
response = eval("(foo 2)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->symbol, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
response = eval("bar");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->symbol, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_lambda(void) {
response = eval("((lambda (x) (+ x 2)) 2)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 4);
TEST_ASSERT_OBJ_INT_V(response, 4);
}
void test_mapcar_lambda(void) {
response = eval("(car (mapcar (quote (lambda (x) (+ x 2))) (list 5)))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
TEST_ASSERT_OBJ_INT_V(response, 7);
}
void test_mapcar_function(void) {
@@ -200,36 +182,31 @@ void test_mapcar_function(void) {
response = eval("(car (mapcar (quote foo) (list 5)))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7);
TEST_ASSERT_OBJ_INT_V(response, 7);
}
void test_car(void) {
response = eval("(car (list 2 3 4))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_cdr(void) {
response = eval("(car (cdr (list 2 3 4)))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 3);
TEST_ASSERT_OBJ_INT_V(response, 3);
}
void test_nth_0(void) {
response = eval("(nth 0 (list 2 3 4))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
TEST_ASSERT_OBJ_INT_V(response, 2);
}
void test_nth_1(void) {
response = eval("(nth 1 (list 2 3 4))");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 3);
TEST_ASSERT_OBJ_INT_V(response, 3);
}
void test_nth_oob(void) {
@@ -241,16 +218,12 @@ void test_nth_oob(void) {
void test_eval_defun_gc(void) {
response = eval("(defun foo (a b) (+ a b))");
TEST_ASSERT_OBJ_SYMBOL(response);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
ucl_gc();
response = eval("(foo 10 15)");
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 25);
TEST_ASSERT_OBJ_INT_V(response, 25);
}
int main(void) {