Initial commit
This commit is contained in:
8
test/CMakeLists.txt
Normal file
8
test/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
link_libraries(refcount)
|
||||
add_compile_options(-Wall)
|
||||
|
||||
add_executable(test_list test_list.c)
|
||||
add_test(NAME list COMMAND test_list)
|
||||
|
||||
add_executable(test_refcount test_refcount.c)
|
||||
add_test(NAME refcount COMMAND test_refcount)
|
75
test/alloc.h
Normal file
75
test/alloc.h
Normal file
@ -0,0 +1,75 @@
|
||||
#ifndef INCLUDED_TEST_ALLOC_H
|
||||
#define INCLUDED_TEST_ALLOC_H
|
||||
|
||||
#include <refcount/allocator.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if __has_attribute(unused)
|
||||
# define UNUSED __attribute__((unused))
|
||||
#else
|
||||
# define UNUSED
|
||||
#endif
|
||||
|
||||
static intmax_t _alloc_count = 0;
|
||||
|
||||
static UNUSED void *counting_malloc(size_t size) {
|
||||
void *ptr = malloc(size);
|
||||
if (!ptr) {
|
||||
return NULL;
|
||||
}
|
||||
++_alloc_count;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static UNUSED void *counting_calloc(size_t n, size_t size) {
|
||||
void *ptr = calloc(n, size);
|
||||
if (!ptr) {
|
||||
return NULL;
|
||||
}
|
||||
++_alloc_count;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static UNUSED void *counting_realloc(void *old_ptr, size_t size) {
|
||||
if (!old_ptr) {
|
||||
return counting_malloc(size);
|
||||
}
|
||||
return realloc(old_ptr, size);
|
||||
}
|
||||
|
||||
static UNUSED void counting_free(void *ptr) {
|
||||
if (ptr) {
|
||||
free(ptr);
|
||||
--_alloc_count;
|
||||
}
|
||||
}
|
||||
|
||||
static UNUSED void check_allocator_status() {
|
||||
if (_alloc_count > 0) {
|
||||
fprintf(stderr, "Memory leak detected, allocation count is %jd!\n",
|
||||
_alloc_count);
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (_alloc_count < 0) {
|
||||
fprintf(stderr, "%jd more free's that malloc's!", _alloc_count);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
static UNUSED char *counting_strdup(const char *str) {
|
||||
size_t len = strlen(str);
|
||||
char *ns = counting_malloc(len + 1);
|
||||
strcpy(ns, str);
|
||||
return ns;
|
||||
}
|
||||
|
||||
static UNUSED const RefcountAllocator COUNTING_ALLOCATOR = {
|
||||
.malloc = counting_malloc,
|
||||
.realloc = counting_realloc,
|
||||
.free = counting_free,
|
||||
};
|
||||
|
||||
#endif
|
141
test/test_list.c
Normal file
141
test/test_list.c
Normal file
@ -0,0 +1,141 @@
|
||||
#include "alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <refcount/list.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
refcount_global_allocator = &COUNTING_ALLOCATOR;
|
||||
|
||||
assert(refcount_list_length(NULL) == 0);
|
||||
refcount_list_free(NULL, counting_free);
|
||||
|
||||
check_allocator_status();
|
||||
|
||||
RefcountList *l = NULL;
|
||||
l = refcount_list_push(l, counting_strdup("str1"));
|
||||
assert(!l->prev);
|
||||
assert(refcount_list_length(l) == 1);
|
||||
assert(strcmp(refcount_list_peek(l), "str1") == 0);
|
||||
assert(refcount_list_peek(l) == refcount_list_nth(l, 0));
|
||||
|
||||
l = refcount_list_push(l, counting_strdup("str2"));
|
||||
assert(!l->prev);
|
||||
assert(l == l->next->prev);
|
||||
assert(refcount_list_length(l) == 2);
|
||||
assert(strcmp(refcount_list_peek(l), "str2") == 0);
|
||||
assert(refcount_list_peek(l) == refcount_list_nth(l, 0));
|
||||
|
||||
l = refcount_list_push(l, counting_strdup("str3"));
|
||||
assert(refcount_list_length(l) == 3);
|
||||
assert(strcmp(refcount_list_peek(l), "str3") == 0);
|
||||
assert(refcount_list_peek(l) == refcount_list_nth(l, 0));
|
||||
|
||||
l = refcount_list_push_back(l, counting_strdup("str4"));
|
||||
assert(refcount_list_length(l) == 4);
|
||||
assert(strcmp(refcount_list_peek(l), "str3") == 0);
|
||||
assert(refcount_list_drop(l, 2) == refcount_list_drop(l, 3)->prev);
|
||||
assert(refcount_list_drop(l, 2) == refcount_list_drop(l, 3)->prev);
|
||||
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 2), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 3), "str4") == 0);
|
||||
assert(!refcount_list_nth(l, 4));
|
||||
|
||||
RefcountList *sl = refcount_list_drop(l, 2);
|
||||
assert(strcmp(refcount_list_nth(sl, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(sl, 1), "str4") == 0);
|
||||
|
||||
sl = sl->next;
|
||||
l = refcount_list_remove(l, sl->prev, counting_free);
|
||||
assert(sl->prev == refcount_list_drop(l, 1));
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 2), "str4") == 0);
|
||||
assert(!refcount_list_nth(l, 3));
|
||||
|
||||
l = refcount_list_pop(l, counting_free);
|
||||
assert(refcount_list_length(l) == 2);
|
||||
assert(strcmp(refcount_list_peek(l), "str2") == 0);
|
||||
|
||||
l = refcount_list_pop(l, counting_free);
|
||||
assert(refcount_list_length(l) == 1);
|
||||
assert(strcmp(refcount_list_peek(l), "str4") == 0);
|
||||
|
||||
l = refcount_list_pop(l, counting_free);
|
||||
assert(!l);
|
||||
assert(refcount_list_length(l) == 0);
|
||||
|
||||
l = refcount_list_build(5, counting_strdup("str1"), counting_strdup("str2"),
|
||||
counting_strdup("str3"), counting_strdup("str4"),
|
||||
counting_strdup("str5"));
|
||||
assert(refcount_list_length(l) == 5);
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 2), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 3), "str4") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 4), "str5") == 0);
|
||||
|
||||
RefcountList *l2 = refcount_list_copy(l, (void *) counting_strdup);
|
||||
|
||||
refcount_list_free(l, counting_free);
|
||||
|
||||
assert(refcount_list_length(l2) == 5);
|
||||
assert(strcmp(refcount_list_nth(l2, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 1), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 2), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 3), "str4") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 4), "str5") == 0);
|
||||
|
||||
l2 = refcount_list_reverse(l2);
|
||||
|
||||
assert(refcount_list_length(l2) == 5);
|
||||
assert(strcmp(refcount_list_nth(l2, 0), "str5") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 1), "str4") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 2), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 3), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l2, 4), "str1") == 0);
|
||||
|
||||
refcount_list_free(l2, counting_free);
|
||||
|
||||
l = refcount_list_build(2, counting_strdup("str1"),
|
||||
counting_strdup("str2"));
|
||||
l2 = refcount_list_build(2, counting_strdup("str3"),
|
||||
counting_strdup("str4"));
|
||||
l = refcount_list_join(l, l2);
|
||||
assert(refcount_list_length(l) == 4);
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 2), "str3") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 3), "str4") == 0);
|
||||
|
||||
sl = refcount_list_drop(l, 3);
|
||||
l = refcount_list_remove(l, sl, counting_free);
|
||||
assert(refcount_list_length(l) == 3);
|
||||
assert(strcmp(refcount_list_nth(l, 2), "str3") == 0);
|
||||
assert(!refcount_list_nth(l, 3));
|
||||
|
||||
refcount_list_free(l, counting_free);
|
||||
|
||||
l = refcount_list_build(2, counting_strdup("str1"),
|
||||
counting_strdup("str2"));
|
||||
l = refcount_list_join(NULL, l);
|
||||
assert(refcount_list_length(l) == 2);
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
|
||||
refcount_list_free(l, counting_free);
|
||||
|
||||
l = refcount_list_build(2, counting_strdup("str1"),
|
||||
counting_strdup("str2"));
|
||||
l = refcount_list_join(l, NULL);
|
||||
assert(refcount_list_length(l) == 2);
|
||||
assert(strcmp(refcount_list_nth(l, 0), "str1") == 0);
|
||||
assert(strcmp(refcount_list_nth(l, 1), "str2") == 0);
|
||||
|
||||
refcount_list_free(l, counting_free);
|
||||
|
||||
check_allocator_status();
|
||||
return 0;
|
||||
}
|
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