Permalink
Browse files

Implement and test booleans

  • Loading branch information...
nelhage committed Jan 3, 2009
1 parent b49cf2d commit 1c6f89c84c12a0a822680f5e276eeba33bef8300
Showing with 43 additions and 0 deletions.
  1. +27 −0 scgc.c
  2. +6 −0 scgc.h
  3. +10 −0 tests.c
View
27 scgc.c
@@ -4,6 +4,8 @@
#define STRLEN2CELLS(x) (ROUNDUP(((uint32_t)(x)),sizeof(gc_handle))/sizeof(gc_handle))
+gc_handle sc_true, sc_false;
+
/* Types */
typedef struct sc_cons {
gc_chunk header;
@@ -25,6 +27,11 @@ typedef struct sc_vector {
gc_handle vector[];
} sc_vector;
+typedef struct sc_boolean {
+ gc_chunk header;
+ int val;
+} sc_boolean;
+
/* Op functions */
uint32_t sc_len_string(gc_chunk *v) {
@@ -39,6 +46,10 @@ uint32_t sc_len_vector(gc_chunk *v) {
return 1 + ((sc_vector*)v)->veclen;
}
+uint32_t sc_len_boolean(gc_chunk *v UNUSED) {
+ return 1;
+}
+
void sc_relocate_cons(gc_chunk *v) {
sc_cons *cons = (sc_cons*)v;
gc_relocate(&cons->car);
@@ -75,6 +86,11 @@ static struct gc_ops sc_vector_ops = {
.op_len = sc_len_vector
};
+static struct gc_ops sc_boolean_ops = {
+ .op_relocate = gc_relocate_nop,
+ .op_len = sc_len_boolean
+};
+
/* Public API */
gc_handle sc_car(gc_handle c) {
@@ -161,6 +177,10 @@ int sc_vectorp(gc_handle c) {
return sc_pointer_typep(c, &sc_vector_ops);
}
+int sc_booleanp(gc_handle c) {
+ return sc_pointer_typep(c, &sc_boolean_ops);
+}
+
int sc_numberp(gc_handle c) {
return gc_numberp(c);
}
@@ -201,3 +221,10 @@ gc_handle sc_make_string(char *string) {
strcpy(sc_string_get(s), string);
return s;
}
+
+void sc_init() {
+ sc_true = gc_tag_pointer(gc_alloc(&sc_boolean_ops, 2));
+ UNTAG_PTR(sc_true, sc_boolean)->val = 1;
+ sc_false = gc_tag_pointer(gc_alloc(&sc_boolean_ops, 2));
+ UNTAG_PTR(sc_false, sc_boolean)->val = 0;
+}
View
6 scgc.h
@@ -35,5 +35,11 @@ int sc_stringp(gc_handle c);
int sc_numberp(gc_handle c);
int sc_symbolp(gc_handle c);
int sc_vectorp(gc_handle c);
+int sc_booleanp(gc_handle c);
+
+extern gc_handle sc_true;
+extern gc_handle sc_false;
+
+void sc_init();
#endif
View
10 tests.c
@@ -9,6 +9,7 @@ gc_handle reg1, reg2;
static void gc_core_setup(void) {
gc_init();
+ sc_init();
reg1 = reg2 = NIL;
gc_register_roots(&reg1, &reg2, NULL);
}
@@ -36,6 +37,14 @@ START_TEST(gc_sanity_check)
}
END_TEST
+START_TEST(gc_booleans)
+{
+ fail_unless(sc_booleanp(sc_true));
+ fail_unless(sc_booleanp(sc_false));
+ fail_unless(sc_true != sc_false);
+}
+END_TEST
+
START_TEST(objs_survive_gc)
{
reg1 = sc_alloc_cons();
@@ -270,6 +279,7 @@ Suite *gc_suite()
gc_core_setup,
gc_core_teardown);
tcase_add_test(tc_core, gc_sanity_check);
+ tcase_add_test(tc_core, gc_booleans);
tcase_add_test(tc_core, objs_survive_gc);
tcase_add_test(tc_core, gc_frees_mem);
tcase_add_test(tc_core, gc_cons_cycle);

0 comments on commit 1c6f89c

Please sign in to comment.