5 #include "derp/common.h"
6 #include "derp/vector.h"
10 #define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
12 int cmpint(const void *a, const void *b, void *aux)
15 return *(int*)a - *(int*)b;
21 int ivals[] = {0,1,2,3,4,5,6,7,8,9},
22 ivals2[] = {10,11,12,13,14,15,16,17,18,19};
24 vector *vint = v_new();
25 assert(v_length(vint) == 0);
26 assert(v_capacity(vint) > 0);
27 assert(v_is_empty(vint));
28 assert(!v_at(vint, 0));
29 assert(!v_at(vint, 1337));
32 * The TEST_HUGE_ALLOCATION_FAILURE macro controls whether we run tests
33 * that attempt to reserve extremely large vector capacities (e.g., SIZE_MAX).
34 * These tests are useful for exercising integer overflow and allocation failure
35 * logic, but can cause instability or fatal errors when run under memory
36 * sanitizers. The macro is automatically disabled when sanitizers are enabled,
37 * and enabled otherwise.
39 #if TEST_HUGE_ALLOCATION_FAILURE
40 // Test integer overflow and huge allocation failure logic
41 size_t too_big = SIZE_MAX;
42 /* too large a number for our address space */
43 assert(v_reserve_capacity(vint, too_big) < too_big);
44 /* still too large for address space */
45 too_big = 1 + SIZE_MAX/sizeof(void*);
46 /* technically possible, but not on today's machines */
48 assert(v_reserve_capacity(vint, too_big) < too_big);
51 v_set_length(vint, 2);
52 assert(v_length(vint) == 2);
53 assert(v_at(vint, 0) == NULL);
54 assert(v_at(vint, 1) == NULL);
57 for (i = 0; i < ARRAY_LEN(ivals); i++)
58 v_prepend(vint, ivals+i);
59 assert(v_length(vint) == ARRAY_LEN(ivals));
60 assert(*(int*)v_first(vint) == 9);
61 assert(*(int*)v_remove_first(vint) == 9);
62 assert(*(int*)v_remove_first(vint) == 8);
63 v_sort(vint, cmpint, NULL);
64 assert(*(int*)v_first(vint) == 0);
67 assert(v_length(vint) == 0);
68 for (i = 0; i < ARRAY_LEN(ivals); i++)
69 v_append(vint, ivals+i);
70 assert(v_length(vint) == ARRAY_LEN(ivals));
71 assert(*(int*)v_last(vint) == 9);
72 assert(*(int*)v_remove_last(vint) == 9);
73 assert(*(int*)v_remove_last(vint) == 8);
75 assert(v_find_index(vint, ivals+5, cmpint, NULL) == 5);
76 v_insert(vint, 5, ivals2+5);
77 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 5);
79 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == 6);
80 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == 6);
82 assert(v_find_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
83 assert(v_find_last_index(vint, ivals2+5, cmpint, NULL) == SIZE_MAX);
85 assert(v_find_last_index(vint, v_last(vint), cmpint, NULL)
89 int revme_even[] = {1,2};
90 for (i = 0; i < ARRAY_LEN(revme_even); i++)
91 v_append(vint, revme_even+i);
93 assert(*(int*)v_at(vint, 0) == 2);
94 assert(*(int*)v_at(vint, 1) == 1);
97 int revme_odd[] = {1,2,3};
98 for (i = 0; i < ARRAY_LEN(revme_odd); i++)
99 v_append(vint, revme_odd+i);
101 assert(*(int*)v_at(vint, 0) == 3);
102 assert(*(int*)v_at(vint, 1) == 2);
103 assert(*(int*)v_at(vint, 2) == 1);
105 v_set_length(vint, 1024);
106 assert(v_at(vint, 1023) == NULL);
107 v_append(vint, ivals);
108 assert(*(int*)v_at(vint, 1024) == 0);
111 /* test for memory leak */
112 v_dtor(vint, derp_free, NULL);
113 int *life = malloc(sizeof *life);
115 v_append(vint, life);