1
0

testargdate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <time.h>
  32. #include "CuTest.h"
  33. #include "argtable3.h"
  34. /*
  35. printf("tm_sec = %d\n", c->tmval->tm_sec);
  36. printf("tm_min = %d\n", c->tmval->tm_min);
  37. printf("tm_hour = %d\n", c->tmval->tm_hour);
  38. printf("tm_mday = %d\n", c->tmval->tm_mday);
  39. printf("tm_mon = %d\n", c->tmval->tm_mon);
  40. printf("tm_year = %d\n", c->tmval->tm_year);
  41. printf("tm_wday = %d\n", c->tmval->tm_wday);
  42. printf("tm_yday = %d\n", c->tmval->tm_yday);
  43. printf("tm_isdst = %d\n", c->tmval->tm_isdst);
  44. */
  45. #if defined(_MSC_VER)
  46. #pragma warning(push)
  47. #pragma warning(disable : 4204)
  48. #endif
  49. void test_argdate_basic_001(CuTest* tc) {
  50. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  51. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  52. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  53. struct arg_end* end = arg_end(20);
  54. void* argtable[] = {a, b, c, end};
  55. int nerrors;
  56. char* argv[] = {"program", "23:59", "--date", "12/31/04", NULL};
  57. int argc = sizeof(argv) / sizeof(char*) - 1;
  58. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  59. nerrors = arg_parse(argc, argv, argtable);
  60. CuAssertTrue(tc, nerrors == 0);
  61. CuAssertTrue(tc, a->count == 1);
  62. CuAssertIntEquals(tc, a->tmval->tm_sec, 0);
  63. CuAssertIntEquals(tc, a->tmval->tm_min, 59);
  64. CuAssertIntEquals(tc, a->tmval->tm_hour, 23);
  65. CuAssertIntEquals(tc, a->tmval->tm_mday, 0);
  66. CuAssertIntEquals(tc, a->tmval->tm_mon, 0);
  67. CuAssertIntEquals(tc, a->tmval->tm_year, 0);
  68. CuAssertIntEquals(tc, a->tmval->tm_wday, 0);
  69. CuAssertIntEquals(tc, a->tmval->tm_yday, 0);
  70. CuAssertIntEquals(tc, a->tmval->tm_isdst, 0);
  71. CuAssertTrue(tc, b->count == 0);
  72. CuAssertTrue(tc, c->count == 1);
  73. CuAssertIntEquals(tc, c->tmval->tm_sec, 0);
  74. CuAssertIntEquals(tc, c->tmval->tm_min, 0);
  75. CuAssertIntEquals(tc, c->tmval->tm_hour, 0);
  76. CuAssertIntEquals(tc, c->tmval->tm_mday, 31);
  77. CuAssertIntEquals(tc, c->tmval->tm_mon, 11);
  78. CuAssertIntEquals(tc, c->tmval->tm_year, 104);
  79. CuAssertIntEquals(tc, c->tmval->tm_wday, 0);
  80. CuAssertIntEquals(tc, c->tmval->tm_yday, 0);
  81. CuAssertIntEquals(tc, c->tmval->tm_isdst, 0);
  82. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  83. }
  84. void test_argdate_basic_002(CuTest* tc) {
  85. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  86. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  87. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  88. struct arg_end* end = arg_end(20);
  89. void* argtable[] = {a, b, c, end};
  90. int nerrors;
  91. char* argv[] = {"program", "--date", "12/31/04", "20:15", NULL};
  92. int argc = sizeof(argv) / sizeof(char*) - 1;
  93. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  94. nerrors = arg_parse(argc, argv, argtable);
  95. CuAssertTrue(tc, nerrors == 0);
  96. CuAssertTrue(tc, a->count == 1);
  97. CuAssertIntEquals(tc, a->tmval->tm_sec, 0);
  98. CuAssertIntEquals(tc, a->tmval->tm_min, 15);
  99. CuAssertIntEquals(tc, a->tmval->tm_hour, 20);
  100. CuAssertIntEquals(tc, a->tmval->tm_mday, 0);
  101. CuAssertIntEquals(tc, a->tmval->tm_mon, 0);
  102. CuAssertIntEquals(tc, a->tmval->tm_year, 0);
  103. CuAssertIntEquals(tc, a->tmval->tm_wday, 0);
  104. CuAssertIntEquals(tc, a->tmval->tm_yday, 0);
  105. CuAssertIntEquals(tc, a->tmval->tm_isdst, 0);
  106. CuAssertTrue(tc, b->count == 0);
  107. CuAssertTrue(tc, c->count == 1);
  108. CuAssertIntEquals(tc, c->tmval->tm_sec, 0);
  109. CuAssertIntEquals(tc, c->tmval->tm_min, 0);
  110. CuAssertIntEquals(tc, c->tmval->tm_hour, 0);
  111. CuAssertIntEquals(tc, c->tmval->tm_mday, 31);
  112. CuAssertIntEquals(tc, c->tmval->tm_mon, 11);
  113. CuAssertIntEquals(tc, c->tmval->tm_year, 104);
  114. CuAssertIntEquals(tc, c->tmval->tm_wday, 0);
  115. CuAssertIntEquals(tc, c->tmval->tm_yday, 0);
  116. CuAssertIntEquals(tc, c->tmval->tm_isdst, 0);
  117. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  118. }
  119. void test_argdate_basic_003(CuTest* tc) {
  120. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  121. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  122. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  123. struct arg_end* end = arg_end(20);
  124. void* argtable[] = {a, b, c, end};
  125. int nerrors;
  126. char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "06/07/84", NULL};
  127. int argc = sizeof(argv) / sizeof(char*) - 1;
  128. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  129. nerrors = arg_parse(argc, argv, argtable);
  130. CuAssertTrue(tc, nerrors == 0);
  131. CuAssertTrue(tc, a->count == 1);
  132. CuAssertIntEquals(tc, a->tmval->tm_sec, 0);
  133. CuAssertIntEquals(tc, a->tmval->tm_min, 15);
  134. CuAssertIntEquals(tc, a->tmval->tm_hour, 20);
  135. CuAssertIntEquals(tc, a->tmval->tm_mday, 0);
  136. CuAssertIntEquals(tc, a->tmval->tm_mon, 0);
  137. CuAssertIntEquals(tc, a->tmval->tm_year, 0);
  138. CuAssertIntEquals(tc, a->tmval->tm_wday, 0);
  139. CuAssertIntEquals(tc, a->tmval->tm_yday, 0);
  140. CuAssertIntEquals(tc, a->tmval->tm_isdst, 0);
  141. CuAssertTrue(tc, b->count == 0);
  142. CuAssertTrue(tc, c->count == 2);
  143. CuAssertIntEquals(tc, c->tmval->tm_sec, 0);
  144. CuAssertIntEquals(tc, c->tmval->tm_min, 0);
  145. CuAssertIntEquals(tc, c->tmval->tm_hour, 0);
  146. CuAssertIntEquals(tc, c->tmval->tm_mday, 31);
  147. CuAssertIntEquals(tc, c->tmval->tm_mon, 11);
  148. CuAssertIntEquals(tc, c->tmval->tm_year, 104);
  149. CuAssertIntEquals(tc, c->tmval->tm_wday, 0);
  150. CuAssertIntEquals(tc, c->tmval->tm_yday, 0);
  151. CuAssertIntEquals(tc, c->tmval->tm_isdst, 0);
  152. CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0);
  153. CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0);
  154. CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0);
  155. CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7);
  156. CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5);
  157. CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84);
  158. CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0);
  159. CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0);
  160. CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0);
  161. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  162. }
  163. void test_argdate_basic_004(CuTest* tc) {
  164. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  165. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  166. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  167. struct arg_end* end = arg_end(20);
  168. void* argtable[] = {a, b, c, end};
  169. int nerrors;
  170. char* argv[] = {"program", "--date", "12/31/04", "20:15", "-b", "1982-11-28", "--date", "06/07/84", NULL};
  171. int argc = sizeof(argv) / sizeof(char*) - 1;
  172. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  173. nerrors = arg_parse(argc, argv, argtable);
  174. CuAssertTrue(tc, nerrors == 0);
  175. CuAssertTrue(tc, a->count == 1);
  176. CuAssertIntEquals(tc, a->tmval->tm_sec, 0);
  177. CuAssertIntEquals(tc, a->tmval->tm_min, 15);
  178. CuAssertIntEquals(tc, a->tmval->tm_hour, 20);
  179. CuAssertIntEquals(tc, a->tmval->tm_mday, 0);
  180. CuAssertIntEquals(tc, a->tmval->tm_mon, 0);
  181. CuAssertIntEquals(tc, a->tmval->tm_year, 0);
  182. CuAssertIntEquals(tc, a->tmval->tm_wday, 0);
  183. CuAssertIntEquals(tc, a->tmval->tm_yday, 0);
  184. CuAssertIntEquals(tc, a->tmval->tm_isdst, 0);
  185. CuAssertTrue(tc, b->count == 1);
  186. CuAssertIntEquals(tc, b->tmval->tm_sec, 0);
  187. CuAssertIntEquals(tc, b->tmval->tm_min, 0);
  188. CuAssertIntEquals(tc, b->tmval->tm_hour, 0);
  189. CuAssertIntEquals(tc, b->tmval->tm_mday, 28);
  190. CuAssertIntEquals(tc, b->tmval->tm_mon, 10);
  191. CuAssertIntEquals(tc, b->tmval->tm_year, 82);
  192. CuAssertIntEquals(tc, b->tmval->tm_wday, 0);
  193. CuAssertIntEquals(tc, b->tmval->tm_yday, 0);
  194. CuAssertIntEquals(tc, b->tmval->tm_isdst, 0);
  195. CuAssertTrue(tc, c->count == 2);
  196. CuAssertIntEquals(tc, c->tmval->tm_sec, 0);
  197. CuAssertIntEquals(tc, c->tmval->tm_min, 0);
  198. CuAssertIntEquals(tc, c->tmval->tm_hour, 0);
  199. CuAssertIntEquals(tc, c->tmval->tm_mday, 31);
  200. CuAssertIntEquals(tc, c->tmval->tm_mon, 11);
  201. CuAssertIntEquals(tc, c->tmval->tm_year, 104);
  202. CuAssertIntEquals(tc, c->tmval->tm_wday, 0);
  203. CuAssertIntEquals(tc, c->tmval->tm_yday, 0);
  204. CuAssertIntEquals(tc, c->tmval->tm_isdst, 0);
  205. CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0);
  206. CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0);
  207. CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0);
  208. CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7);
  209. CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5);
  210. CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84);
  211. CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0);
  212. CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0);
  213. CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0);
  214. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  215. }
  216. void test_argdate_basic_005(CuTest* tc) {
  217. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  218. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  219. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  220. struct arg_end* end = arg_end(20);
  221. void* argtable[] = {a, b, c, end};
  222. int nerrors;
  223. char* argv[] = {"program", NULL};
  224. int argc = sizeof(argv) / sizeof(char*) - 1;
  225. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  226. nerrors = arg_parse(argc, argv, argtable);
  227. CuAssertTrue(tc, nerrors == 2);
  228. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  229. }
  230. void test_argdate_basic_006(CuTest* tc) {
  231. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  232. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  233. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  234. struct arg_end* end = arg_end(20);
  235. void* argtable[] = {a, b, c, end};
  236. int nerrors;
  237. char* argv[] = {"program", "25:59", "--date", "12/31/04", NULL};
  238. int argc = sizeof(argv) / sizeof(char*) - 1;
  239. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  240. nerrors = arg_parse(argc, argv, argtable);
  241. CuAssertTrue(tc, nerrors == 1);
  242. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  243. }
  244. void test_argdate_basic_007(CuTest* tc) {
  245. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  246. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  247. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  248. struct arg_end* end = arg_end(20);
  249. void* argtable[] = {a, b, c, end};
  250. int nerrors;
  251. char* argv[] = {"program", "23:59", "--date", "12/32/04", NULL};
  252. int argc = sizeof(argv) / sizeof(char*) - 1;
  253. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  254. nerrors = arg_parse(argc, argv, argtable);
  255. CuAssertTrue(tc, nerrors == 1);
  256. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  257. }
  258. void test_argdate_basic_008(CuTest* tc) {
  259. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  260. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  261. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  262. struct arg_end* end = arg_end(20);
  263. void* argtable[] = {a, b, c, end};
  264. int nerrors;
  265. char* argv[] = {"program", "23:59", "--date", "12/31/04", "22:58", NULL};
  266. int argc = sizeof(argv) / sizeof(char*) - 1;
  267. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  268. nerrors = arg_parse(argc, argv, argtable);
  269. CuAssertTrue(tc, nerrors == 1);
  270. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  271. }
  272. void test_argdate_basic_009(CuTest* tc) {
  273. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  274. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  275. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  276. struct arg_end* end = arg_end(20);
  277. void* argtable[] = {a, b, c, end};
  278. int nerrors;
  279. char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "26/07/84", NULL};
  280. int argc = sizeof(argv) / sizeof(char*) - 1;
  281. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  282. nerrors = arg_parse(argc, argv, argtable);
  283. CuAssertTrue(tc, nerrors == 1);
  284. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  285. }
  286. void test_argdate_basic_010(CuTest* tc) {
  287. struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59");
  288. struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD");
  289. struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY");
  290. struct arg_end* end = arg_end(20);
  291. void* argtable[] = {a, b, c, end};
  292. int nerrors;
  293. char* argv[] = {"program", "-b", "1982-11-28", "-b", "1976-11-11", "--date", "12/07/84", NULL};
  294. int argc = sizeof(argv) / sizeof(char*) - 1;
  295. CuAssertTrue(tc, arg_nullcheck(argtable) == 0);
  296. nerrors = arg_parse(argc, argv, argtable);
  297. CuAssertTrue(tc, nerrors == 1);
  298. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  299. }
  300. CuSuite* get_argdate_testsuite() {
  301. CuSuite* suite = CuSuiteNew();
  302. SUITE_ADD_TEST(suite, test_argdate_basic_001);
  303. SUITE_ADD_TEST(suite, test_argdate_basic_002);
  304. SUITE_ADD_TEST(suite, test_argdate_basic_003);
  305. SUITE_ADD_TEST(suite, test_argdate_basic_004);
  306. SUITE_ADD_TEST(suite, test_argdate_basic_005);
  307. SUITE_ADD_TEST(suite, test_argdate_basic_006);
  308. SUITE_ADD_TEST(suite, test_argdate_basic_007);
  309. SUITE_ADD_TEST(suite, test_argdate_basic_008);
  310. SUITE_ADD_TEST(suite, test_argdate_basic_009);
  311. SUITE_ADD_TEST(suite, test_argdate_basic_010);
  312. return suite;
  313. }
  314. #if defined(_MSC_VER)
  315. #pragma warning(pop)
  316. #endif