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