root/lib/common/tests/scores/char2score_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input
  2. bad_input
  3. special_values
  4. outside_limits
  5. inside_limits

   1 /*
   2  * Copyright 2022 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 <crm/common/unittest_internal.h>
  13 
  14 extern int pcmk__score_red;
  15 extern int pcmk__score_green;
  16 extern int pcmk__score_yellow;
  17 
  18 static void
  19 empty_input(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21     assert_int_equal(char2score(NULL), 0);
  22 }
  23 
  24 static void
  25 bad_input(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27     assert_int_equal(char2score("PQRST"), 0);
  28     assert_int_equal(char2score("3.141592"), 3);
  29     assert_int_equal(char2score("0xf00d"), 0);
  30 }
  31 
  32 static void
  33 special_values(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35     assert_int_equal(char2score("-INFINITY"), -CRM_SCORE_INFINITY);
  36     assert_int_equal(char2score("INFINITY"), CRM_SCORE_INFINITY);
  37     assert_int_equal(char2score("+INFINITY"), CRM_SCORE_INFINITY);
  38 
  39     pcmk__score_red = 10;
  40     pcmk__score_green = 20;
  41     pcmk__score_yellow = 30;
  42 
  43     assert_int_equal(char2score("red"), pcmk__score_red);
  44     assert_int_equal(char2score("green"), pcmk__score_green);
  45     assert_int_equal(char2score("yellow"), pcmk__score_yellow);
  46 
  47     assert_int_equal(char2score("ReD"), pcmk__score_red);
  48     assert_int_equal(char2score("GrEeN"), pcmk__score_green);
  49     assert_int_equal(char2score("yElLoW"), pcmk__score_yellow);
  50 }
  51 
  52 /* These ridiculous macros turn an integer constant into a string constant. */
  53 #define A(x) #x
  54 #define B(x) A(x)
  55 
  56 static void
  57 outside_limits(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59     assert_int_equal(char2score(B(CRM_SCORE_INFINITY) "00"), CRM_SCORE_INFINITY);
  60     assert_int_equal(char2score("-" B(CRM_SCORE_INFINITY) "00"), -CRM_SCORE_INFINITY);
  61 }
  62 
  63 static void
  64 inside_limits(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66     assert_int_equal(char2score("1234"), 1234);
  67     assert_int_equal(char2score("-1234"), -1234);
  68 }
  69 
  70 PCMK__UNIT_TEST(NULL, NULL,
  71                 cmocka_unit_test(empty_input),
  72                 cmocka_unit_test(bad_input),
  73                 cmocka_unit_test(special_values),
  74                 cmocka_unit_test(outside_limits),
  75                 cmocka_unit_test(inside_limits))

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