xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/calyearstart.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: calyearstart.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2067f5680Schristos 
3f17b710fSchristos #include "config.h"
4f17b710fSchristos 
5a6f3f22fSchristos #include "ntp_stdlib.h"
6f17b710fSchristos #include "ntp_calendar.h"
7f17b710fSchristos #include "unity.h"
8f17b710fSchristos 
9f17b710fSchristos #include "test-libntp.h"
10f17b710fSchristos 
11a6f3f22fSchristos void setUp(void);
12a6f3f22fSchristos void tearDown(void);
13a6f3f22fSchristos void test_NoWrapInDateRange(void);
14a6f3f22fSchristos void test_NoWrapInDateRangeLeapYear(void);
15a6f3f22fSchristos void test_WrapInDateRange(void);
16f17b710fSchristos 
setUp(void)17a6f3f22fSchristos void setUp(void)
18f17b710fSchristos {
19f17b710fSchristos     ntpcal_set_timefunc(timefunc);
20f17b710fSchristos     settime(1970, 1, 1, 0, 0, 0);
21f17b710fSchristos }
22f17b710fSchristos 
tearDown(void)23a6f3f22fSchristos void tearDown(void)
24f17b710fSchristos {
25f17b710fSchristos     ntpcal_set_timefunc(NULL);
26f17b710fSchristos }
27f17b710fSchristos 
28f17b710fSchristos 
test_NoWrapInDateRange(void)29a6f3f22fSchristos void test_NoWrapInDateRange(void) {
30f17b710fSchristos 	const u_int32 input = 3486372600UL; // 2010-06-24 12:50:00.
31f17b710fSchristos 	const u_int32 expected = 3471292800UL; // 2010-01-01 00:00:00
32f17b710fSchristos 
33f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime));
34f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL));
35f17b710fSchristos }
36f17b710fSchristos 
test_NoWrapInDateRangeLeapYear(void)37a6f3f22fSchristos void test_NoWrapInDateRangeLeapYear(void) {
38f17b710fSchristos 	const u_int32 input = 3549528000UL; // 2012-06-24 12:00:00
39f17b710fSchristos 	const u_int32 expected = 3534364800UL; // 2012-01-01 00:00:00
40f17b710fSchristos 
41f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime));
42f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL));
43f17b710fSchristos }
44f17b710fSchristos 
test_WrapInDateRange(void)45a6f3f22fSchristos void test_WrapInDateRange(void) {
46f17b710fSchristos 	const u_int32 input = 19904UL; // 2036-02-07 12:00:00
47f17b710fSchristos 	const u_int32 expected = 4291747200UL; // 2036-01-01 00:00:00
48f17b710fSchristos 
49f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime));
50f17b710fSchristos 	TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL));
51f17b710fSchristos }
52