skopy/tests/parser.h

84 lines
2.0 KiB
C
Raw Normal View History

2024-03-31 21:10:56 +00:00
#ifndef SK_TEST_PARSER_H
#define SK_TEST_PARSER_H
#include <CUnit/CUnit.h>
#include "node.h"
#include <parser.h>
static void test_parser(char const* oracle, char const* source)
{
struct parser parser;
parser_init(&parser, source);
struct node* node = parser_try_parse(&parser);
CU_ASSERT_FATAL(node != NULL);
struct str str;
str_init(&str);
node_str(node, &str);
if (strcmp(oracle, str.value) != 0)
{
fprintf(stderr, "\n%s <> %s\n", oracle, str.value);
}
CU_ASSERT_STRING_EQUAL_FATAL(oracle, str.value);
2024-03-31 21:10:56 +00:00
str_free(&str);
node_free(node);
free(node);
parser_free(&parser);
}
static void test_parser_int()
{
test_parser("ROOT(INT[32])", " 32 ");
2024-03-31 23:32:47 +00:00
2024-03-31 21:10:56 +00:00
test_parser("ROOT(INT[32],INT[-2],INT[24])", " 32 -2 24");
2024-03-31 23:32:47 +00:00
test_parser("ROOT(ADD(INT[1],MUL(INT[2],INT[3])))",
" 1 + 2 * 3 ");
test_parser("ROOT(DIV(SUB(INT[1],INT[2]),INT[3]))",
" (1 - 2) / 3 ");
test_parser("ROOT(USUB(INT[32]))", " -(32) ");
2024-03-31 21:10:56 +00:00
}
static void test_parser_assert()
{
2024-04-01 12:00:06 +00:00
test_parser("ROOT(ASSERT_EQ(INT[32],INT[12]))",
" assert 32 eq 12 ");
}
2024-04-01 12:00:06 +00:00
static void test_parser_bool()
{
test_parser("ROOT(BOOL[true])",
"true");
test_parser("ROOT(NOT(BOOL[false]))",
"not false");
test_parser("ROOT(OR(AND(NOT(BOOL[false]),BOOL[true]),"
"BOOL[false]))",
"not false and true or false");
test_parser("ROOT(AND(NOT(BOOL[false]),OR(BOOL[true],"
"BOOL[false])))",
"not false and (true or false)");
}
2024-04-01 19:42:36 +00:00
static void test_parser_string()
{
test_parser("ROOT(STRING[ok pizza NOW])",
"\"ok pizza NOW\"");
}
2024-03-31 21:10:56 +00:00
void register_parser()
{
CU_pSuite suite = CU_add_suite("Parser", 0, 0);
CU_add_test(suite, "Integers", test_parser_int);
CU_add_test(suite, "Assertions", test_parser_assert);
2024-04-01 12:00:06 +00:00
CU_add_test(suite, "Booleans", test_parser_bool);
2024-04-01 19:42:36 +00:00
CU_add_test(suite, "Strings", test_parser_string);
2024-03-31 21:10:56 +00:00
}
#endif