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

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input_string
  2. missing_separator
  3. only_separator
  4. no_range_end
  5. no_range_start
  6. range_start_and_end
  7. garbage
  8. strtoll_errors
  9. main

   1 /*
   2  * Copyright 2020-2021 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 Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdarg.h>
  13 #include <stddef.h>
  14 #include <stdint.h>
  15 #include <setjmp.h>
  16 #include <cmocka.h>
  17 
  18 static void
  19 empty_input_string(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21     long long start, end;
  22 
  23     assert_int_equal(pcmk__parse_ll_range(NULL, &start, &end), pcmk_rc_unknown_format);
  24     assert_int_equal(pcmk__parse_ll_range("", &start, &end), pcmk_rc_unknown_format);
  25 }
  26 
  27 static void
  28 missing_separator(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     long long start, end;
  31 
  32     assert_int_equal(pcmk__parse_ll_range("1234", &start, &end), pcmk_rc_ok);
  33     assert_int_equal(start, 1234);
  34     assert_int_equal(end, 1234);
  35 }
  36 
  37 static void
  38 only_separator(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40     long long start, end;
  41 
  42     assert_int_equal(pcmk__parse_ll_range("-", &start, &end), pcmk_rc_unknown_format);
  43     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  44     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  45 }
  46 
  47 static void
  48 no_range_end(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50     long long start, end;
  51 
  52     assert_int_equal(pcmk__parse_ll_range("2000-", &start, &end), pcmk_rc_ok);
  53     assert_int_equal(start, 2000);
  54     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  55 }
  56 
  57 static void
  58 no_range_start(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     long long start, end;
  61 
  62     assert_int_equal(pcmk__parse_ll_range("-2020", &start, &end), pcmk_rc_ok);
  63     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  64     assert_int_equal(end, 2020);
  65 }
  66 
  67 static void
  68 range_start_and_end(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70     long long start, end;
  71 
  72     assert_int_equal(pcmk__parse_ll_range("2000-2020", &start, &end), pcmk_rc_ok);
  73     assert_int_equal(start, 2000);
  74     assert_int_equal(end, 2020);
  75 
  76     assert_int_equal(pcmk__parse_ll_range("2000-2020-2030", &start, &end), pcmk_rc_unknown_format);
  77 }
  78 
  79 static void
  80 garbage(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82     long long start, end;
  83 
  84     assert_int_equal(pcmk__parse_ll_range("2000x-", &start, &end), pcmk_rc_unknown_format);
  85     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  86     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  87 
  88     assert_int_equal(pcmk__parse_ll_range("-x2000", &start, &end), pcmk_rc_unknown_format);
  89     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  90     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  91 }
  92 
  93 static void
  94 strtoll_errors(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     long long start, end;
  97 
  98     assert_int_equal(pcmk__parse_ll_range("20000000000000000000-", &start, &end), pcmk_rc_unknown_format);
  99     assert_int_equal(pcmk__parse_ll_range("100-20000000000000000000", &start, &end), pcmk_rc_unknown_format);
 100 }
 101 
 102 int main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     const struct CMUnitTest tests[] = {
 105         cmocka_unit_test(empty_input_string),
 106         cmocka_unit_test(missing_separator),
 107         cmocka_unit_test(only_separator),
 108         cmocka_unit_test(no_range_end),
 109         cmocka_unit_test(no_range_start),
 110         cmocka_unit_test(range_start_and_end),
 111         cmocka_unit_test(strtoll_errors),
 112 
 113         cmocka_unit_test(garbage),
 114     };
 115 
 116     cmocka_set_message_output(CM_OUTPUT_TAP);
 117     return cmocka_run_group_tests(tests, NULL, NULL);
 118 }

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