File: heap_test.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (443 lines) | stat: -rw-r--r-- 14,569 bytes parent folder | download | duplicates (9)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "../adio/include/heap_sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define PREDEF_TESTS 2
/* test types */
#define ALL     0
#define RANDOM -1
#define CUSTOM -2

/* ACTIONS */
#define BUILD          0
#define INSERT         1
#define EXTRACT        2
#define EXTRACT_INSERT 3

typedef struct {
    char name[64];
    int heap_size;
    int print;
    int verify;
    int action_arr_sz;
    int *action_arr;
    int *action_count_arr;
    ADIO_Offset *offsets;
    ADIO_Offset *correct_order;
} test_params_t;

void print_usage();
void print_keys(ADIO_Offset * offsets, int size);
void print_params(test_params_t * params);
int run_test(test_params_t * test);
void fill_random_test(test_params_t * params);
void init_predefined_test(test_params_t * params, int index);
void dumb_sort(test_params_t * params);

int main(int argc, char **argv)
{
    int i, print = 1, verify = 1;
    int adding_elements;
    int curr_add_idx;
    int test_type = RANDOM;
    test_params_t predefined_tests[PREDEF_TESTS];
    test_params_t test;

    /* parse args */
    adding_elements = 0;
    curr_add_idx = 0;
    if (argc == 1) {
        print_usage();
        return 1;
    }
    i = 1;
    while (i < argc) {
        if (!strcmp("-A", argv[i])) {
            adding_elements = 0;
            test_type = ALL;
            i++;
        } else if (!strcmp("-T", argv[i])) {
            adding_elements = 0;
            test_type = atoi(argv[i + 1]);
            i += 2;
        } else if (!strcmp("-r", argv[i])) {
            adding_elements = 0;
            test.heap_size = atoi(argv[i + 1]);
            if (test.heap_size <= 0) {
                printf("heap size should be a positive integer\n");
                return 1;
            }
            test.offsets = (ADIO_Offset *) malloc(test.heap_size * sizeof(ADIO_Offset));
            test_type = RANDOM;
            i += 2;
        } else if (!strcmp("-e", argv[i])) {
            test.heap_size = argc - 2;
            if (test.heap_size <= 0) {
                printf("need at least one key\n");
                return 1;
            }
            test.offsets = (ADIO_Offset *) malloc(test.heap_size * sizeof(ADIO_Offset));
            adding_elements = 1;
            test_type = CUSTOM;
            i++;
        } else if (!strcmp("-v", argv[i])) {
            verify = 1;
            i++;
        } else if (!strcmp("-p", argv[i])) {
            print = 1;
            i++;
        } else if (!strcmp("-V", argv[i])) {
            verify = 0;
            i++;
        } else if (!strcmp("-P", argv[i])) {
            print = 0;
            i++;
        } else if (adding_elements) {
            test.offsets[curr_add_idx] = atoi(argv[i]);
            curr_add_idx++;
            i++;
        } else {
            printf("Illegal argument: %s", argv[i]);
            print_usage();
            return 1;
        }
    }

    if (test_type == RANDOM) {
        fill_random_test(&test);
        strcpy(test.name, "RANDOMIZED TEST");
    } else if (test_type == CUSTOM)
        strcpy(test.name, "CUSTOM TEST");
    if ((test_type == CUSTOM) || (test_type == RANDOM)) {
        test.print = print;
        test.verify = verify;
        test.action_arr_sz = 2;
        test.action_arr = (int *) malloc(test.action_arr_sz * sizeof(int));
        test.action_count_arr = (int *) malloc(test.action_arr_sz * sizeof(int));
        /* build the entire heap */
        /* test.action_arr[0]       = BUILD;
         * test.action_count_arr[0] = 1; */
        /* insert keys one at a time */
        test.action_arr[0] = INSERT;
        test.action_count_arr[0] = test.heap_size;
        /* extract all the keys */
        test.action_arr[1] = EXTRACT;
        test.action_count_arr[1] = test.heap_size;

        if (verify) {
            test.correct_order = (ADIO_Offset *) malloc(test.heap_size * sizeof(ADIO_Offset));
            dumb_sort(&test);
        }
        if (print)
            print_params(&test);
        run_test(&test);
    } else {
        if (test_type == ALL) {
            for (i = 0; i < PREDEF_TESTS; i++) {
                predefined_tests[i].print = print;
                predefined_tests[i].verify = verify;
                init_predefined_test(&predefined_tests[i], i);
                if (print)
                    print_params(&test);
                run_test(&predefined_tests[i]);
            }
        } else {
            predefined_tests[test_type - 1].print = print;
            predefined_tests[test_type - 1].verify = verify;
            init_predefined_test(&predefined_tests[test_type - 1], test_type - 1);
            if (print)
                print_params(&predefined_tests[test_type - 1]);
            run_test(&predefined_tests[test_type - 1]);
        }
    }

    return 0;
}

void print_usage()
{
    printf("Usage: test <options>\n"
           "   -r <size>  Create a random test and verify of size <size>\n"
           "   -e <keys>  test with the space delimited list of keys\n"
           "   -p         print parameters and keys (default)\n"
           "   -P         do not print parameters and keys\n"
           "   -v         verify keys (default)\n" "   -V         do not verify keys\n");
}

void print_keys(ADIO_Offset * offsets, int size)
{
    int i;
    for (i = 0; i < size; i++)
        printf("%lld ", offsets[i]);
}

void print_params(test_params_t * params)
{
    int i;
    static char action_map[3][8] = { "BUILD", "INSERT", "EXTRACT" };

    printf("----------------Test Parameters---------------\n");
    printf("Actions:\n");
    for (i = 0; i < params->action_arr_sz; i++) {
        printf("%sx%d\n", action_map[params->action_arr[i]], params->action_count_arr[i]);
    }

    printf("Initial order :\n");
    print_keys(params->offsets, params->heap_size);
    printf("\n");

    if (params->verify) {
        printf("Expected order:\n");
        print_keys(params->correct_order, params->heap_size);
        printf("\n");
    }
    printf("----------------------------------------------\n");
}

void fill_random_test(test_params_t * params)
{
    int i;
    int max_key;
    time_t seed;
    int order = 0;

    time(&seed);
    srand(seed);

    order = 0;
    max_key = 1;
    while (order < 25) {
        max_key *= 10;
        if (!((int) (params->heap_size / max_key)))
            break;
        order++;
    }
    for (i = 0; i < params->heap_size; i++)
        params->offsets[i] = (rand() % max_key);
}

void dumb_sort(test_params_t * params)
{
    ADIO_Offset *offsets, tmp_offset;
    int i, j;

    offsets = params->correct_order;
    memcpy(offsets, params->offsets, params->heap_size * sizeof(ADIO_Offset));
    for (i = 0; i < params->heap_size; i++) {
        for (j = i; j < params->heap_size; j++) {
            if (offsets[j] < offsets[i]) {
                tmp_offset = offsets[i];
                offsets[i] = offsets[j];
                offsets[j] = tmp_offset;
            }
        }
    }
}

int run_test(test_params_t * test)
{
    heap_t myheap;
    ADIO_Offset *extracted;
    int stored_proc;
    ADIO_Offset stored_reg_max_len;
    int i, j, k, err_flag = 0;
    int curr_insert_idx = 0;
    int curr_extract_idx = 0;

    create_heap(&myheap, test->heap_size);
    myheap.size = 0;

    extracted = (ADIO_Offset *) malloc(test->heap_size * sizeof(ADIO_Offset));
    for (i = 0; i < test->action_arr_sz; i++) {
        for (j = 0; j < test->action_count_arr[i]; j++) {
            switch (test->action_arr[i]) {
                case BUILD:
                    myheap.size = test->heap_size;
                    for (k = 0; k < test->heap_size; k++) {
                        myheap.nodes[k].offset = test->offsets[k];
                        myheap.nodes[k].proc = k;
                    }
                    build_heap(&myheap);
                    break;
                case INSERT:
                    ADIOI_Heap_insert(&myheap, test->offsets[curr_insert_idx],
                                      curr_insert_idx, curr_insert_idx);
                    curr_insert_idx++;
                    break;
                case EXTRACT:
                    heap_extract_min(&myheap, &extracted[curr_extract_idx],
                                     &stored_proc, &stored_reg_max_len);
                    if (test->verify && (extracted[curr_extract_idx] !=
                                         test->correct_order[curr_extract_idx]))
                        err_flag++;
                    curr_extract_idx++;
                    break;
                case EXTRACT_INSERT:
                    heap_extract_min(&myheap, &extracted[curr_extract_idx],
                                     &stored_proc, &stored_reg_max_len);
                    if (test->verify && (extracted[curr_extract_idx] !=
                                         test->correct_order[curr_extract_idx]))
                        err_flag++;
                    curr_extract_idx++;

                    ADIOI_Heap_insert(&myheap, test->offsets[curr_insert_idx],
                                      curr_insert_idx, curr_insert_idx);
                    curr_insert_idx++;
                    break;
                default:
                    break;
            }
        }
    }

    if (test->verify) {
        if (err_flag) {
            printf("***%s FAILED***\n", test->name);
            if (test->print) {
                printf("Min extraction:\n");
                print_keys(extracted, test->heap_size);
                printf("\n");
            }
        } else
            printf("***%s PASSED***\n", test->name);
    }

    free_heap(&myheap);
    free(extracted);
    /* clean up test params */
    free(test->offsets);
    if (test->verify)
        free(test->correct_order);
    free(test->action_arr);
    free(test->action_count_arr);

    return err_flag;
}

void init_predefined_test(test_params_t * params, int index)
{

    switch (index) {
        case 0:
            strcpy(params->name, "TEST 1");
            params->heap_size = 15;
            params->action_arr_sz = 3;

            /* allocate space */
            params->action_arr = (int *) malloc(params->action_arr_sz * sizeof(int));
            params->action_count_arr = (int *) malloc(params->action_arr_sz * sizeof(int));
            params->offsets = (ADIO_Offset *) malloc(params->heap_size * sizeof(ADIO_Offset));
            if (params->verify)
                params->correct_order =
                    (ADIO_Offset *) malloc(params->heap_size * sizeof(ADIO_Offset));

            /* Set procs */
            params->offsets[0] = 65;
            params->offsets[1] = 53;
            params->offsets[2] = 51;
            params->offsets[3] = 74;
            params->offsets[4] = 1;
            params->offsets[5] = 3;
            params->offsets[6] = 86;
            params->offsets[7] = 82;
            params->offsets[8] = 42;
            params->offsets[9] = 62;
            params->offsets[10] = 33;
            params->offsets[11] = 12;
            params->offsets[12] = 79;
            params->offsets[13] = 13;
            params->offsets[14] = 28;

            if (params->verify) {
                params->correct_order[0] = 1;
                params->correct_order[1] = 3;
                params->correct_order[2] = 12;
                params->correct_order[3] = 33;
                params->correct_order[4] = 13;
                params->correct_order[5] = 28;
                params->correct_order[6] = 42;
                params->correct_order[7] = 51;
                params->correct_order[8] = 53;
                params->correct_order[9] = 62;
                params->correct_order[10] = 65;
                params->correct_order[11] = 74;
                params->correct_order[12] = 79;
                params->correct_order[13] = 82;
                params->correct_order[14] = 86;
            }

            params->action_arr[0] = INSERT;
            params->action_arr[1] = EXTRACT_INSERT;
            params->action_arr[11] = EXTRACT;

            params->action_count_arr[0] = 10;
            params->action_count_arr[1] = 5;
            params->action_count_arr[11] = 10;
            break;
        case 1:
            strcpy(params->name, "TEST 1");
            params->heap_size = 15;
            params->action_arr_sz = 3;

            /* allocate space */
            params->action_arr = (int *) malloc(params->action_arr_sz * sizeof(int));
            params->action_count_arr = (int *) malloc(params->action_arr_sz * sizeof(int));
            params->offsets = (ADIO_Offset *) malloc(params->heap_size * sizeof(ADIO_Offset));
            if (params->verify)
                params->correct_order =
                    (ADIO_Offset *) malloc(params->heap_size * sizeof(ADIO_Offset));

            /* Set values */
            params->offsets[0] = 65;
            params->offsets[1] = 53;
            params->offsets[2] = 51;
            params->offsets[3] = 74;
            params->offsets[4] = 1;
            params->offsets[5] = 3;
            params->offsets[6] = 86;
            params->offsets[7] = 82;
            params->offsets[8] = 42;
            params->offsets[9] = 62;
            params->offsets[10] = 33;
            params->offsets[11] = 12;
            params->offsets[12] = 79;
            params->offsets[13] = 13;
            params->offsets[14] = 28;

            if (params->verify) {
                params->correct_order[0] = 1;
                params->correct_order[1] = 3;
                params->correct_order[2] = 12;
                params->correct_order[3] = 33;
                params->correct_order[4] = 13;
                params->correct_order[5] = 28;
                params->correct_order[6] = 42;
                params->correct_order[7] = 51;
                params->correct_order[8] = 53;
                params->correct_order[9] = 62;
                params->correct_order[10] = 65;
                params->correct_order[11] = 74;
                params->correct_order[12] = 79;
                params->correct_order[13] = 82;
                params->correct_order[14] = 86;
            }

            params->action_arr[0] = INSERT;
            params->action_arr[1] = EXTRACT_INSERT;
            params->action_arr[11] = EXTRACT;

            params->action_count_arr[0] = 10;
            params->action_count_arr[1] = 5;
            params->action_count_arr[11] = 10;
            break;
        default:
            break;
    }
}