pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__subtract_lists_test.c
Go to the documentation of this file.
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>
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)
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)
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)
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)
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)
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)
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 }
int main(int argc, char **argv)
GList * pcmk__subtract_lists(GList *from, GList *items, GCompareFunc cmp)
Definition: lists.c:14
pcmk__action_result_t result
Definition: pcmk_fence.c:34