summaryrefslogtreecommitdiffstats
path: root/nserver/tests/darray_tests.c
blob: b33c460237a95c3b6c9c9662b4a0f720e4ed17e5 (plain) (blame)
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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright © 2010, Zed A. Shaw.
 * Copyright © 2020 rsiddharth <s@ricketyspace.net>
 */

#include "minunit.h"
#include <darray.h>

static DArray *array = NULL;
static int *val1 = NULL;
static int *val2 = NULL;

int testcmp(int **a, int **b)
{
    int x = **a, y = **b;

    if (x == y) {
        return 0;
    } else if (x < y) {
        return -1;
    } else {
        return 1;
    }
}

char *test_create()
{
    // Test fail.
    array = DArray_create(0, 0);
    mu_assert(array == NULL, "array must be NULL.");

    // Test success.
    array = DArray_create(sizeof(int), 100);
    mu_assert(array != NULL, "DArray_create failed.");
    mu_assert(array->contents != NULL, "contents are wrong in array.");
    mu_assert(array->end == 0, "end isn't at the right spot");
    mu_assert(array->element_size == sizeof(int),
              "element size is wrong.");
    mu_assert(array->max == 100, "wrong max length on initial size");

    return NULL;
}

char *test_destroy()
{
    DArray_destroy(array);

    return NULL;
}

char *test_new()
{
    // Test fail.
    val1 = DArray_new(NULL);
    mu_assert(val1 == NULL, "val1 must be NULL");

    // Test success
    val1 = DArray_new(array);
    mu_assert(val1 != NULL, "failed to make a new element");

    val2 = DArray_new(array);
    mu_assert(val2 != NULL, "failed to make a new element");

    return NULL;
}

char *test_set()
{
    int rc = 0;

    // Test fail.

    // case where array is NULL
    rc = DArray_set(NULL, 0, val1);
    mu_assert(rc == -1, "rc must be -1");

    // case where i < 0
    rc = DArray_set(array, -1, val1);
    mu_assert(rc == -1, "rc must be -1");

    // case where i > array->max
    rc = DArray_set(array, array->max + 1, val1);
    mu_assert(rc == -1, "rc must be -1");

    // Test success.

    // must set val1 at index 0
    rc = DArray_set(array, 0, val1);
    mu_assert(rc == 0, "rc must be 0");

    // must set val2 at index 1
    rc = DArray_set(array, 1, val2);
    mu_assert(rc == 1, "rc must be 1");

    return NULL;
}

char *test_get()
{
    void *rc = NULL;

    // Test fail.

    // case where array is NULL
    rc = DArray_get(NULL, 0);
    mu_assert(rc == NULL, "rc must be NULL");

    // case where i < 0
    rc = DArray_get(array, -2);
    mu_assert(rc == NULL, "rc must be NULL");

    // case where i > array->max
    rc = DArray_get(array, array->max + 1);
    mu_assert(rc == NULL, "rc must be NULL");

    // Test succcess.
    mu_assert(DArray_get(array, 0) == val1, "Wrong first value");
    mu_assert(DArray_get(array, 1) == val2, "Wrong second value");

    return NULL;
}

char *test_remove()
{
    int *val_check = NULL;

    // Test fail.

    // case where array is NULL.
    val_check = DArray_remove(NULL, 0);
    mu_assert(val_check == NULL, "val_check must be NULL");
    mu_assert(array->contents[0] != NULL,
              "array->contents[0] must not be NULL");

    // case where i < 0
    val_check = DArray_remove(array, -1);
    mu_assert(val_check == NULL, "val_check must be NULL");

    // case where i > array->max
    val_check = DArray_remove(array, array->max + 1);
    mu_assert(val_check == NULL, "val_check must be NULL");

    // Test success.
    val_check = DArray_remove(array, 0);
    mu_assert(val_check !=NULL, "Should not get NULL");
    mu_assert(*val_check == *val1, "Should get the first value.");
    mu_assert(DArray_get(array, 0) == NULL, "Should be gone.");
    DArray_free(val_check);

    val_check = DArray_remove(array, 1);
    mu_assert(val_check != NULL, "Should not get NULL.");
    mu_assert(*val_check == *val2, "Should get second value.");
    mu_assert(DArray_get(array, 1) == NULL, "Should be gone");
    DArray_free(val_check);

    return NULL;
}

char *test_expand_contract()
{
    int old_max = array->max;
    DArray_expand(array);
    mu_assert((unsigned int) array->max == old_max + array->expand_rate,
              "Wrong size after expand.");

    // Check if newly allocated space is all set to 0.
    int i = 0;
    for (i = old_max; i < array->max; i++) {
        mu_assert(array->contents[i] == 0, "contents must be 0");
    }

    int rc = 0;
    rc = DArray_contract(array);
    mu_assert(rc == 0, "rc must be 0");
    mu_assert((unsigned int) array->max == array->expand_rate + 1,
              "Should stay at the expand_rate at least.");

    rc = DArray_contract(array);
    mu_assert(rc == 0, "rc must be 0");
    mu_assert((unsigned int) array->max == array->expand_rate + 1,
              "Should stay at the expand_rate at least");

    return NULL;
}

char *test_push_pop()
{
    int i = 0, rc = 0;
    for (i = 0; i < 1000; i++) {
        int *val = DArray_new(array);
        *val = i * 333;
        rc = DArray_push(array, val);
        mu_assert(rc == 0, "Darray_push failed");
    }

    mu_assert(array->max == 1201, "Wrong max size.");

    for (i = 999; i >= 0; i--) {
        int *val = DArray_pop(array);
        mu_assert(val != NULL, "Shouldn't get a NULL");
        mu_assert(*val == i * 333, "Wrong value.");

        if ((DArray_end(array) > (int) array->expand_rate) &&
            (DArray_end(array) % array->expand_rate)) {
            mu_assert(array->max == DArray_end(array) + 1,
                      "DArray pop contract error");
        }

        DArray_free(val);
    }

    return NULL;
}

int is_sorted(DArray *array)
{
    int i = 0;

    for (i = 0; i < DArray_count(array) -1; i++) {
        if (*((int *) DArray_get(array, i)) > *((int *) DArray_get(array, i + 1))) {
            return 0;
        }
    }

    return 1;
}

char *test_shallow_copy()
{
    // First populate array.
    array = DArray_create(sizeof(int), 100);
    int i = 0, rc = 0;
    int *val = NULL;
    for (i = 0; i < 1000; i++) {
        val = DArray_new(array);
        *val = i * 333;
        rc = DArray_push(array, val);
        mu_assert(rc == 0, "Darray_push failed");
    }
    mu_assert(array->max == 1300, "Wrong max size.");

    // Set at 1100
    val = DArray_new(array);
    *val = 42;
    rc = DArray_set(array, 1100, val);
    mu_assert(rc == 1100, "Error setting value at 1100");

    // Set at 1110
    val = DArray_new(array);
    *val = 4242;
    rc = DArray_set(array, 1110, val);
    mu_assert(rc == 1110, "Error setting value at 1110");

    // Test shallow copy
    DArray *copy = DArray_shallow_copy(array);
    mu_assert(copy != NULL, "Shallow copy failed");

    int expected = 0;
    for (i = 0; i < 1000; i++) {
        expected = i * 333;
        val = DArray_get(copy, i);
        mu_assert(*val == expected, "Unexpected element in copy.");
    }

    expected = 42;
    val = DArray_get(copy, 1100);
    mu_assert(*val == expected, "Unexpected element at in copy.");

    expected = 4242;
    val = DArray_get(copy, 1110);
    mu_assert(*val == expected, "Unexpected element at in copy.");

    // Destroy copy.
    DArray_destroy(copy);

    // Destroy array.
    DArray_clear_destroy(array);
    return NULL;
}

char *test_sort_add()
{
    array = DArray_create(sizeof(int), 100);
    mu_assert(array != NULL, "Error initializing array");

    int i = 0, rc = 0;
    int *val = NULL;
    for (i = 0; i < 2000; i++) {
        val = DArray_new(array);
        mu_assert(val != NULL, "Error creating new element");
        *val = rand();

        rc = DArray_sort_add(array, val, (DArray_compare) testcmp);
        mu_assert(rc == 0, "Error adding element.");
        mu_assert(is_sorted(array) == 1, "array not sorted.");
    }

    DArray_clear_destroy(array);
    return NULL;
}

char *test_find()
{
    array = DArray_create(sizeof(int), 100);
    mu_assert(array != NULL, "Error initializing array");

    // test setup.
    int i = 0, rc = 0;
    int *val = NULL;
    for (i = 0; i < 2000; i++) {
        val = DArray_new(array);
        mu_assert(val != NULL, "Error creating new element");
        *val = i;

        rc = DArray_sort_add(array, val, (DArray_compare) testcmp);
        mu_assert(rc == 0, "Error adding element");
        mu_assert(is_sorted(array) == 1, "array not sorted");
    }

    // tests.
    val = DArray_new(array);
    mu_assert(val != NULL, "Error creating new element");

    for (i = 0; i < 2000; i++) {
        *val = i;

        rc = DArray_find(array, val, (DArray_compare) testcmp);
        mu_assert(rc == i, "val not found in array");
    }

    *val = 3000;
    rc = DArray_find(array, val, (DArray_compare) testcmp);
    mu_assert(rc == -1, "rc must be -1");

    // test teardown.
    DArray_clear_destroy(array);
    free(val);
    return NULL;
}

char *all_tests()
{
    mu_suite_start();

    mu_run_test(test_create);
    mu_run_test(test_new);
    mu_run_test(test_set);
    mu_run_test(test_get);
    mu_run_test(test_remove);
    mu_run_test(test_expand_contract);
    mu_run_test(test_push_pop);
    mu_run_test(test_destroy);
    mu_run_test(test_shallow_copy);
    mu_run_test(test_sort_add);
    mu_run_test(test_find);

    return NULL;
}

RUN_TESTS(all_tests);