172 lines
3.6 KiB
C
172 lines
3.6 KiB
C
#ifndef SK_TEST_LEXER_H
|
|
#define SK_TEST_LEXER_H
|
|
|
|
#include <CUnit/CUnit.h>
|
|
#include <lexer.h>
|
|
#include <token.h>
|
|
|
|
static void test_lexer(char const* source, int count, ...)
|
|
{
|
|
struct lexer lexer;
|
|
lexer_init(&lexer, source);
|
|
|
|
va_list lst;
|
|
va_start(lst, count);
|
|
|
|
for (int i=0; i<count; i++)
|
|
{
|
|
struct token* tok = lexer_try_new_next(&lexer);
|
|
if (tok == NULL)
|
|
{
|
|
fprintf(stderr, "%s == NULL\n", source);
|
|
}
|
|
CU_ASSERT_FATAL(tok != NULL);
|
|
struct str tok_str;
|
|
str_init(&tok_str);
|
|
token_str(tok, &tok_str);
|
|
char* oracle = va_arg(lst, char*);
|
|
if (strcmp(oracle, tok_str.value) != 0)
|
|
{
|
|
fprintf(stderr, "%s != %s\n", oracle, tok_str.value);
|
|
}
|
|
|
|
CU_ASSERT_STRING_EQUAL_FATAL(tok_str.value, oracle);
|
|
str_free(&tok_str);
|
|
token_free(tok);
|
|
free(tok);
|
|
}
|
|
|
|
va_end(lst);
|
|
lexer_free(&lexer);
|
|
}
|
|
|
|
static void test_lexer_int()
|
|
{
|
|
test_lexer(" 4 23 720 ", 3,
|
|
"INT[4]",
|
|
"INT[23]",
|
|
"INT[720]"
|
|
);
|
|
|
|
test_lexer("+-*/^%()", 8,
|
|
"ADD", "SUB", "MUL", "DIV",
|
|
"POW", "MOD", "OPAR", "CPAR"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_assert()
|
|
{
|
|
test_lexer("assert 2 eq 1", 4,
|
|
"ASSERT",
|
|
"INT[2]",
|
|
"ASSERT_EQ",
|
|
"INT[1]"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_bool()
|
|
{
|
|
test_lexer("true false and or not", 5,
|
|
"BOOL[true]",
|
|
"BOOL[false]",
|
|
"AND",
|
|
"OR",
|
|
"NOT"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_decl()
|
|
{
|
|
test_lexer(" var const = x ", 4,
|
|
"VAR",
|
|
"CONST",
|
|
"ASSIGN",
|
|
"IDENT[x]"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_float()
|
|
{
|
|
test_lexer(".4 7. 2.3 6.12", 4,
|
|
"FLOAT[.4]",
|
|
"FLOAT[7.]",
|
|
"FLOAT[2.3]",
|
|
"FLOAT[6.12]"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_string()
|
|
{
|
|
test_lexer("\" hello \" \"\\\"world\\\"\"", 2,
|
|
"STRING[ hello ]",
|
|
"STRING[\"world\"]"
|
|
);
|
|
|
|
test_lexer("\"\\n\\r\\t\\e\"", 1,
|
|
"STRING[\n\r\t\e]"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_cmp()
|
|
{
|
|
test_lexer("< <= > >= == <>", 6,
|
|
"LT",
|
|
"LE",
|
|
"GT",
|
|
"GE",
|
|
"EQUAL",
|
|
"NOT_EQUAL"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_block()
|
|
{
|
|
test_lexer("begin end", 2,
|
|
"BEGIN",
|
|
"END"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_cond()
|
|
{
|
|
test_lexer("if else", 2,
|
|
"IF",
|
|
"ELSE"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_function()
|
|
{
|
|
test_lexer("fun return,", 3,
|
|
"FUN",
|
|
"RETURN",
|
|
"COMMA"
|
|
);
|
|
}
|
|
|
|
static void test_lexer_module()
|
|
{
|
|
test_lexer("module :: import", 3,
|
|
"MODULE",
|
|
"MOD_ACCESS",
|
|
"IMPORT"
|
|
);
|
|
}
|
|
void register_lexer()
|
|
{
|
|
CU_pSuite suite = CU_add_suite("Lexer", 0, 0);
|
|
CU_add_test(suite, "Integers", test_lexer_int);
|
|
CU_add_test(suite, "Assertions", test_lexer_assert);
|
|
CU_add_test(suite, "Booleans", test_lexer_bool);
|
|
CU_add_test(suite, "Floats", test_lexer_float);
|
|
CU_add_test(suite, "Strings", test_lexer_string);
|
|
CU_add_test(suite, "Comparisons", test_lexer_cmp);
|
|
CU_add_test(suite, "Var Declarations", test_lexer_decl);
|
|
CU_add_test(suite, "Blocks", test_lexer_block);
|
|
CU_add_test(suite, "Conditionnals", test_lexer_cond);
|
|
CU_add_test(suite, "Functions", test_lexer_function);
|
|
CU_add_test(suite, "Modules", test_lexer_module);
|
|
}
|
|
|
|
#endif
|