diff options
Diffstat (limited to 'env.c')
| -rw-r--r-- | env.c | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -132,3 +132,92 @@ struct list *env_clone(struct list *env) return clone; } + +#ifdef BUILD_TEST + +#include "test_util.h" + +TEST(simple_lookup_on_empty_env) +{ + struct list *env = env_new(); + ASSERT_TRUE(env != NULL); + ASSERT_EQ(NULL, env_lookup(env, "foobar")); +} + +TEST(simple_set_and_lookup) +{ + struct list *env = env_new(); + ASSERT_TRUE(env != NULL); + + struct atom atom; + + ASSERT_EQ(1, env_set(env, "foobar", &atom)); + ASSERT_EQ(&atom, env_lookup(env, "foobar")); +} + +TEST(lookup_from_inner_env) +{ + struct list *outer = env_new(); + + struct atom *atom1 = atom_new_int(42); + ASSERT_EQ(1, env_set(outer, "foo", atom1)); + + struct atom *atom2 = atom_new_int(6); + struct list *inner = env_extend(outer, 1, "bar", atom2); + + ASSERT_TRUE(inner != NULL); + ASSERT_TRUE(inner != outer); + + struct atom *atom = env_lookup(inner, "foo"); + ASSERT_TRUE(atom != NULL); + ASSERT_EQ(atom1->type, atom->type); + ASSERT_EQ(atom1->l, atom->l); + + atom = env_lookup(inner, "bar"); + ASSERT_TRUE(atom != NULL); + ASSERT_EQ(atom2->type, atom->type); + ASSERT_EQ(atom2->l, atom->l); +} + +TEST(lookup_deeply_nested) +{ + struct list *env = env_new(); + env_set(env, "a", atom_new_int(1)); + env = env_extend(env, 1, "b", atom_new_int(2)); + env = env_extend(env, 1, "c", atom_new_int(3)); + env = env_extend(env, 1, "d", atom_new_int(4)); + env = env_extend(env, 1, "e", atom_new_int(5)); + + struct atom *atom = env_lookup(env, "a"); + ASSERT_TRUE(atom != NULL); + ASSERT_EQ(ATOM_INT, atom->type); + ASSERT_EQ(1, atom->l); + + atom = env_lookup(env, "e"); + ASSERT_TRUE(atom != NULL); + ASSERT_EQ(ATOM_INT, atom->type); + ASSERT_EQ(5, atom->l); +} + +TEST(extend) +{ + struct list *env = env_new(); + + env_set(env, "foo", atom_new_int(1)); + + env = env_extend(env, 1, "foo", atom_new_int(2)); + + struct atom *atom = env_lookup(env, "foo"); + ASSERT_TRUE(atom != NULL); + ASSERT_EQ(ATOM_INT, atom->type); + ASSERT_EQ(2, atom->l); +} + +TEST(redefine_illegal) +{ + struct list *env = env_new(); + ASSERT_EQ(1, env_set(env, "foo", atom_new_int(1))); + ASSERT_EQ(0, env_set(env, "foo", atom_new_int(2))); +} + +#endif /* BUILD_TEST */ |
