aboutsummaryrefslogtreecommitdiff
path: root/env.c
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2014-05-15 23:38:15 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2014-05-15 23:38:15 +0300
commit360e840e5d2574e8770a6c153c3334a8c9a8cd32 (patch)
treecae4585167b0e3d67329d5de78c3ecafdf3b8d5d /env.c
parent06b32874034251d808394b66bcbdf12afe0f41b4 (diff)
downloadlispish-360e840e5d2574e8770a6c153c3334a8c9a8cd32.tar.gz
lispish-360e840e5d2574e8770a6c153c3334a8c9a8cd32.zip
env tests
Diffstat (limited to 'env.c')
-rw-r--r--env.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/env.c b/env.c
index e58ef70..6d873df 100644
--- a/env.c
+++ b/env.c
@@ -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 */