summary refs log blame commit diff stats
path: root/c/gigasecond/test/test_gigasecond.c
blob: dcee855ee6a6328f00afc159c2216c4ae0e5b4dd (plain) (tree)
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { col
#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();
}