Consolidate expression creation functions in expr module

This commit is contained in:
2025-08-10 13:27:20 +01:00
parent fade9395fa
commit 03bd0ff597
9 changed files with 189 additions and 76 deletions

107
tests/expr_tests.c Normal file
View File

@@ -0,0 +1,107 @@
#include "expr.h"
#include "store.h"
#include "unity.h"
static store_t store;
void setUp(void)
{
store_init(&store);
}
void tearDown(void)
{
}
static void test_expr_integer_123(void)
{
expr_t *expr = expr_integer(&store, 123);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
TEST_ASSERT_EQUAL(123, expr->atom.integer);
}
static void test_expr_integer_456(void)
{
expr_t *expr = expr_integer(&store, 456);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
TEST_ASSERT_EQUAL(456, expr->atom.integer);
}
static void test_expr_symbol_foo(void)
{
const symbol_t symbol = { .buf = "foo", .len = 3 };
expr_t *expr = expr_symbol(&store, &symbol);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
TEST_ASSERT_EQUAL(3, expr->atom.symbol.len);
TEST_ASSERT_EQUAL_MEMORY("foo", expr->atom.symbol.buf, 3);
}
static void test_expr_symbol_quux(void)
{
const symbol_t symbol = { .buf = "quux", .len = 4 };
expr_t *expr = expr_symbol(&store, &symbol);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
TEST_ASSERT_EQUAL(4, expr->atom.symbol.len);
TEST_ASSERT_EQUAL_MEMORY("quux", expr->atom.symbol.buf, 4);
}
static void test_expr_str_symbol_foo(void)
{
expr_t *expr = expr_str_symbol(&store, "foo");
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
TEST_ASSERT_EQUAL(3, expr->atom.symbol.len);
TEST_ASSERT_EQUAL_MEMORY("foo", expr->atom.symbol.buf, 3);
}
static void test_expr_str_symbol_quux(void)
{
expr_t *expr = expr_str_symbol(&store, "quux");
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
TEST_ASSERT_EQUAL(4, expr->atom.symbol.len);
TEST_ASSERT_EQUAL_MEMORY("quux", expr->atom.symbol.buf, 4);
}
static void test_expr_empty_list(void)
{
expr_t *expr = expr_empty_list(&store);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_TRUE(expr->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, expr->atom.type);
}
static void test_expr_pair(void)
{
expr_t *car = expr_integer(&store, 123);
expr_t *cdr = expr_integer(&store, 456);
expr_t *expr = expr_pair(&store, car, cdr);
TEST_ASSERT_NOT_NULL(expr);
TEST_ASSERT_FALSE(expr->is_atom);
TEST_ASSERT_EQUAL(car, expr->pair.car);
TEST_ASSERT_EQUAL(cdr, expr->pair.cdr);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_expr_integer_123);
RUN_TEST(test_expr_integer_456);
RUN_TEST(test_expr_symbol_foo);
RUN_TEST(test_expr_symbol_quux);
RUN_TEST(test_expr_str_symbol_foo);
RUN_TEST(test_expr_str_symbol_quux);
RUN_TEST(test_expr_empty_list);
RUN_TEST(test_expr_pair);
return UNITY_END();
}