pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
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 General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
14
15#include <glib.h>
16
17static void
18different_lists(void **state)
19{
20 GList *from = NULL;
21 GList *items = NULL;
22 GList *result = NULL;
23
24 from = g_list_append(from, strdup("abc"));
25 from = g_list_append(from, strdup("def"));
26 from = g_list_append(from, strdup("ghi"));
27
28 items = g_list_append(items, strdup("123"));
29 items = g_list_append(items, strdup("456"));
30
31 result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
32
33 assert_int_equal(g_list_length(result), 3);
34 assert_string_equal(g_list_nth_data(result, 0), "abc");
35 assert_string_equal(g_list_nth_data(result, 1), "def");
36 assert_string_equal(g_list_nth_data(result, 2), "ghi");
37
38 g_list_free(result);
39 g_list_free_full(from, free);
40 g_list_free_full(items, free);
41}
42
43static void
44remove_first_item(void **state)
45{
46 GList *from = NULL;
47 GList *items = NULL;
48 GList *result = NULL;
49
50 from = g_list_append(from, strdup("abc"));
51 from = g_list_append(from, strdup("def"));
52 from = g_list_append(from, strdup("ghi"));
53
54 items = g_list_append(items, strdup("abc"));
55
56 result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
57
58 assert_int_equal(g_list_length(result), 2);
59 assert_string_equal(g_list_nth_data(result, 0), "def");
60 assert_string_equal(g_list_nth_data(result, 1), "ghi");
61
62 g_list_free(result);
63 g_list_free_full(from, free);
64 g_list_free_full(items, free);
65}
66
67static void
68remove_middle_item(void **state)
69{
70 GList *from = NULL;
71 GList *items = NULL;
72 GList *result = NULL;
73
74 from = g_list_append(from, strdup("abc"));
75 from = g_list_append(from, strdup("def"));
76 from = g_list_append(from, strdup("ghi"));
77
78 items = g_list_append(items, strdup("def"));
79
80 result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
81
82 assert_int_equal(g_list_length(result), 2);
83 assert_string_equal(g_list_nth_data(result, 0), "abc");
84 assert_string_equal(g_list_nth_data(result, 1), "ghi");
85
86 g_list_free(result);
87 g_list_free_full(from, free);
88 g_list_free_full(items, free);
89}
90
91static void
92remove_last_item(void **state)
93{
94 GList *from = NULL;
95 GList *items = NULL;
96 GList *result = NULL;
97
98 from = g_list_append(from, strdup("abc"));
99 from = g_list_append(from, strdup("def"));
100 from = g_list_append(from, strdup("ghi"));
101
102 items = g_list_append(items, strdup("ghi"));
103
104 result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
105
106 assert_int_equal(g_list_length(result), 2);
107 assert_string_equal(g_list_nth_data(result, 0), "abc");
108 assert_string_equal(g_list_nth_data(result, 1), "def");
109
110 g_list_free(result);
111 g_list_free_full(from, free);
112 g_list_free_full(items, free);
113}
114
115static void
116remove_all_items(void **state)
117{
118 GList *from = NULL;
119 GList *items = NULL;
120 GList *result = NULL;
121
122 from = g_list_append(from, strdup("abc"));
123 from = g_list_append(from, strdup("def"));
124 from = g_list_append(from, strdup("ghi"));
125
126 items = g_list_append(items, strdup("abc"));
127 items = g_list_append(items, strdup("def"));
128 items = g_list_append(items, strdup("ghi"));
129
130 result = pcmk__subtract_lists(from, items, (GCompareFunc) strcmp);
131
132 assert_int_equal(g_list_length(result), 0);
133
134 g_list_free(result);
135 g_list_free_full(from, free);
136 g_list_free_full(items, free);
137}
138
139PCMK__UNIT_TEST(NULL, NULL,
140 cmocka_unit_test(different_lists),
141 cmocka_unit_test(remove_first_item),
142 cmocka_unit_test(remove_middle_item),
143 cmocka_unit_test(remove_last_item),
144 cmocka_unit_test(remove_all_items))
GList * pcmk__subtract_lists(GList *from, const GList *items, GCompareFunc cmp)
Definition lists.c:14
pcmk__action_result_t result
Definition pcmk_fence.c:37
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)