root/lib/common/tests/lists/pcmk__subtract_lists_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. different_lists
  2. remove_first_item
  3. remove_middle_item
  4. remove_last_item
  5. remove_all_items
  6. 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 #include <crm/common/lists_internal.h>
  12 
  13 #include <stdarg.h>
  14 #include <stddef.h>
  15 #include <stdint.h>
  16 #include <stdlib.h>
  17 #include <string.h>
  18 #include <setjmp.h>
  19 #include <cmocka.h>
  20 
  21 #include <glib.h>
  22 
  23 static void
  24 different_lists(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26     GList *from = NULL;
  27     GList *items = NULL;
  28     GList *result = NULL;
  29 
  30     from = g_list_append(from, strdup("abc"));
  31     from = g_list_append(from, strdup("def"));
  32     from = g_list_append(from, strdup("ghi"));
  33 
  34     items = g_list_append(items, strdup("123"));
  35     items = g_list_append(items, strdup("456"));
  36 
  37     result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
  38 
  39     assert_int_equal(g_list_length(result), 3);
  40     assert_string_equal(g_list_nth_data(result, 0), "abc");
  41     assert_string_equal(g_list_nth_data(result, 1), "def");
  42     assert_string_equal(g_list_nth_data(result, 2), "ghi");
  43 
  44     g_list_free(result);
  45     g_list_free_full(from, free);
  46     g_list_free_full(items, free);
  47 }
  48 
  49 static void
  50 remove_first_item(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     GList *from = NULL;
  53     GList *items = NULL;
  54     GList *result = NULL;
  55 
  56     from = g_list_append(from, strdup("abc"));
  57     from = g_list_append(from, strdup("def"));
  58     from = g_list_append(from, strdup("ghi"));
  59 
  60     items = g_list_append(items, strdup("abc"));
  61 
  62     result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
  63 
  64     assert_int_equal(g_list_length(result), 2);
  65     assert_string_equal(g_list_nth_data(result, 0), "def");
  66     assert_string_equal(g_list_nth_data(result, 1), "ghi");
  67 
  68     g_list_free(result);
  69     g_list_free_full(from, free);
  70     g_list_free_full(items, free);
  71 }
  72 
  73 static void
  74 remove_middle_item(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76     GList *from = NULL;
  77     GList *items = NULL;
  78     GList *result = NULL;
  79 
  80     from = g_list_append(from, strdup("abc"));
  81     from = g_list_append(from, strdup("def"));
  82     from = g_list_append(from, strdup("ghi"));
  83 
  84     items = g_list_append(items, strdup("def"));
  85 
  86     result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
  87 
  88     assert_int_equal(g_list_length(result), 2);
  89     assert_string_equal(g_list_nth_data(result, 0), "abc");
  90     assert_string_equal(g_list_nth_data(result, 1), "ghi");
  91 
  92     g_list_free(result);
  93     g_list_free_full(from, free);
  94     g_list_free_full(items, free);
  95 }
  96 
  97 static void
  98 remove_last_item(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100     GList *from = NULL;
 101     GList *items = NULL;
 102     GList *result = NULL;
 103 
 104     from = g_list_append(from, strdup("abc"));
 105     from = g_list_append(from, strdup("def"));
 106     from = g_list_append(from, strdup("ghi"));
 107 
 108     items = g_list_append(items, strdup("ghi"));
 109 
 110     result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
 111 
 112     assert_int_equal(g_list_length(result), 2);
 113     assert_string_equal(g_list_nth_data(result, 0), "abc");
 114     assert_string_equal(g_list_nth_data(result, 1), "def");
 115 
 116     g_list_free(result);
 117     g_list_free_full(from, free);
 118     g_list_free_full(items, free);
 119 }
 120 
 121 static void
 122 remove_all_items(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124     GList *from = NULL;
 125     GList *items = NULL;
 126     GList *result = NULL;
 127 
 128     from = g_list_append(from, strdup("abc"));
 129     from = g_list_append(from, strdup("def"));
 130     from = g_list_append(from, strdup("ghi"));
 131 
 132     items = g_list_append(items, strdup("abc"));
 133     items = g_list_append(items, strdup("def"));
 134     items = g_list_append(items, strdup("ghi"));
 135 
 136     result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
 137 
 138     assert_int_equal(g_list_length(result), 0);
 139 
 140     g_list_free(result);
 141     g_list_free_full(from, free);
 142     g_list_free_full(items, free);
 143 }
 144 
 145 int
 146 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     const struct CMUnitTest tests[] = {
 149         cmocka_unit_test(different_lists),
 150         cmocka_unit_test(remove_first_item),
 151         cmocka_unit_test(remove_middle_item),
 152         cmocka_unit_test(remove_last_item),
 153         cmocka_unit_test(remove_all_items),
 154     };
 155 
 156     cmocka_set_message_output(CM_OUTPUT_TAP);
 157     return cmocka_run_group_tests(tests, NULL, NULL);
 158 }

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