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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_add_seconds
  2. invalid_argument
  3. add_zero
  4. add_less_than_one_day
  5. add_more_than_one_day
  6. subtract_less_than_one_day
  7. subtract_more_than_one_day

   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_seconds(const char *orig_date_time, int seconds,
     /* [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_seconds(orig, seconds);
  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_seconds(NULL, 1));
  40 }
  41 
  42 static void
  43 add_zero(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45     assert_add_seconds("2024-01-01 00:30:00 +01:00", 0,
  46                        "2024-01-01 00:30:00 +01:00");
  47 }
  48 
  49 static void
  50 add_less_than_one_day(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     // Minute boundary not crossed
  53     assert_add_seconds("2024-01-01 00:30:00 +01:00", 1,
  54                        "2024-01-01 00:30:01 +01:00");
  55     assert_add_seconds("2024-01-01 00:30:00 +01:00", 59,
  56                        "2024-01-01 00:30:59 +01:00");
  57 
  58     // Minute boundary crossed
  59     assert_add_seconds("2024-01-01 00:30:59 +02:00", 1,
  60                        "2024-01-01 00:31:00 +02:00");
  61     assert_add_seconds("2024-01-01 00:44:30 +02:00", 60,
  62                        "2024-01-01 00:45:30 +02:00");
  63     assert_add_seconds("2024-01-01 00:44:30 +02:00", 125,
  64                        "2024-01-01 00:46:35 +02:00");
  65 
  66     // Hour boundary crossed
  67     assert_add_seconds("2024-01-01 00:59:59 -03:00", 1,
  68                        "2024-01-01 01:00:00 -03:00");
  69     assert_add_seconds("2024-01-01 00:23:34 -03:00", 3600,
  70                        "2024-01-01 01:23:34 -03:00");
  71     assert_add_seconds("2024-01-01 00:23:34 -03:00", 7210,
  72                        "2024-01-01 02:23:44 -03:00");
  73 
  74     // Day boundary crossed
  75     assert_add_seconds("2024-01-01 23:59:59 +04:00", 1,
  76                        "2024-01-02 00:00:00 +04:00");
  77     assert_add_seconds("2024-02-28 00:05:00 +04:00", 86200,
  78                        "2024-02-29 00:01:40 +04:00");
  79 
  80     // Month boundary crossed
  81     assert_add_seconds("2023-02-28 00:05:00 -05:00", 86200,
  82                        "2023-03-01 00:01:40 -05:00");
  83     assert_add_seconds("2024-02-29 23:59:00 -05:00", 60,
  84                        "2024-03-01 00:00:00 -05:00");
  85 
  86     // Year boundary crossed
  87     assert_add_seconds("2024-12-31 23:59:59 +06:00", 1,
  88                        "2025-01-01 00:00:00 +06:00");
  89 }
  90 
  91 static void
  92 add_more_than_one_day(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94     // Month boundary not crossed
  95     assert_add_seconds("2024-01-01 00:00:00 +01:00", 86400 * 2,
  96                        "2024-01-03 00:00:00 +01:00");
  97     assert_add_seconds("2024-02-27 23:59:59 +01:00", 86400 * 2,
  98                        "2024-02-29 23:59:59 +01:00");
  99 
 100     // Month boundary crossed
 101     assert_add_seconds("2023-02-26 23:59:59 -02:00", 86400 * 2 + 1,
 102                        "2023-03-01 00:00:00 -02:00");
 103     assert_add_seconds("2024-02-27 23:59:59 -02:00", 86400 * 2 + 1,
 104                        "2024-03-01 00:00:00 -02:00");
 105 
 106     // Year boundary crossed
 107     assert_add_seconds("2024-12-01 00:00:00 +06:00", 86400 * 31,
 108                        "2025-01-01 00:00:00 +06:00");
 109 }
 110 
 111 static void
 112 subtract_less_than_one_day(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114     // Minute boundary not crossed
 115     assert_add_seconds("2024-01-01 00:30:01 +01:00", -1,
 116                        "2024-01-01 00:30:00 +01:00");
 117     assert_add_seconds("2024-01-01 00:30:30 +01:00", -5,
 118                        "2024-01-01 00:30:25 +01:00");
 119     assert_add_seconds("2024-01-01 00:30:59 +01:00", -59,
 120                        "2024-01-01 00:30:00 +01:00");
 121 
 122     // Minute boundary crossed
 123     assert_add_seconds("2024-01-01 00:30:00 +02:00", -1,
 124                        "2024-01-01 00:29:59 +02:00");
 125     assert_add_seconds("2024-01-01 00:44:30 +02:00", -60,
 126                        "2024-01-01 00:43:30 +02:00");
 127     assert_add_seconds("2024-01-01 00:14:30 +02:00", -125,
 128                        "2024-01-01 00:12:25 +02:00");
 129 
 130     // Hour boundary crossed
 131     assert_add_seconds("2024-01-01 01:00:00 -03:00", -1,
 132                        "2024-01-01 00:59:59 -03:00");
 133     assert_add_seconds("2024-01-01 01:23:34 -03:00", -3600,
 134                        "2024-01-01 00:23:34 -03:00");
 135     assert_add_seconds("2024-01-01 02:23:34 -03:00", -7210,
 136                        "2024-01-01 00:23:24 -03:00");
 137 
 138     // Day boundary crossed
 139     assert_add_seconds("2024-01-02 00:00:00 +04:00", -1,
 140                        "2024-01-01 23:59:59 +04:00");
 141     assert_add_seconds("2024-02-29 00:01:40 +04:00", -86200,
 142                        "2024-02-28 00:05:00 +04:00");
 143 
 144     // Month boundary crossed
 145     assert_add_seconds("2023-03-01 00:01:40 -05:00", -86200,
 146                        "2023-02-28 00:05:00 -05:00");
 147     assert_add_seconds("2024-03-01 00:00:00 -05:00", -60,
 148                        "2024-02-29 23:59:00 -05:00");
 149 
 150     // Year boundary crossed
 151     assert_add_seconds("2025-01-01 00:00:00 +06:00", -1,
 152                        "2024-12-31 23:59:59 +06:00");
 153 }
 154 
 155 static void
 156 subtract_more_than_one_day(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158     // Month boundary not crossed
 159     assert_add_seconds("2024-01-03 00:00:00 +01:00", 86400 * -2,
 160                        "2024-01-01 00:00:00 +01:00");
 161     assert_add_seconds("2024-02-29 23:59:59 +01:00", 86400 * -2,
 162                        "2024-02-27 23:59:59 +01:00");
 163 
 164     // Month boundary crossed
 165     assert_add_seconds("2023-03-03 00:00:00 -02:00", 86400 * -2 - 1,
 166                        "2023-02-28 23:59:59 -02:00");
 167     assert_add_seconds("2024-03-03 00:00:00 -02:00", 86400 * -2 - 1,
 168                        "2024-02-29 23:59:59 -02:00");
 169 
 170     // Year boundary crossed
 171     assert_add_seconds("2025-01-01 00:00:00 +06:00", 86400 * -31,
 172                        "2024-12-01 00:00:00 +06:00");
 173 }
 174 
 175 PCMK__UNIT_TEST(NULL, NULL,
 176                 cmocka_unit_test(invalid_argument),
 177                 cmocka_unit_test(add_zero),
 178                 cmocka_unit_test(add_less_than_one_day),
 179                 cmocka_unit_test(add_more_than_one_day),
 180                 cmocka_unit_test(subtract_less_than_one_day),
 181                 cmocka_unit_test(subtract_more_than_one_day));

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