Consolidate expression creation functions in expr module
This commit is contained in:
@@ -11,6 +11,7 @@ endfunction()
|
||||
add_test_suites(
|
||||
am_tests.c
|
||||
env_tests.c
|
||||
expr_tests.c
|
||||
parse_tests.c
|
||||
store_tests.c
|
||||
token_tests.c
|
||||
|
||||
@@ -6,24 +6,6 @@
|
||||
static am_t am;
|
||||
static store_t store;
|
||||
|
||||
static expr_t *integer(int64_t value)
|
||||
{
|
||||
expr_t *expr = store_alloc(&store);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_INTEGER;
|
||||
expr->atom.integer = value;
|
||||
return expr;
|
||||
}
|
||||
|
||||
static expr_t *symbol(const char *s)
|
||||
{
|
||||
expr_t *expr = store_alloc(&store);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_SYMBOL;
|
||||
memcpy(expr->atom.symbol.buf, s, strlen(s));
|
||||
return expr;
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
am_init(&am);
|
||||
@@ -37,11 +19,11 @@ void tearDown(void)
|
||||
|
||||
static void test_set_foo_to_42_then_fetch(void)
|
||||
{
|
||||
am.expr = symbol("foo");
|
||||
am.val = integer(42);
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = expr_integer(&store, 42);
|
||||
env_set(&am, &store);
|
||||
|
||||
am.expr = symbol("foo");
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = NULL;
|
||||
env_fetch(&am);
|
||||
|
||||
@@ -53,13 +35,13 @@ static void test_set_foo_to_42_then_fetch(void)
|
||||
|
||||
static void test_update_foo_from_123_to_456_then_fetch(void)
|
||||
{
|
||||
am.expr = symbol("foo");
|
||||
am.val = integer(123);
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = expr_integer(&store, 123);
|
||||
env_set(&am, &store);
|
||||
am.val = integer(456);
|
||||
am.val = expr_integer(&store, 456);
|
||||
env_set(&am, &store);
|
||||
|
||||
am.expr = symbol("foo");
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = NULL;
|
||||
env_fetch(&am);
|
||||
|
||||
|
||||
107
tests/expr_tests.c
Normal file
107
tests/expr_tests.c
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user