-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_vec.c
229 lines (208 loc) · 8.07 KB
/
test_vec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "dodvec.h"
#include "dodtest.h"
#include <assert.h>
#include <stdio.h>
#include <time.h>
typedef struct {
int x;
int y;
} struct_t;
#define test_dodvec_push_pop_generic(type, ...) \
do { \
type arr[] = {__VA_ARGS__}; \
const size_t sz = (sizeof(arr) / sizeof(type)); \
size_t i = 0; \
dodvec_t *v = dodvec_new(type, sz); \
for (i = 0; i < sz; i++) \
dodvec_push_back(v, arr + i); \
for (i = 0; i < sz; i++) { \
assert(!memcmp(dodvec_back(v), &arr[sz - i - 1], sizeof(type))); \
dodvec_pop_back(v); \
} \
dodvec_free(v); \
} while (0)
#define test_dodvec_erase_generic(type, ...) \
do { \
type arr[] = {__VA_ARGS__}; \
const size_t sz = (sizeof(arr) / sizeof(type)); \
size_t i = 0; \
dodvec_t *v = dodvec_new(type, sz); \
for (i = 0; i < sz; i++) \
dodvec_push_back(v, arr + i); \
assert(!dodvec_erase(v, 0)); \
assert(!memcmp(dodvec_at(v, 0), &arr[1], sizeof(type))); \
dodvec_free(v); \
} while (0)
static void test_dodvec_push_pop(void) {
DODTEST_RUN("push pop for int", test_dodvec_push_pop_generic(
int, 1, 2, 3, 4, 5));
DODTEST_RUN("push pop for float",
test_dodvec_push_pop_generic(
float, 1.0, 2.0, 3.0, 4.0, 5.0));
DODTEST_RUN("push pop for struct_t",
test_dodvec_push_pop_generic(struct_t, {.x = 1, .y = 2},
{.x = 3, .y = 4}, {.x = 5, .y = 6},
{.x = 7, .y = 8}, {.x = 9, .y = 10}));
DODTEST_RUN("push pop for char *",
test_dodvec_push_pop_generic(
char *, "str1", "str2", "str3", "str4",
"str5"));
typedef char str5[5];
DODTEST_RUN("push pop for str5",
test_dodvec_push_pop_generic(str5, "str1", "str2", "str3", "str4",
"str5"));
}
static void test_dodvec_erase(void) {
DODTEST_RUN("erase for int", test_dodvec_erase_generic(
int, 1, 2, 3, 4, 5));
DODTEST_RUN("erase for float",
test_dodvec_erase_generic(
float, 1.0, 2.0, 3.0, 4.0, 5.0));
DODTEST_RUN("erase for struct_t",
test_dodvec_erase_generic(struct_t, {.x = 1, .y = 2},
{.x = 3, .y = 4}, {.x = 5, .y = 6},
{.x = 7, .y = 8}, {.x = 9, .y = 10}));
DODTEST_RUN(
"erase for char *",
test_dodvec_erase_generic(
char *, "str1", "str2", "str3", "str4", "str5"));
typedef char str5[5];
DODTEST_RUN("erase for str5", test_dodvec_erase_generic(str5, "str1", "str2",
"str3", "str4", "str5"));
}
static void test_dodvec_access(void) {
dodvec_t *v = dodvec_new(int, 5);
for (int i = 0; i < 5; ++i)
dodvec_push_back(v, &i);
int *j = (int *) dodvec_at(v, 2);
assert(*j == 2);
j = (int *) dodvec_front(v);
assert(*j == 0);
j = (int *) dodvec_back(v);
assert(*j == 4);
dodvec_free(v);
}
static void test_dodvec_shrink_to_fit(void) {
dodvec_t *v = dodvec_new(int, 5);
for (int i = 0; i < 5; ++i)
dodvec_push_back(v, &i);
int j = 6;
dodvec_push_back(v, &j);
dodvec_shrink_to_fit(v);
assert(dodvec_capacity(v) == 6);
// edge case
dodvec_clear(v);
assert(dodvec_size(v) == 0);
dodvec_shrink_to_fit(v);
assert(dodvec_capacity(v) == 0);
dodvec_push_back(v, &j);
assert(dodvec_size(v) == 1);
dodvec_free(v);
}
static void test_dodvec_reserve(void) {
dodvec_t *v = dodvec_new(int, 5);
dodvec_reserve(v, 12);
assert(dodvec_capacity(v) >= 12);
dodvec_reserve(v, 4);
assert(dodvec_capacity(v) >= 4);
dodvec_reserve(v, 120);
assert(dodvec_capacity(v) >= 120);
dodvec_free(v);
}
static void test_dodvec_resize(void) {
dodvec_t *v = dodvec_new(int, 10);
size_t i;
for (i = 0; i < 10; ++i)
dodvec_push_back(v, &i);
dodvec_resize(v, 5, NULL);
assert(dodvec_size(v) == 5);
int j = 100;
dodvec_resize(v, 8, &j);
assert(dodvec_size(v) == 8);
for (i = 5; i < dodvec_size(v); ++i)
assert(*dodvec_at(v, i) == 100);
dodvec_resize(v, 12, NULL);
assert(dodvec_size(v) == 12);
for (i = 8; i < dodvec_size(v); ++i)
assert(*dodvec_at(v, i) == 0);
dodvec_free(v);
}
static void test_dodvec_assign(void) {
dodvec_t *v1 = dodvec_new(int, 5);
for (int i = 0; i < 5; ++i)
dodvec_push_back(v1, &i);
dodvec_t *v2 = dodvec_new(int, 3);
for (int j = 3; j != 0; --j)
dodvec_push_back(v2, &j);
dodvec_assign_range(v1, dodvec_at(v2, 0), dodvec_at(v2, 2));
assert(dodvec_size(v1) == 2);
assert(*dodvec_at(v1, 0) == *dodvec_at(v2, 0));
assert(*dodvec_at(v1, 1) == *dodvec_at(v2, 1));
dodvec_free(v1);
dodvec_free(v2);
dodvec_t *v3 = dodvec_new(int, 0);
int val = 100;
dodvec_assign_fill(v3, 10, &val);
assert(dodvec_size(v3) == 10);
for (size_t k = 0; k < dodvec_size(v3); ++k)
assert(*dodvec_at(v3, k) == 100);
dodvec_free(v3);
dodvec_t *v4 = dodvec_new(int, 10);
int vals[] = {5, 4, 3, 2, 1};
size_t valsz = (sizeof(vals) / sizeof(*vals));
dodvec_assign_range(v4, vals, vals + valsz);
assert(dodvec_size(v4) == valsz);
for (size_t l = 0; l < dodvec_size(v4); ++l)
assert(*dodvec_at(v4, l) == vals[l]);
dodvec_free(v4);
}
static void test_dodvec_insert(void) {
dodvec_t *v = dodvec_new(int, 3);
int i = 100;
dodvec_assign_fill(v, 3, &i);
i = 200;
dodvec_insert(v, dodvec_front(v), &i);
i = 300;
dodvec_insert(v, dodvec_front(v), &i);
dodvec_insert(v, dodvec_front(v), &i);
dodvec_t *v1 = dodvec_new(int, 2);
i = 400;
dodvec_assign_fill(v1, 2, &i);
dodvec_insert_range(v, dodvec_at(v, 2), dodvec_begin(v1), dodvec_end(v1));
dodvec_free(v1);
int vals[] = {501, 502, 503};
dodvec_insert_range(v, dodvec_front(v), vals, vals + 3);
int test_vals[] = {501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100};
assert(!memcmp(dodvec_data(v), test_vals, sizeof(test_vals)));
dodvec_free(v);
}
static void test_dodvec_swap(void) {
int vals1[] = {5, 4, 3, 2, 1};
size_t valsz1 = (sizeof(vals1) / sizeof(*vals1));
dodvec_t *v1 = dodvec_new(int, valsz1);
dodvec_assign_range(v1, vals1, vals1 + valsz1);
int vals2[] = {1, 2, 3, 4, 5};
size_t valsz2 = (sizeof(vals2) / sizeof(*vals2));
dodvec_t *v2 = dodvec_new(int, valsz2);
dodvec_assign_range(v2, vals2, vals2 + valsz2);
dodvec_swap(v1, v2);
for (size_t l = 0; l < dodvec_size(v1); ++l)
assert(*dodvec_at(v1, l) == vals2[l]);
for (size_t l = 0; l < dodvec_size(v2); ++l)
assert(*dodvec_at(v2, l) == vals1[l]);
dodvec_free(v2);
dodvec_free(v1);
}
int main(void) {
DODTEST_PRINT_THEAD();
test_dodvec_push_pop();
test_dodvec_erase();
DODTEST_RUN("element access", test_dodvec_access());
DODTEST_RUN("shrink to fit", test_dodvec_shrink_to_fit());
DODTEST_RUN("reserve", test_dodvec_reserve());
DODTEST_RUN("resize", test_dodvec_resize());
DODTEST_RUN("assign", test_dodvec_assign());
DODTEST_RUN("insert", test_dodvec_insert());
DODTEST_RUN("swap", test_dodvec_swap());
return 0;
}