4 * Babeltrace basic object system tests
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
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.
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.
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.
24 #include <babeltrace/objects.h>
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");
41 pass("getting NULL does not cause a crash");
43 pass("putting NULL does not cause a crash");
45 ok(bt_object_get_type(NULL
) == BT_OBJECT_TYPE_UNKNOWN
,
46 "bt_object_get_type(NULL) returns BT_OBJECT_TYPE_UNKNOWN");
54 struct bt_object
*obj
;
56 obj
= bt_object_bool_create();
57 ok(obj
&& bt_object_is_bool(obj
),
58 "bt_object_bool_create() returns a boolean object");
61 ret
= bt_object_bool_get(obj
, &value
);
62 ok(!ret
&& !value
, "default boolean object value is false");
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");
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");
75 pass("putting an existing boolean object does not cause a crash")
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
);
82 "bt_object_bool_create_init() sets the appropriate initial value");
88 void test_integer(void)
92 struct bt_object
*obj
;
94 obj
= bt_object_integer_create();
95 ok(obj
&& bt_object_is_integer(obj
),
96 "bt_object_integer_create() returns an integer object");
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");
104 ret
= bt_object_integer_get(obj
, &value
);
105 ok(!ret
&& value
== 0, "default integer object value is 0");
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");
113 pass("putting an existing integer object does not cause a crash")
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");
126 void test_float(void)
130 struct bt_object
*obj
;
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");
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");
142 ret
= bt_object_float_get(obj
, &value
);
143 ok(!ret
&& value
== 0., "default floating point number object value is 0");
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");
151 pass("putting an existing floating point number object does not cause a crash")
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");
164 void test_string(void)
168 struct bt_object
*obj
;
170 obj
= bt_object_string_create();
171 ok(obj
&& bt_object_is_string(obj
),
172 "bt_object_string_create() returns a string object");
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");
179 value
= bt_object_string_get(obj
);
180 ok(value
&& !strcmp(value
, ""),
181 "default string object value is \"\"");
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");
192 pass("putting an existing string object does not cause a crash")
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");
207 void test_array(void)
213 struct bt_object
*obj
;
214 const char *string_value
;
215 struct bt_object
*array_obj
;
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");
225 ok(bt_object_array_append(NULL
, bt_object_null
),
226 "bt_object_array_append() fails with an array object set to NULL");
228 obj
= bt_object_integer_create_init(345);
229 ret
= bt_object_array_append(array_obj
, obj
);
231 obj
= bt_object_float_create_init(-17.45);
232 ret
|= bt_object_array_append(array_obj
, obj
);
234 obj
= bt_object_bool_create_init(true);
235 ret
|= bt_object_array_append(array_obj
, 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");
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");
251 obj
= bt_object_array_get(NULL
, 2);
252 ok(!obj
, "bt_object_array_get() fails with an array object set to NULL");
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)");
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)");
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)");
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)");
279 ok(bt_object_array_set(NULL
, 0, bt_object_null
),
280 "bt_object_array_set() fails with an array object set to NULL");
281 ok(bt_object_array_set(array_obj
, 0, NULL
),
282 "bt_object_array_set() fails with an element object set to NULL");
283 ok(bt_object_array_set(array_obj
, 4, bt_object_null
),
284 "bt_object_array_set() fails with an invalid index");
285 obj
= bt_object_integer_create_init(1001);
287 ok(!bt_object_array_set(array_obj
, 2, obj
),
288 "bt_object_array_set() succeeds");
290 obj
= bt_object_array_get(array_obj
, 2);
291 ok(obj
&& bt_object_is_integer(obj
),
292 "bt_object_array_set() inserts an object with the appropriate type");
293 ret
= bt_object_integer_get(obj
, &int_value
);
295 ok(int_value
== 1001,
296 "bt_object_array_set() inserts an object with the appropriate value");
299 ret
= bt_object_array_append_bool(array_obj
, false);
300 ok(!ret
, "bt_object_array_append_bool() succeeds");
301 ret
= bt_object_array_append_bool(NULL
, true);
302 ok(ret
, "bt_object_array_append_bool() fails with an array object set to NULL");
303 ret
= bt_object_array_append_integer(array_obj
, 98765);
304 ok(!ret
, "bt_object_array_append_integer() succeeds");
305 ret
= bt_object_array_append_integer(NULL
, 18765);
306 ok(ret
, "bt_object_array_append_integer() fails with an array object set to NULL");
307 ret
= bt_object_array_append_float(array_obj
, 2.49578);
308 ok(!ret
, "bt_object_array_append_float() succeeds");
309 ret
= bt_object_array_append_float(NULL
, 1.49578);
310 ok(ret
, "bt_object_array_append_float() fails with an array object set to NULL");
311 ret
= bt_object_array_append_string(array_obj
, "bt_object");
312 ok(!ret
, "bt_object_array_append_string() succeeds");
313 ret
= bt_object_array_append_string(NULL
, "bt_obj");
314 ok(ret
, "bt_object_array_append_string() fails with an array object set to NULL");
315 ret
= bt_object_array_append_array(array_obj
);
316 ok(!ret
, "bt_object_array_append_array() succeeds");
317 ret
= bt_object_array_append_array(NULL
);
318 ok(ret
, "bt_object_array_append_array() fails with an array object set to NULL");
319 ret
= bt_object_array_append_map(array_obj
);
320 ok(!ret
, "bt_object_array_append_map() succeeds");
321 ret
= bt_object_array_append_map(NULL
);
322 ok(ret
, "bt_object_array_append_map() fails with an array object set to NULL");
324 ok(bt_object_array_size(array_obj
) == 10,
325 "the bt_object_array_append_*() functions increment the array object's size");
326 ok(!bt_object_array_is_empty(array_obj
),
327 "map object is not empty");
329 obj
= bt_object_array_get(array_obj
, 4);
330 ok(obj
&& bt_object_is_bool(obj
),
331 "bt_object_array_append_bool() appends a boolean object");
332 ret
= bt_object_bool_get(obj
, &bool_value
);
333 ok(!ret
&& !bool_value
,
334 "bt_object_array_append_bool() appends the appropriate value");
336 obj
= bt_object_array_get(array_obj
, 5);
337 ok(obj
&& bt_object_is_integer(obj
),
338 "bt_object_array_append_integer() appends an integer object");
339 ret
= bt_object_integer_get(obj
, &int_value
);
340 ok(!ret
&& int_value
== 98765,
341 "bt_object_array_append_integer() appends the appropriate value");
343 obj
= bt_object_array_get(array_obj
, 6);
344 ok(obj
&& bt_object_is_float(obj
),
345 "bt_object_array_append_float() appends a floating point number object");
346 ret
= bt_object_float_get(obj
, &float_value
);
347 ok(!ret
&& float_value
== 2.49578,
348 "bt_object_array_append_float() appends the appropriate value");
350 obj
= bt_object_array_get(array_obj
, 7);
351 ok(obj
&& bt_object_is_string(obj
),
352 "bt_object_array_append_string() appends a string object");
353 string_value
= bt_object_string_get(obj
);
354 ok(string_value
&& !strcmp(string_value
, "bt_object"),
355 "bt_object_array_append_string() appends the appropriate value");
357 obj
= bt_object_array_get(array_obj
, 8);
358 ok(obj
&& bt_object_is_array(obj
),
359 "bt_object_array_append_array() appends an array object");
360 ok(bt_object_array_is_empty(obj
),
361 "bt_object_array_append_array() an empty array object");
363 obj
= bt_object_array_get(array_obj
, 9);
364 ok(obj
&& bt_object_is_map(obj
),
365 "bt_object_array_append_map() appends a map object");
366 ok(bt_object_map_is_empty(obj
),
367 "bt_object_array_append_map() an empty map object");
370 BT_OBJECT_PUT(array_obj
);
371 pass("putting an existing array object does not cause a crash")
375 bool test_map_foreach_cb_count(const char *key
, struct bt_object
*object
,
389 struct map_foreach_checklist
{
403 bool test_map_foreach_cb_check(const char *key
, struct bt_object
*object
,
407 struct map_foreach_checklist
*checklist
= data
;
409 if (!strcmp(key
, "bool")) {
410 if (checklist
->bool1
) {
411 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
415 ret
= bt_object_bool_get(object
, &val
);
416 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool\" value");
419 pass("test_map_foreach_cb_check(): \"bool\" object has the right value");
420 checklist
->bool1
= true;
423 } else if (!strcmp(key
, "int")) {
424 if (checklist
->int1
) {
425 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
429 ret
= bt_object_integer_get(object
, &val
);
430 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
433 pass("test_map_foreach_cb_check(): \"int\" object has the right value");
434 checklist
->int1
= true;
437 } else if (!strcmp(key
, "float")) {
438 if (checklist
->float1
) {
439 fail("test_map_foreach_cb_check(): duplicate key \"float\"");
443 ret
= bt_object_float_get(object
, &val
);
444 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float\" value");
447 pass("test_map_foreach_cb_check(): \"float\" object has the right value");
448 checklist
->float1
= true;
451 } else if (!strcmp(key
, "null")) {
452 if (checklist
->null1
) {
453 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
455 ok(bt_object_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" object");
456 checklist
->null1
= true;
458 } else if (!strcmp(key
, "bool2")) {
459 if (checklist
->bool2
) {
460 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
464 ret
= bt_object_bool_get(object
, &val
);
465 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
468 pass("test_map_foreach_cb_check(): \"bool2\" object has the right value");
469 checklist
->bool2
= true;
472 } else if (!strcmp(key
, "int2")) {
473 if (checklist
->int2
) {
474 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
478 ret
= bt_object_integer_get(object
, &val
);
479 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
482 pass("test_map_foreach_cb_check(): \"int2\" object has the right value");
483 checklist
->int2
= true;
486 } else if (!strcmp(key
, "float2")) {
487 if (checklist
->float2
) {
488 fail("test_map_foreach_cb_check(): duplicate key \"float2\"");
492 ret
= bt_object_float_get(object
, &val
);
493 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float2\" value");
495 if (val
== -49.0001) {
496 pass("test_map_foreach_cb_check(): \"float2\" object has the right value");
497 checklist
->float2
= true;
500 } else if (!strcmp(key
, "string2")) {
501 if (checklist
->string2
) {
502 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
504 const char *val
= bt_object_string_get(object
);
506 ok(val
, "test_map_foreach_cb_check(): success getting \"string2\" value");
508 if (val
&& !strcmp(val
, "bt_object")) {
509 pass("test_map_foreach_cb_check(): \"string2\" object has the right value");
510 checklist
->string2
= true;
513 } else if (!strcmp(key
, "array2")) {
514 if (checklist
->array2
) {
515 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
517 ok(bt_object_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" object");
518 ok(bt_object_array_is_empty(object
),
519 "test_map_foreach_cb_check(): \"array2\" object is empty");
520 checklist
->array2
= true;
522 } else if (!strcmp(key
, "map2")) {
523 if (checklist
->map2
) {
524 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
526 ok(bt_object_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" object");
527 ok(bt_object_map_is_empty(object
),
528 "test_map_foreach_cb_check(): \"map2\" object is empty");
529 checklist
->map2
= true;
532 diag("test_map_foreach_cb_check(): unknown map key \"%s\"",
534 fail("test_map_foreach_cb_check(): unknown map key");
548 struct bt_object
*obj
;
549 struct bt_object
*map_obj
;
550 struct map_foreach_checklist checklist
;
552 map_obj
= bt_object_map_create();
553 ok(map_obj
&& bt_object_is_map(map_obj
),
554 "bt_object_map_create() returns a map object");
555 ok(bt_object_map_size(map_obj
) == 0,
556 "initial map object size is 0");
557 ok(bt_object_map_size(NULL
) < 0,
558 "bt_object_map_size() fails with a map object set to NULL");
560 ok(bt_object_map_insert(NULL
, "hello", bt_object_null
),
561 "bt_object_array_insert() fails with a map object set to NULL");
563 ok(bt_object_map_insert(map_obj
, NULL
, bt_object_null
),
564 "bt_object_array_insert() fails with a key set to NULL");
565 ok(bt_object_map_insert(map_obj
, "yeah", NULL
),
566 "bt_object_array_insert() fails with an element object set to NULL");
568 obj
= bt_object_integer_create_init(19457);
569 ret
= bt_object_map_insert(map_obj
, "int", obj
);
571 obj
= bt_object_float_create_init(5.444);
572 ret
|= bt_object_map_insert(map_obj
, "float", obj
);
574 obj
= bt_object_bool_create();
575 ret
|= bt_object_map_insert(map_obj
, "bool", obj
);
577 ret
|= bt_object_map_insert(map_obj
, "null", bt_object_null
);
578 ok(!ret
, "bt_object_map_insert() succeeds");
579 ok(bt_object_map_size(map_obj
) == 4,
580 "inserting an element into a map object increment its size");
582 obj
= bt_object_bool_create_init(true);
583 ret
= bt_object_map_insert(map_obj
, "bool", obj
);
585 ok(!ret
, "bt_object_map_insert() accepts an existing key");
587 obj
= bt_object_map_get(map_obj
, NULL
);
588 ok(!obj
, "bt_object_map_get() fails with a key set to NULL");
589 obj
= bt_object_map_get(NULL
, "bool");
590 ok(!obj
, "bt_object_map_get() fails with a map object set to NULL");
592 obj
= bt_object_map_get(map_obj
, "life");
593 ok(!obj
, "bt_object_map_get() fails with an non existing key");
594 obj
= bt_object_map_get(map_obj
, "float");
595 ok(obj
&& bt_object_is_float(obj
),
596 "bt_object_map_get() returns an object with the appropriate type (float)");
597 ret
= bt_object_float_get(obj
, &float_value
);
598 ok(!ret
&& float_value
== 5.444,
599 "bt_object_map_get() returns an object with the appropriate value (float)");
601 obj
= bt_object_map_get(map_obj
, "int");
602 ok(obj
&& bt_object_is_integer(obj
),
603 "bt_object_map_get() returns an object with the appropriate type (integer)");
604 ret
= bt_object_integer_get(obj
, &int_value
);
605 ok(!ret
&& int_value
== 19457,
606 "bt_object_map_get() returns an object with the appropriate value (integer)");
608 obj
= bt_object_map_get(map_obj
, "null");
609 ok(obj
&& bt_object_is_null(obj
),
610 "bt_object_map_get() returns an object with the appropriate type (null)");
611 obj
= bt_object_map_get(map_obj
, "bool");
612 ok(obj
&& bt_object_is_bool(obj
),
613 "bt_object_map_get() returns an object with the appropriate type (boolean)");
614 ret
= bt_object_bool_get(obj
, &bool_value
);
615 ok(!ret
&& bool_value
,
616 "bt_object_map_get() returns an object with the appropriate value (boolean)");
619 ret
= bt_object_map_insert_bool(map_obj
, "bool2", true);
620 ok(!ret
, "bt_object_map_insert_bool() succeeds");
621 ret
= bt_object_map_insert_bool(NULL
, "bool2", false);
622 ok(ret
, "bt_object_map_insert_bool() fails with a map object set to NULL");
623 ret
= bt_object_map_insert_integer(map_obj
, "int2", 98765);
624 ok(!ret
, "bt_object_map_insert_integer() succeeds");
625 ret
= bt_object_map_insert_integer(NULL
, "int2", 1001);
626 ok(ret
, "bt_object_map_insert_integer() fails with a map object set to NULL");
627 ret
= bt_object_map_insert_float(map_obj
, "float2", -49.0001);
628 ok(!ret
, "bt_object_map_insert_float() succeeds");
629 ret
= bt_object_map_insert_float(NULL
, "float2", 495);
630 ok(ret
, "bt_object_map_insert_float() fails with a map object set to NULL");
631 ret
= bt_object_map_insert_string(map_obj
, "string2", "bt_object");
632 ok(!ret
, "bt_object_map_insert_string() succeeds");
633 ret
= bt_object_map_insert_string(NULL
, "string2", "bt_obj");
634 ok(ret
, "bt_object_map_insert_string() fails with a map object set to NULL");
635 ret
= bt_object_map_insert_array(map_obj
, "array2");
636 ok(!ret
, "bt_object_map_insert_array() succeeds");
637 ret
= bt_object_map_insert_array(NULL
, "array2");
638 ok(ret
, "bt_object_map_insert_array() fails with a map object set to NULL");
639 ret
= bt_object_map_insert_map(map_obj
, "map2");
640 ok(!ret
, "bt_object_map_insert_map() succeeds");
641 ret
= bt_object_map_insert_map(NULL
, "map2");
642 ok(ret
, "bt_object_map_insert_map() fails with a map object set to NULL");
644 ok(bt_object_map_size(map_obj
) == 10,
645 "the bt_object_map_insert*() functions increment the map object's size");
647 ok(!bt_object_map_has_key(map_obj
, "hello"),
648 "map object does not have key \"hello\"");
649 ok(bt_object_map_has_key(map_obj
, "bool"),
650 "map object has key \"bool\"");
651 ok(bt_object_map_has_key(map_obj
, "int"),
652 "map object has key \"int\"");
653 ok(bt_object_map_has_key(map_obj
, "float"),
654 "map object has key \"float\"");
655 ok(bt_object_map_has_key(map_obj
, "null"),
656 "map object has key \"null\"");
657 ok(bt_object_map_has_key(map_obj
, "bool2"),
658 "map object has key \"bool2\"");
659 ok(bt_object_map_has_key(map_obj
, "int2"),
660 "map object has key \"int2\"");
661 ok(bt_object_map_has_key(map_obj
, "float2"),
662 "map object has key \"float2\"");
663 ok(bt_object_map_has_key(map_obj
, "string2"),
664 "map object has key \"string2\"");
665 ok(bt_object_map_has_key(map_obj
, "array2"),
666 "map object has key \"array2\"");
667 ok(bt_object_map_has_key(map_obj
, "map2"),
668 "map object has key \"map2\"");
670 ret
= bt_object_map_foreach(NULL
, test_map_foreach_cb_count
, &count
);
671 ok(ret
== BT_OBJECT_STATUS_ERROR
,
672 "bt_object_map_foreach() fails with a map object set to NULL");
673 ret
= bt_object_map_foreach(map_obj
, NULL
, &count
);
674 ok(ret
== BT_OBJECT_STATUS_ERROR
,
675 "bt_object_map_foreach() fails with a user function set to NULL");
676 ret
= bt_object_map_foreach(map_obj
, test_map_foreach_cb_count
, &count
);
677 ok(ret
== BT_OBJECT_STATUS_CANCELLED
&& count
== 3,
678 "bt_object_map_foreach() breaks the loop when the user function returns false");
680 memset(&checklist
, 0, sizeof(checklist
));
681 ret
= bt_object_map_foreach(map_obj
, test_map_foreach_cb_check
,
683 ok(ret
== BT_OBJECT_STATUS_OK
,
684 "bt_object_map_foreach() succeeds with test_map_foreach_cb_check()");
685 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.float1
&&
686 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
687 checklist
.float2
&& checklist
.string2
&&
688 checklist
.array2
&& checklist
.map2
,
689 "bt_object_map_foreach() iterates over all the map object's elements");
691 BT_OBJECT_PUT(map_obj
);
692 pass("putting an existing map object does not cause a crash")
696 void test_types(void)
708 void test_compare_null(void)
710 ok(!bt_object_compare(bt_object_null
, NULL
),
711 "cannot compare null object and NULL");
712 ok(!bt_object_compare(NULL
, bt_object_null
),
713 "cannot compare NULL and null object");
714 ok(bt_object_compare(bt_object_null
, bt_object_null
),
715 "null objects are equivalent");
719 void test_compare_bool(void)
721 struct bt_object
*bool1
= bt_object_bool_create_init(false);
722 struct bt_object
*bool2
= bt_object_bool_create_init(true);
723 struct bt_object
*bool3
= bt_object_bool_create_init(false);
725 assert(bool1
&& bool2
&& bool3
);
726 ok(!bt_object_compare(bt_object_null
, bool1
),
727 "cannot compare null object and bool object");
728 ok(!bt_object_compare(bool1
, bool2
),
729 "integer objects are not equivalent (false and true)");
730 ok(bt_object_compare(bool1
, bool3
),
731 "integer objects are equivalent (false and false)");
733 BT_OBJECT_PUT(bool1
);
734 BT_OBJECT_PUT(bool2
);
735 BT_OBJECT_PUT(bool3
);
739 void test_compare_integer(void)
741 struct bt_object
*int1
= bt_object_integer_create_init(10);
742 struct bt_object
*int2
= bt_object_integer_create_init(-23);
743 struct bt_object
*int3
= bt_object_integer_create_init(10);
745 assert(int1
&& int2
&& int3
);
746 ok(!bt_object_compare(bt_object_null
, int1
),
747 "cannot compare null object and integer object");
748 ok(!bt_object_compare(int1
, int2
),
749 "integer objects are not equivalent (10 and -23)");
750 ok(bt_object_compare(int1
, int3
),
751 "integer objects are equivalent (10 and 10)");
759 void test_compare_float(void)
761 struct bt_object
*float1
= bt_object_float_create_init(17.38);
762 struct bt_object
*float2
= bt_object_float_create_init(-14.23);
763 struct bt_object
*float3
= bt_object_float_create_init(17.38);
765 assert(float1
&& float2
&& float3
);
767 ok(!bt_object_compare(bt_object_null
, float1
),
768 "cannot compare null object and floating point number object");
769 ok(!bt_object_compare(float1
, float2
),
770 "floating point number objects are not equivalent (17.38 and -14.23)");
771 ok(bt_object_compare(float1
, float3
),
772 "floating point number objects are equivalent (17.38 and 17.38)");
774 BT_OBJECT_PUT(float1
);
775 BT_OBJECT_PUT(float2
);
776 BT_OBJECT_PUT(float3
);
780 void test_compare_string(void)
782 struct bt_object
*string1
= bt_object_string_create_init("hello");
783 struct bt_object
*string2
= bt_object_string_create_init("bt_object");
784 struct bt_object
*string3
= bt_object_string_create_init("hello");
786 assert(string1
&& string2
&& string3
);
788 ok(!bt_object_compare(bt_object_null
, string1
),
789 "cannot compare null object and string object");
790 ok(!bt_object_compare(string1
, string2
),
791 "string objects are not equivalent (\"hello\" and \"bt_object\")");
792 ok(bt_object_compare(string1
, string3
),
793 "string objects are equivalent (\"hello\" and \"hello\")");
795 BT_OBJECT_PUT(string1
);
796 BT_OBJECT_PUT(string2
);
797 BT_OBJECT_PUT(string3
);
801 void test_compare_array(void)
803 struct bt_object
*array1
= bt_object_array_create();
804 struct bt_object
*array2
= bt_object_array_create();
805 struct bt_object
*array3
= bt_object_array_create();
807 assert(array1
&& array2
&& array3
);
809 ok(bt_object_compare(array1
, array2
),
810 "empty array objects are equivalent");
812 assert(!bt_object_array_append_integer(array1
, 23));
813 assert(!bt_object_array_append_float(array1
, 14.2));
814 assert(!bt_object_array_append_bool(array1
, false));
815 assert(!bt_object_array_append_float(array2
, 14.2));
816 assert(!bt_object_array_append_integer(array2
, 23));
817 assert(!bt_object_array_append_bool(array2
, false));
818 assert(!bt_object_array_append_integer(array3
, 23));
819 assert(!bt_object_array_append_float(array3
, 14.2));
820 assert(!bt_object_array_append_bool(array3
, false));
821 assert(bt_object_array_size(array1
) == 3);
822 assert(bt_object_array_size(array2
) == 3);
823 assert(bt_object_array_size(array3
) == 3);
825 ok(!bt_object_compare(bt_object_null
, array1
),
826 "cannot compare null object and array object");
827 ok(!bt_object_compare(array1
, array2
),
828 "array objects are not equivalent ([23, 14.2, false] and [14.2, 23, false])");
829 ok(bt_object_compare(array1
, array3
),
830 "array objects are equivalent ([23, 14.2, false] and [23, 14.2, false])");
832 BT_OBJECT_PUT(array1
);
833 BT_OBJECT_PUT(array2
);
834 BT_OBJECT_PUT(array3
);
838 void test_compare_map(void)
840 struct bt_object
*map1
= bt_object_map_create();
841 struct bt_object
*map2
= bt_object_map_create();
842 struct bt_object
*map3
= bt_object_map_create();
844 assert(map1
&& map2
&& map3
);
846 ok(bt_object_compare(map1
, map2
),
847 "empty map objects are equivalent");
849 assert(!bt_object_map_insert_integer(map1
, "one", 23));
850 assert(!bt_object_map_insert_float(map1
, "two", 14.2));
851 assert(!bt_object_map_insert_bool(map1
, "three", false));
852 assert(!bt_object_map_insert_float(map2
, "one", 14.2));
853 assert(!bt_object_map_insert_integer(map2
, "two", 23));
854 assert(!bt_object_map_insert_bool(map2
, "three", false));
855 assert(!bt_object_map_insert_bool(map3
, "three", false));
856 assert(!bt_object_map_insert_integer(map3
, "one", 23));
857 assert(!bt_object_map_insert_float(map3
, "two", 14.2));
858 assert(bt_object_map_size(map1
) == 3);
859 assert(bt_object_map_size(map2
) == 3);
860 assert(bt_object_map_size(map3
) == 3);
862 ok(!bt_object_compare(bt_object_null
, map1
),
863 "cannot compare null object and map object");
864 ok(!bt_object_compare(map1
, map2
),
865 "map objects are not equivalent");
866 ok(bt_object_compare(map1
, map3
),
867 "map objects are equivalent");
875 void test_compare(void)
877 ok(!bt_object_compare(NULL
, NULL
), "cannot compare NULL and NULL");
880 test_compare_integer();
881 test_compare_float();
882 test_compare_string();
883 test_compare_array();
891 * Here's the deal here. If we make sure that each object
892 * of our deep copy has a different address than its source,
893 * and that bt_object_compare() returns true for the top-level
894 * object, taking into account that we test the correctness of
895 * bt_object_compare() elsewhere, then the deep copy is a
898 struct bt_object
*null_copy_obj
;
899 struct bt_object
*bool_obj
, *bool_copy_obj
;
900 struct bt_object
*integer_obj
, *integer_copy_obj
;
901 struct bt_object
*float_obj
, *float_copy_obj
;
902 struct bt_object
*string_obj
, *string_copy_obj
;
903 struct bt_object
*array_obj
, *array_copy_obj
;
904 struct bt_object
*map_obj
, *map_copy_obj
;
906 bool_obj
= bt_object_bool_create_init(true);
907 integer_obj
= bt_object_integer_create_init(23);
908 float_obj
= bt_object_float_create_init(-3.1416);
909 string_obj
= bt_object_string_create_init("test");
910 array_obj
= bt_object_array_create();
911 map_obj
= bt_object_map_create();
913 assert(bool_obj
&& integer_obj
&& float_obj
&& string_obj
&&
914 array_obj
&& map_obj
);
916 assert(!bt_object_array_append(array_obj
, bool_obj
));
917 assert(!bt_object_array_append(array_obj
, integer_obj
));
918 assert(!bt_object_array_append(array_obj
, float_obj
));
919 assert(!bt_object_array_append(array_obj
, bt_object_null
));
920 assert(!bt_object_map_insert(map_obj
, "array", array_obj
));
921 assert(!bt_object_map_insert(map_obj
, "string", string_obj
));
923 map_copy_obj
= bt_object_copy(NULL
);
925 "bt_object_copy() fails with a source object set to NULL");
927 map_copy_obj
= bt_object_copy(map_obj
);
929 "bt_object_copy() succeeds");
931 ok(map_obj
!= map_copy_obj
,
932 "bt_object_copy() returns a different pointer (map)");
933 string_copy_obj
= bt_object_map_get(map_copy_obj
, "string");
934 ok(string_copy_obj
!= string_obj
,
935 "bt_object_copy() returns a different pointer (string)");
936 array_copy_obj
= bt_object_map_get(map_copy_obj
, "array");
937 ok(array_copy_obj
!= array_obj
,
938 "bt_object_copy() returns a different pointer (array)");
939 bool_copy_obj
= bt_object_array_get(array_copy_obj
, 0);
940 ok(bool_copy_obj
!= bool_obj
,
941 "bt_object_copy() returns a different pointer (bool)");
942 integer_copy_obj
= bt_object_array_get(array_copy_obj
, 1);
943 ok(integer_copy_obj
!= integer_obj
,
944 "bt_object_copy() returns a different pointer (integer)");
945 float_copy_obj
= bt_object_array_get(array_copy_obj
, 2);
946 ok(float_copy_obj
!= float_obj
,
947 "bt_object_copy() returns a different pointer (float)");
948 null_copy_obj
= bt_object_array_get(array_copy_obj
, 3);
949 ok(null_copy_obj
== bt_object_null
,
950 "bt_object_copy() returns the same pointer (null)");
952 ok(bt_object_compare(map_obj
, map_copy_obj
),
953 "source and destination objects have the same content");
955 BT_OBJECT_PUT(bool_copy_obj
);
956 BT_OBJECT_PUT(integer_copy_obj
);
957 BT_OBJECT_PUT(float_copy_obj
);
958 BT_OBJECT_PUT(string_copy_obj
);
959 BT_OBJECT_PUT(array_copy_obj
);
960 BT_OBJECT_PUT(map_copy_obj
);
961 BT_OBJECT_PUT(bool_obj
);
962 BT_OBJECT_PUT(integer_obj
);
963 BT_OBJECT_PUT(float_obj
);
964 BT_OBJECT_PUT(string_obj
);
965 BT_OBJECT_PUT(array_obj
);
966 BT_OBJECT_PUT(map_obj
);
970 void test_macros(void)
972 struct bt_object
*obj
= bt_object_bool_create();
973 struct bt_object
*src
;
974 struct bt_object
*dst
;
978 ok(!obj
, "BT_OBJECT_PUT() resets the variable to NULL");
980 obj
= bt_object_bool_create();
983 BT_OBJECT_MOVE(dst
, src
);
984 ok(!src
, "BT_OBJECT_MOVE() resets the source variable to NULL");
985 ok(dst
== obj
, "BT_OBJECT_MOVE() moves the ownership");