Debian Codegraph @

tests/wp/properties.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/test-log.h"
10 #include <pipewire/pipewire.h>
11
12 static void
13 test_properties_basic (void)
14 {
15 g_autoptr (WpProperties) p = NULL;
16
17 p = wp_properties_new_empty ();
18 g_assert_nonnull (p);
19
20 g_assert_cmpint (wp_properties_set (p, "foo.bar", "test-value"), ==, 1);
21 g_assert_cmpstr (wp_properties_get (p, "foo.bar"), ==, "test-value");
22 g_assert_cmpstr (wp_properties_get (p, "nonexistent"), ==, NULL);
23
24 /* remove the key */
25 g_assert_cmpint (wp_properties_set (p, "foo.bar", NULL), ==, 1);
26 g_assert_cmpstr (wp_properties_get (p, "foo.bar"), ==, NULL);
27 /* now returns 0 because it does not exist */
28 g_assert_cmpint (wp_properties_set (p, "foo.bar", NULL), ==, 0);
29
30 g_assert_true (wp_properties_ref (p) == p);
31 wp_properties_unref (p);
32 }
33
34 static void
35 test_properties_wrap_dict (void)
36 {
37 g_autoptr (WpProperties) p = NULL;
38 const struct spa_dict_item dict_items[] = {
39 { "key1", "value1" },
40 { "key2", "value2" },
41 };
42 const struct spa_dict dict = SPA_DICT_INIT_ARRAY(dict_items);
43
44 p = wp_properties_new_wrap_dict (&dict);
45 g_assert_nonnull (p);
46
47 g_assert_cmpint (wp_properties_get_count (p), ==,
48 SPA_N_ELEMENTS (dict_items));
49 g_assert_cmpstr (wp_properties_get (p, "key1"), ==, "value1");
50 g_assert_cmpstr (wp_properties_get (p, "key2"), ==, "value2");
51 g_assert_cmpstr (wp_properties_get (p, "key3"), ==, NULL);
52
53 g_assert_true (wp_properties_peek_dict (p) == &dict);
54 }
55
56 static void
57 test_properties_copy_dict (void)
58 {
59 g_autoptr (WpProperties) p = NULL;
60 const struct spa_dict_item dict_items[] = {
61 { "key1", "value1" },
62 { "key2", "value2" },
63 };
64 const struct spa_dict dict = SPA_DICT_INIT_ARRAY(dict_items);
65
66 p = wp_properties_new_copy_dict (&dict);
67 g_assert_nonnull (p);
68
69 g_assert_cmpstr (wp_properties_get (p, "key1"), ==, "value1");
70 g_assert_cmpstr (wp_properties_get (p, "key2"), ==, "value2");
71 g_assert_cmpstr (wp_properties_get (p, "key3"), ==, NULL);
72
73 g_assert_true (wp_properties_peek_dict (p) != &dict);
74 }
75
76 static void
77 test_properties_wrap (void)
78 {
79 g_autoptr (WpProperties) p = NULL;
80 struct pw_properties *props;
81
82 props = pw_properties_new ("key1", "value1", NULL);
83 g_assert_nonnull (props);
84 p = wp_properties_new_wrap (props);
85 g_assert_nonnull (p);
86
87 g_assert_true (wp_properties_peek_dict (p) == &props->dict);
88 g_assert_cmpstr (wp_properties_get (p, "key1"), ==, "value1");
89
90 wp_properties_unref (g_steal_pointer (&p));
91 /* because wrap does not free the original object, this should not crash */
92 pw_properties_free (props);
93 }
94
95 static void
96 test_properties_take (void)
97 {
98 g_autoptr (WpProperties) p = NULL;
99 struct pw_properties *props;
100
101 props = pw_properties_new ("key1", "value1", NULL);
102 g_assert_nonnull (props);
103 p = wp_properties_new_take (props);
104 g_assert_nonnull (p);
105
106 g_assert_true (wp_properties_peek_dict (p) == &props->dict);
107 g_assert_cmpstr (wp_properties_get (p, "key1"), ==, "value1");
108
109 /* value changes should be reflected on both objects */
110 g_assert_cmpint (wp_properties_setf (p, "foobar", "%d", 2), ==, 1);
111 g_assert_cmpstr (pw_properties_get (props, "foobar"), ==, "2");
112
113 g_assert_cmpint (pw_properties_setf (props, "test", "some-%s", "value"), ==, 1);
114 g_assert_cmpstr (wp_properties_get (p, "test"), ==, "some-value");
115
116 /* no leaks because p frees props */
117 }
118
119 static void
120 test_properties_to_pw_props (void)
121 {
122 g_autoptr (WpProperties) p = NULL;
123 struct pw_properties *props;
124
125 p = wp_properties_new ("key1", "value1", NULL);
126 g_assert_nonnull (p);
127 g_assert_cmpstr (wp_properties_get (p, "key1"), ==, "value1");
128
129 props = wp_properties_to_pw_properties (p);
130 g_assert_nonnull (props);
131 g_assert_cmpstr (pw_properties_get (props, "key1"), ==, "value1");
132
133 /* we have different underlying objects */
134 g_assert_true (wp_properties_peek_dict (p) != &props->dict);
135 g_assert_cmpint (pw_properties_set (props, "test", "some-value"), ==, 1);
136 g_assert_cmpstr (wp_properties_get (p, "test"), ==, NULL);
137
138 pw_properties_free (props);
139 }
140
141 static void
142 test_properties_iterate (void)
143 {
144 g_autoptr (WpProperties) p = NULL;
145
146 p = wp_properties_new_empty ();
147 g_assert_nonnull (p);
148 g_assert_cmpint (wp_properties_set (p, "key0", "value0"), ==, 1);
149 g_assert_cmpint (wp_properties_set (p, "key1", "value1"), ==, 1);
150 g_assert_cmpint (wp_properties_set (p, "key2", "value2"), ==, 1);
151 g_assert_cmpint (wp_properties_set (p, "key3", "value3"), ==, 1);
152 g_assert_cmpint (wp_properties_set (p, "key4", "value4"), ==, 1);
153
154 g_autoptr (WpIterator) it = NULL;
155 g_auto (GValue) item = G_VALUE_INIT;
156 gint i = 0;
157 for (it = wp_properties_new_iterator (p);
158 wp_iterator_next (it, &item);
159 g_value_unset (&item)) {
160 WpPropertiesItem *pi = g_value_get_boxed (&item);
161 g_autofree gchar *expected_key = g_strdup_printf ("key%d", i);
162 g_autofree gchar *expected_value = g_strdup_printf ("value%d", i);
163 g_assert_cmpstr (expected_value, ==, wp_properties_item_get_value (pi));
164 g_assert_cmpstr (expected_key, ==, wp_properties_item_get_key (pi));
165 i++;
166 }
167 g_assert_cmpint (i, ==, 5);
168 }
169
170 int
171 main (int argc, char *argv[])
172 {
173 g_test_init (&argc, &argv, NULL);
174 g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
175
176 g_test_add_func ("/wp/properties/basic", test_properties_basic);
177 g_test_add_func ("/wp/properties/wrap_dict", test_properties_wrap_dict);
178 g_test_add_func ("/wp/properties/copy_dict", test_properties_copy_dict);
179 g_test_add_func ("/wp/properties/wrap", test_properties_wrap);
180 g_test_add_func ("/wp/properties/take", test_properties_take);
181 g_test_add_func ("/wp/properties/to_pw_props", test_properties_to_pw_props);
182 g_test_add_func ("/wp/properties/iterate", test_properties_iterate);
183
184 return g_test_run ();
185 }