Debian Codegraph @

tests/wp/spa-type.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/test-log.h"
10 #include <spa/utils/type-info.h>
11
12 static void
13 test_spa_type_basic (void)
14 {
15 g_assert_cmpuint (WP_SPA_TYPE_INVALID, ==, SPA_ID_INVALID);
16
17 {
18 WpSpaType type = SPA_TYPE_Int;
19 g_assert_cmpstr (wp_spa_type_name (type), ==, "Spa:Int");
20 g_assert_true (wp_spa_type_is_fundamental (type));
21 g_assert_cmpuint (wp_spa_type_parent (type), ==, SPA_TYPE_Int);
22 }
23
24 {
25 WpSpaType type = wp_spa_type_from_name ("Spa:Enum:ParamId");
26 g_assert_cmpuint (type, ==, WP_SPA_TYPE_INVALID);
27
28 WpSpaIdTable table = wp_spa_id_table_from_name ("Spa:Enum:ParamId");
29 g_assert_nonnull (table);
30 }
31
32 {
33 WpSpaType type = SPA_TYPE_OBJECT_Props;
34 g_assert_cmpstr (wp_spa_type_name (type), ==, "Spa:Pod:Object:Param:Props");
35 g_assert_cmpuint (wp_spa_type_from_name (SPA_TYPE_INFO_Props), ==, type);
36 g_assert_true (wp_spa_type_is_object (type));
37 g_assert_false (wp_spa_type_is_fundamental (type));
38 g_assert_cmpuint (wp_spa_type_parent (type), ==, SPA_TYPE_Object);
39 g_assert_nonnull (wp_spa_type_get_object_id_values_table (type));
40 g_assert_true (wp_spa_type_get_object_id_values_table (type) ==
41 wp_spa_id_table_from_name ("Spa:Enum:ParamId"));
42 }
43
44 /* enums */
45 {
46 WpSpaIdValue id = wp_spa_id_value_from_name ("Spa:Enum:ParamId:Props");
47 g_assert_nonnull (id);
48 g_assert_cmpstr (wp_spa_id_value_name (id), ==, "Spa:Enum:ParamId:Props");
49 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Props");
50 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PARAM_Props);
51
52 g_assert_true (id == wp_spa_id_value_from_short_name (
53 "Spa:Enum:ParamId", "Props"));
54 g_assert_true (id == wp_spa_id_value_from_number (
55 "Spa:Enum:ParamId", SPA_PARAM_Props));
56 }
57
58 {
59 WpSpaIdValue id =
60 wp_spa_id_value_from_name ("Spa:Enum:Control:Properties");
61 g_assert_nonnull (id);
62 g_assert_cmpstr (wp_spa_id_value_name (id), ==,
63 "Spa:Enum:Control:Properties");
64 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Properties");
65 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CONTROL_Properties);
66
67 g_assert_true (id == wp_spa_id_value_from_short_name (
68 "Spa:Enum:Control", "Properties"));
69 g_assert_true (id == wp_spa_id_value_from_number (
70 "Spa:Enum:Control", SPA_CONTROL_Properties));
71 }
72
73 {
74 WpSpaIdValue id = wp_spa_id_value_from_name ("Spa:Enum:Choice:Enum");
75 g_assert_nonnull (id);
76 g_assert_cmpstr (wp_spa_id_value_name (id), ==, "Spa:Enum:Choice:Enum");
77 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Enum");
78 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_Enum);
79
80 g_assert_true (id == wp_spa_id_value_from_short_name (
81 "Spa:Enum:Choice", "Enum"));
82 g_assert_true (id == wp_spa_id_value_from_number (
83 "Spa:Enum:Choice", SPA_CHOICE_Enum));
84 }
85
86 /* objects */
87 {
88 WpSpaIdValue id =
89 wp_spa_id_value_from_name ("Spa:Pod:Object:Param:Props:mute");
90 g_assert_nonnull (id);
91 g_assert_cmpstr (wp_spa_id_value_name (id), ==,
92 "Spa:Pod:Object:Param:Props:mute");
93 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "mute");
94 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_mute);
95
96 g_assert_true (id == wp_spa_id_value_from_short_name (
97 SPA_TYPE_INFO_Props, "mute"));
98 g_assert_true (id == wp_spa_id_value_from_number (
99 SPA_TYPE_INFO_Props, SPA_PROP_mute));
100 }
101
102 {
103 WpSpaIdValue id =
104 wp_spa_id_value_from_name ("Spa:Pod:Object:Param:PropInfo:id");
105 g_assert_nonnull (id);
106 g_assert_cmpstr (wp_spa_id_value_name (id), ==,
107 "Spa:Pod:Object:Param:PropInfo:id");
108 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "id");
109 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_id);
110
111 /* WpSpaIdValue is a pointer to static spa_type_info,
112 so it should be the same on all queries */
113 g_assert_true (id == wp_spa_id_value_from_short_name (
114 SPA_TYPE_INFO_PropInfo, "id"));
115 g_assert_true (id == wp_spa_id_value_from_number (
116 SPA_TYPE_INFO_PropInfo, SPA_PROP_INFO_id));
117 }
118
119 /* array value type check */
120 {
121 WpSpaIdValue id =
122 wp_spa_id_value_from_name ("Spa:Pod:Object:Param:Props:channelVolumes");
123 g_assert_nonnull (id);
124 g_assert_cmpstr (wp_spa_id_value_name (id), ==,
125 "Spa:Pod:Object:Param:Props:channelVolumes");
126 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "channelVolumes");
127 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_channelVolumes);
128
129 g_assert_cmpuint (wp_spa_id_value_array_get_item_type (id, NULL), ==,
130 SPA_TYPE_Float);
131 }
132
133 {
134 WpSpaIdValue id =
135 wp_spa_id_value_from_name ("Spa:Pod:Object:Param:Props:channelMap");
136 g_assert_nonnull (id);
137 g_assert_cmpstr (wp_spa_id_value_name (id), ==,
138 "Spa:Pod:Object:Param:Props:channelMap");
139 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "channelMap");
140 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_channelMap);
141
142 WpSpaIdTable table = NULL;
143 g_assert_cmpuint (wp_spa_id_value_array_get_item_type (id, &table), ==,
144 SPA_TYPE_Id);
145 g_assert_nonnull (table);
146 g_assert_true (table == wp_spa_id_table_from_name ("Spa:Enum:AudioChannel"));
147 }
148 }
149
150 static void
151 test_spa_type_iterate (void)
152 {
153 {
154 WpSpaType type = wp_spa_type_from_name (SPA_TYPE_INFO_PropInfo);
155 g_assert_cmpuint (type, !=, WP_SPA_TYPE_INVALID);
156 g_assert_true (wp_spa_type_is_object (type));
157
158 WpSpaIdTable table = wp_spa_type_get_values_table (type);
159 g_autoptr (WpIterator) it = wp_spa_id_table_new_iterator (table);
160 g_auto (GValue) value = G_VALUE_INIT;
161 WpSpaIdValue id = NULL;
162
163 g_assert_true (wp_iterator_next (it, &value));
164 id = g_value_get_pointer (&value);
165 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "");
166 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_START);
167 table = NULL;
168 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_Id);
169 g_assert_nonnull (table);
170 g_assert_true (table == wp_spa_id_table_from_name ("Spa:Enum:ParamId"));
171 g_value_unset (&value);
172
173 g_assert_true (wp_iterator_next (it, &value));
174 id = g_value_get_pointer (&value);
175 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "id");
176 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_id);
177 table = NULL;
178 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_Id);
179 g_assert_nonnull (table);
180 g_assert_true (table ==
181 wp_spa_id_table_from_name ("Spa:Pod:Object:Param:Props"));
182 g_value_unset (&value);
183
184 g_assert_true (wp_iterator_next (it, &value));
185 id = g_value_get_pointer (&value);
186 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "name");
187 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_name);
188 table = NULL;
189 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_String);
190 g_assert_null (table);
191 g_value_unset (&value);
192
193 g_assert_true (wp_iterator_next (it, &value));
194 id = g_value_get_pointer (&value);
195 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "type");
196 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_type);
197 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_Pod);
198 g_assert_null (table);
199 g_value_unset (&value);
200
201 g_assert_true (wp_iterator_next (it, &value));
202 id = g_value_get_pointer (&value);
203 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "labels");
204 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_labels);
205 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_Struct);
206 g_assert_null (table);
207 g_value_unset (&value);
208
209 g_assert_true (wp_iterator_next (it, &value));
210 id = g_value_get_pointer (&value);
211 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "container");
212 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_PROP_INFO_container);
213 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, &table), ==, SPA_TYPE_Id);
214 g_assert_null (table);
215 g_value_unset (&value);
216 }
217
218 {
219 WpSpaIdTable table = wp_spa_id_table_from_name ("Spa:Enum:Choice");
220 g_assert_nonnull (table);
221
222 g_autoptr (WpIterator) it = wp_spa_id_table_new_iterator (table);
223 g_auto (GValue) value = G_VALUE_INIT;
224 WpSpaIdValue id = NULL;
225
226 g_assert_true (wp_iterator_next (it, &value));
227 id = g_value_get_pointer (&value);
228 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "None");
229 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_None);
230 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
231 g_value_unset (&value);
232
233 g_assert_true (wp_iterator_next (it, &value));
234 id = g_value_get_pointer (&value);
235 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Range");
236 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_Range);
237 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
238 g_value_unset (&value);
239
240 g_assert_true (wp_iterator_next (it, &value));
241 id = g_value_get_pointer (&value);
242 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Step");
243 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_Step);
244 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
245 g_value_unset (&value);
246
247 g_assert_true (wp_iterator_next (it, &value));
248 id = g_value_get_pointer (&value);
249 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Enum");
250 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_Enum);
251 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
252 g_value_unset (&value);
253
254 g_assert_true (wp_iterator_next (it, &value));
255 id = g_value_get_pointer (&value);
256 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Flags");
257 g_assert_cmpuint (wp_spa_id_value_number (id), ==, SPA_CHOICE_Flags);
258 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
259 g_value_unset (&value);
260 }
261 }
262
263 static void
264 test_spa_type_register (void)
265 {
266 static const struct spa_type_info custom_enum_info[] = {
267 { 0, SPA_TYPE_Int, "Spa:Enum:CustomEnum:Invalid", NULL },
268 { 1, SPA_TYPE_Int, "Spa:Enum:CustomEnum:Valid", NULL },
269 { 0, 0, NULL, NULL }
270 };
271
272 static const struct spa_type_info custom_obj_info[] = {
273 { 0, SPA_TYPE_Id, "Spa:Pod:Object:CustomObj:", custom_enum_info },
274 { 1, SPA_TYPE_Int, "Spa:Pod:Object:CustomObj:id", NULL },
275 { 2, SPA_TYPE_String, "Spa:Pod:Object:CustomObj:name", NULL },
276 { 3, SPA_TYPE_Float, "Spa:Pod:Object:CustomObj:volume", NULL },
277 { 4, SPA_TYPE_Rectangle, "Spa:Pod:Object:CustomObj:box", NULL },
278 { 5, SPA_TYPE_Bytes, "Spa:Pod:Object:CustomObj:data", NULL },
279 { 0, 0, NULL, NULL },
280 };
281
282 wp_spa_dynamic_type_init ();
283
284 WpSpaIdTable enum_table =
285 wp_spa_dynamic_id_table_register ("Spa:Enum:CustomEnum", custom_enum_info);
286 WpSpaType obj_type = wp_spa_dynamic_type_register ("Spa:Pod:Object:CustomObj",
287 SPA_TYPE_Object, custom_obj_info);
288
289 g_assert_nonnull (enum_table);
290 g_assert_true (obj_type != WP_SPA_TYPE_INVALID);
291
292 g_assert_true (enum_table ==
293 wp_spa_id_table_from_name ("Spa:Enum:CustomEnum"));
294
295 {
296 g_autoptr (WpIterator) it = wp_spa_id_table_new_iterator (enum_table);
297 g_auto (GValue) value = G_VALUE_INIT;
298 WpSpaIdValue id = NULL;
299
300 g_assert_true (wp_iterator_next (it, &value));
301 id = g_value_get_pointer (&value);
302 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Invalid");
303 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 0);
304 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
305 g_value_unset (&value);
306
307 g_assert_true (wp_iterator_next (it, &value));
308 id = g_value_get_pointer (&value);
309 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "Valid");
310 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 1);
311 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
312 g_value_unset (&value);
313
314 g_assert_false (wp_iterator_next (it, &value));
315 }
316
317 g_assert_cmpstr (wp_spa_type_name (obj_type), ==, "Spa:Pod:Object:CustomObj");
318 g_assert_true (wp_spa_type_is_object (obj_type));
319 g_assert_false (wp_spa_type_is_fundamental (obj_type));
320 g_assert_cmpuint (wp_spa_type_parent (obj_type), ==, SPA_TYPE_Object);
321 g_assert_cmpuint (obj_type, ==,
322 wp_spa_type_from_name ("Spa:Pod:Object:CustomObj"));
323 g_assert_true (enum_table ==
324 wp_spa_type_get_object_id_values_table (obj_type));
325
326 {
327 WpSpaIdTable table = wp_spa_type_get_values_table (obj_type);
328 g_autoptr (WpIterator) it = wp_spa_id_table_new_iterator (table);
329 g_auto (GValue) value = G_VALUE_INIT;
330 WpSpaIdValue id = NULL;
331
332 g_assert_true (wp_iterator_next (it, &value));
333 id = g_value_get_pointer (&value);
334 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "");
335 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 0);
336 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Id);
337 g_value_unset (&value);
338
339 g_assert_true (wp_iterator_next (it, &value));
340 id = g_value_get_pointer (&value);
341 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "id");
342 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 1);
343 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Int);
344 g_value_unset (&value);
345
346 g_assert_true (wp_iterator_next (it, &value));
347 id = g_value_get_pointer (&value);
348 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "name");
349 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 2);
350 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_String);
351 g_value_unset (&value);
352
353 g_assert_true (wp_iterator_next (it, &value));
354 id = g_value_get_pointer (&value);
355 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "volume");
356 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 3);
357 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Float);
358 g_value_unset (&value);
359
360 g_assert_true (wp_iterator_next (it, &value));
361 id = g_value_get_pointer (&value);
362 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "box");
363 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 4);
364 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Rectangle);
365 g_value_unset (&value);
366
367 g_assert_true (wp_iterator_next (it, &value));
368 id = g_value_get_pointer (&value);
369 g_assert_cmpstr (wp_spa_id_value_short_name (id), ==, "data");
370 g_assert_cmpuint (wp_spa_id_value_number (id), ==, 5);
371 g_assert_cmpuint (wp_spa_id_value_get_value_type (id, NULL), ==, SPA_TYPE_Bytes);
372 g_value_unset (&value);
373
374 g_assert_false (wp_iterator_next (it, &value));
375 }
376
377 wp_spa_dynamic_type_deinit ();
378 }
379
380 int
381 main (int argc, char *argv[])
382 {
383 g_test_init (&argc, &argv, NULL);
384 g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
385
386 g_test_add_func ("/wp/spa-type/basic", test_spa_type_basic);
387 g_test_add_func ("/wp/spa-type/iterate", test_spa_type_iterate);
388 g_test_add_func ("/wp/spa-type/register", test_spa_type_register);
389
390 return g_test_run ();
391 }