Skip to content

Commit

Permalink
Implement and test booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
nelhage committed Jan 3, 2009
1 parent b49cf2d commit 1c6f89c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scgc.c
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions scgc.h
Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1c6f89c

Please sign in to comment.