Multiple implementations (JS, Wasm, C) of a Lisp.
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.
 
 
 
 
 

140 lines
4.3 KiB

#include "ptlisp-test.h"
static Pointer STANDARD_INPUT_STREAM;
START_TEST(peek_char_test) {
Stream stream = STREAM(STANDARD_INPUT_STREAM);
ck_assert_uint_eq(peek_char(NIL, stream), 'h');
ck_assert_uint_eq(peek_char(character('o'), stream), 'o');
fseek(stream, 6, SEEK_SET);
ck_assert_uint_eq(peek_char(T, stream), 'w');
rewind(stream);
ck_assert_uint_eq(peek_char(character('z'), stream), 0);
} END_TEST
START_TEST(peek_char_fn_test) {
ck_assert_uint_eq(CHAR(peek_char_fn(LIST(NIL, STANDARD_INPUT_STREAM), NIL)), 'h');
ck_assert_uint_eq(CHAR(peek_char_fn(NIL, NIL)), 'h');
} END_TEST
START_TEST(read_char_fn_test) {
ck_assert_uint_eq(CHAR(read_char_fn(LIST(STANDARD_INPUT_STREAM), NIL)), 'h');
ck_assert_uint_eq(CHAR(read_char_fn(NIL, NIL)), 'e');
ck_assert_uint_eq(CHAR(peek_char_fn(LIST(NIL, STANDARD_INPUT_STREAM), NIL)), 'l');
} END_TEST
START_TEST(read_fn_test) {
FILE* s;
char hello[] = "hello";
s = fmemopen(hello, sizeof(hello), "r");
ck_assert_str_eq(SYMBOL(read_fn(LIST(stream(s)), NIL)).data, hello);
fclose(s);
char funny[] = "69.420";
s = fmemopen(funny, sizeof(funny), "r");
ck_assert_double_eq(NUMBER(read_fn(LIST(stream(s)), NIL)), 69.420);
fclose(s);
char list[] = "(1 2 3)";
s = fmemopen(list, sizeof(list), "r");
Pointer result = read_fn(LIST(stream(s)), NIL);
ck_assert_double_eq(NUMBER(CAR(result)), 1);
ck_assert_double_eq(NUMBER(CAR(CDR(result))), 2);
ck_assert_double_eq(NUMBER(CAR(CDR(CDR(result)))), 3);
fclose(s);
} END_TEST
START_TEST(read_char_macro_fn_test) {
FILE* s;
char a[] = "a";
s = fmemopen(a, sizeof(a), "r");
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), 'a');
fclose(s);
char ab[] = "ab";
s = fmemopen(ab, sizeof(ab), "r");
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
unsigned char smile[] = "😄";
s = fmemopen(smile, sizeof(smile), "r");
Pointer result = read_char_macro(LIST(stream(s)), NIL);
ck_assert_int_eq(CHAR(result) & 0xFF, smile[0]);
ck_assert_int_eq((CHAR(result) >> 8) & 0xFF, smile[1]);
ck_assert_int_eq((CHAR(result) >> 16) & 0xFF, smile[2]);
ck_assert_int_eq((CHAR(result) >> 24) & 0xFF, smile[3]);
fclose(s);
char smile2[] = "😄😄";
s = fmemopen(smile2, sizeof(smile2), "r");
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
char space[] = "space";
s = fmemopen(space, sizeof(space), "r");
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), ' ');
fclose(s);
char tab[] = "tab";
s = fmemopen(tab, sizeof(tab), "r");
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), '\t');
fclose(s);
char newline[] = "newline";
s = fmemopen(newline, sizeof(newline), "r");
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), '\n');
fclose(s);
char space2[] = "space2";
s = fmemopen(space2, sizeof(space2), "r");
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
} END_TEST
START_TEST(read_list_macro_fn_test) {
char list[] = "1 2 3)";
FILE* s = fmemopen(list, sizeof(list), "r");
Pointer result = read_list_macro(LIST(stream(s)), NIL);
ck_assert_double_eq(NUMBER(CAR(result)), 1);
ck_assert_double_eq(NUMBER(CAR(CDR(result))), 2);
ck_assert_double_eq(NUMBER(CAR(CDR(CDR(result)))), 3);
fclose(s);
} END_TEST
START_TEST(read_right_paren_macro_fn_test) {
ck_assert_uint_eq(read_right_paren_macro(NIL, NIL), NIL);
} END_TEST
static char buffer[] = "hello, world";
static void setup(void) {
memory_init(16);
symbol_init();
reader_init();
environment_init();
STANDARD_INPUT_STREAM = stream(fmemopen(buffer, sizeof(buffer), "r"));
environment_set(NIL, STANDARD_INPUT, STANDARD_INPUT_STREAM);
}
static void teardown(void) {
fclose(STREAM(STANDARD_INPUT_STREAM));
memory_free();
}
Suite* make_reader_test_suite(void) {
Suite *s1 = suite_create("Reader");
TCase *tc1_1 = tcase_create("Reader");
suite_add_tcase(s1, tc1_1);
tcase_add_checked_fixture(tc1_1, setup, teardown);
tcase_add_test(tc1_1, peek_char_test);
tcase_add_test(tc1_1, peek_char_fn_test);
tcase_add_test(tc1_1, read_char_fn_test);
tcase_add_test(tc1_1, read_fn_test);
tcase_add_test(tc1_1, read_char_macro_fn_test);
tcase_add_test(tc1_1, read_list_macro_fn_test);
tcase_add_test(tc1_1, read_right_paren_macro_fn_test);
return s1;
}