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

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

DEFINITIONS

This source file includes following definitions.
  1. bad_input
  2. is_true
  3. is_not_true
  4. is_false
  5. is_not_false

   1 /*
   2  * Copyright 2021-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 <crm/common/unittest_internal.h>
  13 
  14 static void
  15 bad_input(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  16     assert_int_equal(crm_str_to_boolean(NULL, NULL), -1);
  17     assert_int_equal(crm_str_to_boolean("", NULL), -1);
  18     assert_int_equal(crm_str_to_boolean("blahblah", NULL), -1);
  19 }
  20 
  21 static void
  22 is_true(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  23     int ret;
  24 
  25     assert_int_equal(crm_str_to_boolean("true", &ret), 1);
  26     assert_true(ret);
  27     assert_int_equal(crm_str_to_boolean("TrUe", &ret), 1);
  28     assert_true(ret);
  29     assert_int_equal(crm_str_to_boolean("on", &ret), 1);
  30     assert_true(ret);
  31     assert_int_equal(crm_str_to_boolean("ON", &ret), 1);
  32     assert_true(ret);
  33     assert_int_equal(crm_str_to_boolean("yes", &ret), 1);
  34     assert_true(ret);
  35     assert_int_equal(crm_str_to_boolean("yES", &ret), 1);
  36     assert_true(ret);
  37     assert_int_equal(crm_str_to_boolean("y", &ret), 1);
  38     assert_true(ret);
  39     assert_int_equal(crm_str_to_boolean("Y", &ret), 1);
  40     assert_true(ret);
  41     assert_int_equal(crm_str_to_boolean("1", &ret), 1);
  42     assert_true(ret);
  43 
  44     // Ensure it still validates the string with a NULL result argument
  45     assert_int_equal(crm_str_to_boolean("true", NULL), 1);
  46     assert_int_equal(crm_str_to_boolean("on", NULL), 1);
  47     assert_int_equal(crm_str_to_boolean("yes", NULL), 1);
  48     assert_int_equal(crm_str_to_boolean("y", NULL), 1);
  49     assert_int_equal(crm_str_to_boolean("1", NULL), 1);
  50 }
  51 
  52 static void
  53 is_not_true(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  54     assert_int_equal(crm_str_to_boolean("truedat", NULL), -1);
  55     assert_int_equal(crm_str_to_boolean("onnn", NULL), -1);
  56     assert_int_equal(crm_str_to_boolean("yep", NULL), -1);
  57     assert_int_equal(crm_str_to_boolean("Y!", NULL), -1);
  58     assert_int_equal(crm_str_to_boolean("100", NULL), -1);
  59 }
  60 
  61 static void
  62 is_false(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  63     int ret;
  64 
  65     assert_int_equal(crm_str_to_boolean("false", &ret), 1);
  66     assert_false(ret);
  67     assert_int_equal(crm_str_to_boolean("fAlSe", &ret), 1);
  68     assert_false(ret);
  69     assert_int_equal(crm_str_to_boolean("off", &ret), 1);
  70     assert_false(ret);
  71     assert_int_equal(crm_str_to_boolean("OFF", &ret), 1);
  72     assert_false(ret);
  73     assert_int_equal(crm_str_to_boolean("no", &ret), 1);
  74     assert_false(ret);
  75     assert_int_equal(crm_str_to_boolean("No", &ret), 1);
  76     assert_false(ret);
  77     assert_int_equal(crm_str_to_boolean("n", &ret), 1);
  78     assert_false(ret);
  79     assert_int_equal(crm_str_to_boolean("N", &ret), 1);
  80     assert_false(ret);
  81     assert_int_equal(crm_str_to_boolean("0", &ret), 1);
  82     assert_false(ret);
  83 
  84     // Ensure it still validates the string with a NULL result argument
  85     assert_int_equal(crm_str_to_boolean("false", NULL), 1);
  86     assert_int_equal(crm_str_to_boolean("off", NULL), 1);
  87     assert_int_equal(crm_str_to_boolean("no", NULL), 1);
  88     assert_int_equal(crm_str_to_boolean("n", NULL), 1);
  89     assert_int_equal(crm_str_to_boolean("0", NULL), 1);
  90 }
  91 
  92 static void
  93 is_not_false(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  94     assert_int_equal(crm_str_to_boolean("falseee", NULL), -1);
  95     assert_int_equal(crm_str_to_boolean("of", NULL), -1);
  96     assert_int_equal(crm_str_to_boolean("nope", NULL), -1);
  97     assert_int_equal(crm_str_to_boolean("N!", NULL), -1);
  98     assert_int_equal(crm_str_to_boolean("000", NULL), -1);
  99 }
 100 
 101 PCMK__UNIT_TEST(NULL, NULL,
 102                 cmocka_unit_test(bad_input),
 103                 cmocka_unit_test(is_true),
 104                 cmocka_unit_test(is_not_true),
 105                 cmocka_unit_test(is_false),
 106                 cmocka_unit_test(is_not_false))

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