pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__guint_from_hash_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_args(void **state)
22 {
23  GHashTable *tbl = pcmk__strkey_table(free, free);
24  guint result;
25 
26  assert_int_equal(pcmk__guint_from_hash(NULL, "abc", 123, &result), EINVAL);
27  assert_int_equal(pcmk__guint_from_hash(tbl, NULL, 123, &result), EINVAL);
28 
29  g_hash_table_destroy(tbl);
30 }
31 
32 static void
33 missing_key(void **state)
34 {
35  GHashTable *tbl = pcmk__strkey_table(free, free);
36  guint result;
37 
38  assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 123, &result), pcmk_rc_ok);
39  assert_int_equal(result, 123);
40 
41  g_hash_table_destroy(tbl);
42 }
43 
44 static void
45 standard_usage(void **state)
46 {
47  GHashTable *tbl = pcmk__strkey_table(free, free);
48  guint result;
49 
50  g_hash_table_insert(tbl, strdup("abc"), strdup("123"));
51 
52  assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 456, &result), pcmk_rc_ok);
53  assert_int_equal(result, 123);
54 
55  g_hash_table_destroy(tbl);
56 }
57 
58 static void
59 conversion_errors(void **state)
60 {
61  GHashTable *tbl = pcmk__strkey_table(free, free);
62  guint result;
63 
64  g_hash_table_insert(tbl, strdup("negative"), strdup("-3"));
65  g_hash_table_insert(tbl, strdup("toobig"), strdup("20000000000000000"));
66 
67  assert_int_equal(pcmk__guint_from_hash(tbl, "negative", 456, &result), ERANGE);
68  assert_int_equal(result, 456);
69 
70  assert_int_equal(pcmk__guint_from_hash(tbl, "toobig", 456, &result), ERANGE);
71  assert_int_equal(result, 456);
72 
73  g_hash_table_destroy(tbl);
74 }
75 
76 int main(int argc, char **argv) {
77  const struct CMUnitTest tests[] = {
78  cmocka_unit_test(null_args),
79  cmocka_unit_test(missing_key),
80  cmocka_unit_test(standard_usage),
81  cmocka_unit_test(conversion_errors),
82  };
83 
84  cmocka_set_message_output(CM_OUTPUT_TAP);
85  return cmocka_run_group_tests(tests, NULL, NULL);
86 }
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition: strings.c:311
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:611
pcmk__action_result_t result
Definition: pcmk_fence.c:34
int main(int argc, char **argv)