aboutsummaryrefslogtreecommitdiff
path: root/test/gc.c
blob: 7796322c0640e65105081864838e607d2896468c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include "kpl_test.h"

#define TEST_OBJECT_COUNT 10

KPL_TEST(basic_mark_and_sweep) {
    kpl_thread thread = {};
    kpl_memory_allocator_init(&thread.memory_allocator);
    kpl_gc_init(&thread.gc);
    kpl_object_gc *objs[TEST_OBJECT_COUNT] = {};
    for (int32_t obj_index = 1; obj_index <= TEST_OBJECT_COUNT; obj_index++) {
        objs[obj_index - 1] = kpl_gc_new(&thread, NULL, sizeof(kpl_object_error));
        if (obj_index % 2 == 0)
            kpl_gc_mark(objs[obj_index - 1]);
    }
    int32_t counter = 0;
    kpl_object_gc *gc_head = thread.gc.gc_head;
    while (gc_head) {
        counter++;
        gc_head = gc_head->gc_next;
    }
    KPL_ASSERT(counter == TEST_OBJECT_COUNT, "should have TEST_OBJECT_COUNT Objects in gc list");
    kpl_gc_prepare_sweep(&thread.gc);
    kpl_gc_sweep(&thread);
    gc_head = thread.gc.gc_head;
    counter = 0;
    while (gc_head) {
        counter++;
        gc_head = gc_head->gc_next;
    }
    KPL_ASSERT(counter == TEST_OBJECT_COUNT / 2, "should have removed half of the objects");
    kpl_gc_free(&thread.gc, &thread);
    kpl_memory_allocator_free(&thread.memory_allocator);
}