root/lib/common/tests/strings/pcmk__scan_ll_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input_string
  2. bad_input_string
  3. trailing_chars
  4. no_result_variable
  5. out_of_range
  6. typical_case

   1 /*
   2  * Copyright 2023-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 <limits.h>
  13 
  14 #include <crm/common/unittest_internal.h>
  15 
  16 static void
  17 empty_input_string(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19     long long result;
  20 
  21     assert_int_equal(pcmk__scan_ll(NULL, &result, 47), pcmk_rc_ok);
  22     assert_int_equal(result, 47);
  23 }
  24 
  25 static void
  26 bad_input_string(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28     long long result;
  29 
  30     assert_int_equal(pcmk__scan_ll("asdf", &result, 47), pcmk_rc_bad_input);
  31     assert_int_equal(result, 47);
  32     assert_int_equal(pcmk__scan_ll("as12", &result, 47), pcmk_rc_bad_input);
  33     assert_int_equal(result, 47);
  34 }
  35 
  36 static void
  37 trailing_chars(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39     long long result;
  40 
  41     assert_int_equal(pcmk__scan_ll("12as", &result, 47), pcmk_rc_ok);
  42     assert_int_equal(result, 12);
  43 }
  44 
  45 static void
  46 no_result_variable(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48     assert_int_equal(pcmk__scan_ll("1234", NULL, 47), pcmk_rc_ok);
  49     assert_int_equal(pcmk__scan_ll("asdf", NULL, 47), pcmk_rc_bad_input);
  50 }
  51 
  52 static void
  53 out_of_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55     long long result = 0LL;
  56     char *very_long = crm_strdup_printf(" %lld0", LLONG_MAX);
  57 
  58     assert_int_equal(pcmk__scan_ll(very_long, &result, 47), ERANGE);
  59     assert_true(result == LLONG_MAX);
  60 
  61     very_long[0] = '-';
  62     assert_int_equal(pcmk__scan_ll(very_long, &result, 47), ERANGE);
  63     assert_true(result == LLONG_MIN);
  64 
  65     free(very_long);
  66 }
  67 
  68 static void
  69 typical_case(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71     long long result;
  72 
  73     assert_int_equal(pcmk__scan_ll("1234", &result, 47), pcmk_rc_ok);
  74     assert_int_equal(result, 1234);
  75 }
  76 
  77 PCMK__UNIT_TEST(NULL, NULL,
  78                 cmocka_unit_test(empty_input_string),
  79                 cmocka_unit_test(bad_input_string),
  80                 cmocka_unit_test(trailing_chars),
  81                 cmocka_unit_test(no_result_variable),
  82                 cmocka_unit_test(out_of_range),
  83                 cmocka_unit_test(typical_case))

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