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

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

DEFINITIONS

This source file includes following definitions.
  1. null_ptr
  2. no_numbers
  3. trailing_numbers
  4. middle_numbers
  5. unequal_lengths

   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 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 null_ptr(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  16 {
  17     pcmk__assert_asserts(pcmk__numeric_strcasecmp(NULL, NULL));
  18     pcmk__assert_asserts(pcmk__numeric_strcasecmp("a", NULL));
  19     pcmk__assert_asserts(pcmk__numeric_strcasecmp(NULL, "a"));
  20 }
  21 
  22 static void
  23 no_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  24 {
  25     /* All comparisons are done case-insensitively. */
  26     assert_int_equal(pcmk__numeric_strcasecmp("abcd", "efgh"), -1);
  27     assert_int_equal(pcmk__numeric_strcasecmp("abcd", "abcd"), 0);
  28     assert_int_equal(pcmk__numeric_strcasecmp("efgh", "abcd"), 1);
  29 
  30     assert_int_equal(pcmk__numeric_strcasecmp("AbCd", "eFgH"), -1);
  31     assert_int_equal(pcmk__numeric_strcasecmp("ABCD", "abcd"), 0);
  32     assert_int_equal(pcmk__numeric_strcasecmp("EFgh", "ABcd"), 1);
  33 }
  34 
  35 static void
  36 trailing_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node2"), -1);
  39     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node1"), 0);
  40     assert_int_equal(pcmk__numeric_strcasecmp("node2", "node1"), 1);
  41 
  42     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node10"), -1);
  43     assert_int_equal(pcmk__numeric_strcasecmp("node10", "node10"), 0);
  44     assert_int_equal(pcmk__numeric_strcasecmp("node10", "node1"), 1);
  45 
  46     assert_int_equal(pcmk__numeric_strcasecmp("node10", "remotenode9"), -1);
  47     assert_int_equal(pcmk__numeric_strcasecmp("remotenode9", "node10"), 1);
  48 
  49     /* Longer numbers sort higher than shorter numbers. */
  50     assert_int_equal(pcmk__numeric_strcasecmp("node001", "node1"), 1);
  51     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node001"), -1);
  52 }
  53 
  54 static void
  55 middle_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1def"), -1);
  58     assert_int_equal(pcmk__numeric_strcasecmp("node1def", "node1abc"), 1);
  59 
  60     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node2abc"), -1);
  61     assert_int_equal(pcmk__numeric_strcasecmp("node2abc", "node1abc"), 1);
  62 }
  63 
  64 static void
  65 unequal_lengths(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     assert_int_equal(pcmk__numeric_strcasecmp("node-ab", "node-abc"), -1);
  68     assert_int_equal(pcmk__numeric_strcasecmp("node-abc", "node-ab"), 1);
  69 
  70     assert_int_equal(pcmk__numeric_strcasecmp("node1ab", "node1abc"), -1);
  71     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1ab"), 1);
  72 }
  73 
  74 PCMK__UNIT_TEST(NULL, NULL,
  75                 cmocka_unit_test(null_ptr),
  76                 cmocka_unit_test(no_numbers),
  77                 cmocka_unit_test(trailing_numbers),
  78                 cmocka_unit_test(middle_numbers),
  79                 cmocka_unit_test(unequal_lengths))

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