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