127 lines
2.9 KiB
C
127 lines
2.9 KiB
C
#include "am.h"
|
|
#include "env.h"
|
|
#include "eval.h"
|
|
#include "unity.h"
|
|
|
|
static am_t am;
|
|
|
|
void test_prim_proc(am_t *am)
|
|
{
|
|
(void)am;
|
|
}
|
|
|
|
void setUp(void)
|
|
{
|
|
am_init(&am);
|
|
}
|
|
|
|
void tearDown(void)
|
|
{
|
|
}
|
|
|
|
static void test_42_self_evals(void)
|
|
{
|
|
am.regs[EXPR] = expr_integer(&am, 42);
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[VAL]->atom.type);
|
|
TEST_ASSERT_EQUAL(42, am.regs[VAL]->atom.integer);
|
|
}
|
|
|
|
static void test_empty_list_self_evals(void)
|
|
{
|
|
am.regs[EXPR] = expr_empty_list(&am);
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, am.regs[VAL]->atom.type);
|
|
}
|
|
|
|
static void test_prim_proc_self_evals(void)
|
|
{
|
|
am.regs[EXPR] = expr_prim_proc(&am, test_prim_proc);
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.regs[VAL]->atom.type);
|
|
TEST_ASSERT_EQUAL(test_prim_proc, am.regs[VAL]->atom.prim_proc);
|
|
}
|
|
|
|
static void test_foo_evals_to_42_when_set_in_env(void)
|
|
{
|
|
am.regs[EXPR] = expr_str_symbol(&am, "foo");
|
|
am.regs[VAL] = expr_integer(&am, 42);
|
|
env_set(&am);
|
|
am.regs[VAL] = NULL;
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[VAL]->atom.type);
|
|
TEST_ASSERT_EQUAL(42, am.regs[VAL]->atom.integer);
|
|
}
|
|
|
|
static void test_add_1_2_3_evals_to_6(void)
|
|
{
|
|
am.regs[EXPR] = expr_pair(
|
|
&am, expr_str_symbol(&am, "+"),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 1),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 2),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 3), expr_empty_list(&am)))));
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[VAL]->atom.type);
|
|
TEST_ASSERT_EQUAL(6, am.regs[VAL]->atom.integer);
|
|
}
|
|
|
|
static void test_add_1_mul_2_3_evals_to_7(void)
|
|
{
|
|
am.regs[EXPR] = expr_pair(
|
|
&am, expr_str_symbol(&am, "+"),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 1),
|
|
expr_pair(
|
|
&am,
|
|
expr_pair(
|
|
&am, expr_str_symbol(&am, "*"),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 2),
|
|
expr_pair(
|
|
&am, expr_integer(&am, 3),
|
|
expr_empty_list(&am)))),
|
|
expr_empty_list(&am))));
|
|
|
|
eval(&am);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[VAL]);
|
|
TEST_ASSERT_TRUE(am.regs[VAL]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[VAL]->atom.type);
|
|
TEST_ASSERT_EQUAL(7, am.regs[VAL]->atom.integer);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_42_self_evals);
|
|
RUN_TEST(test_empty_list_self_evals);
|
|
RUN_TEST(test_prim_proc_self_evals);
|
|
RUN_TEST(test_foo_evals_to_42_when_set_in_env);
|
|
RUN_TEST(test_add_1_2_3_evals_to_6);
|
|
RUN_TEST(test_add_1_mul_2_3_evals_to_7);
|
|
return UNITY_END();
|
|
}
|