forked from WeAreMuffin/c_vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
290 lines (248 loc) · 6.77 KB
/
test.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaubin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/13 00:54:38 by aaubin #+# #+# */
/* Updated: 2014/02/15 05:22:35 by aleger ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "c_vector.h"
#include <unistd.h>
typedef struct s_car
{
int wheels;
int doors;
int max_speed;
int color;
} t_car;
t_car *create_car(int w, int d, int ms, int c)
{
t_car *car;
car = (t_car *)malloc(sizeof(t_car));
if (car)
{
car->color = c;
car->doors = d;
car->max_speed = ms;
car->wheels = w;
}
return (car);
}
void print_car(t_car *c)
{
printf("A car wich have %i wheels, %i doors, %x color and who can go up to %i km/h !\n",
c->wheels, c->doors, c->color, c->max_speed);
}
void test(char *test, int assertion)
{
printf("\e[37m▸ Testing \e[0m%-60s", test);
assertion ? printf("\e[32m✓\e[0m") : printf("\e[31mFAIL\e[0m");
printf("\n");
assert(assertion);
}
void map_function(t_car *elt)
{
elt->max_speed = 999;
}
int main(void)
{
t_vector *v;
t_car *c;
t_car *sec;
t_car *t1;
t_car *t2;
int i;
// create a new vector tests
v = new_vector(sizeof(t_car), NULL);
test("Creating a new vector<t_car>", (v != NULL));
c = create_car(4, 5, 150, 0xFFFFFF);
sec = create_car(0, 0, 0, 0);
assert(c != NULL && sec != NULL);
// Push tests
v->push(c, v);
free(c);
test("Pushing a first t_car element", (v->count == 1));
assert(v->elt_size == sizeof(t_car));
c = create_car(2, 0, 100, 0xffccaa);
assert(c != NULL);
v->push(c, v);
free(c);
test("Pushing a second t_car element", (v->count == 2));
c = create_car(1, 0, 10, 0xffffaa);
assert(c != NULL);
v->push(c, v);
free(c);
test("Pushing a 3th t_car element", (v->count == 3));
c = create_car(8, 3, 150, 0xffffaa);
assert(c != NULL);
v->push(c, v);
free(c);
test("Pushing a 4th t_car element", (v->count == 4));
// Iteration tests
test("[start] Iterate on the t_car elements", 1);
while ((c = v->each(v)))
{
printf("\t - [car %zu] ", v->cursor);
print_car(c);
}
test("[end] Iterate on the t_car elements", (!c && v->cursor == 0));
// Re-Iteration tests
test("[start] Re-Iterate on the t_car elements", 1);
while ((c = v->each(v)))
{
printf("\t - [car %zu] ", v->cursor);
print_car(c);
}
test("[end] Re-Iterate on the t_car elements", (!c && v->cursor == 0));
/*
v->map(v, (void *)map_function);
test("Apply function by mapping on the t_car elements", (v) != NULL);
// Re-Iteration after map tests
test("[start] Re-Iterate after map on the t_car elements", 1);
while ((c = v->each(v)))
{
printf("\t - [car %zu] ", v->cursor);
print_car(c);
assert(c->max_speed == 999);
}
test("[end] Re-Iterate after map on the t_car elements",
(!c && v->cursor == 0));
// Pop tests
v->pop_front(sec, v);
test("Popping the first (1th) element", (sec != NULL && sec->wheels == 4));
print_car(sec);
v->pop_front(sec, v);
test("Popping the first (2th) element", (sec != NULL && sec->wheels == 2));
print_car(sec);
v->pop_front(sec, v);
test("Popping the first (3th) element", (sec != NULL && sec->wheels == 1));
print_car(sec);
*/
while ((c = v->each(v)))
{
write(1, "@@@", 3);
print_car(c);
}
v->swap(v, 0, 3);
v->swap(v, 1, 2);
write(1, "\n", 1);
while ((c = v->each(v)))
{
write(1, "%%%", 3);
print_car(c);
}
// Repush tests
c = create_car(50, 3, 150, 0xffffaa);
assert(c != NULL);
v->push_front(c, v);
free(c);
test("Pushing a 5th t_car element with 50 wheels", (v->count == 5));
//v->map(v, (void *)print_car);
// RemoveOne tests
while ((c = v->each(v)))
{
write(1, "RRR", 3);
print_car(c);
}
v->remove(1, v);
test("Removing the (2th) element", (v->count == 4));
while ((c = v->each(v)))
{
write(1, "TTT", 3);
print_car(c);
}
v->pop(sec, v);
test("Popping the last (1st) element", (sec != NULL && sec->wheels == 4));
test("Is the vector empty ?", (v->count != 0));
// Remove tests
v->free(v);
test("Cleaning up the vector", (v->count == 0 && v->content == NULL));
free(v);
// Memory leaks test loop
i = 0;
test("Trying to make a parking of 300 cars", 1);
write(1, "\n", 1);
while (i < 299)
{
v = new_vector(sizeof(t_car), NULL);
c = create_car(2, 0, 100, 0xffccaa);
assert(c != NULL);
v->push(c, v);
free(c);
c = create_car(2, 0, 100, 0xffccaa);
assert(c != NULL);
v->push(c, v);
free(c);
c = create_car(2, 0, 100, 0xffccaa);
assert(c != NULL);
v->push(c, v);
free(c);
c = create_car(2, 0, 100, 0xffccaa);
assert(c != NULL);
v->push(c, v);
free(c);
v->pop_front(sec, v);
v->pop_front(sec, v);
v->remove(1, v);
v->free(v);
free(v);
i++;
write(1, " ✩ ", 5);
if (i % 23 == 0)
write(1, "\n", 1);
}
free(sec);
write(1, "\n\e[37mRun [leaks test_vector] to check leaks, \
then press\e[34m Enter \e[0m", 77);
read(0, NULL, 1);
/*
** =========================== END OF AAUBIN TESTS =========================
*/
/* Couscous tests */
// printf("\n\n\n================= COUSCOUUUUUUS =====================\n\n");
// create a new vector
v = new_vector(sizeof(t_car), NULL);
assert(v);
// printf("%zu\n", v->size(v));
// printf("%zu\n", v->count);
test("Size is 0", v->size(v) == 0);
c = create_car(4, 0, 150, 0xFFFFFF);
assert(c);
t1 = c;
v->push(c, v);
free(c);
test("Size is 1", v->size(v) == 1);
c = create_car(2, 1, 100, 0xffccaa);
assert(c);
v->push(c, v);
free(c);
test("Size is 2", v->size(v) == 2);
c = create_car(5, 2, 380, 0xCACA);
assert(c);
t2 = c;
v->push(c, v);
free(c);
test("Size is 3", v->size(v) == 3);
test("Good capacity", v->v_capacity(v) == v->capacity);
v->swap(v, 0, 1);
// printf("%d\n", ((t_car *)v->at(v, 0))->max_speed);
// printf("%d\n", ((t_car *)v->at(v, 1))->max_speed);
v->swap(v, 0, 1);
// printf("%d\n", ((t_car *)v->at(v, 0))->max_speed);
// printf("%d\n", ((t_car *)v->at(v, 1))->max_speed);
c = v->data(v, 0);
c->max_speed = 600;
// printf("c: %d at: %d\n", c->max_speed, ((t_car *)v->at(v, 0))->max_speed);
v->free(v);
test("Cleaning up the vector", (v->count == 0 && v->content == NULL));
(void)sec;
(void)i;
printf("\n\t\t\e[32m====\tOkay ! All seems works well !\t====\e[0m\n");
return (0);
}