Commit | Line | Data |
---|---|---|
4f29b187 PP |
1 | /* |
2 | * test_bt_objects.c | |
3 | * | |
4 | * Babeltrace basic object system tests | |
5 | * | |
6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
7 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; under version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
23 | #define _GNU_SOURCE | |
24 | #include <babeltrace/objects.h> | |
25 | #include <assert.h> | |
26 | #include <string.h> | |
27 | #include "tap/tap.h" | |
28 | ||
29 | static | |
30 | void test_null(void) | |
31 | { | |
32 | ok(bt_object_null, "bt_object_null is not NULL"); | |
33 | ok(bt_object_is_null(bt_object_null), | |
34 | "bt_object_null is a null object"); | |
35 | bt_object_get(bt_object_null); | |
36 | pass("getting bt_object_null does not cause a crash"); | |
37 | bt_object_put(bt_object_null); | |
38 | pass("putting bt_object_null does not cause a crash"); | |
39 | ||
40 | bt_object_get(NULL); | |
41 | pass("getting NULL does not cause a crash"); | |
42 | bt_object_put(NULL); | |
43 | pass("putting NULL does not cause a crash"); | |
44 | ||
45 | ok(bt_object_get_type(NULL) == BT_OBJECT_TYPE_UNKNOWN, | |
46 | "bt_object_get_type(NULL) returns BT_OBJECT_TYPE_UNKNOWN"); | |
47 | } | |
48 | ||
49 | static | |
50 | void test_bool(void) | |
51 | { | |
52 | int ret; | |
53 | bool value; | |
54 | struct bt_object *obj; | |
55 | ||
56 | obj = bt_object_bool_create(); | |
57 | ok(obj && bt_object_is_bool(obj), | |
58 | "bt_object_bool_create() returns a boolean object"); | |
59 | ||
60 | value = true; | |
61 | ret = bt_object_bool_get(obj, &value); | |
62 | ok(!ret && !value, "default boolean object value is false"); | |
63 | ||
64 | ret = bt_object_bool_set(NULL, true); | |
65 | ok(ret, "bt_object_bool_set() fails with an object set to NULL"); | |
66 | ret = bt_object_bool_get(NULL, &value); | |
67 | ok(ret, "bt_object_bool_get() fails with an object set to NULL"); | |
68 | ||
69 | ret = bt_object_bool_set(obj, true); | |
70 | ok(!ret, "bt_object_bool_set() succeeds"); | |
71 | ret = bt_object_bool_get(obj, &value); | |
72 | ok(!ret && value, "bt_object_bool_set() works"); | |
73 | ||
74 | BT_OBJECT_PUT(obj); | |
75 | pass("putting an existing boolean object does not cause a crash") | |
76 | ||
77 | obj = bt_object_bool_create_init(true); | |
78 | ok(obj && bt_object_is_bool(obj), | |
79 | "bt_object_bool_create_init() returns a boolean object"); | |
80 | ret = bt_object_bool_get(obj, &value); | |
81 | ok(!ret && value, | |
82 | "bt_object_bool_create_init() sets the appropriate initial value"); | |
83 | ||
84 | BT_OBJECT_PUT(obj); | |
85 | } | |
86 | ||
87 | static | |
88 | void test_integer(void) | |
89 | { | |
90 | int ret; | |
91 | int64_t value; | |
92 | struct bt_object *obj; | |
93 | ||
94 | obj = bt_object_integer_create(); | |
95 | ok(obj && bt_object_is_integer(obj), | |
96 | "bt_object_integer_create() returns an integer object"); | |
97 | ||
98 | ret = bt_object_integer_set(NULL, -12345); | |
99 | ok(ret, "bt_object_integer_set() fails with an object set to NULL"); | |
100 | ret = bt_object_integer_get(NULL, &value); | |
101 | ok(ret, "bt_object_integer_get() fails with an object set to NULL"); | |
102 | ||
103 | value = 1961; | |
104 | ret = bt_object_integer_get(obj, &value); | |
105 | ok(!ret && value == 0, "default integer object value is 0"); | |
106 | ||
107 | ret = bt_object_integer_set(obj, -12345); | |
108 | ok(!ret, "bt_object_integer_set() succeeds"); | |
109 | ret = bt_object_integer_get(obj, &value); | |
110 | ok(!ret && value == -12345, "bt_object_integer_set() works"); | |
111 | ||
112 | BT_OBJECT_PUT(obj); | |
113 | pass("putting an existing integer object does not cause a crash") | |
114 | ||
115 | obj = bt_object_integer_create_init(321456987); | |
116 | ok(obj && bt_object_is_integer(obj), | |
117 | "bt_object_integer_create_init() returns an integer object"); | |
118 | ret = bt_object_integer_get(obj, &value); | |
119 | ok(!ret && value == 321456987, | |
120 | "bt_object_integer_create_init() sets the appropriate initial value"); | |
121 | ||
122 | BT_OBJECT_PUT(obj); | |
123 | } | |
124 | ||
125 | static | |
126 | void test_float(void) | |
127 | { | |
128 | int ret; | |
129 | double value; | |
130 | struct bt_object *obj; | |
131 | ||
132 | obj = bt_object_float_create(); | |
133 | ok(obj && bt_object_is_float(obj), | |
134 | "bt_object_float_create() returns a floating point number object"); | |
135 | ||
136 | ret = bt_object_float_set(NULL, 1.2345); | |
137 | ok(ret, "bt_object_float_set() fails with an object set to NULL"); | |
138 | ret = bt_object_float_get(NULL, &value); | |
139 | ok(ret, "bt_object_float_get() fails with an object set to NULL"); | |
140 | ||
141 | value = 17.34; | |
142 | ret = bt_object_float_get(obj, &value); | |
143 | ok(!ret && value == 0., "default floating point number object value is 0"); | |
144 | ||
145 | ret = bt_object_float_set(obj, -3.1416); | |
146 | ok(!ret, "bt_object_float_set() succeeds"); | |
147 | ret = bt_object_float_get(obj, &value); | |
148 | ok(!ret && value == -3.1416, "bt_object_float_set() works"); | |
149 | ||
150 | BT_OBJECT_PUT(obj); | |
151 | pass("putting an existing floating point number object does not cause a crash") | |
152 | ||
153 | obj = bt_object_float_create_init(33.1649758); | |
154 | ok(obj && bt_object_is_float(obj), | |
155 | "bt_object_float_create_init() returns a floating point number object"); | |
156 | ret = bt_object_float_get(obj, &value); | |
157 | ok(!ret && value == 33.1649758, | |
158 | "bt_object_float_create_init() sets the appropriate initial value"); | |
159 | ||
160 | BT_OBJECT_PUT(obj); | |
161 | } | |
162 | ||
163 | static | |
164 | void test_string(void) | |
165 | { | |
166 | int ret; | |
167 | const char *value; | |
168 | struct bt_object *obj; | |
169 | ||
170 | obj = bt_object_string_create(); | |
171 | ok(obj && bt_object_is_string(obj), | |
172 | "bt_object_string_create() returns a string object"); | |
173 | ||
174 | ret = bt_object_string_set(NULL, "hoho"); | |
175 | ok(ret, "bt_object_string_set() fails with an object set to NULL"); | |
176 | value = bt_object_string_get(NULL); | |
177 | ok(!value, "bt_object_string_get() fails with an object set to NULL"); | |
178 | ||
179 | value = bt_object_string_get(obj); | |
180 | ok(value && !strcmp(value, ""), | |
181 | "default string object value is \"\""); | |
182 | ||
183 | ret = bt_object_string_set(obj, "hello worldz"); | |
184 | ok(!ret, "bt_object_string_set() succeeds"); | |
185 | value = bt_object_string_get(obj); | |
186 | ok(value && !strcmp(value, "hello worldz"), | |
187 | "bt_object_string_set() works"); | |
188 | ret = bt_object_string_set(obj, NULL); | |
189 | ok(ret, "bt_object_string_set() does not accept a NULL value"); | |
190 | ||
191 | BT_OBJECT_PUT(obj); | |
192 | pass("putting an existing string object does not cause a crash") | |
193 | ||
194 | obj = bt_object_string_create_init(NULL); | |
195 | ok(!obj, "bt_object_string_create_init() fails with an initial value set to NULL"); | |
196 | obj = bt_object_string_create_init("initial value"); | |
197 | ok(obj && bt_object_is_string(obj), | |
198 | "bt_object_string_create_init() returns a string object"); | |
199 | value = bt_object_string_get(obj); | |
200 | ok(value && !strcmp(value, "initial value"), | |
201 | "bt_object_string_create_init() sets the appropriate initial value"); | |
202 | ||
203 | BT_OBJECT_PUT(obj); | |
204 | } | |
205 | ||
206 | static | |
207 | void test_array(void) | |
208 | { | |
209 | int ret; | |
210 | bool bool_value; | |
211 | int64_t int_value; | |
212 | double float_value; | |
213 | struct bt_object *obj; | |
214 | const char *string_value; | |
215 | struct bt_object *array_obj; | |
216 | ||
217 | array_obj = bt_object_array_create(); | |
218 | ok(array_obj && bt_object_is_array(array_obj), | |
219 | "bt_object_array_create() returns an array object"); | |
220 | ok(bt_object_array_is_empty(array_obj), | |
221 | "initial array object size is 0"); | |
222 | ok(bt_object_array_size(NULL) < 0, | |
223 | "bt_object_array_size() fails with an array object set to NULL"); | |
224 | ||
225 | ok(bt_object_array_append(NULL, bt_object_null), | |
226 | "bt_object_array_append() fails with an array object set to NULL"); | |
227 | ||
228 | obj = bt_object_integer_create_init(345); | |
229 | ret = bt_object_array_append(array_obj, obj); | |
230 | BT_OBJECT_PUT(obj); | |
231 | obj = bt_object_float_create_init(-17.45); | |
232 | ret |= bt_object_array_append(array_obj, obj); | |
233 | BT_OBJECT_PUT(obj); | |
234 | obj = bt_object_bool_create_init(true); | |
235 | ret |= bt_object_array_append(array_obj, obj); | |
236 | BT_OBJECT_PUT(obj); | |
237 | ret |= bt_object_array_append(array_obj, bt_object_null); | |
238 | ok(!ret, "bt_object_array_append() succeeds"); | |
239 | ret = bt_object_array_append(NULL, bt_object_null); | |
240 | ok(ret, "bt_object_array_append() fails with an array object set to NULL"); | |
241 | ret = bt_object_array_append(array_obj, NULL); | |
242 | ok(ret, "bt_object_array_append() fails with an element object set to NULL"); | |
243 | ok(bt_object_array_size(array_obj) == 4, | |
244 | "appending an element to an array object increment its size"); | |
245 | ||
246 | obj = bt_object_array_get(array_obj, 4); | |
247 | ok(!obj, "getting an array object's element at an index equal to its size fails"); | |
248 | obj = bt_object_array_get(array_obj, 5); | |
249 | ok(!obj, "getting an array object's element at a larger index fails"); | |
250 | ||
251 | obj = bt_object_array_get(NULL, 2); | |
252 | ok(!obj, "bt_object_array_get() fails with an array object set to NULL"); | |
253 | ||
254 | obj = bt_object_array_get(array_obj, 0); | |
255 | ok(obj && bt_object_is_integer(obj), | |
256 | "bt_object_array_get() returns an object with the appropriate type (integer)"); | |
257 | ret = bt_object_integer_get(obj, &int_value); | |
258 | ok(!ret && int_value == 345, | |
259 | "bt_object_array_get() returns an object with the appropriate value (integer)"); | |
260 | BT_OBJECT_PUT(obj); | |
261 | obj = bt_object_array_get(array_obj, 1); | |
262 | ok(obj && bt_object_is_float(obj), | |
263 | "bt_object_array_get() returns an object with the appropriate type (floating point number)"); | |
264 | ret = bt_object_float_get(obj, &float_value); | |
265 | ok(!ret && float_value == -17.45, | |
266 | "bt_object_array_get() returns an object with the appropriate value (floating point number)"); | |
267 | BT_OBJECT_PUT(obj); | |
268 | obj = bt_object_array_get(array_obj, 2); | |
269 | ok(obj && bt_object_is_bool(obj), | |
270 | "bt_object_array_get() returns an object with the appropriate type (boolean)"); | |
271 | ret = bt_object_bool_get(obj, &bool_value); | |
272 | ok(!ret && bool_value, | |
273 | "bt_object_array_get() returns an object with the appropriate value (boolean)"); | |
274 | BT_OBJECT_PUT(obj); | |
275 | obj = bt_object_array_get(array_obj, 3); | |
276 | ok(obj == bt_object_null, | |
277 | "bt_object_array_get() returns an object with the appropriate type (null)"); | |
278 | ||
279 | ret = bt_object_array_append_bool(array_obj, false); | |
280 | ok(!ret, "bt_object_array_append_bool() succeeds"); | |
281 | ret = bt_object_array_append_bool(NULL, true); | |
282 | ok(ret, "bt_object_array_append_bool() fails with an array object set to NULL"); | |
283 | ret = bt_object_array_append_integer(array_obj, 98765); | |
284 | ok(!ret, "bt_object_array_append_integer() succeeds"); | |
285 | ret = bt_object_array_append_integer(NULL, 18765); | |
286 | ok(ret, "bt_object_array_append_integer() fails with an array object set to NULL"); | |
287 | ret = bt_object_array_append_float(array_obj, 2.49578); | |
288 | ok(!ret, "bt_object_array_append_float() succeeds"); | |
289 | ret = bt_object_array_append_float(NULL, 1.49578); | |
290 | ok(ret, "bt_object_array_append_float() fails with an array object set to NULL"); | |
291 | ret = bt_object_array_append_string(array_obj, "bt_object"); | |
292 | ok(!ret, "bt_object_array_append_string() succeeds"); | |
293 | ret = bt_object_array_append_string(NULL, "bt_obj"); | |
294 | ok(ret, "bt_object_array_append_string() fails with an array object set to NULL"); | |
295 | ret = bt_object_array_append_array(array_obj); | |
296 | ok(!ret, "bt_object_array_append_array() succeeds"); | |
297 | ret = bt_object_array_append_array(NULL); | |
298 | ok(ret, "bt_object_array_append_array() fails with an array object set to NULL"); | |
299 | ret = bt_object_array_append_map(array_obj); | |
300 | ok(!ret, "bt_object_array_append_map() succeeds"); | |
301 | ret = bt_object_array_append_map(NULL); | |
302 | ok(ret, "bt_object_array_append_map() fails with an array object set to NULL"); | |
303 | ||
304 | ok(bt_object_array_size(array_obj) == 10, | |
305 | "the bt_object_array_append_*() functions increment the array object's size"); | |
306 | ok(!bt_object_array_is_empty(array_obj), | |
307 | "map object is not empty"); | |
308 | ||
309 | obj = bt_object_array_get(array_obj, 4); | |
310 | ok(obj && bt_object_is_bool(obj), | |
311 | "bt_object_array_append_bool() appends a boolean object"); | |
312 | ret = bt_object_bool_get(obj, &bool_value); | |
313 | ok(!ret && !bool_value, | |
314 | "bt_object_array_append_bool() appends the appropriate value"); | |
315 | BT_OBJECT_PUT(obj); | |
316 | obj = bt_object_array_get(array_obj, 5); | |
317 | ok(obj && bt_object_is_integer(obj), | |
318 | "bt_object_array_append_integer() appends an integer object"); | |
319 | ret = bt_object_integer_get(obj, &int_value); | |
320 | ok(!ret && int_value == 98765, | |
321 | "bt_object_array_append_integer() appends the appropriate value"); | |
322 | BT_OBJECT_PUT(obj); | |
323 | obj = bt_object_array_get(array_obj, 6); | |
324 | ok(obj && bt_object_is_float(obj), | |
325 | "bt_object_array_append_float() appends a floating point number object"); | |
326 | ret = bt_object_float_get(obj, &float_value); | |
327 | ok(!ret && float_value == 2.49578, | |
328 | "bt_object_array_append_float() appends the appropriate value"); | |
329 | BT_OBJECT_PUT(obj); | |
330 | obj = bt_object_array_get(array_obj, 7); | |
331 | ok(obj && bt_object_is_string(obj), | |
332 | "bt_object_array_append_string() appends a string object"); | |
333 | string_value = bt_object_string_get(obj); | |
334 | ok(string_value && !strcmp(string_value, "bt_object"), | |
335 | "bt_object_array_append_string() appends the appropriate value"); | |
336 | BT_OBJECT_PUT(obj); | |
337 | obj = bt_object_array_get(array_obj, 8); | |
338 | ok(obj && bt_object_is_array(obj), | |
339 | "bt_object_array_append_array() appends an array object"); | |
340 | ok(bt_object_array_is_empty(obj), | |
341 | "bt_object_array_append_array() an empty array object"); | |
342 | BT_OBJECT_PUT(obj); | |
343 | obj = bt_object_array_get(array_obj, 9); | |
344 | ok(obj && bt_object_is_map(obj), | |
345 | "bt_object_array_append_map() appends a map object"); | |
346 | ok(bt_object_map_is_empty(obj), | |
347 | "bt_object_array_append_map() an empty map object"); | |
348 | BT_OBJECT_PUT(obj); | |
349 | ||
350 | BT_OBJECT_PUT(array_obj); | |
351 | pass("putting an existing array object does not cause a crash") | |
352 | } | |
353 | ||
354 | static | |
355 | bool test_map_foreach_cb_count(const char *key, struct bt_object *object, | |
356 | void *data) | |
357 | { | |
358 | int *count = data; | |
359 | ||
360 | if (*count == 3) { | |
361 | return false; | |
362 | } | |
363 | ||
364 | (*count)++; | |
365 | ||
366 | return true; | |
367 | } | |
368 | ||
369 | struct map_foreach_checklist { | |
370 | bool bool1; | |
371 | bool int1; | |
372 | bool float1; | |
373 | bool null1; | |
374 | bool bool2; | |
375 | bool int2; | |
376 | bool float2; | |
377 | bool string2; | |
378 | bool array2; | |
379 | bool map2; | |
380 | }; | |
381 | ||
382 | static | |
383 | bool test_map_foreach_cb_check(const char *key, struct bt_object *object, | |
384 | void *data) | |
385 | { | |
386 | int ret; | |
387 | struct map_foreach_checklist *checklist = data; | |
388 | ||
389 | if (!strcmp(key, "bool")) { | |
390 | if (checklist->bool1) { | |
391 | fail("test_map_foreach_cb_check(): duplicate key \"bool\""); | |
392 | } else { | |
393 | bool val = false; | |
394 | ||
395 | ret = bt_object_bool_get(object, &val); | |
396 | ok(!ret, "test_map_foreach_cb_check(): success getting \"bool\" value"); | |
397 | ||
398 | if (val) { | |
399 | pass("test_map_foreach_cb_check(): \"bool\" object has the right value"); | |
400 | checklist->bool1 = true; | |
401 | } | |
402 | } | |
403 | } else if (!strcmp(key, "int")) { | |
404 | if (checklist->int1) { | |
405 | fail("test_map_foreach_cb_check(): duplicate key \"int\""); | |
406 | } else { | |
407 | int64_t val = 0; | |
408 | ||
409 | ret = bt_object_integer_get(object, &val); | |
410 | ok(!ret, "test_map_foreach_cb_check(): success getting \"int\" value"); | |
411 | ||
412 | if (val == 19457) { | |
413 | pass("test_map_foreach_cb_check(): \"int\" object has the right value"); | |
414 | checklist->int1 = true; | |
415 | } | |
416 | } | |
417 | } else if (!strcmp(key, "float")) { | |
418 | if (checklist->float1) { | |
419 | fail("test_map_foreach_cb_check(): duplicate key \"float\""); | |
420 | } else { | |
421 | double val = 0; | |
422 | ||
423 | ret = bt_object_float_get(object, &val); | |
424 | ok(!ret, "test_map_foreach_cb_check(): success getting \"float\" value"); | |
425 | ||
426 | if (val == 5.444) { | |
427 | pass("test_map_foreach_cb_check(): \"float\" object has the right value"); | |
428 | checklist->float1 = true; | |
429 | } | |
430 | } | |
431 | } else if (!strcmp(key, "null")) { | |
432 | if (checklist->null1) { | |
433 | fail("test_map_foreach_cb_check(): duplicate key \"bool\""); | |
434 | } else { | |
435 | ok(bt_object_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" object"); | |
436 | checklist->null1 = true; | |
437 | } | |
438 | } else if (!strcmp(key, "bool2")) { | |
439 | if (checklist->bool2) { | |
440 | fail("test_map_foreach_cb_check(): duplicate key \"bool2\""); | |
441 | } else { | |
442 | bool val = false; | |
443 | ||
444 | ret = bt_object_bool_get(object, &val); | |
445 | ok(!ret, "test_map_foreach_cb_check(): success getting \"bool2\" value"); | |
446 | ||
447 | if (val) { | |
448 | pass("test_map_foreach_cb_check(): \"bool2\" object has the right value"); | |
449 | checklist->bool2 = true; | |
450 | } | |
451 | } | |
452 | } else if (!strcmp(key, "int2")) { | |
453 | if (checklist->int2) { | |
454 | fail("test_map_foreach_cb_check(): duplicate key \"int2\""); | |
455 | } else { | |
456 | int64_t val = 0; | |
457 | ||
458 | ret = bt_object_integer_get(object, &val); | |
459 | ok(!ret, "test_map_foreach_cb_check(): success getting \"int2\" value"); | |
460 | ||
461 | if (val == 98765) { | |
462 | pass("test_map_foreach_cb_check(): \"int2\" object has the right value"); | |
463 | checklist->int2 = true; | |
464 | } | |
465 | } | |
466 | } else if (!strcmp(key, "float2")) { | |
467 | if (checklist->float2) { | |
468 | fail("test_map_foreach_cb_check(): duplicate key \"float2\""); | |
469 | } else { | |
470 | double val = 0; | |
471 | ||
472 | ret = bt_object_float_get(object, &val); | |
473 | ok(!ret, "test_map_foreach_cb_check(): success getting \"float2\" value"); | |
474 | ||
475 | if (val == -49.0001) { | |
476 | pass("test_map_foreach_cb_check(): \"float2\" object has the right value"); | |
477 | checklist->float2 = true; | |
478 | } | |
479 | } | |
480 | } else if (!strcmp(key, "string2")) { | |
481 | if (checklist->string2) { | |
482 | fail("test_map_foreach_cb_check(): duplicate key \"string2\""); | |
483 | } else { | |
484 | const char *val = bt_object_string_get(object); | |
485 | ||
486 | ok(val, "test_map_foreach_cb_check(): success getting \"string2\" value"); | |
487 | ||
488 | if (val && !strcmp(val, "bt_object")) { | |
489 | pass("test_map_foreach_cb_check(): \"string2\" object has the right value"); | |
490 | checklist->string2 = true; | |
491 | } | |
492 | } | |
493 | } else if (!strcmp(key, "array2")) { | |
494 | if (checklist->array2) { | |
495 | fail("test_map_foreach_cb_check(): duplicate key \"array2\""); | |
496 | } else { | |
497 | ok(bt_object_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" object"); | |
498 | ok(bt_object_array_is_empty(object), | |
499 | "test_map_foreach_cb_check(): \"array2\" object is empty"); | |
500 | checklist->array2 = true; | |
501 | } | |
502 | } else if (!strcmp(key, "map2")) { | |
503 | if (checklist->map2) { | |
504 | fail("test_map_foreach_cb_check(): duplicate key \"map2\""); | |
505 | } else { | |
506 | ok(bt_object_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" object"); | |
507 | ok(bt_object_map_is_empty(object), | |
508 | "test_map_foreach_cb_check(): \"map2\" object is empty"); | |
509 | checklist->map2 = true; | |
510 | } | |
511 | } else { | |
512 | diag("test_map_foreach_cb_check(): unknown map key \"%s\"", | |
513 | key); | |
514 | fail("test_map_foreach_cb_check(): unknown map key"); | |
515 | } | |
516 | ||
517 | return true; | |
518 | } | |
519 | ||
520 | static | |
521 | void test_map(void) | |
522 | { | |
523 | int ret; | |
524 | int count = 0; | |
525 | bool bool_value; | |
526 | int64_t int_value; | |
527 | double float_value; | |
528 | struct bt_object *obj; | |
529 | struct bt_object *map_obj; | |
530 | struct map_foreach_checklist checklist; | |
531 | ||
532 | map_obj = bt_object_map_create(); | |
533 | ok(map_obj && bt_object_is_map(map_obj), | |
534 | "bt_object_map_create() returns a map object"); | |
535 | ok(bt_object_map_size(map_obj) == 0, | |
536 | "initial map object size is 0"); | |
537 | ok(bt_object_map_size(NULL) < 0, | |
538 | "bt_object_map_size() fails with a map object set to NULL"); | |
539 | ||
540 | ok(bt_object_map_insert(NULL, "hello", bt_object_null), | |
541 | "bt_object_array_insert() fails with a map object set to NULL"); | |
542 | ||
543 | ok(bt_object_map_insert(map_obj, NULL, bt_object_null), | |
544 | "bt_object_array_insert() fails with a key set to NULL"); | |
545 | ok(bt_object_map_insert(map_obj, "yeah", NULL), | |
546 | "bt_object_array_insert() fails with an element object set to NULL"); | |
547 | ||
548 | obj = bt_object_integer_create_init(19457); | |
549 | ret = bt_object_map_insert(map_obj, "int", obj); | |
550 | BT_OBJECT_PUT(obj); | |
551 | obj = bt_object_float_create_init(5.444); | |
552 | ret |= bt_object_map_insert(map_obj, "float", obj); | |
553 | BT_OBJECT_PUT(obj); | |
554 | obj = bt_object_bool_create(); | |
555 | ret |= bt_object_map_insert(map_obj, "bool", obj); | |
556 | BT_OBJECT_PUT(obj); | |
557 | ret |= bt_object_map_insert(map_obj, "null", bt_object_null); | |
558 | ok(!ret, "bt_object_map_insert() succeeds"); | |
559 | ok(bt_object_map_size(map_obj) == 4, | |
560 | "inserting an element into a map object increment its size"); | |
561 | ||
562 | obj = bt_object_bool_create_init(true); | |
563 | ret = bt_object_map_insert(map_obj, "bool", obj); | |
564 | BT_OBJECT_PUT(obj); | |
565 | ok(!ret, "bt_object_map_insert() accepts an existing key"); | |
566 | ||
567 | obj = bt_object_map_get(map_obj, NULL); | |
568 | ok(!obj, "bt_object_map_get() fails with a key set to NULL"); | |
569 | obj = bt_object_map_get(NULL, "bool"); | |
570 | ok(!obj, "bt_object_map_get() fails with a map object set to NULL"); | |
571 | ||
572 | obj = bt_object_map_get(map_obj, "life"); | |
573 | ok(!obj, "bt_object_map_get() fails with an non existing key"); | |
574 | obj = bt_object_map_get(map_obj, "float"); | |
575 | ok(obj && bt_object_is_float(obj), | |
576 | "bt_object_map_get() returns an object with the appropriate type (float)"); | |
577 | ret = bt_object_float_get(obj, &float_value); | |
578 | ok(!ret && float_value == 5.444, | |
579 | "bt_object_map_get() returns an object with the appropriate value (float)"); | |
580 | BT_OBJECT_PUT(obj); | |
581 | obj = bt_object_map_get(map_obj, "int"); | |
582 | ok(obj && bt_object_is_integer(obj), | |
583 | "bt_object_map_get() returns an object with the appropriate type (integer)"); | |
584 | ret = bt_object_integer_get(obj, &int_value); | |
585 | ok(!ret && int_value == 19457, | |
586 | "bt_object_map_get() returns an object with the appropriate value (integer)"); | |
587 | BT_OBJECT_PUT(obj); | |
588 | obj = bt_object_map_get(map_obj, "null"); | |
589 | ok(obj && bt_object_is_null(obj), | |
590 | "bt_object_map_get() returns an object with the appropriate type (null)"); | |
591 | obj = bt_object_map_get(map_obj, "bool"); | |
592 | ok(obj && bt_object_is_bool(obj), | |
593 | "bt_object_map_get() returns an object with the appropriate type (boolean)"); | |
594 | ret = bt_object_bool_get(obj, &bool_value); | |
595 | ok(!ret && bool_value, | |
596 | "bt_object_map_get() returns an object with the appropriate value (boolean)"); | |
597 | BT_OBJECT_PUT(obj); | |
598 | ||
599 | ret = bt_object_map_insert_bool(map_obj, "bool2", true); | |
600 | ok(!ret, "bt_object_map_insert_bool() succeeds"); | |
601 | ret = bt_object_map_insert_bool(NULL, "bool2", false); | |
602 | ok(ret, "bt_object_map_insert_bool() fails with a map object set to NULL"); | |
603 | ret = bt_object_map_insert_integer(map_obj, "int2", 98765); | |
604 | ok(!ret, "bt_object_map_insert_integer() succeeds"); | |
605 | ret = bt_object_map_insert_integer(NULL, "int2", 1001); | |
606 | ok(ret, "bt_object_map_insert_integer() fails with a map object set to NULL"); | |
607 | ret = bt_object_map_insert_float(map_obj, "float2", -49.0001); | |
608 | ok(!ret, "bt_object_map_insert_float() succeeds"); | |
609 | ret = bt_object_map_insert_float(NULL, "float2", 495); | |
610 | ok(ret, "bt_object_map_insert_float() fails with a map object set to NULL"); | |
611 | ret = bt_object_map_insert_string(map_obj, "string2", "bt_object"); | |
612 | ok(!ret, "bt_object_map_insert_string() succeeds"); | |
613 | ret = bt_object_map_insert_string(NULL, "string2", "bt_obj"); | |
614 | ok(ret, "bt_object_map_insert_string() fails with a map object set to NULL"); | |
615 | ret = bt_object_map_insert_array(map_obj, "array2"); | |
616 | ok(!ret, "bt_object_map_insert_array() succeeds"); | |
617 | ret = bt_object_map_insert_array(NULL, "array2"); | |
618 | ok(ret, "bt_object_map_insert_array() fails with a map object set to NULL"); | |
619 | ret = bt_object_map_insert_map(map_obj, "map2"); | |
620 | ok(!ret, "bt_object_map_insert_map() succeeds"); | |
621 | ret = bt_object_map_insert_map(NULL, "map2"); | |
622 | ok(ret, "bt_object_map_insert_map() fails with a map object set to NULL"); | |
623 | ||
624 | ok(bt_object_map_size(map_obj) == 10, | |
625 | "the bt_object_map_insert*() functions increment the map object's size"); | |
626 | ||
627 | ok(!bt_object_map_has_key(map_obj, "hello"), | |
628 | "map object does not have key \"hello\""); | |
629 | ok(bt_object_map_has_key(map_obj, "bool"), | |
630 | "map object has key \"bool\""); | |
631 | ok(bt_object_map_has_key(map_obj, "int"), | |
632 | "map object has key \"int\""); | |
633 | ok(bt_object_map_has_key(map_obj, "float"), | |
634 | "map object has key \"float\""); | |
635 | ok(bt_object_map_has_key(map_obj, "null"), | |
636 | "map object has key \"null\""); | |
637 | ok(bt_object_map_has_key(map_obj, "bool2"), | |
638 | "map object has key \"bool2\""); | |
639 | ok(bt_object_map_has_key(map_obj, "int2"), | |
640 | "map object has key \"int2\""); | |
641 | ok(bt_object_map_has_key(map_obj, "float2"), | |
642 | "map object has key \"float2\""); | |
643 | ok(bt_object_map_has_key(map_obj, "string2"), | |
644 | "map object has key \"string2\""); | |
645 | ok(bt_object_map_has_key(map_obj, "array2"), | |
646 | "map object has key \"array2\""); | |
647 | ok(bt_object_map_has_key(map_obj, "map2"), | |
648 | "map object has key \"map2\""); | |
649 | ||
650 | ret = bt_object_map_foreach(NULL, test_map_foreach_cb_count, &count); | |
651 | ok(ret, "bt_object_map_foreach() fails with a map object set to NULL"); | |
652 | ret = bt_object_map_foreach(map_obj, NULL, &count); | |
653 | ok(ret, "bt_object_map_foreach() fails with a user function set to NULL"); | |
654 | ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_count, &count); | |
655 | ok(!ret && count == 3, | |
656 | "bt_object_map_foreach() breaks the loop when the user function returns false"); | |
657 | ||
658 | memset(&checklist, 0, sizeof(checklist)); | |
659 | ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_check, | |
660 | &checklist); | |
661 | ok(!ret, "bt_object_map_foreach() succeeds with test_map_foreach_cb_check()"); | |
662 | ok(checklist.bool1 && checklist.int1 && checklist.float1 && | |
663 | checklist.null1 && checklist.bool2 && checklist.int2 && | |
664 | checklist.float2 && checklist.string2 && | |
665 | checklist.array2 && checklist.map2, | |
666 | "bt_object_map_foreach() iterates over all the map object's elements"); | |
667 | ||
668 | BT_OBJECT_PUT(map_obj); | |
669 | pass("putting an existing map object does not cause a crash") | |
670 | } | |
671 | ||
672 | static | |
673 | void test_types(void) | |
674 | { | |
675 | test_null(); | |
676 | test_bool(); | |
677 | test_integer(); | |
678 | test_float(); | |
679 | test_string(); | |
680 | test_array(); | |
681 | test_map(); | |
682 | } | |
683 | ||
684 | static | |
685 | void test_compare_null(void) | |
686 | { | |
687 | ok(!bt_object_compare(bt_object_null, NULL), | |
688 | "cannot compare null object and NULL"); | |
689 | ok(!bt_object_compare(NULL, bt_object_null), | |
690 | "cannot compare NULL and null object"); | |
691 | ok(bt_object_compare(bt_object_null, bt_object_null), | |
692 | "null objects are equivalent"); | |
693 | } | |
694 | ||
695 | static | |
696 | void test_compare_bool(void) | |
697 | { | |
698 | struct bt_object *bool1 = bt_object_bool_create_init(false); | |
699 | struct bt_object *bool2 = bt_object_bool_create_init(true); | |
700 | struct bt_object *bool3 = bt_object_bool_create_init(false); | |
701 | ||
702 | assert(bool1 && bool2 && bool3); | |
703 | ok(!bt_object_compare(bt_object_null, bool1), | |
704 | "cannot compare null object and bool object"); | |
705 | ok(!bt_object_compare(bool1, bool2), | |
706 | "integer objects are not equivalent (false and true)"); | |
707 | ok(bt_object_compare(bool1, bool3), | |
708 | "integer objects are equivalent (false and false)"); | |
709 | ||
710 | BT_OBJECT_PUT(bool1); | |
711 | BT_OBJECT_PUT(bool2); | |
712 | BT_OBJECT_PUT(bool3); | |
713 | } | |
714 | ||
715 | static | |
716 | void test_compare_integer(void) | |
717 | { | |
718 | struct bt_object *int1 = bt_object_integer_create_init(10); | |
719 | struct bt_object *int2 = bt_object_integer_create_init(-23); | |
720 | struct bt_object *int3 = bt_object_integer_create_init(10); | |
721 | ||
722 | assert(int1 && int2 && int3); | |
723 | ok(!bt_object_compare(bt_object_null, int1), | |
724 | "cannot compare null object and integer object"); | |
725 | ok(!bt_object_compare(int1, int2), | |
726 | "integer objects are not equivalent (10 and -23)"); | |
727 | ok(bt_object_compare(int1, int3), | |
728 | "integer objects are equivalent (10 and 10)"); | |
729 | ||
730 | BT_OBJECT_PUT(int1); | |
731 | BT_OBJECT_PUT(int2); | |
732 | BT_OBJECT_PUT(int3); | |
733 | } | |
734 | ||
735 | static | |
736 | void test_compare_float(void) | |
737 | { | |
738 | struct bt_object *float1 = bt_object_float_create_init(17.38); | |
739 | struct bt_object *float2 = bt_object_float_create_init(-14.23); | |
740 | struct bt_object *float3 = bt_object_float_create_init(17.38); | |
741 | ||
742 | assert(float1 && float2 && float3); | |
743 | ||
744 | ok(!bt_object_compare(bt_object_null, float1), | |
745 | "cannot compare null object and floating point number object"); | |
746 | ok(!bt_object_compare(float1, float2), | |
747 | "floating point number objects are not equivalent (17.38 and -14.23)"); | |
748 | ok(bt_object_compare(float1, float3), | |
749 | "floating point number objects are equivalent (17.38 and 17.38)"); | |
750 | ||
751 | BT_OBJECT_PUT(float1); | |
752 | BT_OBJECT_PUT(float2); | |
753 | BT_OBJECT_PUT(float3); | |
754 | } | |
755 | ||
756 | static | |
757 | void test_compare_string(void) | |
758 | { | |
759 | struct bt_object *string1 = bt_object_string_create_init("hello"); | |
760 | struct bt_object *string2 = bt_object_string_create_init("bt_object"); | |
761 | struct bt_object *string3 = bt_object_string_create_init("hello"); | |
762 | ||
763 | assert(string1 && string2 && string3); | |
764 | ||
765 | ok(!bt_object_compare(bt_object_null, string1), | |
766 | "cannot compare null object and string object"); | |
767 | ok(!bt_object_compare(string1, string2), | |
768 | "string objects are not equivalent (\"hello\" and \"bt_object\")"); | |
769 | ok(bt_object_compare(string1, string3), | |
770 | "string objects are equivalent (\"hello\" and \"hello\")"); | |
771 | ||
772 | BT_OBJECT_PUT(string1); | |
773 | BT_OBJECT_PUT(string2); | |
774 | BT_OBJECT_PUT(string3); | |
775 | } | |
776 | ||
777 | static | |
778 | void test_compare_array(void) | |
779 | { | |
780 | struct bt_object *array1 = bt_object_array_create(); | |
781 | struct bt_object *array2 = bt_object_array_create(); | |
782 | struct bt_object *array3 = bt_object_array_create(); | |
783 | ||
784 | assert(array1 && array2 && array3); | |
785 | ||
786 | ok(bt_object_compare(array1, array2), | |
787 | "empty array objects are equivalent"); | |
788 | ||
789 | assert(!bt_object_array_append_integer(array1, 23)); | |
790 | assert(!bt_object_array_append_float(array1, 14.2)); | |
791 | assert(!bt_object_array_append_bool(array1, false)); | |
792 | assert(!bt_object_array_append_float(array2, 14.2)); | |
793 | assert(!bt_object_array_append_integer(array2, 23)); | |
794 | assert(!bt_object_array_append_bool(array2, false)); | |
795 | assert(!bt_object_array_append_integer(array3, 23)); | |
796 | assert(!bt_object_array_append_float(array3, 14.2)); | |
797 | assert(!bt_object_array_append_bool(array3, false)); | |
798 | assert(bt_object_array_size(array1) == 3); | |
799 | assert(bt_object_array_size(array2) == 3); | |
800 | assert(bt_object_array_size(array3) == 3); | |
801 | ||
802 | ok(!bt_object_compare(bt_object_null, array1), | |
803 | "cannot compare null object and array object"); | |
804 | ok(!bt_object_compare(array1, array2), | |
805 | "array objects are not equivalent ([23, 14.2, false] and [14.2, 23, false])"); | |
806 | ok(bt_object_compare(array1, array3), | |
807 | "array objects are equivalent ([23, 14.2, false] and [23, 14.2, false])"); | |
808 | ||
809 | BT_OBJECT_PUT(array1); | |
810 | BT_OBJECT_PUT(array2); | |
811 | BT_OBJECT_PUT(array3); | |
812 | } | |
813 | ||
814 | static | |
815 | void test_compare_map(void) | |
816 | { | |
817 | struct bt_object *map1 = bt_object_map_create(); | |
818 | struct bt_object *map2 = bt_object_map_create(); | |
819 | struct bt_object *map3 = bt_object_map_create(); | |
820 | ||
821 | assert(map1 && map2 && map3); | |
822 | ||
823 | ok(bt_object_compare(map1, map2), | |
824 | "empty map objects are equivalent"); | |
825 | ||
826 | assert(!bt_object_map_insert_integer(map1, "one", 23)); | |
827 | assert(!bt_object_map_insert_float(map1, "two", 14.2)); | |
828 | assert(!bt_object_map_insert_bool(map1, "three", false)); | |
829 | assert(!bt_object_map_insert_float(map2, "one", 14.2)); | |
830 | assert(!bt_object_map_insert_integer(map2, "two", 23)); | |
831 | assert(!bt_object_map_insert_bool(map2, "three", false)); | |
832 | assert(!bt_object_map_insert_bool(map3, "three", false)); | |
833 | assert(!bt_object_map_insert_integer(map3, "one", 23)); | |
834 | assert(!bt_object_map_insert_float(map3, "two", 14.2)); | |
835 | assert(bt_object_map_size(map1) == 3); | |
836 | assert(bt_object_map_size(map2) == 3); | |
837 | assert(bt_object_map_size(map3) == 3); | |
838 | ||
839 | ok(!bt_object_compare(bt_object_null, map1), | |
840 | "cannot compare null object and map object"); | |
841 | ok(!bt_object_compare(map1, map2), | |
842 | "map objects are not equivalent"); | |
843 | ok(bt_object_compare(map1, map3), | |
844 | "map objects are equivalent"); | |
845 | ||
846 | BT_OBJECT_PUT(map1); | |
847 | BT_OBJECT_PUT(map2); | |
848 | BT_OBJECT_PUT(map3); | |
849 | } | |
850 | ||
851 | static | |
852 | void test_compare(void) | |
853 | { | |
854 | ok(!bt_object_compare(NULL, NULL), "cannot compare NULL and NULL"); | |
855 | test_compare_null(); | |
856 | test_compare_bool(); | |
857 | test_compare_integer(); | |
858 | test_compare_float(); | |
859 | test_compare_string(); | |
860 | test_compare_array(); | |
861 | test_compare_map(); | |
862 | } | |
863 | ||
864 | static | |
865 | void test_copy(void) | |
866 | { | |
867 | /* | |
868 | * Here's the deal here. If we make sure that each object | |
869 | * of our deep copy has a different address than its source, | |
870 | * and that bt_object_compare() returns true for the top-level | |
871 | * object, taking into account that we test the correctness of | |
872 | * bt_object_compare() elsewhere, then the deep copy is a | |
873 | * success. | |
874 | */ | |
875 | struct bt_object *null_copy_obj; | |
876 | struct bt_object *bool_obj, *bool_copy_obj; | |
877 | struct bt_object *integer_obj, *integer_copy_obj; | |
878 | struct bt_object *float_obj, *float_copy_obj; | |
879 | struct bt_object *string_obj, *string_copy_obj; | |
880 | struct bt_object *array_obj, *array_copy_obj; | |
881 | struct bt_object *map_obj, *map_copy_obj; | |
882 | ||
883 | bool_obj = bt_object_bool_create_init(true); | |
884 | integer_obj = bt_object_integer_create_init(23); | |
885 | float_obj = bt_object_float_create_init(-3.1416); | |
886 | string_obj = bt_object_string_create_init("test"); | |
887 | array_obj = bt_object_array_create(); | |
888 | map_obj = bt_object_map_create(); | |
889 | ||
890 | assert(bool_obj && integer_obj && float_obj && string_obj && | |
891 | array_obj && map_obj); | |
892 | ||
893 | assert(!bt_object_array_append(array_obj, bool_obj)); | |
894 | assert(!bt_object_array_append(array_obj, integer_obj)); | |
895 | assert(!bt_object_array_append(array_obj, float_obj)); | |
896 | assert(!bt_object_array_append(array_obj, bt_object_null)); | |
897 | assert(!bt_object_map_insert(map_obj, "array", array_obj)); | |
898 | assert(!bt_object_map_insert(map_obj, "string", string_obj)); | |
899 | ||
900 | map_copy_obj = bt_object_copy(NULL); | |
901 | ok(!map_copy_obj, | |
902 | "bt_object_copy() fails with a source object set to NULL"); | |
903 | ||
904 | map_copy_obj = bt_object_copy(map_obj); | |
905 | ok(map_copy_obj, | |
906 | "bt_object_copy() succeeds"); | |
907 | ||
908 | ok(map_obj != map_copy_obj, | |
909 | "bt_object_copy() returns a different pointer (map)"); | |
910 | string_copy_obj = bt_object_map_get(map_copy_obj, "string"); | |
911 | ok(string_copy_obj != string_obj, | |
912 | "bt_object_copy() returns a different pointer (string)"); | |
913 | array_copy_obj = bt_object_map_get(map_copy_obj, "array"); | |
914 | ok(array_copy_obj != array_obj, | |
915 | "bt_object_copy() returns a different pointer (array)"); | |
916 | bool_copy_obj = bt_object_array_get(array_copy_obj, 0); | |
917 | ok(bool_copy_obj != bool_obj, | |
918 | "bt_object_copy() returns a different pointer (bool)"); | |
919 | integer_copy_obj = bt_object_array_get(array_copy_obj, 1); | |
920 | ok(integer_copy_obj != integer_obj, | |
921 | "bt_object_copy() returns a different pointer (integer)"); | |
922 | float_copy_obj = bt_object_array_get(array_copy_obj, 2); | |
923 | ok(float_copy_obj != float_obj, | |
924 | "bt_object_copy() returns a different pointer (float)"); | |
925 | null_copy_obj = bt_object_array_get(array_copy_obj, 3); | |
926 | ok(null_copy_obj == bt_object_null, | |
927 | "bt_object_copy() returns the same pointer (null)"); | |
928 | ||
929 | ok(bt_object_compare(map_obj, map_copy_obj), | |
930 | "source and destination objects have the same content"); | |
931 | ||
932 | BT_OBJECT_PUT(bool_copy_obj); | |
933 | BT_OBJECT_PUT(integer_copy_obj); | |
934 | BT_OBJECT_PUT(float_copy_obj); | |
935 | BT_OBJECT_PUT(string_copy_obj); | |
936 | BT_OBJECT_PUT(array_copy_obj); | |
937 | BT_OBJECT_PUT(map_copy_obj); | |
938 | BT_OBJECT_PUT(bool_obj); | |
939 | BT_OBJECT_PUT(integer_obj); | |
940 | BT_OBJECT_PUT(float_obj); | |
941 | BT_OBJECT_PUT(string_obj); | |
942 | BT_OBJECT_PUT(array_obj); | |
943 | BT_OBJECT_PUT(map_obj); | |
944 | } | |
945 | ||
946 | static | |
947 | void test_macros(void) | |
948 | { | |
949 | struct bt_object *obj = bt_object_bool_create(); | |
950 | struct bt_object *src; | |
951 | struct bt_object *dst; | |
952 | ||
953 | assert(obj); | |
954 | BT_OBJECT_PUT(obj); | |
955 | ok(!obj, "BT_OBJECT_PUT() resets the variable to NULL"); | |
956 | ||
957 | obj = bt_object_bool_create(); | |
958 | assert(obj); | |
959 | src = obj; | |
960 | BT_OBJECT_MOVE(dst, src); | |
961 | ok(!src, "BT_OBJECT_MOVE() resets the source variable to NULL"); | |
962 | ok(dst == obj, "BT_OBJECT_MOVE() moves the ownership"); | |
963 | ||
964 | BT_OBJECT_PUT(dst); | |
965 | } | |
966 | ||
967 | int main(void) | |
968 | { | |
969 | plan_no_plan(); | |
970 | ||
971 | test_macros(); | |
972 | test_types(); | |
973 | test_compare(); | |
974 | test_copy(); | |
975 | ||
976 | return 0; | |
977 | } |