marCsweep/main.c

109 lines
2.2 KiB
C
Raw Normal View History

2024-08-20 13:19:39 -04:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "gc.h"
#include "test_gc.h"
#include "vm.h"
void test_int_alloc() {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
assert(vm->refCount == 4 &&
"test_int_alloc: GARBAGE_INT allocation failure\n");
printf("test_int_alloc: PASS\n");
free(vm);
}
void test_pair_alloc() {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
assert(vm->refCount == 6 &&
"test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
free(vm);
}
void test_obj_count() {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
collect(vm);
assert(vm->refCount == 4 &&
"test_obj_count: FAILED: GC occurred when it shouldn't have\n");
printf("test_obj_count: PASS\n");
free(vm);
}
void test_nested_pair() {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushPair(vm);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
collect(vm);
assert(vm->refCount == 7 &&
"test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
free(vm);
}
void test_unreachable() {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pop(vm);
pop(vm);
collect(vm);
assert(vm->refCount == 0 &&
"test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
printf("test_unreachable: PASS\n");
free(vm);
}
void test_auto_gc() {
struct virtualMachine *vm = initVM();
for (size_t i = 0; i < 505; ++i) {
pushInt(vm, 1);
}
for (size_t i = 0; i < 5; ++i) {
pop(vm);
}
for (size_t i = 0; i < 50; ++i) {
pushInt(vm, 2);
}
assert(vm->refCount == 550 &&
"test_auto_gc: FAILED: 5 references should have been freed\n");
printf("test_auto_gc: PASS\n");
free(vm);
}
int main(void) {
test_int_alloc();
test_pair_alloc();
test_obj_count();
test_nested_pair();
test_unreachable();
test_auto_gc();
return 0;
}