Add mapcar builtin, more tests, fixes
This commit is contained in:
120
test/test_e2e.c
120
test/test_e2e.c
@@ -24,6 +24,13 @@ void setUp(void) {
|
||||
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
|
||||
ucl_state_put(state, "error", ucl_builtin_create(ucl_builtin_error));
|
||||
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
||||
ucl_state_put(state, "setq", ucl_special_create(ucl_special_setq));
|
||||
ucl_state_put(state, "car", ucl_builtin_create(ucl_builtin_car));
|
||||
ucl_state_put(state, "cdr", ucl_builtin_create(ucl_builtin_cdr));
|
||||
ucl_state_put(state, "nth", ucl_builtin_create(ucl_builtin_nth));
|
||||
ucl_state_put(state, "mapcar", ucl_builtin_create(ucl_builtin_mapcar));
|
||||
ucl_state_put(state, "lambda", ucl_special_create(ucl_special_lambda));
|
||||
ucl_state_put(state, "quote", ucl_special_create(ucl_special_quote));
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
@@ -130,6 +137,108 @@ void test_eval_defun(void) {
|
||||
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
|
||||
}
|
||||
|
||||
void test_call_function(void) {
|
||||
response = eval("(defun foo (a b) (+ a b))");
|
||||
|
||||
TEST_ASSERT_OBJ_SYMBOL(response);
|
||||
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
|
||||
|
||||
response = eval("(foo 2 3)");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 5);
|
||||
}
|
||||
|
||||
void test_setq(void) {
|
||||
response = eval("(setq bar 2)");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->symbol, 2);
|
||||
|
||||
response = eval("bar");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->symbol, 2);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
response = eval("(foo 2)");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->symbol, 2);
|
||||
|
||||
response = eval("bar");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->symbol, 2);
|
||||
}
|
||||
|
||||
void test_lambda(void) {
|
||||
response = eval("((lambda (x) (+ x 2)) 2)");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 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);
|
||||
}
|
||||
|
||||
void test_mapcar_function(void) {
|
||||
response = eval("(defun foo (a) (+ a 2))");
|
||||
|
||||
TEST_ASSERT_OBJ_SYMBOL(response);
|
||||
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
|
||||
|
||||
response = eval("(car (mapcar (quote foo) (list 5)))");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 7);
|
||||
}
|
||||
|
||||
void test_car(void) {
|
||||
response = eval("(car (list 2 3 4))");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 2);
|
||||
}
|
||||
|
||||
void test_cdr(void) {
|
||||
response = eval("(car (cdr (list 2 3 4)))");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 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);
|
||||
}
|
||||
|
||||
void test_nth_1(void) {
|
||||
response = eval("(nth 1 (list 2 3 4))");
|
||||
|
||||
TEST_ASSERT_OBJ_INT(response);
|
||||
TEST_ASSERT_EQUAL(response->integer, 3);
|
||||
}
|
||||
|
||||
void test_nth_oob(void) {
|
||||
response = eval("(nth 3 (list 2 3 4))");
|
||||
|
||||
TEST_ASSERT_OBJ_ERROR(response);
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
UNITY_BEGIN();
|
||||
|
||||
@@ -147,6 +256,17 @@ int main(void) {
|
||||
RUN_TEST(test_eval_nested_error);
|
||||
RUN_TEST(test_eval_list);
|
||||
RUN_TEST(test_eval_defun);
|
||||
RUN_TEST(test_call_function);
|
||||
RUN_TEST(test_setq);
|
||||
RUN_TEST(test_setq_from_function);
|
||||
RUN_TEST(test_lambda);
|
||||
RUN_TEST(test_mapcar_lambda);
|
||||
RUN_TEST(test_car);
|
||||
RUN_TEST(test_cdr);
|
||||
RUN_TEST(test_nth_0);
|
||||
RUN_TEST(test_nth_1);
|
||||
RUN_TEST(test_nth_oob);
|
||||
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user