1
0

testargstr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 2013-2019 Tom G. Huang
  5. * <tomghuang@gmail.com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #include <string.h>
  31. #include "CuTest.h"
  32. #include "argtable3.h"
  33. #if defined(_MSC_VER)
  34. #pragma warning(push)
  35. #pragma warning(disable : 4204)
  36. #endif
  37. void test_argstr_basic_001(CuTest* tc) {
  38. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  39. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  40. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  41. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  42. struct arg_end* end = arg_end(20);
  43. void* argtable[] = {a, b, c, d, end};
  44. int nerrors;
  45. char* argv[] = {"program", "--hello=string1", NULL};
  46. int argc = sizeof(argv) / sizeof(char*) - 1;
  47. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  48. nerrors = arg_parse(argc, argv, argtable);
  49. CuAssertTrue(tc, nerrors == 2);
  50. CuAssertTrue(tc, a->count == 1);
  51. CuAssertStrEquals(tc, a->sval[0], "string1");
  52. CuAssertTrue(tc, b->count == 0);
  53. CuAssertTrue(tc, c->count == 0);
  54. CuAssertTrue(tc, d->count == 0);
  55. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  56. }
  57. void test_argstr_basic_002(CuTest* tc) {
  58. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  59. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  60. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  61. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  62. struct arg_end* end = arg_end(20);
  63. void* argtable[] = {a, b, c, d, end};
  64. int nerrors;
  65. char* argv[] = {"program", "-cstring1", "-Dstring2", "-dstring3", NULL};
  66. int argc = sizeof(argv) / sizeof(char*) - 1;
  67. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  68. nerrors = arg_parse(argc, argv, argtable);
  69. if (nerrors > 0)
  70. arg_print_errors(stdout, end, argv[0]);
  71. CuAssertTrue(tc, nerrors == 0);
  72. CuAssertTrue(tc, a->count == 0);
  73. CuAssertTrue(tc, b->count == 0);
  74. CuAssertTrue(tc, c->count == 1);
  75. CuAssertStrEquals(tc, c->sval[0], "string1");
  76. CuAssertTrue(tc, d->count == 2);
  77. CuAssertStrEquals(tc, d->sval[0], "string2");
  78. CuAssertStrEquals(tc, d->sval[1], "string3");
  79. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  80. }
  81. void test_argstr_basic_003(CuTest* tc) {
  82. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  83. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  84. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  85. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  86. struct arg_end* end = arg_end(20);
  87. void* argtable[] = {a, b, c, d, end};
  88. int nerrors;
  89. char* argv[] = {"program", "-Cstring1", "--delta=string2", "--delta=string3", NULL};
  90. int argc = sizeof(argv) / sizeof(char*) - 1;
  91. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  92. nerrors = arg_parse(argc, argv, argtable);
  93. if (nerrors > 0)
  94. arg_print_errors(stdout, end, argv[0]);
  95. CuAssertTrue(tc, nerrors == 0);
  96. CuAssertTrue(tc, a->count == 0);
  97. CuAssertTrue(tc, b->count == 0);
  98. CuAssertTrue(tc, c->count == 1);
  99. CuAssertStrEquals(tc, c->sval[0], "string1");
  100. CuAssertTrue(tc, d->count == 2);
  101. CuAssertStrEquals(tc, d->sval[0], "string2");
  102. CuAssertStrEquals(tc, d->sval[1], "string3");
  103. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  104. }
  105. void test_argstr_basic_004(CuTest* tc) {
  106. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  107. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  108. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  109. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  110. struct arg_end* end = arg_end(20);
  111. void* argtable[] = {a, b, c, d, end};
  112. int nerrors;
  113. char* argv[] = {"program", "--delta=string1", "-cstring2", "-Dstring3", "-bstring4", NULL};
  114. int argc = sizeof(argv) / sizeof(char*) - 1;
  115. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  116. nerrors = arg_parse(argc, argv, argtable);
  117. if (nerrors > 0)
  118. arg_print_errors(stdout, end, argv[0]);
  119. CuAssertTrue(tc, nerrors == 0);
  120. CuAssertTrue(tc, a->count == 0);
  121. CuAssertTrue(tc, b->count == 1);
  122. CuAssertStrEquals(tc, b->sval[0], "string4");
  123. CuAssertTrue(tc, c->count == 1);
  124. CuAssertStrEquals(tc, c->sval[0], "string2");
  125. CuAssertTrue(tc, d->count == 2);
  126. CuAssertStrEquals(tc, d->sval[0], "string1");
  127. CuAssertStrEquals(tc, d->sval[1], "string3");
  128. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  129. }
  130. void test_argstr_basic_005(CuTest* tc) {
  131. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  132. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  133. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  134. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  135. struct arg_end* end = arg_end(20);
  136. void* argtable[] = {a, b, c, d, end};
  137. int nerrors;
  138. char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", NULL};
  139. int argc = sizeof(argv) / sizeof(char*) - 1;
  140. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  141. nerrors = arg_parse(argc, argv, argtable);
  142. if (nerrors > 0)
  143. arg_print_errors(stdout, end, argv[0]);
  144. CuAssertTrue(tc, nerrors == 0);
  145. CuAssertTrue(tc, a->count == 0);
  146. CuAssertTrue(tc, b->count == 1);
  147. CuAssertStrEquals(tc, b->sval[0], "string2");
  148. CuAssertTrue(tc, c->count == 1);
  149. CuAssertStrEquals(tc, c->sval[0], "string4");
  150. CuAssertTrue(tc, d->count == 2);
  151. CuAssertStrEquals(tc, d->sval[0], "string1");
  152. CuAssertStrEquals(tc, d->sval[1], "string3");
  153. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  154. }
  155. void test_argstr_basic_006(CuTest* tc) {
  156. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  157. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  158. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  159. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  160. struct arg_end* end = arg_end(20);
  161. void* argtable[] = {a, b, c, d, end};
  162. int nerrors;
  163. char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", "--hello=string5", NULL};
  164. int argc = sizeof(argv) / sizeof(char*) - 1;
  165. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  166. nerrors = arg_parse(argc, argv, argtable);
  167. if (nerrors > 0)
  168. arg_print_errors(stdout, end, argv[0]);
  169. CuAssertTrue(tc, nerrors == 0);
  170. CuAssertTrue(tc, a->count == 1);
  171. CuAssertStrEquals(tc, a->sval[0], "string5");
  172. CuAssertTrue(tc, b->count == 1);
  173. CuAssertStrEquals(tc, b->sval[0], "string2");
  174. CuAssertTrue(tc, c->count == 1);
  175. CuAssertStrEquals(tc, c->sval[0], "string4");
  176. CuAssertTrue(tc, d->count == 2);
  177. CuAssertStrEquals(tc, d->sval[0], "string1");
  178. CuAssertStrEquals(tc, d->sval[1], "string3");
  179. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  180. }
  181. void test_argstr_basic_007(CuTest* tc) {
  182. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  183. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  184. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  185. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  186. struct arg_end* end = arg_end(20);
  187. void* argtable[] = {a, b, c, d, end};
  188. int nerrors;
  189. char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", "--world=string5", NULL};
  190. int argc = sizeof(argv) / sizeof(char*) - 1;
  191. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  192. nerrors = arg_parse(argc, argv, argtable);
  193. if (nerrors > 0)
  194. arg_print_errors(stdout, end, argv[0]);
  195. CuAssertTrue(tc, nerrors == 0);
  196. CuAssertTrue(tc, a->count == 1);
  197. CuAssertStrEquals(tc, a->sval[0], "string5");
  198. CuAssertTrue(tc, b->count == 1);
  199. CuAssertStrEquals(tc, b->sval[0], "string2");
  200. CuAssertTrue(tc, c->count == 1);
  201. CuAssertStrEquals(tc, c->sval[0], "string4");
  202. CuAssertTrue(tc, d->count == 2);
  203. CuAssertStrEquals(tc, d->sval[0], "string1");
  204. CuAssertStrEquals(tc, d->sval[1], "string3");
  205. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  206. }
  207. void test_argstr_basic_008(CuTest* tc) {
  208. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  209. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  210. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  211. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  212. struct arg_end* end = arg_end(20);
  213. void* argtable[] = {a, b, c, d, end};
  214. int nerrors;
  215. char* argv[] = {"program", "-cstring1", NULL};
  216. int argc = sizeof(argv) / sizeof(char*) - 1;
  217. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  218. nerrors = arg_parse(argc, argv, argtable);
  219. CuAssertTrue(tc, nerrors == 1);
  220. CuAssertTrue(tc, a->count == 0);
  221. CuAssertTrue(tc, b->count == 0);
  222. CuAssertTrue(tc, c->count == 1);
  223. CuAssertStrEquals(tc, c->sval[0], "string1");
  224. CuAssertTrue(tc, d->count == 0);
  225. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  226. }
  227. void test_argstr_basic_009(CuTest* tc) {
  228. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  229. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  230. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  231. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  232. struct arg_end* end = arg_end(20);
  233. void* argtable[] = {a, b, c, d, end};
  234. int nerrors;
  235. char* argv[] = {"program", "-Dstring1", NULL};
  236. int argc = sizeof(argv) / sizeof(char*) - 1;
  237. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  238. nerrors = arg_parse(argc, argv, argtable);
  239. CuAssertTrue(tc, nerrors == 2);
  240. CuAssertTrue(tc, a->count == 0);
  241. CuAssertTrue(tc, b->count == 0);
  242. CuAssertTrue(tc, c->count == 0);
  243. CuAssertTrue(tc, d->count == 1);
  244. CuAssertStrEquals(tc, d->sval[0], "string1");
  245. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  246. }
  247. void test_argstr_basic_010(CuTest* tc) {
  248. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  249. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  250. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  251. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  252. struct arg_end* end = arg_end(20);
  253. void* argtable[] = {a, b, c, d, end};
  254. int nerrors;
  255. char* argv[] = {"program", "-Cstring1", "-Dstring2", NULL};
  256. int argc = sizeof(argv) / sizeof(char*) - 1;
  257. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  258. nerrors = arg_parse(argc, argv, argtable);
  259. CuAssertTrue(tc, nerrors == 1);
  260. CuAssertTrue(tc, a->count == 0);
  261. CuAssertTrue(tc, b->count == 0);
  262. CuAssertTrue(tc, c->count == 1);
  263. CuAssertStrEquals(tc, c->sval[0], "string1");
  264. CuAssertTrue(tc, d->count == 1);
  265. CuAssertStrEquals(tc, d->sval[0], "string2");
  266. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  267. }
  268. void test_argstr_basic_011(CuTest* tc) {
  269. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  270. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  271. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  272. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  273. struct arg_end* end = arg_end(20);
  274. void* argtable[] = {a, b, c, d, end};
  275. int nerrors;
  276. char* argv[] = {"program", "-Dstring1", "-dstring2", NULL};
  277. int argc = sizeof(argv) / sizeof(char*) - 1;
  278. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  279. nerrors = arg_parse(argc, argv, argtable);
  280. CuAssertTrue(tc, nerrors == 1);
  281. CuAssertTrue(tc, a->count == 0);
  282. CuAssertTrue(tc, b->count == 0);
  283. CuAssertTrue(tc, c->count == 0);
  284. CuAssertTrue(tc, d->count == 2);
  285. CuAssertStrEquals(tc, d->sval[0], "string1");
  286. CuAssertStrEquals(tc, d->sval[1], "string2");
  287. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  288. }
  289. void test_argstr_basic_012(CuTest* tc) {
  290. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  291. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  292. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  293. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  294. struct arg_end* end = arg_end(20);
  295. void* argtable[] = {a, b, c, d, end};
  296. int nerrors;
  297. char* argv[] = {"program", "-cs1", "-ds2", "-ds3", "-ds4", "-ds5", "-ds6", NULL};
  298. int argc = sizeof(argv) / sizeof(char*) - 1;
  299. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  300. nerrors = arg_parse(argc, argv, argtable);
  301. CuAssertTrue(tc, nerrors == 1);
  302. CuAssertTrue(tc, a->count == 0);
  303. CuAssertTrue(tc, b->count == 0);
  304. CuAssertTrue(tc, c->count == 1);
  305. CuAssertStrEquals(tc, c->sval[0], "s1");
  306. CuAssertTrue(tc, d->count == 4);
  307. CuAssertStrEquals(tc, d->sval[0], "s2");
  308. CuAssertStrEquals(tc, d->sval[1], "s3");
  309. CuAssertStrEquals(tc, d->sval[2], "s4");
  310. CuAssertStrEquals(tc, d->sval[3], "s5");
  311. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  312. }
  313. void test_argstr_basic_013(CuTest* tc) {
  314. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  315. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  316. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  317. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  318. struct arg_end* end = arg_end(20);
  319. void* argtable[] = {a, b, c, d, end};
  320. int nerrors;
  321. char* argv[] = {"program", "-cs1", "-cs2", "-ds3", "-ds4", "-ds5", NULL};
  322. int argc = sizeof(argv) / sizeof(char*) - 1;
  323. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  324. nerrors = arg_parse(argc, argv, argtable);
  325. CuAssertTrue(tc, nerrors == 1);
  326. CuAssertTrue(tc, a->count == 0);
  327. CuAssertTrue(tc, b->count == 0);
  328. CuAssertTrue(tc, c->count == 1);
  329. CuAssertStrEquals(tc, c->sval[0], "s1");
  330. CuAssertTrue(tc, d->count == 3);
  331. CuAssertStrEquals(tc, d->sval[0], "s3");
  332. CuAssertStrEquals(tc, d->sval[1], "s4");
  333. CuAssertStrEquals(tc, d->sval[2], "s5");
  334. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  335. }
  336. void test_argstr_basic_014(CuTest* tc) {
  337. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  338. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  339. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  340. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  341. struct arg_end* end = arg_end(20);
  342. void* argtable[] = {a, b, c, d, end};
  343. int nerrors;
  344. char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "-bs5", "-Bs6", NULL};
  345. int argc = sizeof(argv) / sizeof(char*) - 1;
  346. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  347. nerrors = arg_parse(argc, argv, argtable);
  348. CuAssertTrue(tc, nerrors == 1);
  349. CuAssertTrue(tc, a->count == 0);
  350. CuAssertTrue(tc, b->count == 1);
  351. CuAssertStrEquals(tc, b->sval[0], "s5");
  352. CuAssertTrue(tc, c->count == 1);
  353. CuAssertStrEquals(tc, c->sval[0], "s1");
  354. CuAssertTrue(tc, d->count == 3);
  355. CuAssertStrEquals(tc, d->sval[0], "s2");
  356. CuAssertStrEquals(tc, d->sval[1], "s3");
  357. CuAssertStrEquals(tc, d->sval[2], "s4");
  358. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  359. }
  360. void test_argstr_basic_015(CuTest* tc) {
  361. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  362. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  363. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  364. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  365. struct arg_end* end = arg_end(20);
  366. void* argtable[] = {a, b, c, d, end};
  367. int nerrors;
  368. char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "--hello=s5", "--world=s6", NULL};
  369. int argc = sizeof(argv) / sizeof(char*) - 1;
  370. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  371. nerrors = arg_parse(argc, argv, argtable);
  372. CuAssertTrue(tc, nerrors == 1);
  373. CuAssertTrue(tc, a->count == 1);
  374. CuAssertStrEquals(tc, a->sval[0], "s5");
  375. CuAssertTrue(tc, b->count == 0);
  376. CuAssertTrue(tc, c->count == 1);
  377. CuAssertStrEquals(tc, c->sval[0], "s1");
  378. CuAssertTrue(tc, d->count == 3);
  379. CuAssertStrEquals(tc, d->sval[0], "s2");
  380. CuAssertStrEquals(tc, d->sval[1], "s3");
  381. CuAssertStrEquals(tc, d->sval[2], "s4");
  382. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  383. }
  384. void test_argstr_basic_016(CuTest* tc) {
  385. struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none");
  386. struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none");
  387. struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C");
  388. struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences");
  389. struct arg_end* end = arg_end(20);
  390. void* argtable[] = {a, b, c, d, end};
  391. int nerrors;
  392. char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "--hello=s5", "X", NULL};
  393. int argc = sizeof(argv) / sizeof(char*) - 1;
  394. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  395. nerrors = arg_parse(argc, argv, argtable);
  396. CuAssertTrue(tc, nerrors == 1);
  397. CuAssertTrue(tc, a->count == 1);
  398. CuAssertStrEquals(tc, a->sval[0], "s5");
  399. CuAssertTrue(tc, b->count == 0);
  400. CuAssertTrue(tc, c->count == 1);
  401. CuAssertStrEquals(tc, c->sval[0], "s1");
  402. CuAssertTrue(tc, d->count == 3);
  403. CuAssertStrEquals(tc, d->sval[0], "s2");
  404. CuAssertStrEquals(tc, d->sval[1], "s3");
  405. CuAssertStrEquals(tc, d->sval[2], "s4");
  406. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  407. }
  408. CuSuite* get_argstr_testsuite() {
  409. CuSuite* suite = CuSuiteNew();
  410. SUITE_ADD_TEST(suite, test_argstr_basic_001);
  411. SUITE_ADD_TEST(suite, test_argstr_basic_002);
  412. SUITE_ADD_TEST(suite, test_argstr_basic_003);
  413. SUITE_ADD_TEST(suite, test_argstr_basic_004);
  414. SUITE_ADD_TEST(suite, test_argstr_basic_005);
  415. SUITE_ADD_TEST(suite, test_argstr_basic_006);
  416. SUITE_ADD_TEST(suite, test_argstr_basic_007);
  417. SUITE_ADD_TEST(suite, test_argstr_basic_008);
  418. SUITE_ADD_TEST(suite, test_argstr_basic_009);
  419. SUITE_ADD_TEST(suite, test_argstr_basic_010);
  420. SUITE_ADD_TEST(suite, test_argstr_basic_011);
  421. SUITE_ADD_TEST(suite, test_argstr_basic_012);
  422. SUITE_ADD_TEST(suite, test_argstr_basic_013);
  423. SUITE_ADD_TEST(suite, test_argstr_basic_014);
  424. SUITE_ADD_TEST(suite, test_argstr_basic_015);
  425. SUITE_ADD_TEST(suite, test_argstr_basic_016);
  426. return suite;
  427. }
  428. #if defined(_MSC_VER)
  429. #pragma warning(pop)
  430. #endif