1 /* $NetBSD: caljulian.c,v 1.1.1.3 2015/10/23 17:47:45 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 char * CalendarToString(const struct calendar cal); 15 int IsEqual(const struct calendar expected, const struct calendar actual); 16 void setUp(void); 17 void tearDown(void); 18 void test_RegularTime(void); 19 void test_LeapYear(void); 20 void test_uLongBoundary(void); 21 void test_uLongWrapped(void); 22 23 24 char * 25 CalendarToString(const struct calendar cal) { 26 char * str = emalloc (sizeof (char) * 100); 27 28 char buffer[100] =""; 29 snprintf(buffer, 100, "%u", cal.year); 30 strcat(str, buffer); 31 strcat(str, "-"); 32 snprintf(buffer, 100, "%u", (u_int)cal.month); 33 strcat(str, buffer); 34 strcat(str, "-"); 35 snprintf(buffer, 100, "%u", (u_int)cal.monthday); 36 strcat(str, buffer); 37 strcat(str, " ("); 38 snprintf(buffer, 100, "%u", (u_int) cal.yearday); 39 strcat(str, buffer); 40 strcat(str, ") "); 41 snprintf(buffer, 100, "%u", (u_int)cal.hour); 42 strcat(str, buffer); 43 strcat(str, ":"); 44 snprintf(buffer, 100, "%u", (u_int)cal.minute); 45 strcat(str, buffer); 46 strcat(str, ":"); 47 snprintf(buffer, 100, "%u", (u_int)cal.second); 48 strcat(str, buffer); 49 return str; 50 } 51 52 int // technically boolean 53 IsEqual(const struct calendar expected, const struct calendar actual) { 54 if (expected.year == actual.year && 55 (expected.yearday == actual.yearday || 56 (expected.month == actual.month && 57 expected.monthday == actual.monthday)) && 58 expected.hour == actual.hour && 59 expected.minute == actual.minute && 60 expected.second == actual.second) { 61 return TRUE; 62 } else { 63 printf("expected: %s but was %s", CalendarToString(expected) ,CalendarToString(actual)); 64 return FALSE; 65 66 } 67 } 68 69 70 void 71 setUp() 72 { 73 ntpcal_set_timefunc(timefunc); 74 settime(1970, 1, 1, 0, 0, 0); 75 } 76 77 void 78 tearDown() 79 { 80 ntpcal_set_timefunc(NULL); 81 } 82 83 84 void 85 test_RegularTime(void) { 86 u_long testDate = 3485080800UL; // 2010-06-09 14:00:00 87 struct calendar expected = {2010,160,6,9,14,0,0}; 88 89 struct calendar actual; 90 91 caljulian(testDate, &actual); 92 93 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 94 } 95 96 void 97 test_LeapYear(void) { 98 u_long input = 3549902400UL; // 2012-06-28 20:00:00Z 99 struct calendar expected = {2012, 179, 6, 28, 20, 0, 0}; 100 101 struct calendar actual; 102 103 caljulian(input, &actual); 104 105 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 106 } 107 108 void 109 test_uLongBoundary(void) { 110 u_long time = 4294967295UL; // 2036-02-07 6:28:15 111 struct calendar expected = {2036,0,2,7,6,28,15}; 112 113 struct calendar actual; 114 115 caljulian(time, &actual); 116 117 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 118 } 119 120 void 121 test_uLongWrapped(void) { 122 u_long time = 0; 123 struct calendar expected = {2036,0,2,7,6,28,16}; 124 125 struct calendar actual; 126 127 caljulian(time, &actual); 128 129 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 130 } 131