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