You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
256 lines
6.6 KiB
256 lines
6.6 KiB
3 years ago
|
#include "ptlisp-test.h"
|
||
|
|
||
|
START_TEST(addition_zero_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(add_fn(NIL)), 0);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(addition_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(add_fn(LIST(number(69)))), 69);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(addition_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(add_fn(LIST(number(69), number(420)))), 489);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(substraction_zero_args_test) {
|
||
|
ck_assert_uint_eq(sub_fn(NIL), UNDEFINED);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(substraction_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(sub_fn(LIST(number(69)))), -69);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(substraction_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(sub_fn(LIST(number(69), number(420)))), -351);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(multiplication_zero_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(mul_fn(NIL)), 1);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(multiplication_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(mul_fn(LIST(number(69)))), 69);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(multiplication_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(mul_fn(LIST(number(69), number(420)))), 28980);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(div_zero_args_test) {
|
||
|
ck_assert_double_eq(div_fn(NIL), UNDEFINED);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(div_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(div_fn(LIST(number(69)))), 1.0 / 69.0);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(div_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(div_fn(LIST(number(69), number(420)))), 69.0 / 420.0);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(pow_test) {
|
||
|
ck_assert_double_eq(NUMBER(pow_fn(LIST(number(69), number(3)))), pow(69.0, 3.0));
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(sqrt_test) {
|
||
|
ck_assert_double_eq(NUMBER(sqrt_fn(LIST(number(69)))), sqrt(69.0));
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logand_zero_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logand_fn(NIL)), -1l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logand_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logand_fn(LIST(number(69)))), -1l & 69l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logand_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logand_fn(LIST(number(69), number(420)))), 69l & 420l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logor_zero_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logor_fn(NIL)), 0l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logor_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logor_fn(LIST(number(69)))), 0l | 69l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logor_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logor_fn(LIST(number(69), number(420)))), 69l | 420l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logxor_zero_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logxor_fn(NIL)), 0l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logxor_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logxor_fn(LIST(number(69)))), 0l ^ 69l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(logxor_many_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(logxor_fn(LIST(number(69), number(420)))), 69l ^ 420l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(lognot_zero_args_test) {
|
||
|
ck_assert_double_eq(lognot_fn(NIL), UNDEFINED);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(lognot_one_args_test) {
|
||
|
ck_assert_double_eq(NUMBER(lognot_fn(LIST(number(69)))), ~69l);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(if_test) {
|
||
|
ck_assert_double_eq(NUMBER(if_fn(LIST(number(1), number(69), number(420)), NIL)), 69);
|
||
|
ck_assert_double_eq(NUMBER(if_fn(LIST(NIL, number(69), number(420)), NIL)), 420);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(let_test) {
|
||
|
Pointer add = symbol("+", 1);
|
||
|
Pointer a = symbol("a", 1);
|
||
|
Pointer b = symbol("b", 1);
|
||
|
Pointer add_func = native_func(add_fn);
|
||
|
Pointer func_table = table(1);
|
||
|
func_table = table_set(func_table, add, add_func);
|
||
|
Pointer env = cons(func_table, NIL);
|
||
|
ck_assert_uint_ne(table_get(func_table, add), UNDEFINED);
|
||
|
Pointer result = let_fn(LIST(LIST(LIST(a, number(69)),
|
||
|
LIST(b, number(420))),
|
||
|
LIST(add, a, b)),
|
||
|
env);
|
||
|
ck_assert_double_eq(NUMBER(result), 489);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(let_empty_test) {
|
||
|
Pointer result = let_fn(LIST(NIL, number(69)), NIL);
|
||
|
ck_assert_double_eq(NUMBER(result), 69);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(quote_test) {
|
||
|
Pointer result = quote_fn(LIST(number(69)), NIL);
|
||
|
ck_assert_double_eq(NUMBER(result), 69);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(and_test) {
|
||
|
Pointer result = and_fn(LIST(number(69), number(420)), NIL);
|
||
|
ck_assert_double_eq(NUMBER(result), 420);
|
||
|
result = and_fn(LIST(NIL, number(420)), NIL);
|
||
|
ck_assert_uint_eq(result, NIL);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(or_test) {
|
||
|
Pointer result = or_fn(LIST(number(69), number(420)), NIL);
|
||
|
ck_assert_double_eq(NUMBER(result), 69);
|
||
|
result = or_fn(LIST(NIL, number(420)), NIL);
|
||
|
ck_assert_double_eq(NUMBER(result), 420);
|
||
|
result = or_fn(LIST(NIL, NIL), NIL);
|
||
|
ck_assert_uint_eq(result, NIL);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(not_test) {
|
||
|
ck_assert_double_eq(not_fn(LIST(NIL)), T);
|
||
|
ck_assert_uint_eq(not_fn(LIST(T)), NIL);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(def_test) {
|
||
|
Pointer a = symbol("a", 1);
|
||
|
Pointer num = number(69);
|
||
|
ck_assert_uint_eq(def_fn(LIST(a, num), NIL), a);
|
||
|
ck_assert_uint_eq(environment_get(NIL, a), num);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
START_TEST(set_test) {
|
||
|
Pointer a = symbol("a", 1);
|
||
|
Pointer num = number(69);
|
||
|
ck_assert_uint_eq(set_fn(LIST(a, num), NIL), num);
|
||
|
ck_assert_uint_eq(environment_get(NIL, a), num);
|
||
|
}
|
||
|
END_TEST
|
||
|
|
||
|
static void setup(void) {
|
||
|
memory_init(16);
|
||
|
symbol_init();
|
||
|
environment_init();
|
||
|
}
|
||
|
|
||
|
static void teardown(void) {
|
||
|
memory_free();
|
||
|
}
|
||
|
|
||
|
Suite* make_lisp_test_suite(void) {
|
||
|
Suite *s1 = suite_create("Lisp");
|
||
|
TCase *tc1_1 = tcase_create("Lisp");
|
||
|
suite_add_tcase(s1, tc1_1);
|
||
|
|
||
|
tcase_add_checked_fixture(tc1_1, setup, teardown);
|
||
|
tcase_add_test(tc1_1, addition_zero_args_test);
|
||
|
tcase_add_test(tc1_1, addition_one_args_test);
|
||
|
tcase_add_test(tc1_1, addition_many_args_test);
|
||
|
tcase_add_test(tc1_1, substraction_zero_args_test);
|
||
|
tcase_add_test(tc1_1, substraction_one_args_test);
|
||
|
tcase_add_test(tc1_1, substraction_many_args_test);
|
||
|
tcase_add_test(tc1_1, multiplication_zero_args_test);
|
||
|
tcase_add_test(tc1_1, multiplication_one_args_test);
|
||
|
tcase_add_test(tc1_1, multiplication_many_args_test);
|
||
|
tcase_add_test(tc1_1, div_zero_args_test);
|
||
|
tcase_add_test(tc1_1, div_one_args_test);
|
||
|
tcase_add_test(tc1_1, div_many_args_test);
|
||
|
tcase_add_test(tc1_1, pow_test);
|
||
|
tcase_add_test(tc1_1, sqrt_test);
|
||
|
tcase_add_test(tc1_1, logand_zero_args_test);
|
||
|
tcase_add_test(tc1_1, logand_one_args_test);
|
||
|
tcase_add_test(tc1_1, logand_many_args_test);
|
||
|
tcase_add_test(tc1_1, logor_zero_args_test);
|
||
|
tcase_add_test(tc1_1, logor_one_args_test);
|
||
|
tcase_add_test(tc1_1, logor_many_args_test);
|
||
|
tcase_add_test(tc1_1, logxor_zero_args_test);
|
||
|
tcase_add_test(tc1_1, logxor_one_args_test);
|
||
|
tcase_add_test(tc1_1, logxor_many_args_test);
|
||
|
tcase_add_test(tc1_1, lognot_zero_args_test);
|
||
|
tcase_add_test(tc1_1, lognot_one_args_test);
|
||
|
tcase_add_test(tc1_1, if_test);
|
||
|
tcase_add_test(tc1_1, let_test);
|
||
|
tcase_add_test(tc1_1, let_empty_test);
|
||
|
tcase_add_test(tc1_1, quote_test);
|
||
|
tcase_add_test(tc1_1, and_test);
|
||
|
tcase_add_test(tc1_1, or_test);
|
||
|
tcase_add_test(tc1_1, not_test);
|
||
|
tcase_add_test(tc1_1, def_test);
|
||
|
tcase_add_test(tc1_1, set_test);
|
||
|
|
||
|
return s1;
|
||
|
}
|