root/lib/common/tests/xml/pcmk__xe_set_score_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. assert_set_score
  2. value_is_name_plus_plus
  3. value_is_name_plus_equals_integer
  4. target_is_NULL
  5. name_is_NULL
  6. value_is_NULL
  7. value_is_wrong_name
  8. value_is_only_an_integer
  9. variable_is_initialized_to_be_non_numeric
  10. variable_is_initialized_to_be_non_numeric_2
  11. variable_is_initialized_to_be_numeric_and_decimal_point_containing
  12. variable_is_initialized_to_be_numeric_and_decimal_point_containing_2
  13. variable_is_initialized_to_be_numeric_and_decimal_point_containing_3
  14. value_is_non_numeric
  15. value_is_numeric_and_decimal_point_containing
  16. value_is_numeric_and_decimal_point_containing_2
  17. value_is_numeric_and_decimal_point_containing_3
  18. name_is_undefined
  19. assignment_result_is_too_large

   1 /*
   2  * Copyright 2022-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 #include <glib.h>
  15 
  16 #include "crmcommon_private.h"  // pcmk__xe_set_score()
  17 
  18 /*!
  19  * \internal
  20  * \brief Update an XML attribute value and check it against a reference value
  21  *
  22  * The attribute name is hard-coded as \c "X".
  23  *
  24  * \param[in] initial        Initial value
  25  * \param[in] new            Value to set
  26  * \param[in] reference_val  Expected attribute value after update
  27  * \param[in] reference_rc   Expected return code from \c pcmk__xe_set_score()
  28  */
  29 static void
  30 assert_set_score(const char *initial, const char *new,
     /* [previous][next][first][last][top][bottom][index][help] */
  31                  const char *reference_val, int reference_rc)
  32 {
  33     const char *name = "X";
  34     xmlNode *test_xml = pcmk__xe_create(NULL, "test_xml");
  35 
  36     crm_xml_add(test_xml, name, initial);
  37     assert_int_equal(pcmk__xe_set_score(test_xml, name, new), reference_rc);
  38     assert_string_equal(crm_element_value(test_xml, name), reference_val);
  39 
  40     free_xml(test_xml);
  41 }
  42 
  43 static void
  44 value_is_name_plus_plus(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46     assert_set_score("5", "X++", "6", pcmk_rc_ok);
  47 }
  48 
  49 static void
  50 value_is_name_plus_equals_integer(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     assert_set_score("5", "X+=2", "7", pcmk_rc_ok);
  53 }
  54 
  55 // NULL input
  56 
  57 static void
  58 target_is_NULL(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     // Dumps core via CRM_CHECK()
  61     assert_int_equal(pcmk__xe_set_score(NULL, "X", "X++"), EINVAL);
  62 }
  63 
  64 static void
  65 name_is_NULL(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     xmlNode *test_xml = pcmk__xe_create(NULL, "test_xml");
  68 
  69     crm_xml_add(test_xml, "X", "5");
  70 
  71     // Dumps core via CRM_CHECK()
  72     assert_int_equal(pcmk__xe_set_score(test_xml, NULL, "X++"), EINVAL);
  73     assert_string_equal(crm_element_value(test_xml, "X"), "5");
  74 
  75     free_xml(test_xml);
  76 }
  77 
  78 static void
  79 value_is_NULL(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     assert_set_score("5", NULL, "5", pcmk_rc_ok);
  82 }
  83 
  84 // the value input doesn't start with the name input
  85 
  86 static void
  87 value_is_wrong_name(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     assert_set_score("5", "Y++", "Y++", pcmk_rc_ok);
  90 }
  91 
  92 static void
  93 value_is_only_an_integer(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  94 {
  95     assert_set_score("5", "2", "2", pcmk_rc_ok);
  96 }
  97 
  98 // non-integers
  99 
 100 static void
 101 variable_is_initialized_to_be_non_numeric(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     assert_set_score("hello", "X++", "1", pcmk_rc_ok);
 104 }
 105 
 106 static void
 107 variable_is_initialized_to_be_non_numeric_2(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109     assert_set_score("hello", "X+=2", "2", pcmk_rc_ok);
 110 }
 111 
 112 static void
 113 variable_is_initialized_to_be_numeric_and_decimal_point_containing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115     assert_set_score("5.01", "X++", "6", pcmk_rc_ok);
 116 }
 117 
 118 static void
 119 variable_is_initialized_to_be_numeric_and_decimal_point_containing_2(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121     assert_set_score("5.50", "X++", "6", pcmk_rc_ok);
 122 }
 123 
 124 static void
 125 variable_is_initialized_to_be_numeric_and_decimal_point_containing_3(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127     assert_set_score("5.99", "X++", "6", pcmk_rc_ok);
 128 }
 129 
 130 static void
 131 value_is_non_numeric(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133     assert_set_score("5", "X+=hello", "5", pcmk_rc_ok);
 134 }
 135 
 136 static void
 137 value_is_numeric_and_decimal_point_containing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139     assert_set_score("5", "X+=2.01", "7", pcmk_rc_ok);
 140 }
 141 
 142 static void
 143 value_is_numeric_and_decimal_point_containing_2(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145     assert_set_score("5", "X+=1.50", "6", pcmk_rc_ok);
 146 }
 147 
 148 static void
 149 value_is_numeric_and_decimal_point_containing_3(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151     assert_set_score("5", "X+=1.99", "6", pcmk_rc_ok);
 152 }
 153 
 154 // undefined input
 155 
 156 static void
 157 name_is_undefined(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159     assert_set_score(NULL, "X++", "X++", pcmk_rc_ok);
 160 }
 161 
 162 // large input
 163 
 164 static void
 165 assignment_result_is_too_large(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167     assert_set_score("5", "X+=100000000000", "1000000", pcmk_rc_ok);
 168 }
 169 
 170 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
 171                 cmocka_unit_test(value_is_name_plus_plus),
 172                 cmocka_unit_test(value_is_name_plus_equals_integer),
 173                 cmocka_unit_test(target_is_NULL),
 174                 cmocka_unit_test(name_is_NULL),
 175                 cmocka_unit_test(value_is_NULL),
 176                 cmocka_unit_test(value_is_wrong_name),
 177                 cmocka_unit_test(value_is_only_an_integer),
 178                 cmocka_unit_test(variable_is_initialized_to_be_non_numeric),
 179                 cmocka_unit_test(variable_is_initialized_to_be_non_numeric_2),
 180                 cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing),
 181                 cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_2),
 182                 cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_3),
 183                 cmocka_unit_test(value_is_non_numeric),
 184                 cmocka_unit_test(value_is_numeric_and_decimal_point_containing),
 185                 cmocka_unit_test(value_is_numeric_and_decimal_point_containing_2),
 186                 cmocka_unit_test(value_is_numeric_and_decimal_point_containing_3),
 187                 cmocka_unit_test(name_is_undefined),
 188                 cmocka_unit_test(assignment_result_is_too_large))

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