Initial commit
This commit is contained in:
57
test/test_refcount.c
Normal file
57
test/test_refcount.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include "alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <refcount/refcount.h>
|
||||
|
||||
typedef struct A {
|
||||
int num;
|
||||
RefcountEntry refcount;
|
||||
char *str;
|
||||
} 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);
|
||||
refcount_context_init_obj(c, a);
|
||||
return a;
|
||||
}
|
||||
|
||||
void destroy_callback(void *a_raw) {
|
||||
A *a = a_raw;
|
||||
counting_free(a->str);
|
||||
counting_free(a);
|
||||
}
|
||||
|
||||
int main() {
|
||||
refcount_global_allocator = &COUNTING_ALLOCATOR;
|
||||
|
||||
RefcountContext *c =
|
||||
refcount_make_context(offsetof(A, refcount), NULL, destroy_callback);
|
||||
|
||||
A *a = make_a(c, 10, "Hello world\n");
|
||||
assert(!refcount_context_is_static(c, a));
|
||||
assert(refcount_context_num_refs(c, a) == 0);
|
||||
|
||||
refcount_context_ref(c, a);
|
||||
assert(refcount_context_num_refs(c, a) == 1);
|
||||
|
||||
refcount_context_ref(c, a);
|
||||
assert(refcount_context_num_refs(c, a) == 2);
|
||||
|
||||
refcount_context_unref(c, a);
|
||||
assert(refcount_context_num_refs(c, a) == 1);
|
||||
|
||||
refcount_context_float(c, a);
|
||||
assert(refcount_context_num_refs(c, a) == 0);
|
||||
|
||||
refcount_context_ref(c, a);
|
||||
assert(refcount_context_num_refs(c, a) == 1);
|
||||
|
||||
refcount_context_unref(c, a);
|
||||
|
||||
refcount_context_destroy(c);
|
||||
|
||||
check_allocator_status();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user