Debian Codegraph @

subprojects/libglnx/tests/test-libglnx-macros.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2017 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include "libglnx-config.h"
23 #include "libglnx.h"
24 #include <glib.h>
25 #include <stdlib.h>
26 #include <gio/gio.h>
27 #include <string.h>
28
29 static void
30 test_info (void)
31 {
32 g_info ("hello, world");
33 g_info ("answer=%d", 42);
34 }
35
36 static void
37 test_inset (void)
38 {
39 g_assert (G_IN_SET (7, 7));
40 g_assert (G_IN_SET (7, 42, 7));
41 g_assert (G_IN_SET (7, 7,42,3,9));
42 g_assert (G_IN_SET (42, 7,42,3,9));
43 g_assert (G_IN_SET (3, 7,42,3,9));
44 g_assert (G_IN_SET (9, 7,42,3,9));
45 g_assert (!G_IN_SET (8, 7,42,3,9));
46 g_assert (!G_IN_SET (-1, 7,42,3,9));
47 g_assert (G_IN_SET ('x', 'a', 'x', 'c'));
48 g_assert (!G_IN_SET ('y', 'a', 'x', 'c'));
49 }
50
51 static void
52 test_hash_table_foreach (void)
53 {
54 /* use var names all different from the macro metavars to ensure proper
55 * substitution */
56 g_autoptr(GHashTable) table = g_hash_table_new (g_str_hash, g_str_equal);
57 const char *keys[] = {"key1", "key2"};
58 const char *vals[] = {"val1", "val2"};
59 g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
60 g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
61
62 guint i = 0;
63 GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
64 {
65 g_assert_cmpstr (key, ==, keys[i]);
66 g_assert_cmpstr (val, ==, vals[i]);
67 i++;
68 }
69 g_assert_cmpuint (i, ==, 2);
70
71 i = 0;
72 GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
73 {
74 g_hash_table_iter_remove (&it);
75 break;
76 }
77 g_assert_cmpuint (g_hash_table_size (table), ==, 1);
78
79 g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
80 g_assert_cmpuint (g_hash_table_size (table), ==, 1);
81
82 g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
83 g_assert_cmpuint (g_hash_table_size (table), ==, 2);
84
85 i = 0;
86 GLNX_HASH_TABLE_FOREACH_KV (table, const char*, key, const char*, val)
87 {
88 g_assert_cmpstr (key, ==, keys[i]);
89 g_assert_cmpstr (val, ==, vals[i]);
90 i++;
91 }
92 g_assert_cmpuint (i, ==, 2);
93
94 i = 0;
95 GLNX_HASH_TABLE_FOREACH (table, const char*, key)
96 {
97 g_assert_cmpstr (key, ==, keys[i]);
98 i++;
99 }
100 g_assert_cmpuint (i, ==, 2);
101
102 i = 0;
103 GLNX_HASH_TABLE_FOREACH_V (table, const char*, val)
104 {
105 g_assert_cmpstr (val, ==, vals[i]);
106 i++;
107 }
108 g_assert_cmpuint (i, ==, 2);
109 }
110
111 int main (int argc, char **argv)
112 {
113 g_test_init (&argc, &argv, NULL);
114 g_test_add_func ("/info", test_info);
115 g_test_add_func ("/inset", test_inset);
116 g_test_add_func ("/hash_table_foreach", test_hash_table_foreach);
117 return g_test_run();
118 }