]> begriffs open source - libderp/blob - test/t_vector.c
Some tests
[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)
9 {
10         return *(int*)a - *(int*)b;
11 }
12
13 int main(void)
14 {
15         int ivals[] =  {0,1,2,3,4,5,6,7,8,9},
16             ivals2[] = {10,11,12,13,14,15,16,17,18,19};
17         size_t i;
18         vector *vint = v_new(NULL);
19         assert(v_length(vint) == 0);
20         assert(v_is_empty(vint));
21
22         for (i = 0; i < ARRAY_LEN(ivals); i++)
23                 v_prepend(vint, ivals+i);
24         assert(v_length(vint) == ARRAY_LEN(ivals));
25         assert(*(int*)v_first(vint) == 9);
26         assert(*(int*)v_remove_first(vint) == 9);
27         assert(*(int*)v_remove_first(vint) == 8);
28         v_sort(vint, cmpint);
29         assert(*(int*)v_first(vint) == 0);
30
31         v_clear(vint);
32         assert(v_length(vint) == 0);
33         for (i = 0; i < ARRAY_LEN(ivals); i++)
34                 v_append(vint, ivals+i);
35         assert(v_length(vint) == ARRAY_LEN(ivals));
36         assert(*(int*)v_last(vint) == 9);
37         assert(*(int*)v_remove_last(vint) == 9);
38         assert(*(int*)v_remove_last(vint) == 8);
39
40         assert(v_find_index(vint, ivals+5, cmpint) == 5);
41         v_insert(vint, 5, ivals2+5);
42         assert(v_find_index(vint, ivals2+5, cmpint) == 5);
43         v_swap(vint, 5, 6);
44         assert(v_find_index(vint, ivals2+5, cmpint) == 6);
45         v_remove(vint, 6);
46         assert(v_find_index(vint, ivals2+5, cmpint) == SIZE_MAX);
47         assert(v_find_last_index(vint, ivals2+5, cmpint) == SIZE_MAX);
48
49         v_free(vint);
50
51         return 0;
52 }