1 /* $NetBSD: caljulian.c,v 1.3 2024/08/18 20:47:26 christos Exp $ */ 2 3 #include "config.h" 4 5 #include "ntp_calendar.h" 6 #include "ntp_stdlib.h" 7 8 #include "unity.h" 9 #include "test-libntp.h" 10 11 #include <string.h> 12 13 14 void setUp(void); 15 void tearDown(void); 16 void test_RegularTime(void); 17 void test_LeapYear(void); 18 void test_uLongBoundary(void); 19 void test_uLongWrapped(void); 20 21 22 static const char * 23 CalendarToString(const struct calendar cal) 24 { 25 char * str; 26 27 LIB_GETBUF(str); 28 snprintf(str, LIB_BUFLENGTH, 29 "%04hu-%02hhu-%02hhu (%hu) %02hhu:%02hhu:%02hhu", 30 cal.year, cal.month, cal.monthday, cal.yearday, 31 cal.hour, cal.minute, cal.second); 32 return str; 33 } 34 35 static int // technically boolean 36 IsEqual(const struct calendar expected, const struct calendar actual) 37 { 38 if ( expected.year == actual.year 39 && ( expected.yearday == actual.yearday 40 || ( expected.month == actual.month 41 && expected.monthday == actual.monthday)) 42 && expected.hour == actual.hour 43 && expected.minute == actual.minute 44 && expected.second == actual.second) { 45 return TRUE; 46 } else { 47 const char * p_exp = CalendarToString(expected); 48 const char * p_act = CalendarToString(actual); 49 printf("expected: %s but was %s", p_exp, p_act); 50 return FALSE; 51 } 52 } 53 54 55 void setUp(void) 56 { 57 ntpcal_set_timefunc(timefunc); 58 settime(1970, 1, 1, 0, 0, 0); 59 init_lib(); 60 } 61 62 void tearDown(void) 63 { 64 ntpcal_set_timefunc(NULL); 65 } 66 67 68 void test_RegularTime(void) 69 { 70 u_long testDate = 3485080800UL; // 2010-06-09 14:00:00 71 struct calendar expected = {2010,160,6,9,14,0,0}; 72 73 struct calendar actual; 74 75 caljulian(testDate, &actual); 76 77 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 78 } 79 80 void test_LeapYear(void) 81 { 82 u_long input = 3549902400UL; // 2012-06-28 20:00:00Z 83 struct calendar expected = {2012, 179, 6, 28, 20, 0, 0}; 84 85 struct calendar actual; 86 87 caljulian(input, &actual); 88 89 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 90 } 91 92 void test_uLongBoundary(void) 93 { 94 u_long enc_time = 4294967295UL; // 2036-02-07 6:28:15 95 struct calendar expected = {2036,0,2,7,6,28,15}; 96 97 struct calendar actual; 98 99 caljulian(enc_time, &actual); 100 101 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 102 } 103 104 void test_uLongWrapped(void) 105 { 106 u_long enc_time = 0; 107 struct calendar expected = {2036,0,2,7,6,28,16}; 108 109 struct calendar actual; 110 111 caljulian(enc_time, &actual); 112 113 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 114 } 115