pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__guint_from_hash_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022-2024 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 
13 
14 #include <glib.h>
15 
16 static void
17 null_args(void **state)
18 {
19  GHashTable *tbl = pcmk__strkey_table(free, free);
20  guint result;
21 
22  assert_int_equal(pcmk__guint_from_hash(NULL, "abc", 123, &result), EINVAL);
23  assert_int_equal(pcmk__guint_from_hash(tbl, NULL, 123, &result), EINVAL);
24 
25  g_hash_table_destroy(tbl);
26 }
27 
28 static void
29 missing_key(void **state)
30 {
31  GHashTable *tbl = pcmk__strkey_table(free, free);
32  guint result;
33 
34  assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 123, &result), pcmk_rc_ok);
35  assert_int_equal(result, 123);
36 
37  g_hash_table_destroy(tbl);
38 }
39 
40 static void
41 standard_usage(void **state)
42 {
43  GHashTable *tbl = pcmk__strkey_table(free, free);
44  guint result;
45 
46  g_hash_table_insert(tbl, strdup("abc"), strdup("123"));
47 
48  assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 456, &result), pcmk_rc_ok);
49  assert_int_equal(result, 123);
50 
51  g_hash_table_destroy(tbl);
52 }
53 
54 static void
55 conversion_errors(void **state)
56 {
57  GHashTable *tbl = pcmk__strkey_table(free, free);
58  guint result;
59 
60  g_hash_table_insert(tbl, strdup("negative"), strdup("-3"));
61  g_hash_table_insert(tbl, strdup("toobig"), strdup("20000000000000000"));
62  g_hash_table_insert(tbl, strdup("baddata"), strdup("asdf"));
63 
64  assert_int_equal(pcmk__guint_from_hash(tbl, "negative", 456, &result), ERANGE);
65  assert_int_equal(result, 456);
66 
67  assert_int_equal(pcmk__guint_from_hash(tbl, "toobig", 456, &result), ERANGE);
68  assert_int_equal(result, 456);
69 
70  assert_int_equal(pcmk__guint_from_hash(tbl, "baddata", 456, &result), EINVAL);
71  assert_int_equal(result, 456);
72 
73  g_hash_table_destroy(tbl);
74 }
75 
76 PCMK__UNIT_TEST(NULL, NULL,
77  cmocka_unit_test(null_args),
78  cmocka_unit_test(missing_key),
79  cmocka_unit_test(standard_usage),
80  cmocka_unit_test(conversion_errors))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition: strings.c:313
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:695
pcmk__action_result_t result
Definition: pcmk_fence.c:35