root/lib/common/tests/rules/pcmk__cmp_by_type_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. null_compares_lesser
  2. invalid_compares_equal
  3. compare_string_type
  4. compare_integer_type
  5. compare_number_type
  6. compare_version_type

   1 /*
   2  * Copyright 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 <limits.h>                         // INT_MIN, INT_MAX
  13 
  14 #include <crm/common/util.h>                // crm_strdup_printf()
  15 #include <crm/common/rules_internal.h>
  16 #include <crm/common/unittest_internal.h>
  17 #include "crmcommon_private.h"
  18 
  19 static void
  20 null_compares_lesser(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22     assert_int_equal(pcmk__cmp_by_type(NULL, NULL, pcmk__type_string), 0);
  23     assert_true(pcmk__cmp_by_type("0", NULL, pcmk__type_integer) > 0);
  24     assert_true(pcmk__cmp_by_type(NULL, "0", pcmk__type_number) < 0);
  25 }
  26 
  27 static void
  28 invalid_compares_equal(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     assert_int_equal(pcmk__cmp_by_type("0", "1", pcmk__type_unknown), 0);
  31     assert_int_equal(pcmk__cmp_by_type("hi", "bye", pcmk__type_unknown), 0);
  32     assert_int_equal(pcmk__cmp_by_type("-1.0", "2.0", pcmk__type_unknown), 0);
  33 }
  34 
  35 static void
  36 compare_string_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38     assert_int_equal(pcmk__cmp_by_type("bye", "bye", pcmk__type_string), 0);
  39     assert_int_equal(pcmk__cmp_by_type("bye", "BYE", pcmk__type_string), 0);
  40     assert_true(pcmk__cmp_by_type("bye", "hello", pcmk__type_string) < 0);
  41     assert_true(pcmk__cmp_by_type("bye", "HELLO", pcmk__type_string) < 0);
  42     assert_true(pcmk__cmp_by_type("bye", "boo", pcmk__type_string) > 0);
  43     assert_true(pcmk__cmp_by_type("bye", "Boo", pcmk__type_string) > 0);
  44 }
  45 
  46 static void
  47 compare_integer_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49     char *int_min = crm_strdup_printf("%d", INT_MIN);
  50     char *int_max = crm_strdup_printf("%d", INT_MAX);
  51 
  52     assert_int_equal(pcmk__cmp_by_type("0", "0", pcmk__type_integer), 0);
  53     assert_true(pcmk__cmp_by_type("0", "1", pcmk__type_integer) < 0);
  54     assert_true(pcmk__cmp_by_type("1", "0", pcmk__type_integer) > 0);
  55     assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_integer) > 0);
  56     assert_true(pcmk__cmp_by_type(int_min, int_max, pcmk__type_integer) < 0);
  57     assert_true(pcmk__cmp_by_type(int_max, int_min, pcmk__type_integer) > 0);
  58     free(int_min);
  59     free(int_max);
  60 
  61     // Non-integers compare as strings
  62     assert_int_equal(pcmk__cmp_by_type("0", "x", pcmk__type_integer),
  63                      pcmk__cmp_by_type("0", "x", pcmk__type_string));
  64     assert_int_equal(pcmk__cmp_by_type("x", "0", pcmk__type_integer),
  65                      pcmk__cmp_by_type("x", "0", pcmk__type_string));
  66     assert_int_equal(pcmk__cmp_by_type("x", "X", pcmk__type_integer),
  67                      pcmk__cmp_by_type("x", "X", pcmk__type_string));
  68 }
  69 
  70 static void
  71 compare_number_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     assert_int_equal(pcmk__cmp_by_type("0", "0.0", pcmk__type_number), 0);
  74     assert_true(pcmk__cmp_by_type("0.345", "0.5", pcmk__type_number) < 0);
  75     assert_true(pcmk__cmp_by_type("5", "3.1", pcmk__type_number) > 0);
  76     assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_number) > 0);
  77 
  78     // Non-numbers compare as strings
  79     assert_int_equal(pcmk__cmp_by_type("0.0", "x", pcmk__type_number),
  80                      pcmk__cmp_by_type("0.0", "x", pcmk__type_string));
  81     assert_int_equal(pcmk__cmp_by_type("x", "0.0", pcmk__type_number),
  82                      pcmk__cmp_by_type("x", "0.0", pcmk__type_string));
  83     assert_int_equal(pcmk__cmp_by_type("x", "X", pcmk__type_number),
  84                      pcmk__cmp_by_type("x", "X", pcmk__type_string));
  85 }
  86 
  87 static void
  88 compare_version_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90     assert_int_equal(pcmk__cmp_by_type("1.0", "1.0", pcmk__type_version), 0);
  91     assert_true(pcmk__cmp_by_type("1.0.0", "1.0.1", pcmk__type_version) < 0);
  92     assert_true(pcmk__cmp_by_type("5.0", "3.1.15", pcmk__type_version) > 0);
  93     assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_version) > 0);
  94 }
  95 
  96 PCMK__UNIT_TEST(NULL, NULL,
  97                 cmocka_unit_test(null_compares_lesser),
  98                 cmocka_unit_test(invalid_compares_equal),
  99                 cmocka_unit_test(compare_string_type),
 100                 cmocka_unit_test(compare_integer_type),
 101                 cmocka_unit_test(compare_number_type),
 102                 cmocka_unit_test(compare_version_type))

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