5 #include "derp/common.h"
6 #include "derp/vector.h"
12 #define malloc(x) GC_malloc(x)
14 #define realloc(x,y) GC_realloc(x,y)
16 #define free(x) GC_free(x)
19 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
21 int cmpint(const void *a, const void *b, void *aux)
24 return *(int*)a - *(int*)b;
31 derp_use_alloc_funcs(GC_malloc, GC_realloc, GC_free);
34 int ivals[] = {0,1,2,3,4,5,6,7,8,9},
35 ivals2[] = {10,11,12,13,14,15,16,17,18,19};
37 vector *vint = v_new();
38 assert(v_length(vint) == 0);
39 assert(v_capacity(vint) > 0);
40 assert(v_is_empty(vint));
41 assert(!v_at(vint, 0));
42 assert(!v_at(vint, 1337));
44 size_t too_big = SIZE_MAX;
45 /* too large a number for our address space */
46 assert(v_reserve_capacity(vint, too_big) < too_big);
47 /* still too large for address space */
48 too_big = 1 + SIZE_MAX/sizeof(void*);
49 /* technically possible, but not on today's machines */
51 assert(v_reserve_capacity(vint, too_big) < too_big);
53 v_set_length(vint, 2);
54 assert(v_length(vint) == 2);
55 assert(v_at(vint, 0) == NULL);
56 assert(v_at(vint, 1) == NULL);
59 for (i = 0; i < ARRAY_LEN(ivals); i++)
60 v_prepend(vint, ivals+i);
61 assert(v_length(vint) == ARRAY_LEN(ivals));
62 assert(*(int*)v_first(vint) == 9);
63 assert(*(int*)v_remove_first(vint) == 9);
64 assert(*(int*)v_remove_first(vint) == 8);
65 v_sort(vint, cmpint, NULL);
66 assert(*(int*)v_first(vint) == 0);
69 assert(v_length(vint) == 0);
70 for (i = 0; i < ARRAY_LEN(ivals); i++)
71 v_append(vint, ivals+i);
72 assert(v_length(vint) == ARRAY_LEN(ivals));
73 assert(*(int*)v_last(vint) == 9);
74 assert(*(int*)v_remove_last(vint) == 9);
75 assert(*(int*)v_remove_last(vint) == 8);
77 assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
78 v_insert(vint, 5, ivals2+5);
79 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
81 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
82 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == 6);
84 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
85 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
87 assert(v_find_last_index(vint, v_last(vint), cmpint, NULL)
91 int revme_even[] = {1,2};
92 for (i = 0; i < ARRAY_LEN(revme_even); i++)
93 v_append(vint, revme_even+i);
95 assert(*(int*)v_at(vint, 0) == 2);
96 assert(*(int*)v_at(vint, 1) == 1);
99 int revme_odd[] = {1,2,3};
100 for (i = 0; i < ARRAY_LEN(revme_odd); i++)
101 v_append(vint, revme_odd+i);
103 assert(*(int*)v_at(vint, 0) == 3);
104 assert(*(int*)v_at(vint, 1) == 2);
105 assert(*(int*)v_at(vint, 2) == 1);
107 v_set_length(vint, 1024);
108 assert(v_at(vint, 1023) == NULL);
109 v_append(vint, ivals);
110 assert(*(int*)v_at(vint, 1024) == 0);
113 /* test for memory leak */
114 v_dtor(vint, derp_free, NULL);
115 int *life = malloc(sizeof *life);
117 v_append(vint, life);