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