]> begriffs open source - libderp/blob - test/t_vector.c
Allow allocator customization at runtime
[libderp] / test / t_vector.c
1 #include <assert.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4
5 #include "derp/common.h"
6 #include "derp/vector.h"
7
8 #ifdef HAVE_BOEHM_GC
9 #include <gc/leak_detector.h>
10 #endif
11
12 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
13
14 int cmpint(const void *a, const void *b, void *aux)
15 {
16         (void)aux;
17         return *(int*)a - *(int*)b;
18 }
19
20 int main(void)
21 {
22 #ifdef HAVE_BOEHM_GC
23         GC_set_find_leak(1);
24         derp_use_alloc_funcs(
25                 GC_debug_malloc_replacement,
26                 GC_debug_realloc_replacement, GC_debug_free);
27 #endif
28
29         int ivals[] =  {0,1,2,3,4,5,6,7,8,9},
30             ivals2[] = {10,11,12,13,14,15,16,17,18,19};
31         size_t i;
32         vector *vint = v_new();
33         assert(v_length(vint) == 0);
34         assert(v_capacity(vint) > 0);
35         assert(v_is_empty(vint));
36         assert(!v_at(vint, 0));
37         assert(!v_at(vint, 1337));
38
39         size_t too_big = SIZE_MAX;
40         /* too large a number for our address space */
41         assert(v_reserve_capacity(vint, too_big) < too_big);
42         /* still too large for address space */
43         too_big = 1 + SIZE_MAX/sizeof(void*);
44         /* technically possible, but not on today's machines */
45         too_big /= 2;
46         assert(v_reserve_capacity(vint, too_big) < too_big);
47
48         v_set_length(vint, 2);
49         assert(v_length(vint) == 2);
50         assert(v_at(vint, 0) == NULL);
51         assert(v_at(vint, 1) == NULL);
52         v_clear(vint);
53
54         for (i = 0; i < ARRAY_LEN(ivals); i++)
55                 v_prepend(vint, ivals+i);
56         assert(v_length(vint) == ARRAY_LEN(ivals));
57         assert(*(int*)v_first(vint) == 9);
58         assert(*(int*)v_remove_first(vint) == 9);
59         assert(*(int*)v_remove_first(vint) == 8);
60         v_sort(vint, cmpint, NULL);
61         assert(*(int*)v_first(vint) == 0);
62
63         v_clear(vint);
64         assert(v_length(vint) == 0);
65         for (i = 0; i < ARRAY_LEN(ivals); i++)
66                 v_append(vint, ivals+i);
67         assert(v_length(vint) == ARRAY_LEN(ivals));
68         assert(*(int*)v_last(vint) == 9);
69         assert(*(int*)v_remove_last(vint) == 9);
70         assert(*(int*)v_remove_last(vint) == 8);
71
72         assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
73         v_insert(vint, 5, ivals2+5);
74         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
75         v_swap(vint, 5, 6);
76         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
77         assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == 6);
78         v_remove(vint, 6);
79         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
80         assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
81
82         assert(v_find_last_index(vint, v_last(vint), cmpint, NULL)
83                         == v_length(vint)-1);
84
85         v_clear(vint);
86         int revme_even[] = {1,2};
87         for (i = 0; i < ARRAY_LEN(revme_even); i++)
88                 v_append(vint, revme_even+i);
89         v_reverse(vint);
90         assert(*(int*)v_at(vint, 0) == 2);
91         assert(*(int*)v_at(vint, 1) == 1);
92
93         v_clear(vint);
94         int revme_odd[] = {1,2,3};
95         for (i = 0; i < ARRAY_LEN(revme_odd); i++)
96                 v_append(vint, revme_odd+i);
97         v_reverse(vint);
98         assert(*(int*)v_at(vint, 0) == 3);
99         assert(*(int*)v_at(vint, 1) == 2);
100         assert(*(int*)v_at(vint, 2) == 1);
101
102         v_set_length(vint, 1024);
103         assert(v_at(vint, 1023) == NULL);
104         v_append(vint, ivals);
105         assert(*(int*)v_at(vint, 1024) == 0);
106
107         v_clear(vint);
108         /* test for memory leak */
109         v_dtor(vint, derp_free, NULL);
110         int *life = malloc(sizeof *life);
111         *life = 42;
112         v_append(vint, life);
113
114         v_free(vint);
115
116 #ifdef HAVE_BOEHM_GC
117         CHECK_LEAKS();
118 #endif
119         return 0;
120 }