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