root/lib/common/tests/strings/pcmk__guint_from_hash_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. null_args
  2. missing_key
  3. standard_usage
  4. conversion_errors
  5. main

   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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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 }

/* [previous][next][first][last][top][bottom][index][help] */