Files
refcount/test/test_refcount.c

212 lines
6.2 KiB
C

#undef NDEBUG
#include "alloc.h"
#include <assert.h>
#include <refcount/refcount.h>
typedef struct A {
int num;
RefcountEntry refcount;
char *str;
struct A *next;
} A;
A *make_a(RefcountContext *c, int n, const char *s) {
A *a = counting_malloc(sizeof(A));
a->num = n;
a->str = counting_strdup(s);
a->next = NULL;
refcount_context_init_obj(c, a);
return a;
}
bool held_refs_callback(void *a_raw, RefcountList **out, void *ignored) {
A *a = a_raw;
*out = refcount_list_push_full(*out, a->next, &COUNTING_ALLOCATOR);
return true;
}
struct ContextAndFlag {
const RefcountContext *ctx;
bool should_be_doing_gc;
};
void destroy_callback(void *a_raw, void *ctx_and_flag_raw) {
struct ContextAndFlag *ctx_and_flag = ctx_and_flag_raw;
if (ctx_and_flag->should_be_doing_gc) {
assert(refcount_context_is_doing_gc(ctx_and_flag->ctx));
}
A *a = a_raw;
counting_free(a->str);
counting_free(a);
}
void reref_destructor(void *a, void *ctx_raw) {
const RefcountContext *ctx = ctx_raw;
refcount_context_ref(ctx, a);
}
int main(int argc, const char **argv) {
struct ContextAndFlag ctx_and_flag = {.should_be_doing_gc = false};
RefcountContext *c = refcount_make_context(
offsetof(A, refcount), held_refs_callback, destroy_callback,
&ctx_and_flag, &COUNTING_ALLOCATOR);
ctx_and_flag.ctx = c;
#define STATIC_A(id) \
A static_a_##id = { \
.num = __LINE__, \
.str = counting_strdup("static " #id), \
.next = make_a(c, 0, "in static " #id), \
}; \
refcount_context_init_static(c, &static_a_##id)
STATIC_A(1);
STATIC_A(2);
STATIC_A(3);
assert(refcount_debug_context_count_object(c, &static_a_1, static_a_1.next)
== 1);
assert(refcount_debug_context_count_object(c, &static_a_1, static_a_2.next)
== 0);
A *a = make_a(c, 10, "Hello world\n");
assert(!refcount_context_is_static(c, a));
assert(refcount_context_num_refs(c, a) == 1);
a = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
a = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 3);
a = refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
a = refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
a = refcount_context_unref(c, a);
assert(!a);
a = make_a(c, 10, "Hello World\n");
a->next = make_a(c, 42, "The answer!");
assert(refcount_context_num_refs(c, a->next) == 1);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_unref(c, a);
a = make_a(c, 'a', "a");
a->next = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
refcount_context_unref(c, a);
assert(refcount_context_garbage_collect(c) == 1);
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
A *na = make_a(c, i, "");
na->next = a;
a = na;
}
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_unref(c, a);
A *first = NULL;
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
A *na = make_a(c, i, "");
if (!first) {
first = na;
}
na->next = a;
a = na;
}
assert(refcount_context_num_refs(c, a) == 1);
first->next = a;
a = first;
assert(refcount_context_num_refs(c, a) == 1);
assert(refcount_debug_context_count_object(c, a, a) == 1);
refcount_context_ref(c, a);
refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
int key;
A *a_with_destructor = a;
assert(refcount_context_add_destructor(c, a, &key, reref_destructor, c));
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
A *na = make_a(c, i, "");
na->next = a;
a = na;
}
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_unref(c, a);
assert(refcount_context_garbage_collect(c) == 0);
assert(refcount_context_num_refs(c, a_with_destructor) == 1);
assert(refcount_context_remove_destructor(c, a_with_destructor, &key));
assert(refcount_context_ref(c, a_with_destructor));
assert(refcount_context_garbage_collect(c) == 0);
assert(refcount_context_unref(c, a_with_destructor));
assert(!refcount_context_is_doing_gc(c));
ctx_and_flag.should_be_doing_gc = true;
assert(refcount_context_garbage_collect(c) == 26);
ctx_and_flag.should_be_doing_gc = false;
assert(!refcount_context_is_doing_gc(c));
a = make_a(c, 0, "a");
RefcountWeakref *w = refcount_context_make_weakref(c, a);
assert(w);
assert(refcount_context_num_refs(c, a) == 1);
A *b = refcount_context_strengthen(c, w);
assert(b);
assert(a == b);
assert(refcount_context_num_refs(c, a) == 2);
RefcountWeakref *x = refcount_context_weaken(c, a);
w = refcount_context_make_weakref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
refcount_context_destroy_weakref(c, x);
refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 2);
x = refcount_context_weaken(c, a);
assert(refcount_context_num_refs(c, a) == 1);
assert(refcount_context_weakref_is_valid(c, w));
assert(refcount_context_weakref_is_valid(c, x));
assert(!refcount_context_unref(c, a));
assert(!refcount_context_weakref_is_valid(c, w));
assert(!refcount_context_weakref_is_valid(c, x));
refcount_context_destroy_weakref(c, w);
refcount_context_destroy_weakref(c, x);
a = make_a(c, 10, "test destructor");
assert(refcount_context_num_refs(c, a) == 1);
assert(refcount_context_add_destructor(c, a, &key, reref_destructor, c));
assert(refcount_context_num_refs(c, a) == 1);
assert(!refcount_context_unref(c, a));
assert(refcount_context_num_refs(c, a) == 1);
assert(refcount_context_remove_destructor(c, a, &key));
assert(refcount_context_num_refs(c, a) == 1);
assert(!refcount_context_unref(c, a));
refcount_context_deinit_static(c, &static_a_1);
counting_free(static_a_1.str);
refcount_context_destroy(c);
counting_free(static_a_2.str);
counting_free(static_a_3.str);
check_allocator_status();
return 0;
}