]> begriffs open source - libderp/blob - test/t_vector.c
WIP: begriffs style .so installation
[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
9
10 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
11
12 int cmpint(const void *a, const void *b, void *aux)
13 {
14         (void)aux;
15         return *(int*)a - *(int*)b;
16 }
17
18 int main(void)
19 {
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         /*
32          * The TEST_HUGE_ALLOCATION_FAILURE macro controls whether we run tests
33          * that attempt to reserve extremely large vector capacities (e.g., SIZE_MAX).
34          * These tests are useful for exercising integer overflow and allocation failure
35          * logic, but can cause instability or fatal errors when run under memory
36          * sanitizers. The macro is automatically disabled when sanitizers are enabled,
37          * and enabled otherwise.
38          */
39 #if TEST_HUGE_ALLOCATION_FAILURE
40         // Test integer overflow and huge allocation failure logic
41         size_t too_big = SIZE_MAX;
42         /* too large a number for our address space */
43         assert(v_reserve_capacity(vint, too_big) < too_big);
44         /* still too large for address space */
45         too_big = 1 + SIZE_MAX/sizeof(void*);
46         /* technically possible, but not on today's machines */
47         too_big /= 2;
48         assert(v_reserve_capacity(vint, too_big) < too_big);
49 #endif
50
51         v_set_length(vint, 2);
52         assert(v_length(vint) == 2);
53         assert(v_at(vint, 0) == NULL);
54         assert(v_at(vint, 1) == NULL);
55         v_clear(vint);
56
57         for (i = 0; i < ARRAY_LEN(ivals); i++)
58                 v_prepend(vint, ivals+i);
59         assert(v_length(vint) == ARRAY_LEN(ivals));
60         assert(*(int*)v_first(vint) == 9);
61         assert(*(int*)v_remove_first(vint) == 9);
62         assert(*(int*)v_remove_first(vint) == 8);
63         v_sort(vint, cmpint, NULL);
64         assert(*(int*)v_first(vint) == 0);
65
66         v_clear(vint);
67         assert(v_length(vint) == 0);
68         for (i = 0; i < ARRAY_LEN(ivals); i++)
69                 v_append(vint, ivals+i);
70         assert(v_length(vint) == ARRAY_LEN(ivals));
71         assert(*(int*)v_last(vint) == 9);
72         assert(*(int*)v_remove_last(vint) == 9);
73         assert(*(int*)v_remove_last(vint) == 8);
74
75         assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
76         v_insert(vint, 5, ivals2+5);
77         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
78         v_swap(vint, 5, 6);
79         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
80         assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == 6);
81         v_remove(vint, 6);
82         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
83         assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
84
85         assert(v_find_last_index(vint, v_last(vint), cmpint, NULL)
86                         == v_length(vint)-1);
87
88         v_clear(vint);
89         int revme_even[] = {1,2};
90         for (i = 0; i < ARRAY_LEN(revme_even); i++)
91                 v_append(vint, revme_even+i);
92         v_reverse(vint);
93         assert(*(int*)v_at(vint, 0) == 2);
94         assert(*(int*)v_at(vint, 1) == 1);
95
96         v_clear(vint);
97         int revme_odd[] = {1,2,3};
98         for (i = 0; i < ARRAY_LEN(revme_odd); i++)
99                 v_append(vint, revme_odd+i);
100         v_reverse(vint);
101         assert(*(int*)v_at(vint, 0) == 3);
102         assert(*(int*)v_at(vint, 1) == 2);
103         assert(*(int*)v_at(vint, 2) == 1);
104
105         v_set_length(vint, 1024);
106         assert(v_at(vint, 1023) == NULL);
107         v_append(vint, ivals);
108         assert(*(int*)v_at(vint, 1024) == 0);
109
110         v_clear(vint);
111         /* test for memory leak */
112         v_dtor(vint, derp_free, NULL);
113         int *life = malloc(sizeof *life);
114         *life = 42;
115         v_append(vint, life);
116
117         v_free(vint);
118
119         return 0;
120 }