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

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

DEFINITIONS

This source file includes following definitions.
  1. same_pointer
  2. one_is_null
  3. case_matters
  4. case_insensitive
  5. regex
  6. main

   1 /*
   2  * Copyright 2020-2021 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 <stdio.h>
  13 #include <stdbool.h>
  14 #include <glib.h>
  15 
  16 static void
  17 same_pointer(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  18     const char *s1 = "abcd";
  19     const char *s2 = "wxyz";
  20 
  21     g_assert_cmpint(pcmk__strcmp(s1, s1, pcmk__str_none), ==, 0);
  22     g_assert_true(pcmk__str_eq(s1, s1, pcmk__str_none));
  23     g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_none), !=, 0);
  24     g_assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
  25     g_assert_cmpint(pcmk__strcmp(NULL, NULL, pcmk__str_none), ==, 0);
  26 }
  27 
  28 static void
  29 one_is_null(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  30     const char *s1 = "abcd";
  31 
  32     g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_null_matches), ==, 0);
  33     g_assert_true(pcmk__str_eq(s1, NULL, pcmk__str_null_matches));
  34     g_assert_cmpint(pcmk__strcmp(NULL, s1, pcmk__str_null_matches), ==, 0);
  35     g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_none), >, 0);
  36     g_assert_false(pcmk__str_eq(s1, NULL, pcmk__str_none));
  37     g_assert_cmpint(pcmk__strcmp(NULL, s1, pcmk__str_none), <, 0);
  38 }
  39 
  40 static void
  41 case_matters(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  42     const char *s1 = "abcd";
  43     const char *s2 = "ABCD";
  44 
  45     g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_none), >, 0);
  46     g_assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
  47     g_assert_cmpint(pcmk__strcmp(s2, s1, pcmk__str_none), <, 0);
  48 }
  49 
  50 static void
  51 case_insensitive(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  52     const char *s1 = "abcd";
  53     const char *s2 = "ABCD";
  54 
  55     g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_casei), ==, 0);
  56     g_assert_true(pcmk__str_eq(s1, s2, pcmk__str_casei));
  57 }
  58 
  59 static void
  60 regex(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  61     const char *s1 = "abcd";
  62     const char *s2 = "ABCD";
  63 
  64     g_assert_cmpint(pcmk__strcmp(NULL, "a..d", pcmk__str_regex), ==, 1);
  65     g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_regex), ==, 1);
  66     g_assert_cmpint(pcmk__strcmp(s1, "a..d", pcmk__str_regex), ==, 0);
  67     g_assert_true(pcmk__str_eq(s1, "a..d", pcmk__str_regex));
  68     g_assert_cmpint(pcmk__strcmp(s1, "xxyy", pcmk__str_regex), !=, 0);
  69     g_assert_false(pcmk__str_eq(s1, "xxyy", pcmk__str_regex));
  70     g_assert_cmpint(pcmk__strcmp(s2, "a..d", pcmk__str_regex|pcmk__str_casei), ==, 0);
  71     g_assert_true(pcmk__str_eq(s2, "a..d", pcmk__str_regex|pcmk__str_casei));
  72     g_assert_cmpint(pcmk__strcmp(s2, "a..d", pcmk__str_regex), !=, 0);
  73     g_assert_false(pcmk__str_eq(s2, "a..d", pcmk__str_regex));
  74     g_assert_cmpint(pcmk__strcmp(s2, "*ab", pcmk__str_regex), ==, 1);
  75 }
  76 
  77 int main(int argc, char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  78     g_test_init(&argc, &argv, NULL);
  79 
  80     g_test_add_func("/common/strings/strcmp/same_pointer", same_pointer);
  81     g_test_add_func("/common/strings/strcmp/one_is_null", one_is_null);
  82     g_test_add_func("/common/strings/strcmp/case_matters", case_matters);
  83     g_test_add_func("/common/strings/strcmp/case_insensitive", case_insensitive);
  84     g_test_add_func("/common/strings/strcmp/regex", regex);
  85 
  86     return g_test_run();
  87 }

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