gux/lang/tests/sym_table.c

92 lines
2.3 KiB
C

#include <criterion/criterion.h>
#include <sym_table.h>
#include <type.h>
Test(sym_table, simple) {
struct sym_table table;
sym_table_init(&table, NULL);
struct sym* sym = sym_table_fetch(&table, "hello", NULL);
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);
sym = sym_table_fetch(&table, "hello", NULL);
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;
sym_table_init(&table, NULL);
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;
sym_table_init(&table, NULL);
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;
sym = sym_table_fetch(&table, "abc", NULL);
cr_assert_eq(sym->type->kind, TYPE_STRING);
sym_table_pop_table(&table);
sym = sym_table_fetch(&table, "abc", NULL);
cr_assert_eq(sym->type->kind, TYPE_FLOAT);
sym_table_pop_table(&table);
sym = sym_table_fetch(&table, "abc", NULL);
cr_assert_eq(sym->type->kind, TYPE_INT);
sym_table_free(&table);
}
Test(sym_table, outer_scope) {
struct sym_table table;
sym_table_init(&table, NULL);
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);
struct sym* sym = sym_table_fetch(&table, "abc", NULL);
cr_assert_eq(sym->type->kind, TYPE_INT);
sym_table_free(&table);
}