-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
364 lines (333 loc) · 13.9 KB
/
tests.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "test_malloc.h"
#include "testing.h"
#include "tp3.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool test_create_and_destroy_dict() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(free);
tests_result &= test_assert("El diccionario fue creado", dict != NULL);
dictionary_destroy(dict);
return tests_result;
}
bool test_create_failed() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
set_malloc_status(false);
dictionary_t *dict = dictionary_create(free);
set_malloc_status(true);
tests_result &= test_assert("El diccionario no fue creado", dict == NULL);
return tests_result;
}
bool test_create_dict_nodestroy() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
tests_result &= test_assert("El diccionario fue creado", dict != NULL);
dictionary_destroy(dict);
return tests_result;
}
bool test_put_malloc_fail() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
tests_result &= test_assert("El tamaño es cero", dictionary_size(dict) == 0);
int one = 1, two = 2;
tests_result &=
test_assert("Se puede insertar key1", dictionary_put(dict, "key1", &one));
tests_result &= test_assert("El diccionario contiene key1",
dictionary_contains(dict, "key1"));
tests_result &= test_assert("El diccionario no contiene key2",
!dictionary_contains(dict, "key2"));
tests_result &= test_assert("El tamaño es uno", dictionary_size(dict) == 1);
set_malloc_status(false);
tests_result &= test_assert("No se puede insertar key2",
!dictionary_put(dict, "key2", &two));
set_malloc_status(true);
tests_result &= test_assert("El diccionario contiene key1",
dictionary_contains(dict, "key1"));
tests_result &= test_assert("El diccionario no contiene key2",
!dictionary_contains(dict, "key2"));
tests_result &= test_assert("El tamaño es dos", dictionary_size(dict) == 1);
dictionary_destroy(dict);
return tests_result;
}
bool test_put_size() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
tests_result &= test_assert("El tamaño es cero", dictionary_size(dict) == 0);
int one = 1, two = 2;
tests_result &=
test_assert("Se puede insertar key1", dictionary_put(dict, "key1", &one));
tests_result &= test_assert("El diccionario contiene key1",
dictionary_contains(dict, "key1"));
tests_result &= test_assert("El diccionario no contiene key2",
!dictionary_contains(dict, "key2"));
tests_result &= test_assert("El tamaño es uno", dictionary_size(dict) == 1);
tests_result &=
test_assert("Se puede insertar key2", dictionary_put(dict, "key2", &two));
tests_result &= test_assert("El diccionario contiene key1",
dictionary_contains(dict, "key1"));
tests_result &= test_assert("El diccionario contiene key2",
dictionary_contains(dict, "key2"));
tests_result &= test_assert("El tamaño es dos", dictionary_size(dict) == 2);
dictionary_destroy(dict);
return tests_result;
}
bool test_pop_get() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
int one = 1;
bool err;
dictionary_put(dict, "key0", &one);
tests_result &= test_assert("Obtener key0 retorna el puntero correcto",
dictionary_get(dict, "key0", &err) == &one);
tests_result &= test_assert("No hay error", !err);
tests_result &= test_assert("Obtener key0 y comparar valor",
*(int *)dictionary_get(dict, "key0", &err) == 1);
tests_result &= test_assert("No hay error", err == false);
tests_result &= test_assert("Hacer pop de key0",
dictionary_pop(dict, "key0", &err) == &one);
tests_result &= test_assert("No hay error", err == false);
tests_result &=
test_assert("key0 es NULL", dictionary_get(dict, "key0", &err) == NULL);
tests_result &= test_assert("Hay error", err == true);
dictionary_destroy(dict);
return tests_result;
}
bool test_get_errcode() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
int one = 1;
bool err;
void *retval = dictionary_get(dict, "key1", &err);
tests_result &= test_assert("Se retorna NULL", retval == NULL);
tests_result &= test_assert("Se marca error al obtener", err == true);
tests_result &=
test_assert("Se inserta el uno", dictionary_put(dict, "key1", &one));
retval = dictionary_get(dict, "key1", &err);
tests_result &= test_assert("Se retorna one", retval == &one);
tests_result &= test_assert("El valor es correcto", *(int *)retval == 1);
tests_result &= test_assert("No hay error al obtener", err == false);
dictionary_destroy(dict);
return tests_result;
}
bool test_put_get_delete_loop() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(free);
for (size_t i = 0; i < 5; i++) {
char *key = malloc(sizeof("key"));
strcpy(key, "key");
int *value0 = malloc(sizeof(int));
*value0 = 0;
int *value = malloc(sizeof(int));
*value = 1;
bool err = true;
tests_result &= test_assert("No existe la clave key",
NULL == dictionary_get(dict, key, &err));
tests_result &= test_assert("Err es true", err == true);
err = false;
tests_result &= test_assert("No existe la clave key",
NULL == dictionary_get(dict, key, &err));
tests_result &= test_assert("Err es true", err == true);
tests_result &= test_assert("Se puede insertar key-value",
dictionary_put(dict, key, value0));
tests_result &= test_assert("Se puede re-insertar key-value",
dictionary_put(dict, key, value));
err = true;
void *result = dictionary_get(dict, key, &err);
tests_result &= test_assert("No hay error al hacer get", !err);
tests_result &=
test_assert("El puntero es correcto", (int *)result == value);
tests_result &=
test_assert("El valor del puntero es correcto", *(int *)result == 1);
err = false;
result = dictionary_get(dict, key, &err);
tests_result &= test_assert("No hay error al hacer get", !err);
tests_result &= test_assert("El valor es correcto", (int *)result == value);
err = true;
result = dictionary_pop(dict, key, &err);
tests_result &= test_assert("No hay error al hacer pop", !err);
tests_result &=
test_assert("El valor de pop es correcto", (int *)result == value);
tests_result &= test_assert("El valor contenido en el puntero es correcto",
*(int *)result == 1);
result = dictionary_pop(dict, key, &err);
tests_result &= test_assert("Hay error al hacer pop", err);
tests_result &= test_assert("Result is NULL", result == NULL);
tests_result &= test_assert("Se puede insertar de nuevo",
dictionary_put(dict, key, value));
tests_result &= test_assert("Se puede eliminar el par clave-valor",
dictionary_delete(dict, key));
free(key);
}
dictionary_destroy(dict);
return tests_result;
}
bool test_put_NULL() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
dictionary_t *dict = dictionary_create(NULL);
tests_result &= test_assert("Se puede insertar key con valor NULL",
dictionary_put(dict, "key0", NULL));
bool err;
void *result = dictionary_get(dict, "key0", &err);
tests_result &= test_assert("No hay error", !err);
tests_result &= test_assert("El valor es NULL", result == NULL);
dictionary_destroy(dict);
return tests_result;
}
bool test_insert_random_sequence(size_t n, unsigned int seed, bool delete) {
printf("========== %s (n=%lu, seed=%u, delete=%s) ==========\n",
__PRETTY_FUNCTION__, n, seed, delete ? "true" : "false");
srand(seed);
bool tests_result = true;
dictionary_t *dict = dictionary_create(free);
size_t repeat_arr_size = 1;
int *repeats = calloc(repeat_arr_size, sizeof(int));
bool insert = true;
bool size_correct = true;
size_t uniques = 0;
for (size_t i = 0; i < n; i++) {
int random_number = rand();
int length = snprintf(NULL, 0, "%d", random_number);
char *str = malloc(length + 1);
snprintf(str, length + 1, "%d", random_number);
int *random_number_copy = malloc(sizeof(int));
*random_number_copy = random_number;
// Puede haber claves que se repitan en la secuencia
uniques += !dictionary_contains(dict, str);
if (dictionary_contains(dict, str)) {
repeat_arr_size += 1;
repeats = realloc(repeats, repeat_arr_size * sizeof(int));
repeats[repeat_arr_size - 1] = random_number;
}
insert &= dictionary_put(dict, str, random_number_copy);
size_correct &= (dictionary_size(dict) == uniques);
free(str);
}
tests_result &= test_assert("Todas las inserciones fueron exitosas", insert);
tests_result &= test_assert("El tamaño fue siempre correcto", size_correct);
srand(seed);
bool contains = true;
bool correct = true;
size_t rep_idx = 0;
for (size_t i = 0; i < n; i++) {
int random_number = rand();
if (random_number == repeats[rep_idx]) {
rep_idx += 1;
continue;
}
int length = snprintf(NULL, 0, "%d", random_number);
char *str = malloc(length + 1);
snprintf(str, length + 1, "%d", random_number);
contains &= dictionary_contains(dict, str);
bool err;
void *retval = dictionary_get(dict, str, &err);
correct &= (retval && *(int *)retval == random_number);
free(str);
}
tests_result &= test_assert("Todas las claves están presentes", contains);
tests_result &= test_assert("Todos los valores son correctos", correct);
if (delete) {
srand(seed);
bool delete_ok = true;
bool delete_size_ok = true;
rep_idx = 0;
bool first_pass = true;
for (size_t i = 0; i < n; i++) {
int random_number = rand();
if (repeats[rep_idx] == random_number) {
if (first_pass) {
first_pass = false;
} else {
printf("Found the %lu-th repeated number\n", rep_idx);
rep_idx += 1;
continue;
}
}
int length = snprintf(NULL, 0, "%d", random_number);
char *str = malloc(length + 1);
snprintf(str, length + 1, "%d", random_number);
bool this_delete_ok = dictionary_delete(dict, str);
if (!this_delete_ok) {
printf("No se pudo eliminar la clave %s\n", str);
}
delete_ok &= this_delete_ok;
bool this_delete_size_ok =
dictionary_size(dict) == (uniques - i - 1 + rep_idx);
if (!this_delete_size_ok) {
printf("El tamaño luego de borrar es erróneo: se esperaban %lu pero "
"hay %lu (%lu)\n",
uniques - i - 1 + rep_idx, dictionary_size(dict), rep_idx);
}
delete_size_ok &= this_delete_size_ok;
free(str);
}
tests_result &=
test_assert("Todos los deletes fueron correctos", delete_ok);
tests_result &= test_assert("Todos los tamaños al borrar fueron correctos",
delete_size_ok);
}
dictionary_destroy(dict);
free(repeats);
return tests_result;
}
bool test_malloc_fail_create() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
set_malloc_status(false);
dictionary_t *dict = dictionary_create(NULL);
tests_result &= test_assert(
"La creación del diccionario falló y se retorna NULL", dict == NULL);
set_malloc_status(true);
return tests_result;
}
bool test_fail_insert() {
printf("========== %s ==========\n", __PRETTY_FUNCTION__);
bool tests_result = true;
set_malloc_status(true);
dictionary_t *dict = dictionary_create(free);
set_malloc_status(false);
bool insert = dictionary_put(dict, "key", NULL);
test_assert("No se pudo insertar", !insert);
tests_result &= !insert;
set_malloc_status(true);
dictionary_destroy(dict);
return tests_result;
}
int main(void) {
srand(117);
int return_code = 0;
return_code += !test_put_malloc_fail();
return_code += !test_put_size();
return_code += !test_get_errcode();
return_code += !test_create_and_destroy_dict();
return_code += !test_create_failed();
return_code += !test_create_dict_nodestroy();
return_code += !test_pop_get();
return_code += !test_put_NULL();
return_code += !test_malloc_fail_create();
return_code += !test_fail_insert();
return_code += !test_put_get_delete_loop();
return_code += !test_insert_random_sequence(512, 117, false);
return_code += !test_insert_random_sequence(512, 117, true);
return_code += !test_insert_random_sequence(2048, 117, false);
return_code += !test_insert_random_sequence(2048, 117, true);
return_code += !test_insert_random_sequence(65536, 117, false);
return_code += !test_insert_random_sequence(65536, 117, true);
return_code += !test_insert_random_sequence(1048576, 117, false);
if (return_code == 0) {
printf("Todo ok!\n");
} else {
printf("Error code is %d\n", return_code);
}
return return_code;
}