gux/lang/tests/sym_table.c

92 lines
2.3 KiB
C
Raw Normal View History

2024-02-16 21:47:37 +00:00
#include <criterion/criterion.h>
#include <sym_table.h>
#include <type.h>
Test(sym_table, simple) {
struct sym_table table;
2024-02-17 20:49:39 +00:00
sym_table_init(&table, NULL);
2024-02-16 21:47:37 +00:00
2024-02-17 22:35:58 +00:00
struct sym* sym = sym_table_fetch(&table, "hello", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_eq(sym, 0);
struct type* ty = malloc(sizeof(struct type));
type_init(ty, TYPE_INT);
int err = sym_table_declare_const(&table, "hello", ty);
cr_assert_eq(err, 0);
2024-02-17 22:35:58 +00:00
sym = sym_table_fetch(&table, "hello", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_neq(sym, 0);
cr_assert_str_eq("hello", sym->name);
sym_table_free(&table);
}
Test(sym_table, already_exists) {
struct sym_table table;
2024-02-17 20:49:39 +00:00
sym_table_init(&table, NULL);
2024-02-16 21:47:37 +00:00
struct type* ty = malloc(sizeof(struct type));
type_init(ty, TYPE_INT);
int err = sym_table_declare_const(&table, "hello", ty);
cr_assert_eq(err, 0);
err = sym_table_declare_const(&table, "hello", ty);
cr_assert_eq(err, 1);
sym_table_free(&table);
}
Test(sym_table, scope) {
struct sym_table table;
2024-02-17 20:49:39 +00:00
sym_table_init(&table, NULL);
2024-02-16 21:47:37 +00:00
struct type* ty_int = malloc(sizeof(struct type));
type_init(ty_int, TYPE_INT);
sym_table_declare_const(&table, "abc", ty_int);
sym_table_push_table(&table);
struct type* ty_float = malloc(sizeof(struct type));
type_init(ty_float, TYPE_FLOAT);
sym_table_declare_const(&table, "abc", ty_float);
sym_table_push_table(&table);
struct type* ty_string = malloc(sizeof(struct type));
type_init(ty_string, TYPE_STRING);
sym_table_declare_const(&table, "abc", ty_string);
struct sym* sym;
2024-02-17 22:35:58 +00:00
sym = sym_table_fetch(&table, "abc", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_eq(sym->type->kind, TYPE_STRING);
sym_table_pop_table(&table);
2024-02-17 22:35:58 +00:00
sym = sym_table_fetch(&table, "abc", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_eq(sym->type->kind, TYPE_FLOAT);
sym_table_pop_table(&table);
2024-02-17 22:35:58 +00:00
sym = sym_table_fetch(&table, "abc", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_eq(sym->type->kind, TYPE_INT);
sym_table_free(&table);
}
Test(sym_table, outer_scope) {
struct sym_table table;
2024-02-17 20:49:39 +00:00
sym_table_init(&table, NULL);
2024-02-16 21:47:37 +00:00
struct type* ty_int = malloc(sizeof(struct type));
type_init(ty_int, TYPE_INT);
sym_table_declare_const(&table, "abc", ty_int);
sym_table_push_table(&table);
sym_table_push_table(&table);
sym_table_push_table(&table);
2024-02-17 22:35:58 +00:00
struct sym* sym = sym_table_fetch(&table, "abc", NULL);
2024-02-16 21:47:37 +00:00
cr_assert_eq(sym->type->kind, TYPE_INT);
sym_table_free(&table);
}