Added basic testing

This commit is contained in:
Andrew Scott 2024-08-20 13:19:39 -04:00
parent 8568ebe52b
commit ad5dfd4146
Signed by: a
GPG key ID: 7CD5A5977E4931C1
2 changed files with 124 additions and 1 deletions

109
main.c
View file

@ -1 +1,108 @@
int main(void) { return 0; }
#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;
}

16
test_gc.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef TEST_GC_H
#define TEST_GC_H
void test_int_alloc(void);
void test_pair_alloc(void);
void test_obj_count(void);
void test_nested_pair(void);
void test_unreachable(void);
void test_auto_gc(void);
#endif /* TEST_GC_H */