Files
refcount/test/test_refcount.c
T

212 lines
6.2 KiB
C
Raw Normal View History

2025-09-06 04:29:50 -07:00
#undef NDEBUG
2025-08-28 04:59:23 -07:00
#include "alloc.h"
#include <assert.h>
#include <refcount/refcount.h>
typedef struct A {
int num;
RefcountEntry refcount;
char *str;
2025-08-30 21:21:02 -07:00
struct A *next;
2025-08-28 04:59:23 -07:00
} 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);
2025-08-30 21:21:02 -07:00
a->next = NULL;
2025-08-28 04:59:23 -07:00
refcount_context_init_obj(c, a);
return a;
}
2025-08-30 21:21:02 -07:00
bool held_refs_callback(void *a_raw, RefcountList **out, void *ignored) {
A *a = a_raw;
2025-08-31 00:34:08 -07:00
*out = refcount_list_push_full(*out, a->next, &COUNTING_ALLOCATOR);
2025-08-30 21:21:02 -07:00
return true;
}
2025-09-08 15:55:18 -07:00
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));
}
2025-08-28 04:59:23 -07:00
A *a = a_raw;
counting_free(a->str);
counting_free(a);
}
2025-09-07 05:16:23 -07:00
void reref_destructor(void *a, void *ctx_raw) {
const RefcountContext *ctx = ctx_raw;
refcount_context_ref(ctx, a);
}
2025-08-30 21:21:02 -07:00
int main(int argc, const char **argv) {
2025-09-08 15:55:18 -07:00
struct ContextAndFlag ctx_and_flag = {.should_be_doing_gc = false};
RefcountContext *c = refcount_make_context(
2025-09-09 15:13:23 -07:00
offsetof(A, refcount), NULL, held_refs_callback, destroy_callback,
2025-09-08 15:55:18 -07:00
&ctx_and_flag, &COUNTING_ALLOCATOR);
ctx_and_flag.ctx = c;
2025-08-28 04:59:23 -07:00
#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);
2025-09-09 05:45:57 -07:00
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);
2025-09-07 05:16:23 -07:00
2025-08-28 04:59:23 -07:00
A *a = make_a(c, 10, "Hello world\n");
assert(!refcount_context_is_static(c, a));
assert(refcount_context_num_refs(c, a) == 1);
2025-08-30 21:21:02 -07:00
a = refcount_context_ref(c, a);
2025-08-28 04:59:23 -07:00
assert(refcount_context_num_refs(c, a) == 2);
2025-09-07 16:54:57 -07:00
a = refcount_context_ref(c, a);
assert(refcount_context_num_refs(c, a) == 3);
2025-08-30 21:21:02 -07:00
a = refcount_context_unref(c, a);
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 2);
2025-08-28 04:59:23 -07:00
2025-09-07 16:54:57 -07:00
a = refcount_context_unref(c, a);
2025-08-28 04:59:23 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-08-30 21:21:02 -07:00
a = refcount_context_unref(c, a);
assert(!a);
a = make_a(c, 10, "Hello World\n");
2025-09-07 16:54:57 -07:00
a->next = make_a(c, 42, "The answer!");
2025-08-30 21:21:02 -07:00
assert(refcount_context_num_refs(c, a->next) == 1);
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-08-30 21:21:02 -07:00
2025-08-28 04:59:23 -07:00
refcount_context_unref(c, a);
2025-08-30 21:21:02 -07:00
a = make_a(c, 'a', "a");
a->next = refcount_context_ref(c, a);
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 2);
2025-08-30 21:21:02 -07:00
2025-08-31 00:34:08 -07:00
refcount_context_unref(c, a);
2025-09-07 16:54:57 -07:00
assert(refcount_context_garbage_collect(c) == 1);
2025-08-31 00:34:08 -07:00
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
A *na = make_a(c, i, "");
2025-09-07 16:54:57 -07:00
na->next = a;
2025-08-31 00:34:08 -07:00
a = na;
}
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-08-31 00:34:08 -07:00
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;
}
2025-09-07 16:54:57 -07:00
na->next = a;
2025-08-31 00:34:08 -07:00
a = na;
}
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
first->next = a;
2025-08-31 00:34:08 -07:00
a = first;
assert(refcount_context_num_refs(c, a) == 1);
2025-09-09 05:45:57 -07:00
assert(refcount_debug_context_count_object(c, a, a) == 1);
2025-08-31 00:34:08 -07:00
2025-08-30 21:21:02 -07:00
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));
2025-08-30 21:21:02 -07:00
2025-08-31 00:34:08 -07:00
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
A *na = make_a(c, i, "");
2025-09-07 16:54:57 -07:00
na->next = a;
2025-08-31 00:34:08 -07:00
a = na;
}
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-08-31 00:34:08 -07:00
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));
2025-09-08 15:55:18 -07:00
assert(!refcount_context_is_doing_gc(c));
ctx_and_flag.should_be_doing_gc = true;
2025-08-31 00:34:08 -07:00
assert(refcount_context_garbage_collect(c) == 26);
2025-09-08 15:55:18 -07:00
ctx_and_flag.should_be_doing_gc = false;
assert(!refcount_context_is_doing_gc(c));
2025-08-30 21:21:02 -07:00
2025-09-05 20:01:36 -07:00
a = make_a(c, 0, "a");
RefcountWeakref *w = refcount_context_make_weakref(c, a);
assert(w);
assert(refcount_context_num_refs(c, a) == 1);
2025-09-07 16:54:57 -07:00
A *b = refcount_context_strengthen(c, w);
assert(b);
assert(a == b);
2025-09-05 20:01:36 -07:00
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);
2025-09-07 05:16:23 -07:00
a = make_a(c, 10, "test destructor");
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-09-07 05:16:23 -07:00
assert(refcount_context_add_destructor(c, a, &key, reref_destructor, c));
2025-09-07 16:54:57 -07:00
assert(refcount_context_num_refs(c, a) == 1);
2025-09-07 05:16:23 -07:00
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);
2025-09-07 05:16:23 -07:00
2025-08-28 04:59:23 -07:00
refcount_context_destroy(c);
counting_free(static_a_2.str);
counting_free(static_a_3.str);
2025-08-28 04:59:23 -07:00
check_allocator_status();
return 0;
}