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. null_input_variables
  3. missing_separator
  4. only_separator
  5. no_range_end
  6. no_range_start
  7. range_start_and_end
  8. garbage
  9. strtoll_errors

   1 /*
   2  * Copyright 2020-2023 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/common/results.h"
  11 #include <crm_internal.h>
  12 
  13 #include <crm/common/unittest_internal.h>
  14 
  15 static void
  16 empty_input_string(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  17 {
  18     long long start, end;
  19 
  20     assert_int_equal(pcmk__parse_ll_range(NULL, &start, &end), ENODATA);
  21     assert_int_equal(pcmk__parse_ll_range("", &start, &end), ENODATA);
  22 }
  23 
  24 static void
  25 null_input_variables(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27     long long start, end;
  28 
  29     pcmk__assert_asserts(pcmk__parse_ll_range("1234", NULL, &end));
  30     pcmk__assert_asserts(pcmk__parse_ll_range("1234", &start, NULL));
  31 }
  32 
  33 static void
  34 missing_separator(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36     long long start, end;
  37 
  38     assert_int_equal(pcmk__parse_ll_range("1234", &start, &end), pcmk_rc_ok);
  39     assert_int_equal(start, 1234);
  40     assert_int_equal(end, 1234);
  41 }
  42 
  43 static void
  44 only_separator(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46     long long start, end;
  47 
  48     assert_int_equal(pcmk__parse_ll_range("-", &start, &end), pcmk_rc_bad_input);
  49     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  50     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  51 }
  52 
  53 static void
  54 no_range_end(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56     long long start, end;
  57 
  58     assert_int_equal(pcmk__parse_ll_range("2000-", &start, &end), pcmk_rc_ok);
  59     assert_int_equal(start, 2000);
  60     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  61 }
  62 
  63 static void
  64 no_range_start(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66     long long start, end;
  67 
  68     assert_int_equal(pcmk__parse_ll_range("-2020", &start, &end), pcmk_rc_ok);
  69     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  70     assert_int_equal(end, 2020);
  71 }
  72 
  73 static void
  74 range_start_and_end(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76     long long start, end;
  77 
  78     assert_int_equal(pcmk__parse_ll_range("2000-2020", &start, &end), pcmk_rc_ok);
  79     assert_int_equal(start, 2000);
  80     assert_int_equal(end, 2020);
  81 
  82     assert_int_equal(pcmk__parse_ll_range("2000-2020-2030", &start, &end), pcmk_rc_bad_input);
  83 }
  84 
  85 static void
  86 garbage(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     long long start, end;
  89 
  90     assert_int_equal(pcmk__parse_ll_range("2000x-", &start, &end), pcmk_rc_bad_input);
  91     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  92     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  93 
  94     assert_int_equal(pcmk__parse_ll_range("-x2000", &start, &end), pcmk_rc_bad_input);
  95     assert_int_equal(start, PCMK__PARSE_INT_DEFAULT);
  96     assert_int_equal(end, PCMK__PARSE_INT_DEFAULT);
  97 }
  98 
  99 static void
 100 strtoll_errors(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102     long long start, end;
 103 
 104     assert_int_equal(pcmk__parse_ll_range("20000000000000000000-", &start, &end), EOVERFLOW);
 105     assert_int_equal(pcmk__parse_ll_range("100-20000000000000000000", &start, &end), EOVERFLOW);
 106 }
 107 
 108 PCMK__UNIT_TEST(NULL, NULL,
 109                 cmocka_unit_test(empty_input_string),
 110                 cmocka_unit_test(null_input_variables),
 111                 cmocka_unit_test(missing_separator),
 112                 cmocka_unit_test(only_separator),
 113                 cmocka_unit_test(no_range_end),
 114                 cmocka_unit_test(no_range_start),
 115                 cmocka_unit_test(range_start_and_end),
 116                 cmocka_unit_test(strtoll_errors),
 117                 cmocka_unit_test(garbage))

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