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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_hr_format
  2. null_format
  3. no_specifiers
  4. without_nano
  5. with_nano

   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 
  14 #include <crm/common/iso8601_internal.h>
  15 #include <crm/common/unittest_internal.h>
  16 
  17 #define TEST_TIME pcmk__time_hr_new("2024-01-02 03:04:05 +00:00")
  18 
  19 /*!
  20  * \internal
  21  * \brief Assert that pcmk__time_format_hr() produced expected result
  22  *
  23  * \param[in] format     Time format string
  24  * \param[in] expected   Assertion succeeds if result matches this
  25  * \param[in] alternate  If this is not NULL, assertion may also succeed if
  26  *                       result matches this
  27  *
  28  * \note This allows two possible results because different strftime()
  29  *       implementations handle certain format syntax differently.
  30  */
  31 static void
  32 assert_hr_format(const char *format, const char *expected,
     /* [previous][next][first][last][top][bottom][index][help] */
  33                  const char *alternate)
  34 {
  35     pcmk__time_hr_t *hr = TEST_TIME;
  36     char *result = pcmk__time_format_hr(format, hr);
  37 
  38     pcmk__time_hr_free(hr);
  39 
  40     if (expected == NULL) {
  41         assert_null(result);
  42         return;
  43     }
  44 
  45     assert_non_null(result);
  46 
  47     if (alternate == NULL) {
  48         assert_string_equal(result, expected);
  49     } else {
  50         assert_true((strcmp(result, expected) == 0)
  51                     || (strcmp(result, alternate) == 0));
  52     }
  53 
  54     free(result);
  55 }
  56 
  57 static void
  58 null_format(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     assert_null(pcmk__time_format_hr(NULL, NULL));
  61     assert_hr_format(NULL, NULL, NULL); // for pcmk__time_format_hr(NULL, hr)
  62 }
  63 
  64 static void
  65 no_specifiers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     assert_hr_format("no specifiers", "no specifiers", NULL);
  68     assert_hr_format("this has a literal % in it",
  69                      "this has a literal % in it",
  70                      // *BSD strftime() strips single %
  71                      "this has a literal  in it");
  72     assert_hr_format("this has a literal %01 in it",
  73                      "this has a literal %01 in it",
  74                      // *BSD strftime() strips %0
  75                      "this has a literal 1 in it");
  76     assert_hr_format("%2 this starts and ends with %",
  77                      "%2 this starts and ends with %",
  78                      // *BSD strftime() strips % in front of nonzero number
  79                      "2 this starts and ends with %");
  80 
  81     /* strftime() treats % with a number (and no specifier) as a literal string
  82      * to be formatted with a field width (undocumented and probably a bug ...)
  83      */
  84     assert_hr_format("this ends with %10", "this ends with        %10",
  85                      // *BSD strftime() strips % in front of nonzero number
  86                      "this ends with 10");
  87 }
  88 
  89 static void
  90 without_nano(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92     assert_hr_format("%H:%M %a %b %d", "03:04 Tue Jan 02", NULL);
  93     assert_hr_format("%H:%M:%S", "03:04:05", NULL);
  94     assert_hr_format("The time is %H:%M right now",
  95                      "The time is 03:04 right now", NULL);
  96     assert_hr_format("%3S seconds", "005 seconds",
  97                      // *BSD strftime() doesn't support field widths
  98                      "3S seconds");
  99 
 100     // strftime() treats %% as a literal %
 101     assert_hr_format("%%H %%N", "%H %N", NULL);
 102 }
 103 
 104 static void
 105 with_nano(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107     assert_hr_format("%H:%M:%S.%06N", "03:04:05.000000", NULL);
 108     assert_hr_format("The time is %H:%M:%S.%06N right NOW",
 109                      "The time is 03:04:05.000000 right NOW", NULL);
 110 }
 111 
 112 PCMK__UNIT_TEST(NULL, NULL,
 113                 cmocka_unit_test(null_format),
 114                 cmocka_unit_test(no_specifiers),
 115                 cmocka_unit_test(without_nano),
 116                 cmocka_unit_test(with_nano))

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