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. no_numbers
  2. trailing_numbers
  3. middle_numbers
  4. unequal_lengths
  5. main

   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 <stdarg.h>
  13 #include <stddef.h>
  14 #include <stdint.h>
  15 #include <setjmp.h>
  16 #include <cmocka.h>
  17 
  18 static void
  19 no_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21     /* All comparisons are done case-insensitively. */
  22     assert_int_equal(pcmk__numeric_strcasecmp("abcd", "efgh"), -1);
  23     assert_int_equal(pcmk__numeric_strcasecmp("abcd", "abcd"), 0);
  24     assert_int_equal(pcmk__numeric_strcasecmp("efgh", "abcd"), 1);
  25 
  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 
  31 static void
  32 trailing_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node2"), -1);
  35     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node1"), 0);
  36     assert_int_equal(pcmk__numeric_strcasecmp("node2", "node1"), 1);
  37 
  38     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node10"), -1);
  39     assert_int_equal(pcmk__numeric_strcasecmp("node10", "node10"), 0);
  40     assert_int_equal(pcmk__numeric_strcasecmp("node10", "node1"), 1);
  41 
  42     assert_int_equal(pcmk__numeric_strcasecmp("node10", "remotenode9"), -1);
  43     assert_int_equal(pcmk__numeric_strcasecmp("remotenode9", "node10"), 1);
  44 
  45     /* Longer numbers sort higher than shorter numbers. */
  46     assert_int_equal(pcmk__numeric_strcasecmp("node001", "node1"), 1);
  47     assert_int_equal(pcmk__numeric_strcasecmp("node1", "node001"), -1);
  48 }
  49 
  50 static void
  51 middle_numbers(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1def"), -1);
  54     assert_int_equal(pcmk__numeric_strcasecmp("node1def", "node1abc"), 1);
  55 
  56     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node2abc"), -1);
  57     assert_int_equal(pcmk__numeric_strcasecmp("node2abc", "node1abc"), 1);
  58 }
  59 
  60 static void
  61 unequal_lengths(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63     assert_int_equal(pcmk__numeric_strcasecmp("node-ab", "node-abc"), -1);
  64     assert_int_equal(pcmk__numeric_strcasecmp("node-abc", "node-ab"), 1);
  65 
  66     assert_int_equal(pcmk__numeric_strcasecmp("node1ab", "node1abc"), -1);
  67     assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1ab"), 1);
  68 }
  69 
  70 int
  71 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     const struct CMUnitTest tests[] = {
  74         cmocka_unit_test(no_numbers),
  75         cmocka_unit_test(trailing_numbers),
  76         cmocka_unit_test(middle_numbers),
  77         cmocka_unit_test(unequal_lengths),
  78     };
  79 
  80     cmocka_set_message_output(CM_OUTPUT_TAP);
  81     return cmocka_run_group_tests(tests, NULL, NULL);
  82 }

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