exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

test_gigasecond.c (2771B)


      1 #include "vendor/unity.h"
      2 #include "../src/gigasecond.h"
      3 
      4 static inline size_t is_leap_year(int year)
      5 {
      6    if (year % 400 == 0)
      7       return 1;
      8    if (year % 100 == 0)
      9       return 0;
     10    if (year % 4 == 0)
     11       return 1;
     12    return 0;
     13 }
     14 
     15 static inline time_t days_from_1ad(int year)
     16 {
     17    --year;                      // The gegorian calander is without a year 0. This is years from 1AD.
     18    // Little complex, add a day for all of the leap years
     19    // This is a quarter of the days since 0 execpt one in a hundred are lost except 1 in 400 are gained ... simple.
     20    return 365 * year + (year / 400) - (year / 100) + (year / 4);
     21 }
     22 
     23 time_t construct_date(int year, int month, int day, int hour, int min, int sec)
     24 {
     25    static const time_t seconds_in_day = 86400;  // 60 * 60 * 24
     26    static const time_t days[2][12] = {
     27       {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
     28       {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
     29    };
     30 
     31    time_t days_since_epoch =
     32        (days_from_1ad(year) - days_from_1ad(1970)) +
     33        days[is_leap_year(year)][(month - 1)] + (day - 1);
     34    time_t result =
     35        (seconds_in_day * days_since_epoch) + (60 * ((hour * 60) + min) + sec);
     36 
     37    return result;
     38 }
     39 
     40 void setUp(void)
     41 {
     42 }
     43 
     44 void tearDown(void)
     45 {
     46 }
     47 
     48 void test_date(void)
     49 {
     50    time_t expected = construct_date(2043, 1, 1, 1, 46, 40);
     51    time_t actual = gigasecond_after(construct_date(2011, 4, 25, 0, 0, 0));
     52    TEST_ASSERT(expected == actual);
     53 }
     54 
     55 void test_another_date(void)
     56 {
     57    time_t expected = construct_date(2009, 2, 19, 1, 46, 40);
     58    time_t actual = gigasecond_after(construct_date(1977, 6, 13, 0, 0, 0));
     59    TEST_ASSERT(expected == actual);
     60 }
     61 
     62 void test_third_date(void)
     63 {
     64    time_t expected = construct_date(1991, 3, 27, 1, 46, 40);
     65    time_t actual = gigasecond_after(construct_date(1959, 7, 19, 0, 0, 0));
     66    TEST_ASSERT(expected == actual);
     67 }
     68 
     69 void test_date_and_time(void)
     70 {
     71    time_t expected = construct_date(2046, 10, 2, 23, 46, 40);
     72    time_t actual = gigasecond_after(construct_date(2015, 1, 24, 22, 0, 0));
     73    TEST_ASSERT(expected == actual);
     74 }
     75 
     76 void test_date_and_time_with_day_rollover(void)
     77 {
     78    time_t expected = construct_date(2046, 10, 3, 1, 46, 39);
     79    time_t actual = gigasecond_after(construct_date(2015, 1, 24, 23, 59, 59));
     80    TEST_ASSERT(expected == actual);
     81 }
     82 
     83 /*
     84 void test_your_birthday(void)
     85 {
     86    time_t birthday = construct_date(1989, 1, 1, 1, 1, 1);
     87    time_t gigday = gigasecond_after(birthday);
     88    printf("%s", ctime(&gigday));
     89 }
     90 */
     91 
     92 int main(void)
     93 {
     94    UnityBegin("test/test_gigasecond.c");
     95 
     96    RUN_TEST(test_date);
     97    RUN_TEST(test_another_date);
     98    RUN_TEST(test_third_date);
     99    RUN_TEST(test_date_and_time);
    100    RUN_TEST(test_date_and_time_with_day_rollover);
    101    //RUN_TEST(test_your_birthday);
    102 
    103    UnityEnd();
    104    return 0;
    105 }