root/test/util/opal_argv.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. test1
  3. test2
  4. test3
  5. test4
  6. test5
  7. test6
  8. test7
  9. test8
  10. test9
  11. test10

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * $COPYRIGHT$
  13  *
  14  * Additional copyrights may follow
  15  *
  16  * $HEADER$
  17  */
  18 
  19 #include "ompi_config.h"
  20 #include <stdio.h>
  21 #include <string.h>
  22 #include <stdlib.h>
  23 
  24 #include "support.h"
  25 #include "opal/constants.h"
  26 #include "opal/util/argv.h"
  27 
  28 static bool test1(void);
  29 static bool test2(void);
  30 static bool test3(void);
  31 static bool test4(void);
  32 static bool test5(void);
  33 static bool test6(void);
  34 static bool test7(void);
  35 static bool test8(void);
  36 static bool test9(void);
  37 static bool test10(void);
  38 
  39 
  40 int main(int argc, char* argv[])
  41 {
  42 
  43   test_init("opal_argv_t");
  44 
  45   if( test1() ) test_success();
  46   else test_failure("test1 argv test failed");
  47 
  48   if( test2() ) test_success();
  49   else test_failure("test2 argv test failed");
  50 
  51   if( test3() ) test_success();
  52   else test_failure("test3 argv test failed");
  53 
  54   if( test4() ) test_success();
  55   else test_failure("test4 argv test failed");
  56 
  57   if( test5() ) test_success();
  58   else test_failure("test5 argv test failed");
  59 
  60   if( test6() ) test_success();
  61   else test_failure("test6 argv test failed");
  62 
  63   if( test7() ) test_success();
  64   else test_failure("test7 argv test failed");
  65 
  66   if( test8() ) test_success();
  67   else test_failure("test8 argv test failed");
  68 
  69   if (test9()) {
  70       test_success();
  71   } else {
  72       test_failure("test9 argv test failed");
  73   }
  74 
  75   if (test10()) {
  76       test_success();
  77   } else {
  78       test_failure("test10 argv test failed");
  79   }
  80 
  81   /* All done */
  82   return test_finalize();
  83 }
  84 
  85 
  86 static bool test1(void)
  87 {
  88   char *a[] = { "argv1", "argv2", "argv3", NULL };
  89   char **argv = NULL;
  90   int i, j, argc = 28;
  91 
  92   /* Test basic functionality.  Start with a NULL argv and add the
  93      contents of the a array.
  94 
  95      Set argc to be an initiall bogus number -- opal_argv_add() should
  96      reset it back to zero after the first iteration.
  97 
  98      After adding the a[i], ensure that argv[0 - (i-1)] are the same
  99      as a[0 - (i-1)].
 100 
 101      Also ensure that a[i + 1] == NULL and that argc is the proper
 102      value. */
 103 
 104   for (i = 0; a[i] != NULL; ++i) {
 105     if (opal_argv_append(&argc, &argv, a[i]) != OPAL_SUCCESS) {
 106       return false;
 107     }
 108     for (j = 0; j <= i; ++j) {
 109       if (strcmp(argv[j], a[j]) != 0) {
 110         return false;
 111       }
 112     }
 113     if (NULL != argv[i + 1]) {
 114       return false;
 115     }
 116     if (argc != i + 1) {
 117       return false;
 118     }
 119   }
 120   opal_argv_free(argv);
 121 
 122   return true;
 123 }
 124 
 125 
 126 static bool test2(void)
 127 {
 128   int i, j;
 129   char **argv = NULL;
 130   int argc;
 131   char *a[] = { "aaa", "bbb", "ccc", NULL };
 132   char *b[4];
 133 
 134   /* Similar test to above, but ensure that opal_argv_add is actually
 135      *copying* the string by value, not by reference.  So copy the a
 136      array into b, and then opal_argv_add() from b.  After that,
 137      scribble in the first character of the b[] string that we just
 138      added, and compare all entries in a to argv -- they should be
 139      identical (even though b is now corrupted). */
 140 
 141   for (i = 0; a[i] != NULL; ++i) {
 142     b[i] = strdup(a[i]);
 143   }
 144   b[i] = NULL;
 145 
 146   for (i = 0; b[i] != NULL; ++i) {
 147     if (opal_argv_append(&argc, &argv, b[i]) != OPAL_SUCCESS) {
 148       return false;
 149     }
 150     ++b[i][0];
 151     for (j = 0; j <= i; ++j) {
 152       if (strcmp(argv[j], a[j]) != 0) {
 153         return false;
 154       }
 155     }
 156     if (NULL != argv[i + 1]) {
 157       return false;
 158     }
 159     if (argc != i + 1) {
 160       return false;
 161     }
 162   }
 163 
 164   opal_argv_free(argv);
 165   for (i = 0; b[i] != NULL; ++i) {
 166     free(b[i]);
 167   }
 168 
 169   return true;
 170 }
 171 
 172 
 173 static bool test3(void)
 174 {
 175   int i;
 176   int argc;
 177   char **argv = NULL;
 178   char *a[] = { "aaa", "bbb", "ccc", NULL };
 179   char *b[4];
 180 
 181   /* Try to free a null argv -- should be harmless (we'll seg fault if
 182      it's not!) */
 183 
 184   opal_argv_free(argv);
 185 
 186   /* Now add some stuff and try to free it.  We'll seg fault if
 187      anything goes wrong.  a is on the stack, so if it mistakenly
 188      tries to free it, we should get a seg fault. */
 189 
 190   for (i = 0; a[i] != NULL; ++i) {
 191     if (opal_argv_append(&argc, &argv, a[i]) != OPAL_SUCCESS) {
 192       return false;
 193     }
 194   }
 195   opal_argv_free(argv);
 196 
 197   /* Do the same thing but guarantee that the copied array was from
 198      the heap and was freed before we call opal_argv_free(). */
 199 
 200   argc = 0;
 201   argv = NULL;
 202   for (i = 0; a[i] != NULL; ++i) {
 203     b[i] = strdup(a[i]);
 204   }
 205   b[i] = NULL;
 206   for (i = 0; b[i] != NULL; ++i) {
 207     if (opal_argv_append(&argc, &argv, b[i]) != OPAL_SUCCESS) {
 208       return false;
 209     }
 210   }
 211   for (i = 0; b[i] != NULL; ++i) {
 212     free(b[i]);
 213   }
 214   opal_argv_free(argv);
 215 
 216   /* All done */
 217 
 218   return true;
 219 }
 220 
 221 
 222 static bool test4(void)
 223 {
 224   size_t i, count;
 225   char *a = strdup("the quick  brown fox jumped over  the lazy  dog a_really_long_argument_to_force_a_long_copy_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
 226   char **b;
 227   char *start;
 228 
 229   /* split a string into an argv, and compare it against the original
 230      string.  Add double spaces into the string; opal_argv_split()
 231      should skip them. */
 232 
 233   b = opal_argv_split(a, ' ');
 234 
 235   for (count = i = 1; i < strlen(a); ++i) {
 236     if (a[i] != ' ' && a[i - 1] == ' ') {
 237       ++count;
 238     }
 239   }
 240   for (i = 0; b[i] != NULL; ++i) {
 241     continue;
 242   }
 243   if (i != count) {
 244     return false;
 245   }
 246 
 247   /* now do the same thing and compare each token in b */
 248 
 249   for (start = a, count = i = 0; i < strlen(a); ++i) {
 250     if (a[i] == ' ' && a[i - 1] != ' ') {
 251       a[i] = '\0';
 252       if (strcmp(start, b[count]) != 0) {
 253         return false;
 254       }
 255       ++count;
 256       a[i] = ' ';
 257     }
 258     if (a[i] == ' ' && a[i + 1] != ' ') {
 259       start = a + i + 1;
 260     }
 261   }
 262   if (strcmp(start, b[count]) != 0) {
 263     return false;
 264   }
 265 
 266   /* all done */
 267 
 268   opal_argv_free(b);
 269   free(a);
 270   return true;
 271 }
 272 
 273 
 274 static bool test5(void)
 275 {
 276   char *a[] = { "aaa", "bbb", "ccc", NULL };
 277 
 278   return (opal_argv_count(NULL) == 0 && opal_argv_count(a) == 3);
 279 }
 280 
 281 
 282 static bool test6(void)
 283 {
 284   char *a = "the quick brown fox jumped over the lazy dog a_really_long_argument_to_force_a_long_copy_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
 285   char **b;
 286   char *c;
 287 
 288   /* split the string above and then join it -- the joined version
 289      should be just like the original */
 290 
 291   b = opal_argv_split(a, ' ');
 292   c = opal_argv_join(b, ' ');
 293 
 294   if (strcmp(a, c) != 0) {
 295     return false;
 296   }
 297 
 298   /* All done */
 299 
 300   free(c);
 301   opal_argv_free(b);
 302   return true;
 303 }
 304 
 305 
 306 static bool test7(void)
 307 {
 308   char *a[] = { "a", "b", "c", NULL };
 309   size_t a_len = (1 + 1 + sizeof(char *)) * 3 + sizeof(char **);
 310 
 311   /* check a NULL pointer first -- should return 0 */
 312 
 313   if (opal_argv_len(NULL) != (size_t) 0) {
 314     return false;
 315   }
 316 
 317   /* now check a real string */
 318   /* size should be (sizeof(char **) + (sizeof(char) + sizeof('\0') +
 319      sizeof(char*)) * 3) */
 320 
 321   if (opal_argv_len(a) != a_len) {
 322     return false;
 323   }
 324 
 325   /* All done */
 326 
 327   return true;
 328 }
 329 
 330 
 331 static bool test8(void)
 332 {
 333   char *a[] = { "aaa", "bbbbbbbb", "cccccccccc", NULL };
 334   int i;
 335   char **b;
 336 
 337   /* bozo case */
 338 
 339   if (NULL != opal_argv_copy(NULL)) {
 340     return false;
 341   }
 342 
 343   /* dup the a array and compare it (array length, contents, etc.) */
 344 
 345   b = opal_argv_copy(a);
 346 
 347   if (opal_argv_count(a) != opal_argv_count(b)) {
 348     return false;
 349   }
 350   for (i = 0; a[i] != NULL; ++i) {
 351     if (0 != strcmp(a[i], b[i])) {
 352       return false;
 353     }
 354   }
 355 
 356   /* All done */
 357 
 358   opal_argv_free(b);
 359   return true;
 360 }
 361 
 362 
 363 static bool test9(void)
 364 {
 365     char **a = NULL;
 366     int argc;
 367 
 368     /* bozo cases */
 369 
 370     if (OPAL_SUCCESS != opal_argv_delete(NULL, NULL, 0, 0)) {
 371         return false;
 372     }
 373 
 374     a = NULL;
 375     argc = 0;
 376     opal_argv_append(&argc, &a, "foo");
 377     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 7, 1) ||
 378         1 != opal_argv_count(a)) {
 379         return false;
 380     }
 381     opal_argv_free(a);
 382 
 383     a = NULL;
 384     argc = 0;
 385     opal_argv_append(&argc, &a, "foo");
 386     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 0, 0) ||
 387         1 != opal_argv_count(a)) {
 388         return false;
 389     }
 390     opal_argv_free(a);
 391 
 392     /* now some real tests */
 393     /* delete 1 off the top */
 394 
 395     a = NULL;
 396     argc = 0;
 397     opal_argv_append(&argc, &a, "a");
 398     opal_argv_append(&argc, &a, "b");
 399     opal_argv_append(&argc, &a, "c");
 400     opal_argv_append(&argc, &a, "d");
 401     opal_argv_append(&argc, &a, "e");
 402     opal_argv_append(&argc, &a, "f");
 403     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 0, 1) ||
 404         5 != opal_argv_count(a) ||
 405         0 != strcmp(a[0], "b") ||
 406         0 != strcmp(a[1], "c") ||
 407         0 != strcmp(a[2], "d") ||
 408         0 != strcmp(a[3], "e") ||
 409         0 != strcmp(a[4], "f")) {
 410         return false;
 411     }
 412     opal_argv_free(a);
 413 
 414     /* delete 2 off the top */
 415 
 416     a = NULL;
 417     argc = 0;
 418     opal_argv_append(&argc, &a, "a");
 419     opal_argv_append(&argc, &a, "b");
 420     opal_argv_append(&argc, &a, "c");
 421     opal_argv_append(&argc, &a, "d");
 422     opal_argv_append(&argc, &a, "e");
 423     opal_argv_append(&argc, &a, "f");
 424     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 0, 2) ||
 425         4 != opal_argv_count(a) ||
 426         0 != strcmp(a[0], "c") ||
 427         0 != strcmp(a[1], "d") ||
 428         0 != strcmp(a[2], "e") ||
 429         0 != strcmp(a[3], "f")) {
 430         return false;
 431     }
 432     opal_argv_free(a);
 433 
 434     /* delete 1 in the middle */
 435 
 436     a = NULL;
 437     argc = 0;
 438     opal_argv_append(&argc, &a, "a");
 439     opal_argv_append(&argc, &a, "b");
 440     opal_argv_append(&argc, &a, "c");
 441     opal_argv_append(&argc, &a, "d");
 442     opal_argv_append(&argc, &a, "e");
 443     opal_argv_append(&argc, &a, "f");
 444     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 1, 1) ||
 445         5 != opal_argv_count(a) ||
 446         0 != strcmp(a[0], "a") ||
 447         0 != strcmp(a[1], "c") ||
 448         0 != strcmp(a[2], "d") ||
 449         0 != strcmp(a[3], "e") ||
 450         0 != strcmp(a[4], "f")) {
 451         return false;
 452     }
 453     opal_argv_free(a);
 454 
 455     /* delete 2 in the middle */
 456 
 457     a = NULL;
 458     argc = 0;
 459     opal_argv_append(&argc, &a, "a");
 460     opal_argv_append(&argc, &a, "b");
 461     opal_argv_append(&argc, &a, "c");
 462     opal_argv_append(&argc, &a, "d");
 463     opal_argv_append(&argc, &a, "e");
 464     opal_argv_append(&argc, &a, "f");
 465     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 1, 2) ||
 466         4 != opal_argv_count(a) ||
 467         0 != strcmp(a[0], "a") ||
 468         0 != strcmp(a[1], "d") ||
 469         0 != strcmp(a[2], "e") ||
 470         0 != strcmp(a[3], "f")) {
 471         return false;
 472     }
 473     opal_argv_free(a);
 474 
 475     /* delete everything from the top */
 476 
 477     a = NULL;
 478     argc = 0;
 479     opal_argv_append(&argc, &a, "a");
 480     opal_argv_append(&argc, &a, "b");
 481     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 0, 99) ||
 482         0 != opal_argv_count(a)) {
 483         return false;
 484     }
 485     opal_argv_free(a);
 486 
 487     /* delete everything from the middle */
 488 
 489     a = NULL;
 490     argc = 0;
 491     opal_argv_append(&argc, &a, "a");
 492     opal_argv_append(&argc, &a, "b");
 493     if (OPAL_SUCCESS != opal_argv_delete(&argc, &a, 1, 99) ||
 494         1 != opal_argv_count(a) ||
 495         0 != strcmp(a[0], "a")) {
 496         return false;
 497     }
 498     opal_argv_free(a);
 499 
 500     /* All done */
 501 
 502     return true;
 503 }
 504 
 505 
 506 static bool test10(void)
 507 {
 508     char **orig;
 509     char **insert;
 510     int o, i;
 511 
 512     /* bozo cases */
 513 
 514     orig = NULL;
 515     o = 0;
 516     insert = NULL;
 517     i = 0;
 518     opal_argv_append(&i, &insert, "insert a");
 519     if (OPAL_SUCCESS == opal_argv_insert(NULL, 0, insert)) {
 520         return false;
 521     }
 522     opal_argv_append(&o, &orig, "orig a");
 523     if (OPAL_SUCCESS != opal_argv_insert(&orig, 0, NULL)) {
 524         return false;
 525     }
 526     if (OPAL_SUCCESS == opal_argv_insert(&orig, -1, insert)) {
 527         return false;
 528     }
 529     opal_argv_free(orig);
 530     opal_argv_free(insert);
 531 
 532     /* append to the end */
 533 
 534     orig = NULL;
 535     o = 0;
 536     insert = NULL;
 537     i = 0;
 538     opal_argv_append(&i, &insert, "insert a");
 539     opal_argv_append(&i, &insert, "insert b");
 540     opal_argv_append(&i, &insert, "insert c");
 541     opal_argv_append(&o, &orig, "orig a");
 542     opal_argv_append(&o, &orig, "orig b");
 543     opal_argv_append(&o, &orig, "orig c");
 544     if (OPAL_SUCCESS != opal_argv_insert(&orig, 99, insert) ||
 545         6 != opal_argv_count(orig) ||
 546         0 != strcmp(orig[0], "orig a") ||
 547         0 != strcmp(orig[1], "orig b") ||
 548         0 != strcmp(orig[2], "orig c") ||
 549         0 != strcmp(orig[3], "insert a") ||
 550         0 != strcmp(orig[4], "insert b") ||
 551         0 != strcmp(orig[5], "insert c")) {
 552         return false;
 553     }
 554     opal_argv_free(orig);
 555     opal_argv_free(insert);
 556 
 557     /* insert at the beginning */
 558 
 559     orig = NULL;
 560     o = 0;
 561     insert = NULL;
 562     i = 0;
 563     opal_argv_append(&i, &insert, "insert a");
 564     opal_argv_append(&i, &insert, "insert b");
 565     opal_argv_append(&i, &insert, "insert c");
 566     opal_argv_append(&o, &orig, "orig a");
 567     opal_argv_append(&o, &orig, "orig b");
 568     opal_argv_append(&o, &orig, "orig c");
 569     if (OPAL_SUCCESS != opal_argv_insert(&orig, 0, insert) ||
 570         6 != opal_argv_count(orig) ||
 571         0 != strcmp(orig[3], "orig a") ||
 572         0 != strcmp(orig[4], "orig b") ||
 573         0 != strcmp(orig[5], "orig c") ||
 574         0 != strcmp(orig[0], "insert a") ||
 575         0 != strcmp(orig[1], "insert b") ||
 576         0 != strcmp(orig[2], "insert c")) {
 577         return false;
 578     }
 579     opal_argv_free(orig);
 580     opal_argv_free(insert);
 581 
 582     /* insert in the middle */
 583 
 584     orig = NULL;
 585     o = 0;
 586     insert = NULL;
 587     i = 0;
 588     opal_argv_append(&i, &insert, "insert a");
 589     opal_argv_append(&i, &insert, "insert b");
 590     opal_argv_append(&i, &insert, "insert c");
 591     opal_argv_append(&o, &orig, "orig a");
 592     opal_argv_append(&o, &orig, "orig b");
 593     opal_argv_append(&o, &orig, "orig c");
 594     if (OPAL_SUCCESS != opal_argv_insert(&orig, 1, insert) ||
 595         6 != opal_argv_count(orig) ||
 596         0 != strcmp(orig[0], "orig a") ||
 597         0 != strcmp(orig[4], "orig b") ||
 598         0 != strcmp(orig[5], "orig c") ||
 599         0 != strcmp(orig[1], "insert a") ||
 600         0 != strcmp(orig[2], "insert b") ||
 601         0 != strcmp(orig[3], "insert c")) {
 602         return false;
 603     }
 604     opal_argv_free(orig);
 605     opal_argv_free(insert);
 606 
 607     /* insert in the middle */
 608 
 609     orig = NULL;
 610     o = 0;
 611     insert = NULL;
 612     i = 0;
 613     opal_argv_append(&i, &insert, "insert a");
 614     opal_argv_append(&i, &insert, "insert b");
 615     opal_argv_append(&i, &insert, "insert c");
 616     opal_argv_append(&o, &orig, "orig a");
 617     opal_argv_append(&o, &orig, "orig b");
 618     opal_argv_append(&o, &orig, "orig c");
 619     opal_argv_append(&o, &orig, "orig d");
 620     opal_argv_append(&o, &orig, "orig e");
 621     opal_argv_append(&o, &orig, "orig f");
 622     if (OPAL_SUCCESS != opal_argv_insert(&orig, 1, insert) ||
 623         9 != opal_argv_count(orig) ||
 624         0 != strcmp(orig[0], "orig a") ||
 625         0 != strcmp(orig[4], "orig b") ||
 626         0 != strcmp(orig[5], "orig c") ||
 627         0 != strcmp(orig[6], "orig d") ||
 628         0 != strcmp(orig[7], "orig e") ||
 629         0 != strcmp(orig[8], "orig f") ||
 630         0 != strcmp(orig[1], "insert a") ||
 631         0 != strcmp(orig[2], "insert b") ||
 632         0 != strcmp(orig[3], "insert c")) {
 633         return false;
 634     }
 635     opal_argv_free(orig);
 636     opal_argv_free(insert);
 637 
 638     /* All done */
 639 
 640     return true;
 641 }

/* [<][>][^][v][top][bottom][index][help] */