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