summary refs log tree commit diff stats
path: root/c/gigasecond/test/test_gigasecond.c
blob: dcee855ee6a6328f00afc159c2216c4ae0e5b4dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "vendor/unity.h"
#include "../src/gigasecond.h"

static inline size_t is_leap_year(int year)
{
   if (year % 400 == 0)
      return 1;
   if (year % 100 == 0)
      return 0;
   if (year % 4 == 0)
      return 1;
   return 0;
}

static inline time_t days_from_1ad(int year)
{
   --year;                      // The gregorian calander is without a year 0. This is years from 1AD.
   // Little complex, add a day for all of the leap years
   // This is a quarter of the days since 0 execpt one in a hundred are lost except 1 in 400 are gained ... simple.
   return 365 * year + (year / 400) - (year / 100) + (year / 4);
}

static time_t
construct_date(int year, int month, int day, int hour, int min, int sec)
{
   static const time_t seconds_in_day = 86400;  // 60 * 60 * 24
   static const time_t days[2][12] = {
      {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
      {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
   };

   time_t days_since_epoch =
       (days_from_1ad(year) - days_from_1ad(1970)) +
       days[is_leap_year(year)][(month - 1)] + (day - 1);
   time_t result =
       (seconds_in_day * days_since_epoch) + (60 * ((hour * 60) + min) + sec);

   return result;
}

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_date(void)
{
   time_t expected = construct_date(2043, 1, 1, 1, 46, 40);
   time_t actual = gigasecond_after(construct_date(2011, 4, 25, 0, 0, 0));
   TEST_ASSERT(expected == actual);
}

static void test_another_date(void)
{
   time_t expected = construct_date(2009, 2, 19, 1, 46, 40);
   time_t actual = gigasecond_after(construct_date(1977, 6, 13, 0, 0, 0));
   TEST_ASSERT(expected == actual);
}

static void test_third_date(void)
{
   time_t expected = construct_date(1991, 3, 27, 1, 46, 40);
   time_t actual = gigasecond_after(construct_date(1959, 7, 19, 0, 0, 0));
   TEST_ASSERT(expected == actual);
}

static void test_date_and_time(void)
{
   time_t expected = construct_date(2046, 10, 2, 23, 46, 40);
   time_t actual = gigasecond_after(construct_date(2015, 1, 24, 22, 0, 0));
   TEST_ASSERT(expected == actual);
}

static void test_date_and_time_with_day_rollover(void)
{
   time_t expected = construct_date(2046, 10, 3, 1, 46, 39);
   time_t actual = gigasecond_after(construct_date(2015, 1, 24, 23, 59, 59));
   TEST_ASSERT(expected == actual);
}

/*
static void test_your_birthday(void)
{
   time_t birthday = construct_date(1989, 1, 1, 1, 1, 1);
   time_t gigday = gigasecond_after(birthday);
   printf("%s", ctime(&gigday));
}
*/

int main(void)
{
   UnityBegin("test/test_gigasecond.c");

   RUN_TEST(test_date);
   RUN_TEST(test_another_date);
   RUN_TEST(test_third_date);
   RUN_TEST(test_date_and_time);
   RUN_TEST(test_date_and_time_with_day_rollover);
   //RUN_TEST(test_your_birthday);

   return UnityEnd();
}