Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add some more tests to values

  • Loading branch information...
commit 8075052e51787d343251dc2cd303d28236e7acf4 1 parent b719015
@txus authored
Showing with 62 additions and 17 deletions.
  1. +1 −9 src/forkix/value.c
  2. +4 −1 src/forkix/vm.c
  3. +57 −7 tests/value_tests.c
View
10 src/forkix/value.c
@@ -113,14 +113,6 @@ Value_set(VALUE receiver, char *key, VALUE value)
VALUE
Value_get(VALUE receiver, char *key)
{
- VALUE value = (VALUE)Hashmap_get(receiver->table, bfromcstr(key));
-
- if(value != NULL) {
- return value;
- } else {
- return NilObject;
- }
+ return (VALUE)Hashmap_get(receiver->table, bfromcstr(key));
}
-
-
View
5 src/forkix/vm.c
@@ -132,7 +132,10 @@ VALUE VM_run(STATE state, Stack *frames)
check(slot->type == StringType, "Slot name must be a String.");
- Stack_push(stack, Value_get(receiver, VAL2STR(slot)));
+ VALUE value = Value_get(receiver, VAL2STR(slot));
+ check(value, "Undefined slot %s.", VAL2STR(slot));
+
+ Stack_push(stack, value);
break;
}
case SETSLOT: {
View
64 tests/value_tests.c
@@ -2,12 +2,9 @@
#include <forkix/value.h>
#include <assert.h>
-VALUE intval = NULL;
-VALUE strval = NULL;
-
char *test_integer_new()
{
- intval = Integer_new(123);
+ VALUE intval = Integer_new(123);
mu_assert(intval->type == IntegerType, "failed assigning type");
mu_assert(VAL2INT(intval) == 123, "failed assigning integer value");
@@ -17,7 +14,7 @@ char *test_integer_new()
char *test_string_new()
{
- strval = String_new("abc");
+ VALUE strval = String_new("abc");
mu_assert(strval->type == StringType, "failed assigning type");
mu_assert(strcmp(VAL2STR(strval), "abc") == 0, "failed assigning string value");
@@ -25,10 +22,57 @@ char *test_string_new()
return NULL;
}
+char *test_closure_new()
+{
+ Function *fn = Function_new(NULL, NULL);
+ VALUE closure = Closure_new(fn);
+ mu_assert(closure->type == ClosureType, "failed creating closure");
+ mu_assert(VAL2FN(closure) == fn, "failed assigning function to closure");
+
+ return NULL;
+}
+
+char *test_main_new()
+{
+ VALUE main = Main_new();
+ mu_assert(main->type == MainType, "failed creating Main");
+
+ return NULL;
+}
+
char *test_destroy()
{
- Value_destroy(intval);
- mu_assert(!intval->type, "failed destroying integer value")
+ VALUE obj = Integer_new(123);
+ Value_destroy(obj);
+ mu_assert(!obj->type, "failed destroying object");
+
+ return NULL;
+}
+
+char *test_get()
+{
+ VALUE obj = Integer_new(123);
+ VALUE closure = Value_get(obj, "+");
+ mu_assert(closure->type == ClosureType, "failed getting closure");
+
+ return NULL;
+}
+
+char *test_get_undefined()
+{
+ VALUE obj = Integer_new(123);
+ VALUE closure = Value_get(obj, "foo");
+ mu_assert(closure == NULL, "expected NULL, got something");
+
+ return NULL;
+}
+
+char *test_set()
+{
+ VALUE obj = Integer_new(123);
+ VALUE integer = Integer_new(99);
+ Value_set(obj, "foo", integer);
+ mu_assert(Value_get(obj, "foo") == integer, "failed assigning foo");
return NULL;
}
@@ -38,8 +82,14 @@ char *all_tests() {
mu_run_test(test_integer_new);
mu_run_test(test_string_new);
+ mu_run_test(test_closure_new);
+ mu_run_test(test_main_new);
mu_run_test(test_destroy);
+ mu_run_test(test_get);
+ mu_run_test(test_get_undefined);
+ mu_run_test(test_set);
+
return NULL;
}
Please sign in to comment.
Something went wrong with that request. Please try again.