5 #include "derp/common.h"
6 #include "derp/vector.h"
9 #include <gc/leak_detector.h>
12 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
14 int cmpint(const void *a, const void *b, void *aux)
17 return *(int*)a - *(int*)b;
25 GC_debug_malloc_replacement,
26 GC_debug_realloc_replacement, GC_debug_free);
29 int ivals[] = {0,1,2,3,4,5,6,7,8,9},
30 ivals2[] = {10,11,12,13,14,15,16,17,18,19};
32 vector *vint = v_new();
33 assert(v_length(vint) == 0);
34 assert(v_capacity(vint) > 0);
35 assert(v_is_empty(vint));
36 assert(!v_at(vint, 0));
37 assert(!v_at(vint, 1337));
39 size_t too_big = SIZE_MAX;
40 /* too large a number for our address space */
41 assert(v_reserve_capacity(vint, too_big) < too_big);
42 /* still too large for address space */
43 too_big = 1 + SIZE_MAX/sizeof(void*);
44 /* technically possible, but not on today's machines */
46 assert(v_reserve_capacity(vint, too_big) < too_big);
48 v_set_length(vint, 2);
49 assert(v_length(vint) == 2);
50 assert(v_at(vint, 0) == NULL);
51 assert(v_at(vint, 1) == NULL);
54 for (i = 0; i < ARRAY_LEN(ivals); i++)
55 v_prepend(vint, ivals+i);
56 assert(v_length(vint) == ARRAY_LEN(ivals));
57 assert(*(int*)v_first(vint) == 9);
58 assert(*(int*)v_remove_first(vint) == 9);
59 assert(*(int*)v_remove_first(vint) == 8);
60 v_sort(vint, cmpint, NULL);
61 assert(*(int*)v_first(vint) == 0);
64 assert(v_length(vint) == 0);
65 for (i = 0; i < ARRAY_LEN(ivals); i++)
66 v_append(vint, ivals+i);
67 assert(v_length(vint) == ARRAY_LEN(ivals));
68 assert(*(int*)v_last(vint) == 9);
69 assert(*(int*)v_remove_last(vint) == 9);
70 assert(*(int*)v_remove_last(vint) == 8);
72 assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
73 v_insert(vint, 5, ivals2+5);
74 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
76 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
77 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == 6);
79 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
80 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
82 assert(v_find_last_index(vint, v_last(vint), cmpint, NULL)
86 int revme_even[] = {1,2};
87 for (i = 0; i < ARRAY_LEN(revme_even); i++)
88 v_append(vint, revme_even+i);
90 assert(*(int*)v_at(vint, 0) == 2);
91 assert(*(int*)v_at(vint, 1) == 1);
94 int revme_odd[] = {1,2,3};
95 for (i = 0; i < ARRAY_LEN(revme_odd); i++)
96 v_append(vint, revme_odd+i);
98 assert(*(int*)v_at(vint, 0) == 3);
99 assert(*(int*)v_at(vint, 1) == 2);
100 assert(*(int*)v_at(vint, 2) == 1);
102 v_set_length(vint, 1024);
103 assert(v_at(vint, 1023) == NULL);
104 v_append(vint, ivals);
105 assert(*(int*)v_at(vint, 1024) == 0);
108 /* test for memory leak */
109 v_dtor(vint, derp_free, NULL);
110 int *life = malloc(sizeof *life);
112 v_append(vint, life);