pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__str_table_dup_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>
11 
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <setjmp.h>
16 #include <cmocka.h>
17 
18 #include <glib.h>
19 
20 static void
21 null_input_table(void **state)
22 {
23  assert_null(pcmk__str_table_dup(NULL));
24 }
25 
26 static void
27 empty_input_table(void **state)
28 {
29  GHashTable *tbl = pcmk__strkey_table(free, free);
30  GHashTable *copy = NULL;
31 
32  copy = pcmk__str_table_dup(tbl);
33  assert_int_equal(g_hash_table_size(copy), 0);
34 
35  g_hash_table_destroy(tbl);
36  g_hash_table_destroy(copy);
37 }
38 
39 static void
40 regular_input_table(void **state)
41 {
42  GHashTable *tbl = pcmk__strkey_table(free, free);
43  GHashTable *copy = NULL;
44 
45  g_hash_table_insert(tbl, strdup("abc"), strdup("123"));
46  g_hash_table_insert(tbl, strdup("def"), strdup("456"));
47  g_hash_table_insert(tbl, strdup("ghi"), strdup("789"));
48 
49  copy = pcmk__str_table_dup(tbl);
50  assert_int_equal(g_hash_table_size(copy), 3);
51 
52  assert_string_equal(g_hash_table_lookup(tbl, "abc"), "123");
53  assert_string_equal(g_hash_table_lookup(tbl, "def"), "456");
54  assert_string_equal(g_hash_table_lookup(tbl, "ghi"), "789");
55 
56  g_hash_table_destroy(tbl);
57  g_hash_table_destroy(copy);
58 }
59 
60 int main(int argc, char **argv) {
61  const struct CMUnitTest tests[] = {
62  cmocka_unit_test(null_input_table),
63  cmocka_unit_test(empty_input_table),
64  cmocka_unit_test(regular_input_table),
65  };
66 
67  cmocka_set_message_output(CM_OUTPUT_TAP);
68  return cmocka_run_group_tests(tests, NULL, NULL);
69 }
int main(int argc, char **argv)
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:611
GHashTable * pcmk__str_table_dup(GHashTable *old_table)
Definition: strings.c:675