00001 #include "../adio/include/heap-sort.h"
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <time.h>
00006
00007 #define PREDEF_TESTS 2
00008
00009 #define ALL 0
00010 #define RANDOM -1
00011 #define CUSTOM -2
00012
00013
00014 #define BUILD 0
00015 #define INSERT 1
00016 #define EXTRACT 2
00017 #define EXTRACT_INSERT 3
00018
00019 typedef struct {
00020 char name[64];
00021 int heap_size;
00022 int print;
00023 int verify;
00024 int action_arr_sz;
00025 int *action_arr;
00026 int *action_count_arr;
00027 ADIO_Offset *offsets;
00028 ADIO_Offset *correct_order;
00029 } test_params_t;
00030
00031 void print_usage();
00032 void print_keys(ADIO_Offset* offsets, int size);
00033 void print_params(test_params_t *params);
00034 int run_test(test_params_t *test);
00035 void fill_random_test(test_params_t *params);
00036 void init_predefined_test(test_params_t *params, int index);
00037 void dumb_sort(test_params_t *params);
00038
00039 int main(int argc, char **argv) {
00040 int i, print = 1, verify = 1;
00041 int adding_elements;
00042 int curr_add_idx;
00043 int test_type = RANDOM;
00044 test_params_t predefined_tests[PREDEF_TESTS];
00045 test_params_t test;
00046
00047
00048 adding_elements = 0;
00049 curr_add_idx = 0;
00050 if (argc == 1) {
00051 print_usage();
00052 return 1;
00053 }
00054 i = 1;
00055 while (i < argc) {
00056 if (!strcmp("-A", argv[i])) {
00057 adding_elements = 0;
00058 test_type = ALL;
00059 i++;
00060 }
00061 else if (!strcmp("-T", argv[i])) {
00062 adding_elements = 0;
00063 test_type = atoi(argv[i+1]);
00064 i += 2;
00065 }
00066 else if (!strcmp("-r", argv[i])) {
00067 adding_elements = 0;
00068 test.heap_size = atoi(argv[i+1]);
00069 if (test.heap_size <= 0) {
00070 printf("heap size should be a positive integer\n");
00071 return 1;
00072 }
00073 test.offsets = (ADIO_Offset *) malloc(test.heap_size*sizeof(ADIO_Offset));
00074 test_type = RANDOM;
00075 i += 2;
00076 }
00077 else if (!strcmp("-e", argv[i])) {
00078 test.heap_size = argc - 2;
00079 if (test.heap_size <= 0) {
00080 printf("need at least one key\n");
00081 return 1;
00082 }
00083 test.offsets = (ADIO_Offset *) malloc(test.heap_size*sizeof(ADIO_Offset));
00084 adding_elements = 1;
00085 test_type = CUSTOM;
00086 i++;
00087 }
00088 else if (!strcmp("-v", argv[i])) {
00089 verify = 1;
00090 i++;
00091 }
00092 else if (!strcmp("-p", argv[i])) {
00093 print = 1;
00094 i++;
00095 }
00096 else if (!strcmp("-V", argv[i])) {
00097 verify = 0;
00098 i++;
00099 }
00100 else if (!strcmp("-P", argv[i])) {
00101 print = 0;
00102 i++;
00103 }
00104 else if (adding_elements) {
00105 test.offsets[curr_add_idx] = atoi(argv[i]);
00106 curr_add_idx++;
00107 i++;
00108 }
00109 else {
00110 printf("Illegal argument: %s", argv[i]);
00111 print_usage();
00112 return 1;
00113 }
00114 }
00115
00116 if (test_type == RANDOM) {
00117 fill_random_test(&test);
00118 strcpy(test.name, "RANDOMIZED TEST");
00119 }
00120 else if (test_type == CUSTOM)
00121 strcpy(test.name, "CUSTOM TEST");
00122 if ((test_type == CUSTOM) || (test_type == RANDOM)) {
00123 test.print = print;
00124 test.verify = verify;
00125 test.action_arr_sz = 2;
00126 test.action_arr = (int *) malloc(test.action_arr_sz*sizeof(int));
00127 test.action_count_arr = (int *) malloc(test.action_arr_sz*sizeof(int));
00128
00129
00130
00131
00132 test.action_arr[0] = INSERT;
00133 test.action_count_arr[0] = test.heap_size;
00134
00135 test.action_arr[1] = EXTRACT;
00136 test.action_count_arr[1] = test.heap_size;
00137
00138 if (verify) {
00139 test.correct_order = (ADIO_Offset *)malloc(test.heap_size*sizeof(ADIO_Offset));
00140 dumb_sort(&test);
00141 }
00142 if (print)
00143 print_params(&test);
00144 run_test(&test);
00145 }
00146 else {
00147 if (test_type == ALL) {
00148 for (i=0; i<PREDEF_TESTS; i++) {
00149 predefined_tests[i].print = print;
00150 predefined_tests[i].verify = verify;
00151 init_predefined_test(&predefined_tests[i], i);
00152 if (print)
00153 print_params(&test);
00154 run_test(&predefined_tests[i]);
00155 }
00156 }
00157 else {
00158 predefined_tests[test_type-1].print = print;
00159 predefined_tests[test_type-1].verify = verify;
00160 init_predefined_test(&predefined_tests[test_type-1], test_type-1);
00161 if (print)
00162 print_params(&predefined_tests[test_type-1]);
00163 run_test(&predefined_tests[test_type-1]);
00164 }
00165 }
00166
00167 return 0;
00168 }
00169
00170 void print_usage() {
00171 printf(
00172 "Usage: test <options>\n"
00173 " -r <size> Create a random test and verify of size <size>\n"
00174 " -e <keys> test with the space delimited list of keys\n"
00175 " -p print parameters and keys (default)\n"
00176 " -P do not print parameters and keys\n"
00177 " -v verify keys (default)\n"
00178 " -V do not verify keys\n"
00179 );
00180 }
00181
00182 void print_keys(ADIO_Offset *offsets, int size) {
00183 int i;
00184 for (i=0; i < size; i++)
00185 printf("%lld ", offsets[i]);
00186 }
00187
00188 void print_params(test_params_t *params) {
00189 int i;
00190 static char action_map[3][8] = {"BUILD", "INSERT", "EXTRACT"};
00191
00192 printf("----------------Test Parameters---------------\n");
00193 printf("Actions:\n");
00194 for (i=0; i<params->action_arr_sz; i++) {
00195 printf("%sx%d\n", action_map[params->action_arr[i]],
00196 params->action_count_arr[i]);
00197 }
00198
00199 printf("Initial order :\n");
00200 print_keys(params->offsets, params->heap_size);
00201 printf("\n");
00202
00203 if (params->verify) {
00204 printf("Expected order:\n");
00205 print_keys(params->correct_order, params->heap_size);
00206 printf("\n");
00207 }
00208 printf("----------------------------------------------\n");
00209 }
00210
00211 void fill_random_test(test_params_t *params) {
00212 int i;
00213 int max_key;
00214 time_t seed;
00215 int order = 0;
00216
00217 time(&seed);
00218 srand(seed);
00219
00220 order = 0;
00221 max_key = 1;
00222 while (order < 25) {
00223 max_key *= 10;
00224 if (!((int) (params->heap_size / max_key)))
00225 break;
00226 order++;
00227 }
00228 for (i=0; i < params->heap_size; i++)
00229 params->offsets[i] = (rand() % max_key);
00230 }
00231
00232 void dumb_sort(test_params_t *params) {
00233 ADIO_Offset *offsets, tmp_offset;
00234 int i, j;
00235
00236 offsets = params->correct_order;
00237 memcpy(offsets, params->offsets, params->heap_size*sizeof(ADIO_Offset));
00238 for (i=0; i < params->heap_size; i++) {
00239 for (j=i; j < params->heap_size; j++) {
00240 if (offsets[j] < offsets[i]) {
00241 tmp_offset = offsets[i];
00242 offsets[i] = offsets[j];
00243 offsets[j] = tmp_offset;
00244 }
00245 }
00246 }
00247 }
00248
00249 int run_test(test_params_t *test) {
00250 heap_t myheap;
00251 ADIO_Offset *extracted;
00252 int stored_proc;
00253 ADIO_Offset stored_reg_max_len;
00254 int i, j, k, err_flag = 0;
00255 int curr_insert_idx = 0;
00256 int curr_extract_idx = 0;
00257
00258 create_heap(&myheap, test->heap_size);
00259 myheap.size = 0;
00260
00261 extracted = (ADIO_Offset *) malloc(test->heap_size * sizeof(ADIO_Offset));
00262 for (i=0; i < test->action_arr_sz; i++) {
00263 for (j=0; j<test->action_count_arr[i]; j++) {
00264 switch (test->action_arr[i])
00265 {
00266 case BUILD:
00267 myheap.size = test->heap_size;
00268 for (k=0; k < test->heap_size; k++) {
00269 myheap.nodes[k].offset = test->offsets[k];
00270 myheap.nodes[k].proc = k;
00271 }
00272 build_heap(&myheap);
00273 break;
00274 case INSERT:
00275 ADIOI_Heap_insert(&myheap, test->offsets[curr_insert_idx],
00276 curr_insert_idx, curr_insert_idx);
00277 curr_insert_idx++;
00278 break;
00279 case EXTRACT:
00280 heap_extract_min(&myheap, &extracted[curr_extract_idx],
00281 &stored_proc, &stored_reg_max_len);
00282 if (test->verify && (extracted[curr_extract_idx] !=
00283 test->correct_order[curr_extract_idx]))
00284 err_flag++;
00285 curr_extract_idx++;
00286 break;
00287 case EXTRACT_INSERT:
00288 heap_extract_min(&myheap, &extracted[curr_extract_idx],
00289 &stored_proc, &stored_reg_max_len);
00290 if (test->verify &&(extracted[curr_extract_idx] !=
00291 test->correct_order[curr_extract_idx]))
00292 err_flag++;
00293 curr_extract_idx++;
00294
00295 ADIOI_Heap_insert(&myheap, test->offsets[curr_insert_idx],
00296 curr_insert_idx, curr_insert_idx);
00297 curr_insert_idx++;
00298 break;
00299 default:
00300 break;
00301 }
00302 }
00303 }
00304
00305 if (test->verify) {
00306 if (err_flag) {
00307 printf("***%s FAILED***\n", test->name);
00308 if (test->print) {
00309 printf("Min extraction:\n");
00310 print_keys(extracted, test->heap_size);
00311 printf("\n");
00312 }
00313 }
00314 else
00315 printf("***%s PASSED***\n", test->name);
00316 }
00317
00318 free_heap(&myheap);
00319 free(extracted);
00320
00321 free(test->offsets);
00322 if (test->verify)
00323 free(test->correct_order);
00324 free(test->action_arr);
00325 free(test->action_count_arr);
00326
00327 return err_flag;
00328 }
00329
00330 void init_predefined_test(test_params_t *params, int index) {
00331
00332 switch (index)
00333 {
00334 case 0:
00335 strcpy(params->name, "TEST 1");
00336 params->heap_size = 15;
00337 params->action_arr_sz = 3;
00338
00339
00340 params->action_arr =
00341 (int *) malloc (params->action_arr_sz*sizeof(int));
00342 params->action_count_arr =
00343 (int *) malloc (params->action_arr_sz*sizeof(int));
00344 params->offsets = (ADIO_Offset *) malloc(params->heap_size*sizeof(ADIO_Offset));
00345 if (params->verify)
00346 params->correct_order =
00347 (ADIO_Offset *) malloc(params->heap_size*sizeof(ADIO_Offset));
00348
00349
00350 params->offsets[0] = 65;
00351 params->offsets[1] = 53;
00352 params->offsets[2] = 51;
00353 params->offsets[3] = 74;
00354 params->offsets[4] = 1;
00355 params->offsets[5] = 3;
00356 params->offsets[6] = 86;
00357 params->offsets[7] = 82;
00358 params->offsets[8] = 42;
00359 params->offsets[9] = 62;
00360 params->offsets[10] = 33;
00361 params->offsets[11] = 12;
00362 params->offsets[12] = 79;
00363 params->offsets[13] = 13;
00364 params->offsets[14] = 28;
00365
00366 if (params->verify) {
00367 params->correct_order[0] = 1;
00368 params->correct_order[1] = 3;
00369 params->correct_order[2] = 12;
00370 params->correct_order[3] = 33;
00371 params->correct_order[4] = 13;
00372 params->correct_order[5] = 28;
00373 params->correct_order[6] = 42;
00374 params->correct_order[7] = 51;
00375 params->correct_order[8] = 53;
00376 params->correct_order[9] = 62;
00377 params->correct_order[10] = 65;
00378 params->correct_order[11] = 74;
00379 params->correct_order[12] = 79;
00380 params->correct_order[13] = 82;
00381 params->correct_order[14] = 86;
00382 }
00383
00384 params->action_arr[0] = INSERT;
00385 params->action_arr[1] = EXTRACT_INSERT;
00386 params->action_arr[11] = EXTRACT;
00387
00388 params->action_count_arr[0] = 10;
00389 params->action_count_arr[1] = 5;
00390 params->action_count_arr[11] = 10;
00391 break;
00392 case 1:
00393 strcpy(params->name, "TEST 1");
00394 params->heap_size = 15;
00395 params->action_arr_sz = 3;
00396
00397
00398 params->action_arr =
00399 (int *) malloc (params->action_arr_sz*sizeof(int));
00400 params->action_count_arr =
00401 (int *) malloc (params->action_arr_sz*sizeof(int));
00402 params->offsets = (ADIO_Offset *) malloc(params->heap_size*sizeof(ADIO_Offset));
00403 if (params->verify)
00404 params->correct_order =
00405 (ADIO_Offset *) malloc(params->heap_size*sizeof(ADIO_Offset));
00406
00407
00408 params->offsets[0] = 65;
00409 params->offsets[1] = 53;
00410 params->offsets[2] = 51;
00411 params->offsets[3] = 74;
00412 params->offsets[4] = 1;
00413 params->offsets[5] = 3;
00414 params->offsets[6] = 86;
00415 params->offsets[7] = 82;
00416 params->offsets[8] = 42;
00417 params->offsets[9] = 62;
00418 params->offsets[10] = 33;
00419 params->offsets[11] = 12;
00420 params->offsets[12] = 79;
00421 params->offsets[13] = 13;
00422 params->offsets[14] = 28;
00423
00424 if (params->verify) {
00425 params->correct_order[0] = 1;
00426 params->correct_order[1] = 3;
00427 params->correct_order[2] = 12;
00428 params->correct_order[3] = 33;
00429 params->correct_order[4] = 13;
00430 params->correct_order[5] = 28;
00431 params->correct_order[6] = 42;
00432 params->correct_order[7] = 51;
00433 params->correct_order[8] = 53;
00434 params->correct_order[9] = 62;
00435 params->correct_order[10] = 65;
00436 params->correct_order[11] = 74;
00437 params->correct_order[12] = 79;
00438 params->correct_order[13] = 82;
00439 params->correct_order[14] = 86;
00440 }
00441
00442 params->action_arr[0] = INSERT;
00443 params->action_arr[1] = EXTRACT_INSERT;
00444 params->action_arr[11] = EXTRACT;
00445
00446 params->action_count_arr[0] = 10;
00447 params->action_count_arr[1] = 5;
00448 params->action_count_arr[11] = 10;
00449 break;
00450 default:
00451 break;
00452 }
00453 }