root/lib/common/tests/iso8601/crm_time_add_days_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. assert_add_days
  2. invalid_argument
  3. positive_same_year
  4. negative_same_year
  5. positive_year_changes
  6. negative_year_changes
  7. year_out_of_range

   1 /*
   2  * Copyright 2024 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdio.h>      // NULL
  13 #include <limits.h>     // INT_MAX
  14 
  15 #include <crm/common/unittest_internal.h>
  16 
  17 #include <crm/common/iso8601.h>
  18 
  19 static void
  20 assert_add_days(const char *orig_date_time, int days,
     /* [previous][next][first][last][top][bottom][index][help] */
  21                 const char *expected_date_time)
  22 {
  23     crm_time_t *orig = crm_time_new(orig_date_time);
  24     crm_time_t *expected = crm_time_new(expected_date_time);
  25 
  26     assert_non_null(orig);
  27     assert_non_null(expected);
  28 
  29     crm_time_add_days(orig, days);
  30     assert_int_equal(crm_time_compare(orig, expected), 0);
  31 
  32     crm_time_free(orig);
  33     crm_time_free(expected);
  34 }
  35 
  36 static void
  37 invalid_argument(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39     pcmk__assert_asserts(crm_time_add_days(NULL, 1));
  40 }
  41 
  42 static void
  43 positive_same_year(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45     assert_add_days("2024-01-01 00:30:00 +01:00", 1,
  46                     "2024-01-02 00:30:00 +01:00");
  47 
  48     assert_add_days("2024-01-31 01:40:50 +02:00", 1,
  49                     "2024-02-01 01:40:50 +02:00");
  50 
  51     assert_add_days("2024-02-28 11:45:11 +03:00", 1,
  52                     "2024-02-29 11:45:11 +03:00");
  53 
  54     assert_add_days("2024-02-28 12:59:59 -03:00", 2,
  55                     "2024-03-01 12:59:59 -03:00");
  56 
  57     assert_add_days("2024-01-01 00:00:00 +00:00", 365,
  58                     "2024-12-31 00:00:00 +00:00");
  59 
  60     assert_add_days("2025-01-01 23:00:00 +00:00", 364,
  61                     "2025-12-31 23:00:00 +00:00");
  62 }
  63 
  64 static void
  65 negative_same_year(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     assert_add_days("2024-01-02 00:30:00 +01:00", -1,
  68                     "2024-01-01 00:30:00 +01:00");
  69 
  70     assert_add_days("2024-02-01 01:40:50 +02:00", -1,
  71                     "2024-01-31 01:40:50 +02:00");
  72 
  73     assert_add_days("2024-03-01 11:45:11 +03:00", -1,
  74                     "2024-02-29 11:45:11 +03:00");
  75 
  76     assert_add_days("2024-03-01 12:59:59 -03:00", -2,
  77                     "2024-02-28 12:59:59 -03:00");
  78 
  79     assert_add_days("2024-12-31 00:00:00 +00:00", -365,
  80                     "2024-01-01 00:00:00 +00:00");
  81 
  82     assert_add_days("2025-12-31 23:00:00 +00:00", -364,
  83                     "2025-01-01 23:00:00 +00:00");
  84 }
  85 
  86 static void
  87 positive_year_changes(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     // Non-leap year before March to leap year before March
  90     assert_add_days("2023-01-01 00:40:20 +02:00", 365,
  91                     "2024-01-01 00:40:20 +02:00");
  92 
  93     // Non-leap year before March to leap year after February
  94     assert_add_days("2023-01-01 00:40:20 +02:00", 426,
  95                     "2024-03-02 00:40:20 +02:00");
  96 
  97     // Non-leap year after February to leap year before March
  98     assert_add_days("2023-03-02 00:40:20 +02:00", 325,
  99                     "2024-01-21 00:40:20 +02:00");
 100 
 101     // Non-leap year after February to leap year after February
 102     assert_add_days("2023-03-02 00:40:20 +02:00", 385,
 103                     "2024-03-21 00:40:20 +02:00");
 104 
 105     // Leap year before March to non-leap year before March
 106     assert_add_days("2024-01-01 00:40:20 +02:00", 366,
 107                     "2025-01-01 00:40:20 +02:00");
 108 
 109     // Leap year before March to non-leap year after February
 110     assert_add_days("2024-01-01 00:40:20 +02:00", 430,
 111                     "2025-03-06 00:40:20 +02:00");
 112 
 113     // Leap year after February to non-leap year before March
 114     assert_add_days("2024-12-31 09:41:23 +06:00", 1,
 115                     "2025-01-01 09:41:23 +06:00");
 116 
 117     // Leap year after February to non-leap year after February
 118     assert_add_days("2024-12-31 09:41:23 +06:00", 90,
 119                     "2025-03-31 09:41:23 +06:00");
 120 
 121     // From and to non-leap years
 122     assert_add_days("2025-01-01 01:00:00 -02:00", 366,
 123                     "2026-01-02 01:00:00 -02:00");
 124 
 125     // Past "leap year if divisible by 4"
 126     assert_add_days("2025-01-01 00:00:00 +00:00", 1500,
 127                     "2029-02-09 00:00:00 +00:00");
 128 
 129     // Past "except if divisible by 100"
 130     assert_add_days("2025-01-01 00:00:00 +00:00", 28000,
 131                     "2101-08-31 00:00:00 +00:00");
 132 
 133     // Past "except if divisible by 400"
 134     assert_add_days("2025-01-01 00:00:00 +00:00", 150000,
 135                     "2435-09-09 00:00:00 +00:00");
 136 }
 137 
 138 static void
 139 negative_year_changes(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141     // Non-leap year before March to leap year before March
 142     assert_add_days("2025-01-01 00:40:20 +02:00", -366,
 143                     "2024-01-01 00:40:20 +02:00");
 144 
 145     // Non-leap year before March to leap year after February
 146     assert_add_days("2025-01-01 00:40:20 +02:00", -300,
 147                     "2024-03-07 00:40:20 +02:00");
 148 
 149     // Leap year before March to non-leap year before March
 150     assert_add_days("2024-01-01 00:40:20 +02:00", -365,
 151                     "2023-01-01 00:40:20 +02:00");
 152 
 153     // Leap year before March to non-leap year after February
 154     assert_add_days("2024-01-01 00:40:20 +02:00", -1,
 155                     "2023-12-31 00:40:20 +02:00");
 156 
 157     // Past "leap year if divisible by 4"
 158     assert_add_days("1990-01-01 00:00:00 +00:00", -2000,
 159                     "1984-07-11 00:00:00 +00:00");
 160 
 161     // Past "except if divisible by 100"
 162     assert_add_days("1990-01-01 00:00:00 +00:00", -33000,
 163                     "1899-08-26 00:00:00 +00:00");
 164 
 165     // Past "except if divisible by 400"
 166     assert_add_days("1990-01-01 00:00:00 +00:00", -150000,
 167                     "1579-04-26 00:00:00 +00:00");
 168 }
 169 
 170 static void
 171 year_out_of_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173     char *orig_datetime = NULL;
 174     char *expected_datetime = NULL;
 175 
 176     // Year too large
 177     orig_datetime = crm_strdup_printf("%d-01-01 00:00:00 +00:00", INT_MAX);
 178     expected_datetime = crm_strdup_printf("%d-12-31 00:00:00 +00:00", INT_MAX);
 179     assert_add_days(orig_datetime, 400, expected_datetime);
 180     free(orig_datetime);
 181     free(expected_datetime);
 182 
 183     // Year too small
 184     assert_add_days("01-02-01 00:00:00 +00:00", -40,
 185                     "01-01-01 00:00:00 +00:00");
 186 }
 187 
 188 PCMK__UNIT_TEST(NULL, NULL,
 189                 cmocka_unit_test(invalid_argument),
 190                 cmocka_unit_test(positive_same_year),
 191                 cmocka_unit_test(negative_same_year),
 192                 cmocka_unit_test(positive_year_changes),
 193                 cmocka_unit_test(negative_year_changes),
 194                 cmocka_unit_test(year_out_of_range));

/* [previous][next][first][last][top][bottom][index][help] */