leapc
Loading...
Searching...
No Matches
leap_helpers_test.c
1#include "leap.h"
2
3/* Keep assert-based tests enabled in Release builds. */
4#ifdef NDEBUG
5#undef NDEBUG
6#endif
7#include <assert.h>
8#include <stdlib.h>
9
10int leap_helpers_test(int argc, char **argv) {
11 (void)argc;
12 (void)argv;
13
14 static const struct leap_off offsets[] = {
15 {2000, 59},
16 {2001, 59},
17 {2000, 60},
18 };
19 static const struct leap_date dates[] = {
20 {2000, 2, 29},
21 {2001, 2, 29},
22 {2000, 3, 29},
23 {2000, 2, 28},
24 };
25
26 for (size_t i = 0; i < sizeof offsets / sizeof offsets[0]; i++)
27 for (size_t j = 0; j < sizeof offsets / sizeof offsets[0]; j++)
28 assert(equal_leap_off(offsets[i], offsets[j]) == (i == j));
29
30 for (size_t i = 0; i < sizeof dates / sizeof dates[0]; i++)
31 for (size_t j = 0; j < sizeof dates / sizeof dates[0]; j++)
32 assert(equal_leap_date(dates[i], dates[j]) == (i == j));
33
34 /* Test conversion between leap_date and leap_off structures. */
35 static const struct {
36 struct leap_date date;
37 struct leap_off off;
38 int absolute;
39 } cases[] = {
40 {{0, 1, 1}, {0, 0}, 0}, {{0, 2, 29}, {0, 59}, 59}, {{0, 12, 31}, {0, 365}, 365},
41 {{1, 1, 1}, {1, 0}, 366}, {{-1, 12, 31}, {-1, 364}, -1},
42 };
43
44 for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
45 const struct leap_off off = leap_from_date(cases[i].date);
46 assert(off.year == cases[i].off.year);
47 assert(off.day == cases[i].off.day);
48 assert(leap_abs_from_date(cases[i].date) == cases[i].absolute);
49
50 const struct leap_date date = leap_date_from_off(cases[i].off);
51 assert(date.year == cases[i].date.year);
52 assert(date.month == cases[i].date.month);
53 assert(date.day == cases[i].date.day);
54 }
55
56 return EXIT_SUCCESS;
57}
Leap year function prototypes.
static bool equal_leap_off(struct leap_off lhs, struct leap_off rhs)
Compares two leap_off structures for equality.
Definition leap.h:105
static int leap_abs_from_date(struct leap_date date)
Absolute date from leap_date structure.
Definition leap.h:275
static struct leap_date leap_date_from_off(struct leap_off off)
Date from leap offset.
Definition leap.h:218
struct leap_date leap_date(int year, int day_off)
Date from year and day of year.
Definition leap.c:104
static struct leap_off leap_from_date(struct leap_date date)
Day from leap date.
Definition leap.h:240
struct leap_off leap_off(int year, int day_off)
Offsets year and day of year.
Definition leap.c:70
static bool equal_leap_date(struct leap_date lhs, struct leap_date rhs)
Compares two leap_date structures for equality.
Definition leap.h:193
Leap year date structure.
Definition leap.h:163
int year
Year.
Definition leap.h:167
int month
Month of year starting from 1 for January.
Definition leap.h:174
int day
Day of month starting from 1 for the first day of the month.
Definition leap.h:181
Leap offset by year and day.
Definition leap.h:77
int day
Day of year offset.
Definition leap.h:85
int year
Year offset.
Definition leap.h:81