]> begriffs open source - libderp/blob - test/t_vector.c
WIP: pass auxiliary arg to comparator
[libderp] / test / t_vector.c
1 #include "vector.h"
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
7
8 int cmpint(const void *a, const void *b, void *aux)
9 {
10         (void)aux;
11         return *(int*)a - *(int*)b;
12 }
13
14 int main(void)
15 {
16         int ivals[] =  {0,1,2,3,4,5,6,7,8,9},
17             ivals2[] = {10,11,12,13,14,15,16,17,18,19};
18         size_t i;
19         vector *vint = v_new(NULL);
20         assert(v_length(vint) == 0);
21         assert(v_is_empty(vint));
22
23         for (i = 0; i < ARRAY_LEN(ivals); i++)
24                 v_prepend(vint, ivals+i);
25         assert(v_length(vint) == ARRAY_LEN(ivals));
26         assert(*(int*)v_first(vint) == 9);
27         assert(*(int*)v_remove_first(vint) == 9);
28         assert(*(int*)v_remove_first(vint) == 8);
29         v_sort(vint, cmpint, NULL);
30         assert(*(int*)v_first(vint) == 0);
31
32         v_clear(vint);
33         assert(v_length(vint) == 0);
34         for (i = 0; i < ARRAY_LEN(ivals); i++)
35                 v_append(vint, ivals+i);
36         assert(v_length(vint) == ARRAY_LEN(ivals));
37         assert(*(int*)v_last(vint) == 9);
38         assert(*(int*)v_remove_last(vint) == 9);
39         assert(*(int*)v_remove_last(vint) == 8);
40
41         assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
42         v_insert(vint, 5, ivals2+5);
43         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
44         v_swap(vint, 5, 6);
45         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
46         v_remove(vint, 6);
47         assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
48         assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
49
50         v_clear(vint);
51         int revme_even[] = {1,2};
52         for (i = 0; i < ARRAY_LEN(revme_even); i++)
53                 v_append(vint, revme_even+i);
54         v_reverse(vint);
55         assert(*(int*)v_at(vint, 0) == 2);
56         assert(*(int*)v_at(vint, 1) == 1);
57
58         v_clear(vint);
59         int revme_odd[] = {1,2,3};
60         for (i = 0; i < ARRAY_LEN(revme_odd); i++)
61                 v_append(vint, revme_odd+i);
62         v_reverse(vint);
63         assert(*(int*)v_at(vint, 0) == 3);
64         assert(*(int*)v_at(vint, 1) == 2);
65         assert(*(int*)v_at(vint, 2) == 1);
66
67         v_free(vint);
68
69         return 0;
70 }