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 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");
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");
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");
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");
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");
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");
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");
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");
350 BT_OBJECT_PUT(array_obj
);
351 pass("putting an existing array object does not cause a crash")
355 bool test_map_foreach_cb_count(const char *key
, struct bt_object
*object
,
369 struct map_foreach_checklist
{
383 bool test_map_foreach_cb_check(const char *key
, struct bt_object
*object
,
387 struct map_foreach_checklist
*checklist
= data
;
389 if (!strcmp(key
, "bool")) {
390 if (checklist
->bool1
) {
391 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
395 ret
= bt_object_bool_get(object
, &val
);
396 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool\" value");
399 pass("test_map_foreach_cb_check(): \"bool\" object has the right value");
400 checklist
->bool1
= true;
403 } else if (!strcmp(key
, "int")) {
404 if (checklist
->int1
) {
405 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
409 ret
= bt_object_integer_get(object
, &val
);
410 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
413 pass("test_map_foreach_cb_check(): \"int\" object has the right value");
414 checklist
->int1
= true;
417 } else if (!strcmp(key
, "float")) {
418 if (checklist
->float1
) {
419 fail("test_map_foreach_cb_check(): duplicate key \"float\"");
423 ret
= bt_object_float_get(object
, &val
);
424 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float\" value");
427 pass("test_map_foreach_cb_check(): \"float\" object has the right value");
428 checklist
->float1
= true;
431 } else if (!strcmp(key
, "null")) {
432 if (checklist
->null1
) {
433 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
435 ok(bt_object_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" object");
436 checklist
->null1
= true;
438 } else if (!strcmp(key
, "bool2")) {
439 if (checklist
->bool2
) {
440 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
444 ret
= bt_object_bool_get(object
, &val
);
445 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
448 pass("test_map_foreach_cb_check(): \"bool2\" object has the right value");
449 checklist
->bool2
= true;
452 } else if (!strcmp(key
, "int2")) {
453 if (checklist
->int2
) {
454 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
458 ret
= bt_object_integer_get(object
, &val
);
459 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
462 pass("test_map_foreach_cb_check(): \"int2\" object has the right value");
463 checklist
->int2
= true;
466 } else if (!strcmp(key
, "float2")) {
467 if (checklist
->float2
) {
468 fail("test_map_foreach_cb_check(): duplicate key \"float2\"");
472 ret
= bt_object_float_get(object
, &val
);
473 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float2\" value");
475 if (val
== -49.0001) {
476 pass("test_map_foreach_cb_check(): \"float2\" object has the right value");
477 checklist
->float2
= true;
480 } else if (!strcmp(key
, "string2")) {
481 if (checklist
->string2
) {
482 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
484 const char *val
= bt_object_string_get(object
);
486 ok(val
, "test_map_foreach_cb_check(): success getting \"string2\" value");
488 if (val
&& !strcmp(val
, "bt_object")) {
489 pass("test_map_foreach_cb_check(): \"string2\" object has the right value");
490 checklist
->string2
= true;
493 } else if (!strcmp(key
, "array2")) {
494 if (checklist
->array2
) {
495 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
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;
502 } else if (!strcmp(key
, "map2")) {
503 if (checklist
->map2
) {
504 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
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;
512 diag("test_map_foreach_cb_check(): unknown map key \"%s\"",
514 fail("test_map_foreach_cb_check(): unknown map key");
528 struct bt_object
*obj
;
529 struct bt_object
*map_obj
;
530 struct map_foreach_checklist checklist
;
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");
540 ok(bt_object_map_insert(NULL
, "hello", bt_object_null
),
541 "bt_object_array_insert() fails with a map object set to NULL");
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");
548 obj
= bt_object_integer_create_init(19457);
549 ret
= bt_object_map_insert(map_obj
, "int", obj
);
551 obj
= bt_object_float_create_init(5.444);
552 ret
|= bt_object_map_insert(map_obj
, "float", obj
);
554 obj
= bt_object_bool_create();
555 ret
|= bt_object_map_insert(map_obj
, "bool", 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");
562 obj
= bt_object_bool_create_init(true);
563 ret
= bt_object_map_insert(map_obj
, "bool", obj
);
565 ok(!ret
, "bt_object_map_insert() accepts an existing key");
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");
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)");
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)");
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)");
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");
624 ok(bt_object_map_size(map_obj
) == 10,
625 "the bt_object_map_insert*() functions increment the map object's size");
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\"");
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");
658 memset(&checklist
, 0, sizeof(checklist
));
659 ret
= bt_object_map_foreach(map_obj
, test_map_foreach_cb_check
,
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");
668 BT_OBJECT_PUT(map_obj
);
669 pass("putting an existing map object does not cause a crash")
673 void test_types(void)
685 void test_compare_null(void)
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");
696 void test_compare_bool(void)
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);
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)");
710 BT_OBJECT_PUT(bool1
);
711 BT_OBJECT_PUT(bool2
);
712 BT_OBJECT_PUT(bool3
);
716 void test_compare_integer(void)
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);
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)");
736 void test_compare_float(void)
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);
742 assert(float1
&& float2
&& float3
);
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)");
751 BT_OBJECT_PUT(float1
);
752 BT_OBJECT_PUT(float2
);
753 BT_OBJECT_PUT(float3
);
757 void test_compare_string(void)
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");
763 assert(string1
&& string2
&& string3
);
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\")");
772 BT_OBJECT_PUT(string1
);
773 BT_OBJECT_PUT(string2
);
774 BT_OBJECT_PUT(string3
);
778 void test_compare_array(void)
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();
784 assert(array1
&& array2
&& array3
);
786 ok(bt_object_compare(array1
, array2
),
787 "empty array objects are equivalent");
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);
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])");
809 BT_OBJECT_PUT(array1
);
810 BT_OBJECT_PUT(array2
);
811 BT_OBJECT_PUT(array3
);
815 void test_compare_map(void)
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();
821 assert(map1
&& map2
&& map3
);
823 ok(bt_object_compare(map1
, map2
),
824 "empty map objects are equivalent");
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);
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");
852 void test_compare(void)
854 ok(!bt_object_compare(NULL
, NULL
), "cannot compare NULL and NULL");
857 test_compare_integer();
858 test_compare_float();
859 test_compare_string();
860 test_compare_array();
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
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
;
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();
890 assert(bool_obj
&& integer_obj
&& float_obj
&& string_obj
&&
891 array_obj
&& map_obj
);
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
));
900 map_copy_obj
= bt_object_copy(NULL
);
902 "bt_object_copy() fails with a source object set to NULL");
904 map_copy_obj
= bt_object_copy(map_obj
);
906 "bt_object_copy() succeeds");
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)");
929 ok(bt_object_compare(map_obj
, map_copy_obj
),
930 "source and destination objects have the same content");
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
);
947 void test_macros(void)
949 struct bt_object
*obj
= bt_object_bool_create();
950 struct bt_object
*src
;
951 struct bt_object
*dst
;
955 ok(!obj
, "BT_OBJECT_PUT() resets the variable to NULL");
957 obj
= bt_object_bool_create();
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");