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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_add_years
  2. invalid_argument
  3. add_positive
  4. add_negative
  5. 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_years(const char *orig_date_time, int years,
     /* [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_years(orig, years);
  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_years(NULL, 1));
  40 }
  41 
  42 static void
  43 add_positive(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45     assert_add_years("2024-01-01 00:30:00 +01:00", 1,
  46                      "2025-01-01 00:30:00 +01:00");
  47 
  48     assert_add_years("2024-12-31 01:40:50 +02:00", 1000,
  49                      "3024-12-31 01:40:50 +02:00");
  50 }
  51 
  52 static void
  53 add_negative(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55     assert_add_years("2024-01-01 00:30:00 +01:00", -1,
  56                      "2023-01-01 00:30:00 +01:00");
  57 
  58     assert_add_years("2024-12-31 01:40:50 +02:00", -1000,
  59                      "1024-12-31 01:40:50 +02:00");
  60 }
  61 
  62 static void
  63 out_of_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65     char *expected_datetime = NULL;
  66 
  67     expected_datetime = crm_strdup_printf("%d-01-01 00:00:00 +00:00", INT_MAX);
  68     assert_add_years("2024-01-01 00:00:00 +00:00", INT_MAX, expected_datetime);
  69     free(expected_datetime);
  70 
  71     assert_add_years("2024-01-01 00:00:00 +00:00", -3000,
  72                      "01-01-01 00:00:00 +00:00");
  73 }
  74 
  75 PCMK__UNIT_TEST(NULL, NULL,
  76                 cmocka_unit_test(invalid_argument),
  77                 cmocka_unit_test(add_positive),
  78                 cmocka_unit_test(add_negative),
  79                 cmocka_unit_test(out_of_range));

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