- commit
- ecb0d75a6e9a390da159ddb37edbcb1ea9762970
- parent
- 5d9f3c1f6ac55eda0c9e02c7fe49a7f9e5250ed1
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2018-11-19 22:27
c: add tests
Diffstat
| M | calendar.c | 2 | ++ |
| A | test.c | 402 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 404 insertions, 0 deletions
diff --git a/calendar.c b/calendar.c
@@ -379,6 +379,7 @@ void help() {
379 379 );
380 380 }
381 381
-1 382 #ifndef test
382 383 int main(int argc, char *argv[]) {
383 384 int days = 3;
384 385
@@ -408,3 +409,4 @@ int main(int argc, char *argv[]) {
408 409
409 410 free_lines(first);
410 411 }
-1 412 #endif
diff --git a/test.c b/test.c
@@ -0,0 +1,402 @@
-1 1 #define test
-1 2 #include "calendar.c"
-1 3
-1 4 #define run_test(test) do { char *message = test(); tests_run++; if (message) { tests_failed++; printf("Failed: %s\n", message); } } while (0)
-1 5
-1 6 int tests_run = 0;
-1 7 int tests_failed = 0;
-1 8
-1 9 static char *test_easter() {
-1 10 if (!date_comp(easter(2000, false), mkdate(2000, 4, 23))) return "easter 2000";
-1 11 if (!date_comp(easter(2001, false), mkdate(2001, 4, 15))) return "easter 2001";
-1 12 if (!date_comp(easter(2002, false), mkdate(2002, 3, 31))) return "easter 2002";
-1 13 if (!date_comp(easter(2003, false), mkdate(2003, 4, 20))) return "easter 2003";
-1 14 if (!date_comp(easter(2004, false), mkdate(2004, 4, 11))) return "easter 2004";
-1 15 if (!date_comp(easter(2005, false), mkdate(2005, 3, 27))) return "easter 2005";
-1 16 if (!date_comp(easter(2006, false), mkdate(2006, 4, 16))) return "easter 2006";
-1 17 if (!date_comp(easter(2007, false), mkdate(2007, 4, 8))) return "easter 2007";
-1 18 if (!date_comp(easter(2008, false), mkdate(2008, 3, 23))) return "easter 2008";
-1 19 if (!date_comp(easter(2009, false), mkdate(2009, 4, 12))) return "easter 2009";
-1 20 if (!date_comp(easter(2010, false), mkdate(2010, 4, 4))) return "easter 2010";
-1 21 if (!date_comp(easter(2011, false), mkdate(2011, 4, 24))) return "easter 2011";
-1 22 if (!date_comp(easter(2012, false), mkdate(2012, 4, 8))) return "easter 2012";
-1 23 if (!date_comp(easter(2013, false), mkdate(2013, 3, 31))) return "easter 2013";
-1 24 if (!date_comp(easter(2014, false), mkdate(2014, 4, 20))) return "easter 2014";
-1 25 if (!date_comp(easter(2015, false), mkdate(2015, 4, 5))) return "easter 2015";
-1 26 if (!date_comp(easter(2016, false), mkdate(2016, 3, 27))) return "easter 2016";
-1 27 if (!date_comp(easter(2017, false), mkdate(2017, 4, 16))) return "easter 2017";
-1 28 if (!date_comp(easter(2018, false), mkdate(2018, 4, 1))) return "easter 2018";
-1 29 if (!date_comp(easter(2019, false), mkdate(2019, 4, 21))) return "easter 2019";
-1 30 if (!date_comp(easter(2020, false), mkdate(2020, 4, 12))) return "easter 2020";
-1 31 if (!date_comp(easter(2021, false), mkdate(2021, 4, 4))) return "easter 2021";
-1 32 if (!date_comp(easter(2022, false), mkdate(2022, 4, 17))) return "easter 2022";
-1 33 if (!date_comp(easter(2023, false), mkdate(2023, 4, 9))) return "easter 2023";
-1 34 if (!date_comp(easter(2024, false), mkdate(2024, 3, 31))) return "easter 2024";
-1 35 if (!date_comp(easter(2025, false), mkdate(2025, 4, 20))) return "easter 2025";
-1 36 if (!date_comp(easter(2026, false), mkdate(2026, 4, 5))) return "easter 2026";
-1 37 if (!date_comp(easter(2027, false), mkdate(2027, 3, 28))) return "easter 2027";
-1 38 if (!date_comp(easter(2028, false), mkdate(2028, 4, 16))) return "easter 2028";
-1 39 if (!date_comp(easter(2029, false), mkdate(2029, 4, 1))) return "easter 2029";
-1 40 if (!date_comp(easter(2030, false), mkdate(2030, 4, 21))) return "easter 2030";
-1 41 return 0;
-1 42 }
-1 43
-1 44 static char *test_paskha() {
-1 45 if (!date_comp(easter(2000, true), mkdate(2000, 4, 30))) return "paskha 2000";
-1 46 if (!date_comp(easter(2001, true), mkdate(2001, 4, 15))) return "paskha 2001";
-1 47 if (!date_comp(easter(2002, true), mkdate(2002, 5, 5))) return "paskha 2002";
-1 48 if (!date_comp(easter(2003, true), mkdate(2003, 4, 27))) return "paskha 2003";
-1 49 if (!date_comp(easter(2004, true), mkdate(2004, 4, 11))) return "paskha 2004";
-1 50 if (!date_comp(easter(2005, true), mkdate(2005, 5, 1))) return "paskha 2005";
-1 51 if (!date_comp(easter(2006, true), mkdate(2006, 4, 23))) return "paskha 2006";
-1 52 if (!date_comp(easter(2007, true), mkdate(2007, 4, 8))) return "paskha 2007";
-1 53 if (!date_comp(easter(2008, true), mkdate(2008, 4, 27))) return "paskha 2008";
-1 54 if (!date_comp(easter(2009, true), mkdate(2009, 4, 19))) return "paskha 2009";
-1 55 if (!date_comp(easter(2010, true), mkdate(2010, 4, 4))) return "paskha 2010";
-1 56 if (!date_comp(easter(2011, true), mkdate(2011, 4, 24))) return "paskha 2011";
-1 57 if (!date_comp(easter(2012, true), mkdate(2012, 4, 15))) return "paskha 2012";
-1 58 if (!date_comp(easter(2013, true), mkdate(2013, 5, 5))) return "paskha 2013";
-1 59 if (!date_comp(easter(2014, true), mkdate(2014, 4, 20))) return "paskha 2014";
-1 60 if (!date_comp(easter(2015, true), mkdate(2015, 4, 12))) return "paskha 2015";
-1 61 if (!date_comp(easter(2016, true), mkdate(2016, 5, 1))) return "paskha 2016";
-1 62 if (!date_comp(easter(2017, true), mkdate(2017, 4, 16))) return "paskha 2017";
-1 63 if (!date_comp(easter(2018, true), mkdate(2018, 4, 8))) return "paskha 2018";
-1 64 if (!date_comp(easter(2019, true), mkdate(2019, 4, 28))) return "paskha 2019";
-1 65 if (!date_comp(easter(2020, true), mkdate(2020, 4, 19))) return "paskha 2020";
-1 66 if (!date_comp(easter(2021, true), mkdate(2021, 5, 2))) return "paskha 2021";
-1 67 if (!date_comp(easter(2022, true), mkdate(2022, 4, 24))) return "paskha 2022";
-1 68 if (!date_comp(easter(2023, true), mkdate(2023, 4, 16))) return "paskha 2023";
-1 69 if (!date_comp(easter(2024, true), mkdate(2024, 5, 5))) return "paskha 2024";
-1 70 if (!date_comp(easter(2025, true), mkdate(2025, 4, 20))) return "paskha 2025";
-1 71 if (!date_comp(easter(2026, true), mkdate(2026, 4, 12))) return "paskha 2026";
-1 72 if (!date_comp(easter(2027, true), mkdate(2027, 5, 2))) return "paskha 2027";
-1 73 if (!date_comp(easter(2028, true), mkdate(2028, 4, 16))) return "paskha 2028";
-1 74 if (!date_comp(easter(2029, true), mkdate(2029, 4, 8))) return "paskha 2029";
-1 75 if (!date_comp(easter(2030, true), mkdate(2030, 4, 28))) return "paskha 2030";
-1 76 return 0;
-1 77 }
-1 78
-1 79 bool tpl_comp(struct tpl a, struct tpl b) {
-1 80 return \
-1 81 a.year == b.year && \
-1 82 a.month == b.month && \
-1 83 a.day == b.day && \
-1 84 a.repeat == b.repeat && \
-1 85 a.weekday == b.weekday && \
-1 86 a.nth_of_month == b.nth_of_month && \
-1 87 a.from_easter == b.from_easter && \
-1 88 a.from_paskha == b.from_paskha;
-1 89 }
-1 90
-1 91 struct tpl parse_date_mut(const char *s) {
-1 92 char *_s = malloc(strlen(s));
-1 93 strcpy(_s, s);
-1 94 struct tpl tpl = parse_date(_s);
-1 95 free(_s);
-1 96 return tpl;
-1 97 }
-1 98
-1 99 char *test_parse_date_dd() {
-1 100 struct tpl actual = parse_date_mut("13");
-1 101 struct tpl expected = mktpl();
-1 102 expected.year = today()->tm_year + 1900;
-1 103 expected.day = 13;
-1 104
-1 105 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"13\")";
-1 106 return 0;
-1 107 }
-1 108
-1 109 char *test_parse_date_dd_star() {
-1 110 struct tpl actual = parse_date_mut("13*");
-1 111 struct tpl expected = mktpl();
-1 112 expected.day = 13;
-1 113
-1 114 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"13*\")";
-1 115 return 0;
-1 116 }
-1 117
-1 118 char *test_parse_date_mmdd() {
-1 119 struct tpl actual = parse_date_mut("02/13");
-1 120 struct tpl expected = mktpl();
-1 121 expected.year = today()->tm_year + 1900 + (today()->tm_mon + 1 > 2 ? 1 : 0);
-1 122 expected.month = 2;
-1 123 expected.day = 13;
-1 124
-1 125 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"02/13\")";
-1 126 return 0;
-1 127 }
-1 128
-1 129 char *test_parse_date_mmdd_star() {
-1 130 struct tpl actual = parse_date_mut("02/13*");
-1 131 struct tpl expected = mktpl();
-1 132 expected.month = 2;
-1 133 expected.day = 13;
-1 134
-1 135 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"02/13*\")";
-1 136 return 0;
-1 137 }
-1 138
-1 139 char *test_parse_date_yyyymmdd() {
-1 140 struct tpl actual = parse_date_mut("1999/02/13");
-1 141 struct tpl expected = mktpl();
-1 142 expected.year = 1999;
-1 143 expected.month = 2;
-1 144 expected.day = 13;
-1 145
-1 146 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"1999/02/13\")";
-1 147 return 0;
-1 148 }
-1 149
-1 150 char *test_parse_date_yyyymmdd_star() {
-1 151 struct tpl actual = parse_date_mut("1999/02/13*");
-1 152 struct tpl expected = mktpl();
-1 153 expected.month = 2;
-1 154 expected.day = 13;
-1 155
-1 156 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"1999/02/13*\")";
-1 157 return 0;
-1 158 }
-1 159
-1 160 char *test_parse_date_yyyymmdd_repeat() {
-1 161 struct tpl actual = parse_date_mut("1999/02/13+2");
-1 162 struct tpl expected = mktpl();
-1 163 expected.year = 1999;
-1 164 expected.month = 2;
-1 165 expected.day = 13;
-1 166 expected.repeat = 2;
-1 167
-1 168 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"1999/02/13+2\")";
-1 169 return 0;
-1 170 }
-1 171
-1 172 char *test_parse_date_weekday() {
-1 173 struct tpl actual = parse_date_mut("Sat");
-1 174 struct tpl expected = mktpl();
-1 175 expected.weekday = 7;
-1 176
-1 177 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"Sat\")";
-1 178 return 0;
-1 179 }
-1 180
-1 181 char *test_parse_date_weekday_with_nth_of_month() {
-1 182 struct tpl actual = parse_date_mut("Sat-1");
-1 183 struct tpl expected = mktpl();
-1 184 expected.weekday = 7;
-1 185 expected.nth_of_month = -1;
-1 186
-1 187 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"Sat-1\")";
-1 188 return 0;
-1 189 }
-1 190
-1 191 char *test_parse_date_mm_weekday() {
-1 192 struct tpl actual = parse_date_mut("02/Sat+2");
-1 193 struct tpl expected = mktpl();
-1 194 expected.month = 2;
-1 195 expected.weekday = 7;
-1 196 expected.nth_of_month = 2;
-1 197
-1 198 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"02/Sat+2\")";
-1 199 return 0;
-1 200 }
-1 201
-1 202 char *test_parse_date_yyyymm_weekday() {
-1 203 struct tpl actual = parse_date_mut("1999/02/Sat+2");
-1 204 struct tpl expected = mktpl();
-1 205 expected.year = 1999;
-1 206 expected.month = 2;
-1 207 expected.weekday = 7;
-1 208 expected.nth_of_month = 2;
-1 209
-1 210 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"1999/02/Sat+2\")";
-1 211 return 0;
-1 212 }
-1 213
-1 214 char *test_parse_date_easter() {
-1 215 struct tpl actual = parse_date_mut("Easter");
-1 216 struct tpl expected = mktpl();
-1 217 expected.from_easter = 0;
-1 218
-1 219 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"Easter\")";
-1 220 return 0;
-1 221 }
-1 222
-1 223 char *test_parse_date_easter_with_offset() {
-1 224 struct tpl actual = parse_date_mut("Easter+30");
-1 225 struct tpl expected = mktpl();
-1 226 expected.from_easter = 30;
-1 227
-1 228 if (!tpl_comp(actual, expected)) return "parse_date_mut(\"Easter+30\")";
-1 229 return 0;
-1 230 }
-1 231
-1 232 char *test_is_match_dd() {
-1 233 struct tpl tpl = mktpl();
-1 234 tpl.day = 13;
-1 235
-1 236 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match dd";
-1 237 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match dd";
-1 238 return 0;
-1 239 }
-1 240
-1 241 char *test_is_match_mmdd() {
-1 242 struct tpl tpl = mktpl();
-1 243 tpl.month = 2;
-1 244 tpl.day = 13;
-1 245
-1 246 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match mmdd";
-1 247 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match mmdd";
-1 248 if (is_match(tpl, mkdate(1999, 3, 13))) return "!is_match mmdd";
-1 249 return 0;
-1 250 }
-1 251
-1 252 char *test_is_match_yyyymmdd() {
-1 253 struct tpl tpl = mktpl();
-1 254 tpl.year = 1999;
-1 255 tpl.month = 2;
-1 256 tpl.day = 13;
-1 257
-1 258 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match yyyymmdd";
-1 259 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match yyyymmdd";
-1 260 if (is_match(tpl, mkdate(1999, 3, 13))) return "!is_match yyyymmdd";
-1 261 if (is_match(tpl, mkdate(2000, 2, 13))) return "!is_match yyyymmdd";
-1 262 return 0;
-1 263 }
-1 264
-1 265 char *test_is_match_yyyymmdd_repeat() {
-1 266 struct tpl tpl = mktpl();
-1 267 tpl.year = 1999;
-1 268 tpl.month = 2;
-1 269 tpl.day = 13;
-1 270 tpl.repeat = 2;
-1 271
-1 272 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match yyyymmdd repeat";
-1 273 if (!is_match(tpl, mkdate(1999, 2, 27))) return "is_match yyyymmdd repeat";
-1 274 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match yyyymmdd repeat";
-1 275 if (is_match(tpl, mkdate(1999, 2, 20))) return "!is_match yyyymmdd repeat";
-1 276 return 0;
-1 277 }
-1 278
-1 279 char *test_is_match_weekday() {
-1 280 struct tpl tpl = mktpl();
-1 281 tpl.weekday = 7;
-1 282
-1 283 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match weekday";
-1 284 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match weekday";
-1 285 return 0;
-1 286 }
-1 287
-1 288 char *test_is_match_weekday_with_nth_of_month() {
-1 289 struct tpl tpl = mktpl();
-1 290 tpl.weekday = 7;
-1 291 tpl.nth_of_month = 2;
-1 292
-1 293 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match weekday with nth_of_month";
-1 294 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match weekday with nth_of_month";
-1 295 if (is_match(tpl, mkdate(1999, 2, 20))) return "!is_match weekday with nth_of_month";
-1 296 return 0;
-1 297 }
-1 298
-1 299 char *test_is_match_mm_weekday() {
-1 300 struct tpl tpl = mktpl();
-1 301 tpl.month = 2;
-1 302 tpl.weekday = 7;
-1 303 tpl.nth_of_month = 2;
-1 304
-1 305 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match mm weekday";
-1 306 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match mm weekday";
-1 307 if (is_match(tpl, mkdate(1999, 2, 20))) return "!is_match mm weekday";
-1 308 if (is_match(tpl, mkdate(1999, 3, 13))) return "!is_match mm weekday";
-1 309 return 0;
-1 310 }
-1 311
-1 312 char *test_is_match_yyyymm_weekday() {
-1 313 struct tpl tpl = mktpl();
-1 314 tpl.year = 1999;
-1 315 tpl.month = 2;
-1 316 tpl.weekday = 7;
-1 317 tpl.nth_of_month = 2;
-1 318
-1 319 if (!is_match(tpl, mkdate(1999, 2, 13))) return "is_match yyyymm weekday";
-1 320 if (is_match(tpl, mkdate(1999, 2, 14))) return "!is_match yyyymm weekday";
-1 321 if (is_match(tpl, mkdate(1999, 2, 20))) return "!is_match yyyymm weekday";
-1 322 if (is_match(tpl, mkdate(1999, 3, 13))) return "!is_match yyyymm weekday";
-1 323 if (is_match(tpl, mkdate(2000, 2, 12))) return "!is_match yyyymm weekday";
-1 324 return 0;
-1 325 }
-1 326
-1 327 char *test_is_match_easter() {
-1 328 struct tpl tpl = mktpl();
-1 329 tpl.from_easter = 0;
-1 330 tpl.easter = true;
-1 331
-1 332 if (!is_match(tpl, mkdate(2000, 4, 23))) return "is_match easter";
-1 333 if (is_match(tpl, mkdate(2000, 4, 25))) return "!is_match easter";
-1 334 return 0;
-1 335 }
-1 336
-1 337 char *test_is_match_easter_with_offset() {
-1 338 struct tpl tpl = mktpl();
-1 339 tpl.from_easter = 2;
-1 340 tpl.easter = true;
-1 341
-1 342 if (!is_match(tpl, mkdate(2000, 4, 25))) return "is_match easter+2";
-1 343 if (is_match(tpl, mkdate(2000, 4, 23))) return "!is_match easter+2";
-1 344 return 0;
-1 345 }
-1 346
-1 347 char *test_is_match_paskha() {
-1 348 struct tpl tpl = mktpl();
-1 349 tpl.from_paskha = 0;
-1 350 tpl.paskha = true;
-1 351
-1 352 if (!is_match(tpl, mkdate(2000, 4, 30))) return "is_match paskha";
-1 353 if (is_match(tpl, mkdate(2000, 4, 28))) return "!is_match paskha";
-1 354 return 0;
-1 355 }
-1 356
-1 357 char *test_is_match_paskha_with_offset() {
-1 358 struct tpl tpl = mktpl();
-1 359 tpl.from_paskha = -2;
-1 360 tpl.paskha = true;
-1 361
-1 362 if (!is_match(tpl, mkdate(2000, 4, 28))) return "is_match paskha-2";
-1 363 if (is_match(tpl, mkdate(2000, 4, 30))) return "!is_match paskha-2";
-1 364 return 0;
-1 365 }
-1 366
-1 367 int main(int argc, char **argv) {
-1 368 run_test(test_easter);
-1 369 run_test(test_paskha);
-1 370
-1 371 run_test(test_parse_date_dd);
-1 372 run_test(test_parse_date_dd_star);
-1 373 run_test(test_parse_date_mmdd);
-1 374 run_test(test_parse_date_mmdd_star);
-1 375 run_test(test_parse_date_yyyymmdd);
-1 376 run_test(test_parse_date_yyyymmdd_star);
-1 377 run_test(test_parse_date_yyyymmdd_repeat);
-1 378 run_test(test_parse_date_weekday);
-1 379 run_test(test_parse_date_weekday_with_nth_of_month);
-1 380 run_test(test_parse_date_mm_weekday);
-1 381 run_test(test_parse_date_yyyymm_weekday);
-1 382 run_test(test_parse_date_easter);
-1 383 run_test(test_parse_date_easter_with_offset);
-1 384
-1 385 run_test(test_is_match_dd);
-1 386 run_test(test_is_match_mmdd);
-1 387 run_test(test_is_match_yyyymmdd);
-1 388 run_test(test_is_match_yyyymmdd_repeat);
-1 389 run_test(test_is_match_weekday);
-1 390 run_test(test_is_match_weekday_with_nth_of_month);
-1 391 run_test(test_is_match_mm_weekday);
-1 392 run_test(test_is_match_yyyymm_weekday);
-1 393 run_test(test_is_match_easter);
-1 394 run_test(test_is_match_easter_with_offset);
-1 395 run_test(test_is_match_paskha);
-1 396 run_test(test_is_match_paskha_with_offset);
-1 397
-1 398 printf("Tests run: %d\n", tests_run);
-1 399 if (tests_failed) {
-1 400 return EXIT_FAILURE;
-1 401 }
-1 402 }