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