run-iter.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2011 Rusty Russell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License as published by the Free Software
  6. * Foundation; either version 2 of the License, or (at your option) any later
  7. * version. See LICENSE for more details.
  8. */
  9. #include <ccan/tap/tap.h>
  10. #include <stdarg.h>
  11. #include <setjmp.h>
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include "utils.h"
  15. #include <ccan/opt/opt.c>
  16. #include <ccan/opt/usage.c>
  17. #include <ccan/opt/helpers.c>
  18. #include <ccan/opt/parse.c>
  19. static void reset_options(void)
  20. {
  21. free(opt_table);
  22. opt_table = NULL;
  23. opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
  24. }
  25. /* Test iterators. */
  26. int main(int argc, char *argv[])
  27. {
  28. unsigned j, i, len = 0;
  29. const char *p;
  30. plan_tests(37 * 2);
  31. for (j = 0; j < 2; j ++) {
  32. reset_options();
  33. /* Giving subtable a title makes an extra entry! */
  34. opt_register_table(subtables, j == 0 ? NULL : "subtable");
  35. p = first_lopt(&i, &len);
  36. ok1(i == j + 0);
  37. ok1(len == 3);
  38. ok1(strncmp(p, "jjj", len) == 0);
  39. p = next_lopt(p, &i, &len);
  40. ok1(i == j + 0);
  41. ok1(len == 3);
  42. ok1(strncmp(p, "lll", len) == 0);
  43. p = next_lopt(p, &i, &len);
  44. ok1(i == j + 1);
  45. ok1(len == 3);
  46. ok1(strncmp(p, "mmm", len) == 0);
  47. p = next_lopt(p, &i, &len);
  48. ok1(i == j + 5);
  49. ok1(len == 3);
  50. ok1(strncmp(p, "ddd", len) == 0);
  51. p = next_lopt(p, &i, &len);
  52. ok1(i == j + 6);
  53. ok1(len == 3);
  54. ok1(strncmp(p, "eee", len) == 0);
  55. p = next_lopt(p, &i, &len);
  56. ok1(i == j + 7);
  57. ok1(len == 3);
  58. ok1(strncmp(p, "ggg", len) == 0);
  59. p = next_lopt(p, &i, &len);
  60. ok1(i == j + 8);
  61. ok1(len == 3);
  62. ok1(strncmp(p, "hhh", len) == 0);
  63. p = next_lopt(p, &i, &len);
  64. ok1(!p);
  65. p = first_sopt(&i);
  66. ok1(i == j + 0);
  67. ok1(*p == 'j');
  68. p = next_sopt(p, &i);
  69. ok1(i == j + 0);
  70. ok1(*p == 'l');
  71. p = next_sopt(p, &i);
  72. ok1(i == j + 1);
  73. ok1(*p == 'm');
  74. p = next_sopt(p, &i);
  75. ok1(i == j + 2);
  76. ok1(*p == 'a');
  77. p = next_sopt(p, &i);
  78. ok1(i == j + 3);
  79. ok1(*p == 'b');
  80. p = next_sopt(p, &i);
  81. ok1(i == j + 7);
  82. ok1(*p == 'g');
  83. p = next_sopt(p, &i);
  84. ok1(i == j + 8);
  85. ok1(*p == 'h');
  86. p = next_sopt(p, &i);
  87. ok1(!p);
  88. }
  89. return exit_status();
  90. }