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

   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 
  12 #include <crm/common/unittest_internal.h>
  13 
  14 #include <glib.h>
  15 
  16 static void
  17 null_args(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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))

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