155 lines
3.9 KiB
C
155 lines
3.9 KiB
C
#include "am.h"
|
|
#include "unity.h"
|
|
|
|
#define CAR(expr) (expr->pair.car)
|
|
#define CDR(expr) (expr->pair.cdr)
|
|
#define CADR(expr) CAR(CDR(expr))
|
|
|
|
static am_t am;
|
|
static bool test_prim_proc_called;
|
|
|
|
static void test_prim_proc(am_t *am)
|
|
{
|
|
(void)am;
|
|
test_prim_proc_called = true;
|
|
}
|
|
|
|
void setUp(void)
|
|
{
|
|
am_init(&am);
|
|
test_prim_proc_called = false;
|
|
}
|
|
|
|
void tearDown(void)
|
|
{
|
|
}
|
|
|
|
static void test_expr_value_restored_after_push_modify_pop(void)
|
|
{
|
|
expr_t a, b;
|
|
|
|
am.regs[EXPR] = &a;
|
|
am_push(&am, EXPR);
|
|
am.regs[EXPR] = &b;
|
|
am_pop(&am, EXPR);
|
|
|
|
TEST_ASSERT_EQUAL(&a, am.regs[EXPR]);
|
|
}
|
|
|
|
static void test_argl_value_restored_after_push_modify_pop(void)
|
|
{
|
|
expr_t a, b;
|
|
|
|
am.regs[ARGL] = &a;
|
|
am_push(&am, ARGL);
|
|
am.regs[ARGL] = &b;
|
|
am_pop(&am, ARGL);
|
|
|
|
TEST_ASSERT_EQUAL(&a, am.regs[ARGL]);
|
|
}
|
|
|
|
static void test_append_val_42_to_empty_argl(void)
|
|
{
|
|
am.regs[ARGL] = expr_empty_list(&am);
|
|
am.regs[VAL] = expr_integer(&am, 42);
|
|
am_append(&am, ARGL, VAL);
|
|
|
|
TEST_ASSERT_FALSE(am.regs[ARGL]->is_atom);
|
|
TEST_ASSERT_NOT_NULL(CAR(am.regs[ARGL]));
|
|
|
|
TEST_ASSERT_TRUE(CAR(am.regs[ARGL])->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CAR(am.regs[ARGL])->atom.type);
|
|
TEST_ASSERT_EQUAL(42, CAR(am.regs[ARGL])->atom.integer);
|
|
|
|
TEST_ASSERT_TRUE(CDR(am.regs[ARGL])->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, CDR(am.regs[ARGL])->atom.type);
|
|
}
|
|
|
|
static void test_append_unev_2_to_expr_list_1(void)
|
|
{
|
|
am.regs[EXPR]
|
|
= expr_pair(&am, expr_integer(&am, 1), expr_empty_list(&am));
|
|
am.regs[UNEV] = expr_integer(&am, 2);
|
|
am_append(&am, EXPR, UNEV);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[EXPR]);
|
|
TEST_ASSERT_FALSE(am.regs[EXPR]->is_atom);
|
|
|
|
TEST_ASSERT_NOT_NULL(CAR(am.regs[EXPR]));
|
|
TEST_ASSERT_TRUE(CAR(am.regs[EXPR])->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CAR(am.regs[EXPR])->atom.type);
|
|
TEST_ASSERT_EQUAL(1, CAR(am.regs[EXPR])->atom.integer);
|
|
|
|
TEST_ASSERT_NOT_NULL(CDR(am.regs[EXPR]));
|
|
TEST_ASSERT_FALSE(CDR(am.regs[EXPR])->is_atom);
|
|
|
|
TEST_ASSERT_NOT_NULL(CADR(am.regs[EXPR]));
|
|
TEST_ASSERT_TRUE(CADR(am.regs[EXPR])->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CADR(am.regs[EXPR])->atom.type);
|
|
TEST_ASSERT_EQUAL(2, CADR(am.regs[EXPR])->atom.integer);
|
|
}
|
|
|
|
static void test_car_of_expr_pair_1_2(void)
|
|
{
|
|
am.regs[EXPR]
|
|
= expr_pair(&am, expr_integer(&am, 1), expr_integer(&am, 2));
|
|
am_car(&am, EXPR, EXPR);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[EXPR]);
|
|
TEST_ASSERT_TRUE(am.regs[EXPR]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[EXPR]->atom.type);
|
|
TEST_ASSERT_EQUAL(1, am.regs[EXPR]->atom.integer);
|
|
}
|
|
|
|
static void test_cdr_of_expr_pair_1_2(void)
|
|
{
|
|
am.regs[EXPR]
|
|
= expr_pair(&am, expr_integer(&am, 1), expr_integer(&am, 2));
|
|
am_cdr(&am, EXPR, EXPR);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[EXPR]);
|
|
TEST_ASSERT_TRUE(am.regs[EXPR]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[EXPR]->atom.type);
|
|
TEST_ASSERT_EQUAL(2, am.regs[EXPR]->atom.integer);
|
|
}
|
|
|
|
static void test_assign_expr_to_val(void)
|
|
{
|
|
am.regs[EXPR] = expr_integer(&am, 42);
|
|
am_assign(&am, VAL, EXPR);
|
|
|
|
TEST_ASSERT_NOT_NULL(am.regs[EXPR]);
|
|
TEST_ASSERT_TRUE(am.regs[EXPR]->is_atom);
|
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.regs[EXPR]->atom.type);
|
|
TEST_ASSERT_EQUAL(42, am.regs[EXPR]->atom.integer);
|
|
}
|
|
|
|
static void test_call_prim_proc_val(void)
|
|
{
|
|
am.regs[VAL] = expr_prim_proc(&am, test_prim_proc);
|
|
am_call(&am, VAL);
|
|
TEST_ASSERT_TRUE(test_prim_proc_called);
|
|
}
|
|
|
|
static void test_call_prim_proc_expr(void)
|
|
{
|
|
am.regs[EXPR] = expr_prim_proc(&am, test_prim_proc);
|
|
am_call(&am, EXPR);
|
|
TEST_ASSERT_TRUE(test_prim_proc_called);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_expr_value_restored_after_push_modify_pop);
|
|
RUN_TEST(test_argl_value_restored_after_push_modify_pop);
|
|
RUN_TEST(test_append_val_42_to_empty_argl);
|
|
RUN_TEST(test_append_unev_2_to_expr_list_1);
|
|
RUN_TEST(test_car_of_expr_pair_1_2);
|
|
RUN_TEST(test_cdr_of_expr_pair_1_2);
|
|
RUN_TEST(test_assign_expr_to_val);
|
|
RUN_TEST(test_call_prim_proc_val);
|
|
RUN_TEST(test_call_prim_proc_expr);
|
|
return UNITY_END();
|
|
}
|