4 * Babeltrace value objects 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.
23 #include <babeltrace/object.h>
24 #include <babeltrace/values.h>
25 #include <babeltrace/private-values.h>
26 #include <babeltrace/assert-internal.h>
35 ok(bt_value_null
, "bt_value_null is not NULL");
36 ok(bt_value_is_null(bt_value_null
),
37 "bt_value_null is a null value object");
38 bt_object_get_ref(bt_value_null
);
39 pass("getting bt_value_null does not cause a crash");
40 bt_object_put_ref(bt_value_null
);
41 pass("putting bt_value_null does not cause a crash");
48 struct bt_private_value
*priv_obj
;
51 priv_obj
= bt_private_value_bool_create();
52 obj
= bt_private_value_borrow_value(priv_obj
);
53 ok(obj
&& bt_value_is_bool(obj
),
54 "bt_private_value_bool_create() returns a boolean value object");
57 value
= bt_value_bool_get(obj
);
58 ok(!value
, "default boolean value object value is BT_FALSE");
60 bt_private_value_bool_set(priv_obj
, BT_FALSE
);
61 bt_private_value_bool_set(priv_obj
, BT_TRUE
);
62 value
= bt_value_bool_get(obj
);
63 ok(value
, "bt_private_value_bool_set() works");
65 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
66 pass("putting an existing boolean value object does not cause a crash")
69 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
70 obj
= bt_private_value_borrow_value(priv_obj
);
71 ok(obj
&& bt_value_is_bool(obj
),
72 "bt_private_value_bool_create_init() returns a boolean value object");
73 value
= bt_value_bool_get(obj
);
75 "bt_private_value_bool_create_init() sets the appropriate initial value");
77 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
81 void test_integer(void)
84 struct bt_private_value
*priv_obj
;
87 priv_obj
= bt_private_value_integer_create();
88 obj
= bt_private_value_borrow_value(priv_obj
);
89 ok(obj
&& bt_value_is_integer(obj
),
90 "bt_private_value_integer_create() returns an integer value object");
93 value
= bt_value_integer_get(obj
);
94 ok(value
== 0, "default integer value object value is 0");
96 bt_private_value_integer_set(priv_obj
, -98765);
97 value
= bt_value_integer_get(obj
);
98 ok(value
== -98765, "bt_private_integer_bool_set() works");
100 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
101 pass("putting an existing integer value object does not cause a crash")
103 priv_obj
= bt_private_value_integer_create_init(321456987);
104 obj
= bt_private_value_borrow_value(priv_obj
);
105 ok(obj
&& bt_value_is_integer(obj
),
106 "bt_private_value_integer_create_init() returns an integer value object");
107 value
= bt_value_integer_get(obj
);
108 ok(value
== 321456987,
109 "bt_private_value_integer_create_init() sets the appropriate initial value");
111 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
118 struct bt_private_value
*priv_obj
;
119 struct bt_value
*obj
;
121 priv_obj
= bt_private_value_real_create();
122 obj
= bt_private_value_borrow_value(priv_obj
);
123 ok(obj
&& bt_value_is_real(obj
),
124 "bt_private_value_real_create() returns a real number value object");
127 value
= bt_value_real_get(obj
);
129 "default real number value object value is 0");
131 bt_private_value_real_set(priv_obj
, -3.1416);
132 value
= bt_value_real_get(obj
);
133 ok(value
== -3.1416, "bt_private_value_real_set() works");
135 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
136 pass("putting an existing real number value object does not cause a crash")
138 priv_obj
= bt_private_value_real_create_init(33.1649758);
139 obj
= bt_private_value_borrow_value(priv_obj
);
140 ok(obj
&& bt_value_is_real(obj
),
141 "bt_private_value_real_create_init() returns a real number value object");
142 value
= bt_value_real_get(obj
);
143 ok(value
== 33.1649758,
144 "bt_private_value_real_create_init() sets the appropriate initial value");
146 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
150 void test_string(void)
153 struct bt_private_value
*priv_obj
;
154 struct bt_value
*obj
;
156 priv_obj
= bt_private_value_string_create();
157 obj
= bt_private_value_borrow_value(priv_obj
);
158 ok(obj
&& bt_value_is_string(obj
),
159 "bt_private_value_string_create() returns a string value object");
161 value
= bt_value_string_get(obj
);
162 ok(value
&& !strcmp(value
, ""),
163 "default string value object value is \"\"");
165 bt_private_value_string_set(priv_obj
, "hello worldz");
166 value
= bt_value_string_get(obj
);
167 ok(value
&& !strcmp(value
, "hello worldz"),
168 "bt_value_string_get() works");
170 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
171 pass("putting an existing string value object does not cause a crash")
173 priv_obj
= bt_private_value_string_create_init("initial value");
174 obj
= bt_private_value_borrow_value(priv_obj
);
175 ok(obj
&& bt_value_is_string(obj
),
176 "bt_private_value_string_create_init() returns a string value object");
177 value
= bt_value_string_get(obj
);
178 ok(value
&& !strcmp(value
, "initial value"),
179 "bt_private_value_string_create_init() sets the appropriate initial value");
181 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
185 void test_array(void)
191 struct bt_private_value
*priv_obj
;
192 struct bt_value
*obj
;
193 const char *string_value
;
194 struct bt_private_value
*priv_array_obj
;
195 struct bt_value
*array_obj
;
197 priv_array_obj
= bt_private_value_array_create();
198 array_obj
= bt_private_value_borrow_value(priv_array_obj
);
199 ok(array_obj
&& bt_value_is_array(array_obj
),
200 "bt_private_value_array_create() returns an array value object");
201 ok(bt_value_array_is_empty(array_obj
),
202 "initial array value object size is 0");
204 priv_obj
= bt_private_value_integer_create_init(345);
205 obj
= bt_private_value_borrow_value(priv_obj
);
206 ret
= bt_private_value_array_append_element(priv_array_obj
, obj
);
207 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
208 priv_obj
= bt_private_value_real_create_init(-17.45);
209 obj
= bt_private_value_borrow_value(priv_obj
);
210 ret
|= bt_private_value_array_append_element(priv_array_obj
, obj
);
211 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
212 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
213 obj
= bt_private_value_borrow_value(priv_obj
);
214 ret
|= bt_private_value_array_append_element(priv_array_obj
, obj
);
215 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
216 ret
|= bt_private_value_array_append_element(priv_array_obj
,
218 ok(!ret
, "bt_private_value_array_append_element() succeeds");
219 ok(bt_value_array_get_size(array_obj
) == 4,
220 "appending an element to an array value object increment its size");
222 obj
= bt_value_array_borrow_element_by_index(array_obj
, 0);
223 ok(obj
&& bt_value_is_integer(obj
),
224 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (integer)");
225 int_value
= bt_value_integer_get(obj
);
227 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (integer)");
228 obj
= bt_value_array_borrow_element_by_index(array_obj
, 1);
229 ok(obj
&& bt_value_is_real(obj
),
230 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
231 real_value
= bt_value_real_get(obj
);
232 ok(real_value
== -17.45,
233 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
234 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
235 ok(obj
&& bt_value_is_bool(obj
),
236 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
237 bool_value
= bt_value_bool_get(obj
);
239 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
240 obj
= bt_value_array_borrow_element_by_index(array_obj
, 3);
241 ok(obj
== bt_value_null
,
242 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
244 priv_obj
= bt_private_value_integer_create_init(1001);
245 obj
= bt_private_value_borrow_value(priv_obj
);
247 ok(!bt_private_value_array_set_element_by_index(priv_array_obj
, 2, obj
),
248 "bt_value_array_set_element_by_index() succeeds");
249 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
250 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
251 ok(obj
&& bt_value_is_integer(obj
),
252 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
253 int_value
= bt_value_integer_get(obj
);
255 ok(int_value
== 1001,
256 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
258 ret
= bt_private_value_array_append_bool_element(priv_array_obj
,
260 ok(!ret
, "bt_private_value_array_append_bool_element() succeeds");
261 ret
= bt_private_value_array_append_integer_element(priv_array_obj
,
263 ok(!ret
, "bt_private_value_array_append_integer_element() succeeds");
264 ret
= bt_private_value_array_append_real_element(priv_array_obj
,
266 ok(!ret
, "bt_private_value_array_append_real_element() succeeds");
267 ret
= bt_private_value_array_append_string_element(priv_array_obj
,
269 ok(!ret
, "bt_private_value_array_append_string_element() succeeds");
270 ret
= bt_private_value_array_append_empty_array_element(priv_array_obj
);
271 ok(!ret
, "bt_private_value_array_append_empty_array_element() succeeds");
272 ret
= bt_private_value_array_append_empty_map_element(priv_array_obj
);
273 ok(!ret
, "bt_private_value_array_append_empty_map_element() succeeds");
275 ok(bt_value_array_get_size(array_obj
) == 10,
276 "the bt_private_value_array_append_element_*() functions increment the array value object's size");
277 ok(!bt_value_array_is_empty(array_obj
),
278 "map value object is not empty");
280 obj
= bt_value_array_borrow_element_by_index(array_obj
, 4);
281 ok(obj
&& bt_value_is_bool(obj
),
282 "bt_private_value_array_append_bool_element() appends a boolean value object");
283 bool_value
= bt_value_bool_get(obj
);
285 "bt_private_value_array_append_bool_element() appends the appropriate value");
286 obj
= bt_value_array_borrow_element_by_index(array_obj
, 5);
287 ok(obj
&& bt_value_is_integer(obj
),
288 "bt_private_value_array_append_integer_element() appends an integer value object");
289 int_value
= bt_value_integer_get(obj
);
290 ok(int_value
== 98765,
291 "bt_private_value_array_append_integer_element() appends the appropriate value");
292 obj
= bt_value_array_borrow_element_by_index(array_obj
, 6);
293 ok(obj
&& bt_value_is_real(obj
),
294 "bt_private_value_array_append_real_element() appends a real number value object");
295 real_value
= bt_value_real_get(obj
);
296 ok(real_value
== 2.49578,
297 "bt_private_value_array_append_real_element() appends the appropriate value");
298 obj
= bt_value_array_borrow_element_by_index(array_obj
, 7);
299 ok(obj
&& bt_value_is_string(obj
),
300 "bt_private_value_array_append_string_element() appends a string value object");
301 string_value
= bt_value_string_get(obj
);
302 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
303 "bt_private_value_array_append_string_element() appends the appropriate value");
304 obj
= bt_value_array_borrow_element_by_index(array_obj
, 8);
305 ok(obj
&& bt_value_is_array(obj
),
306 "bt_private_value_array_append_empty_array_element() appends an array value object");
307 ok(bt_value_array_is_empty(obj
),
308 "bt_private_value_array_append_empty_array_element() an empty array value object");
309 obj
= bt_value_array_borrow_element_by_index(array_obj
, 9);
310 ok(obj
&& bt_value_is_map(obj
),
311 "bt_private_value_array_append_empty_map_element() appends a map value object");
312 ok(bt_value_map_is_empty(obj
),
313 "bt_private_value_array_append_empty_map_element() an empty map value object");
315 BT_OBJECT_PUT_REF_AND_RESET(priv_array_obj
);
316 pass("putting an existing array value object does not cause a crash")
320 bt_bool
test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
334 struct map_foreach_checklist
{
348 bt_bool
test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
351 struct map_foreach_checklist
*checklist
= data
;
353 if (!strcmp(key
, "bt_bool")) {
354 if (checklist
->bool1
) {
355 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
357 bt_bool val
= BT_FALSE
;
359 val
= bt_value_bool_get(object
);
362 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
363 checklist
->bool1
= BT_TRUE
;
365 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
368 } else if (!strcmp(key
, "int")) {
369 if (checklist
->int1
) {
370 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
374 val
= bt_value_integer_get(object
);
377 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
378 checklist
->int1
= BT_TRUE
;
380 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
383 } else if (!strcmp(key
, "real")) {
384 if (checklist
->real1
) {
385 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
389 val
= bt_value_real_get(object
);
392 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
393 checklist
->real1
= BT_TRUE
;
395 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
398 } else if (!strcmp(key
, "null")) {
399 if (checklist
->null1
) {
400 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
402 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
403 checklist
->null1
= BT_TRUE
;
405 } else if (!strcmp(key
, "bool2")) {
406 if (checklist
->bool2
) {
407 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
409 bt_bool val
= BT_FALSE
;
411 val
= bt_value_bool_get(object
);
414 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
415 checklist
->bool2
= BT_TRUE
;
417 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
420 } else if (!strcmp(key
, "int2")) {
421 if (checklist
->int2
) {
422 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
426 val
= bt_value_integer_get(object
);
429 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
430 checklist
->int2
= BT_TRUE
;
432 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
435 } else if (!strcmp(key
, "real2")) {
436 if (checklist
->real2
) {
437 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
441 val
= bt_value_real_get(object
);
443 if (val
== -49.0001) {
444 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
445 checklist
->real2
= BT_TRUE
;
447 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
450 } else if (!strcmp(key
, "string2")) {
451 if (checklist
->string2
) {
452 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
456 val
= bt_value_string_get(object
);
458 if (val
&& !strcmp(val
, "bt_value")) {
459 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
460 checklist
->string2
= BT_TRUE
;
462 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
465 } else if (!strcmp(key
, "array2")) {
466 if (checklist
->array2
) {
467 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
469 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
470 ok(bt_value_array_is_empty(object
),
471 "test_map_foreach_cb_check(): \"array2\" value object is empty");
472 checklist
->array2
= BT_TRUE
;
474 } else if (!strcmp(key
, "map2")) {
475 if (checklist
->map2
) {
476 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
478 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
479 ok(bt_value_map_is_empty(object
),
480 "test_map_foreach_cb_check(): \"map2\" value object is empty");
481 checklist
->map2
= BT_TRUE
;
484 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
499 struct bt_private_value
*priv_obj
;
500 struct bt_value
*obj
;
501 struct bt_private_value
*priv_map_obj
;
502 struct bt_value
*map_obj
;
503 struct map_foreach_checklist checklist
;
505 priv_map_obj
= bt_private_value_map_create();
506 map_obj
= bt_private_value_borrow_value(priv_map_obj
);
507 ok(map_obj
&& bt_value_is_map(map_obj
),
508 "bt_private_value_map_create() returns a map value object");
509 ok(bt_value_map_get_size(map_obj
) == 0,
510 "initial map value object size is 0");
512 priv_obj
= bt_private_value_integer_create_init(19457);
513 obj
= bt_private_value_borrow_value(priv_obj
);
514 ret
= bt_private_value_map_insert_entry(priv_map_obj
, "int", obj
);
515 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
516 priv_obj
= bt_private_value_real_create_init(5.444);
517 obj
= bt_private_value_borrow_value(priv_obj
);
518 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "real", obj
);
519 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
520 priv_obj
= bt_private_value_bool_create();
521 obj
= bt_private_value_borrow_value(priv_obj
);
522 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "bt_bool", obj
);
523 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
524 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "null",
526 ok(!ret
, "bt_private_value_map_insert_entry() succeeds");
527 ok(bt_value_map_get_size(map_obj
) == 4,
528 "inserting an element into a map value object increment its size");
530 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
531 obj
= bt_private_value_borrow_value(priv_obj
);
532 ret
= bt_private_value_map_insert_entry(priv_map_obj
, "bt_bool", obj
);
533 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
534 ok(!ret
, "bt_private_value_map_insert_entry() accepts an existing key");
536 obj
= bt_value_map_borrow_entry_value(map_obj
, "life");
537 ok(!obj
, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
538 obj
= bt_value_map_borrow_entry_value(map_obj
, "real");
539 ok(obj
&& bt_value_is_real(obj
),
540 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
541 real_value
= bt_value_real_get(obj
);
542 ok(real_value
== 5.444,
543 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
544 obj
= bt_value_map_borrow_entry_value(map_obj
, "int");
545 ok(obj
&& bt_value_is_integer(obj
),
546 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
547 int_value
= bt_value_integer_get(obj
);
548 ok(int_value
== 19457,
549 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (integer)");
550 obj
= bt_value_map_borrow_entry_value(map_obj
, "null");
551 ok(obj
&& bt_value_is_null(obj
),
552 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
553 obj
= bt_value_map_borrow_entry_value(map_obj
, "bt_bool");
554 ok(obj
&& bt_value_is_bool(obj
),
555 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
556 bool_value
= bt_value_bool_get(obj
);
558 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
560 ret
= bt_private_value_map_insert_bool_entry(priv_map_obj
, "bool2",
562 ok(!ret
, "bt_private_value_map_insert_bool_entry() succeeds");
563 ret
= bt_private_value_map_insert_integer_entry(priv_map_obj
, "int2",
565 ok(!ret
, "bt_private_value_map_insert_integer_entry() succeeds");
566 ret
= bt_private_value_map_insert_real_entry(priv_map_obj
, "real2",
568 ok(!ret
, "bt_private_value_map_insert_real_entry() succeeds");
569 ret
= bt_private_value_map_insert_string_entry(priv_map_obj
, "string2",
571 ok(!ret
, "bt_private_value_map_insert_string_entry() succeeds");
572 ret
= bt_private_value_map_insert_empty_array_entry(priv_map_obj
,
574 ok(!ret
, "bt_private_value_map_insert_empty_array_entry() succeeds");
575 ret
= bt_private_value_map_insert_empty_map_entry(priv_map_obj
, "map2");
576 ok(!ret
, "bt_private_value_map_insert_empty_map_entry() succeeds");
578 ok(bt_value_map_get_size(map_obj
) == 10,
579 "the bt_value_map_insert*() functions increment the map value object's size");
581 ok(!bt_value_map_has_entry(map_obj
, "hello"),
582 "map value object does not have key \"hello\"");
583 ok(bt_value_map_has_entry(map_obj
, "bt_bool"),
584 "map value object has key \"bt_bool\"");
585 ok(bt_value_map_has_entry(map_obj
, "int"),
586 "map value object has key \"int\"");
587 ok(bt_value_map_has_entry(map_obj
, "real"),
588 "map value object has key \"real\"");
589 ok(bt_value_map_has_entry(map_obj
, "null"),
590 "map value object has key \"null\"");
591 ok(bt_value_map_has_entry(map_obj
, "bool2"),
592 "map value object has key \"bool2\"");
593 ok(bt_value_map_has_entry(map_obj
, "int2"),
594 "map value object has key \"int2\"");
595 ok(bt_value_map_has_entry(map_obj
, "real2"),
596 "map value object has key \"real2\"");
597 ok(bt_value_map_has_entry(map_obj
, "string2"),
598 "map value object has key \"string2\"");
599 ok(bt_value_map_has_entry(map_obj
, "array2"),
600 "map value object has key \"array2\"");
601 ok(bt_value_map_has_entry(map_obj
, "map2"),
602 "map value object has key \"map2\"");
604 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_count
,
606 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
607 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
609 memset(&checklist
, 0, sizeof(checklist
));
610 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_check
,
612 ok(ret
== BT_VALUE_STATUS_OK
,
613 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
614 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.real1
&&
615 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
616 checklist
.real2
&& checklist
.string2
&&
617 checklist
.array2
&& checklist
.map2
,
618 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
620 BT_OBJECT_PUT_REF_AND_RESET(priv_map_obj
);
621 pass("putting an existing map value object does not cause a crash")
625 void test_types(void)
637 void test_compare_null(void)
639 ok(bt_value_compare(bt_value_null
, bt_value_null
),
640 "null value objects are equivalent");
644 void test_compare_bool(void)
646 struct bt_private_value
*bool1
=
647 bt_private_value_bool_create_init(BT_FALSE
);
648 struct bt_private_value
*bool2
=
649 bt_private_value_bool_create_init(BT_TRUE
);
650 struct bt_private_value
*bool3
=
651 bt_private_value_bool_create_init(BT_FALSE
);
653 BT_ASSERT(bool1
&& bool2
&& bool3
);
654 ok(!bt_value_compare(bt_value_null
,
655 bt_private_value_borrow_value(bool1
)),
656 "cannot compare null value object and bt_bool value object");
657 ok(!bt_value_compare(bt_private_value_borrow_value(bool1
),
658 bt_private_value_borrow_value(bool2
)),
659 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
660 ok(bt_value_compare(bt_private_value_borrow_value(bool1
),
661 bt_private_value_borrow_value(bool3
)),
662 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
664 BT_OBJECT_PUT_REF_AND_RESET(bool1
);
665 BT_OBJECT_PUT_REF_AND_RESET(bool2
);
666 BT_OBJECT_PUT_REF_AND_RESET(bool3
);
670 void test_compare_integer(void)
672 struct bt_private_value
*int1
=
673 bt_private_value_integer_create_init(10);
674 struct bt_private_value
*int2
=
675 bt_private_value_integer_create_init(-23);
676 struct bt_private_value
*int3
=
677 bt_private_value_integer_create_init(10);
679 BT_ASSERT(int1
&& int2
&& int3
);
680 ok(!bt_value_compare(bt_value_null
,
681 bt_private_value_borrow_value(int1
)),
682 "cannot compare null value object and integer value object");
683 ok(!bt_value_compare(bt_private_value_borrow_value(int1
),
684 bt_private_value_borrow_value(int2
)),
685 "integer value objects are not equivalent (10 and -23)");
686 ok(bt_value_compare(bt_private_value_borrow_value(int1
),
687 bt_private_value_borrow_value(int3
)),
688 "integer value objects are equivalent (10 and 10)");
690 BT_OBJECT_PUT_REF_AND_RESET(int1
);
691 BT_OBJECT_PUT_REF_AND_RESET(int2
);
692 BT_OBJECT_PUT_REF_AND_RESET(int3
);
696 void test_compare_real(void)
698 struct bt_private_value
*real1
=
699 bt_private_value_real_create_init(17.38);
700 struct bt_private_value
*real2
=
701 bt_private_value_real_create_init(-14.23);
702 struct bt_private_value
*real3
=
703 bt_private_value_real_create_init(17.38);
705 BT_ASSERT(real1
&& real2
&& real3
);
707 ok(!bt_value_compare(bt_value_null
,
708 bt_private_value_borrow_value(real1
)),
709 "cannot compare null value object and real number value object");
710 ok(!bt_value_compare(bt_private_value_borrow_value(real1
),
711 bt_private_value_borrow_value(real2
)),
712 "real number value objects are not equivalent (17.38 and -14.23)");
713 ok(bt_value_compare(bt_private_value_borrow_value(real1
),
714 bt_private_value_borrow_value(real3
)),
715 "real number value objects are equivalent (17.38 and 17.38)");
717 BT_OBJECT_PUT_REF_AND_RESET(real1
);
718 BT_OBJECT_PUT_REF_AND_RESET(real2
);
719 BT_OBJECT_PUT_REF_AND_RESET(real3
);
723 void test_compare_string(void)
725 struct bt_private_value
*string1
=
726 bt_private_value_string_create_init("hello");
727 struct bt_private_value
*string2
=
728 bt_private_value_string_create_init("bt_value");
729 struct bt_private_value
*string3
=
730 bt_private_value_string_create_init("hello");
732 BT_ASSERT(string1
&& string2
&& string3
);
734 ok(!bt_value_compare(bt_value_null
,
735 bt_private_value_borrow_value(string1
)),
736 "cannot compare null value object and string value object");
737 ok(!bt_value_compare(bt_private_value_borrow_value(string1
),
738 bt_private_value_borrow_value(string2
)),
739 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
740 ok(bt_value_compare(bt_private_value_borrow_value(string1
),
741 bt_private_value_borrow_value(string3
)),
742 "string value objects are equivalent (\"hello\" and \"hello\")");
744 BT_OBJECT_PUT_REF_AND_RESET(string1
);
745 BT_OBJECT_PUT_REF_AND_RESET(string2
);
746 BT_OBJECT_PUT_REF_AND_RESET(string3
);
750 void test_compare_array(void)
752 struct bt_private_value
*array1
= bt_private_value_array_create();
753 struct bt_private_value
*array2
= bt_private_value_array_create();
754 struct bt_private_value
*array3
= bt_private_value_array_create();
755 enum bt_value_status status
;
757 BT_ASSERT(array1
&& array2
&& array3
);
759 ok(bt_value_compare(bt_private_value_borrow_value(array1
),
760 bt_private_value_borrow_value(array2
)),
761 "empty array value objects are equivalent");
763 status
= bt_private_value_array_append_integer_element(array1
, 23);
764 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
765 status
= bt_private_value_array_append_real_element(array1
, 14.2);
766 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
767 status
= bt_private_value_array_append_bool_element(array1
, BT_FALSE
);
768 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
769 status
= bt_private_value_array_append_real_element(array2
, 14.2);
770 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
771 status
= bt_private_value_array_append_integer_element(array2
, 23);
772 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
773 status
= bt_private_value_array_append_bool_element(array2
, BT_FALSE
);
774 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
775 status
= bt_private_value_array_append_integer_element(array3
, 23);
776 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
777 status
= bt_private_value_array_append_real_element(array3
, 14.2);
778 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
779 status
= bt_private_value_array_append_bool_element(array3
, BT_FALSE
);
780 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
781 BT_ASSERT(bt_value_array_get_size(
782 bt_private_value_borrow_value(array1
)) == 3);
783 BT_ASSERT(bt_value_array_get_size(
784 bt_private_value_borrow_value(array2
)) == 3);
785 BT_ASSERT(bt_value_array_get_size(
786 bt_private_value_borrow_value(array3
)) == 3);
788 ok(!bt_value_compare(bt_value_null
,
789 bt_private_value_borrow_value(array1
)),
790 "cannot compare null value object and array value object");
791 ok(!bt_value_compare(bt_private_value_borrow_value(array1
),
792 bt_private_value_borrow_value(array2
)),
793 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
794 ok(bt_value_compare(bt_private_value_borrow_value(array1
),
795 bt_private_value_borrow_value(array3
)),
796 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
798 BT_OBJECT_PUT_REF_AND_RESET(array1
);
799 BT_OBJECT_PUT_REF_AND_RESET(array2
);
800 BT_OBJECT_PUT_REF_AND_RESET(array3
);
804 void test_compare_map(void)
806 struct bt_private_value
*map1
= bt_private_value_map_create();
807 struct bt_private_value
*map2
= bt_private_value_map_create();
808 struct bt_private_value
*map3
= bt_private_value_map_create();
809 enum bt_value_status status
;
811 BT_ASSERT(map1
&& map2
&& map3
);
813 ok(bt_value_compare(bt_private_value_borrow_value(map1
),
814 bt_private_value_borrow_value(map2
)),
815 "empty map value objects are equivalent");
818 status
= bt_private_value_map_insert_integer_entry(map1
, "one", 23);
819 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
820 status
= bt_private_value_map_insert_real_entry(map1
, "two", 14.2);
821 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
822 status
= bt_private_value_map_insert_bool_entry(map1
, "three",
824 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
825 status
= bt_private_value_map_insert_real_entry(map2
, "one", 14.2);
826 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
827 status
= bt_private_value_map_insert_integer_entry(map2
, "two", 23);
828 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
829 status
= bt_private_value_map_insert_bool_entry(map2
, "three",
831 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
832 status
= bt_private_value_map_insert_bool_entry(map3
, "three",
834 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
835 status
= bt_private_value_map_insert_integer_entry(map3
, "one", 23);
836 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
837 status
= bt_private_value_map_insert_real_entry(map3
, "two", 14.2);
838 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
839 BT_ASSERT(bt_value_map_get_size(
840 bt_private_value_borrow_value(map1
)) == 3);
841 BT_ASSERT(bt_value_map_get_size(
842 bt_private_value_borrow_value(map2
)) == 3);
843 BT_ASSERT(bt_value_map_get_size(
844 bt_private_value_borrow_value(map3
)) == 3);
846 ok(!bt_value_compare(bt_value_null
,
847 bt_private_value_borrow_value(map1
)),
848 "cannot compare null value object and map value object");
849 ok(!bt_value_compare(bt_private_value_borrow_value(map1
),
850 bt_private_value_borrow_value(map2
)),
851 "map value objects are not equivalent");
852 ok(bt_value_compare(bt_private_value_borrow_value(map1
),
853 bt_private_value_borrow_value(map3
)),
854 "map value objects are equivalent");
856 BT_OBJECT_PUT_REF_AND_RESET(map1
);
857 BT_OBJECT_PUT_REF_AND_RESET(map2
);
858 BT_OBJECT_PUT_REF_AND_RESET(map3
);
862 void test_compare(void)
866 test_compare_integer();
868 test_compare_string();
869 test_compare_array();
877 * Here's the deal here. If we make sure that each value object
878 * of our deep copy has a different address than its source,
879 * and that bt_value_compare() returns BT_TRUE for the top-level
880 * value object, taking into account that we test the correctness of
881 * bt_value_compare() elsewhere, then the deep copy is a
884 struct bt_private_value
*null_copy_obj
;
885 struct bt_private_value
*bool_obj
, *bool_copy_obj
;
886 struct bt_private_value
*integer_obj
, *integer_copy_obj
;
887 struct bt_private_value
*real_obj
, *real_copy_obj
;
888 struct bt_private_value
*string_obj
, *string_copy_obj
;
889 struct bt_private_value
*array_obj
, *array_copy_obj
;
890 struct bt_private_value
*map_obj
, *map_copy_obj
;
891 enum bt_value_status status
;
893 bool_obj
= bt_private_value_bool_create_init(BT_TRUE
);
894 integer_obj
= bt_private_value_integer_create_init(23);
895 real_obj
= bt_private_value_real_create_init(-3.1416);
896 string_obj
= bt_private_value_string_create_init("test");
897 array_obj
= bt_private_value_array_create();
898 map_obj
= bt_private_value_map_create();
900 BT_ASSERT(bool_obj
&& integer_obj
&& real_obj
&& string_obj
&&
901 array_obj
&& map_obj
);
903 status
= bt_private_value_array_append_element(array_obj
,
904 bt_private_value_borrow_value(bool_obj
));
905 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
906 status
= bt_private_value_array_append_element(array_obj
,
907 bt_private_value_borrow_value(integer_obj
));
908 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
909 status
= bt_private_value_array_append_element(array_obj
,
910 bt_private_value_borrow_value(real_obj
));
911 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
912 status
= bt_private_value_array_append_element(array_obj
,
914 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
915 status
= bt_private_value_map_insert_entry(map_obj
, "array",
916 bt_private_value_borrow_value(array_obj
));
917 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
918 status
= bt_private_value_map_insert_entry(map_obj
, "string",
919 bt_private_value_borrow_value(string_obj
));
920 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
922 status
= bt_value_copy(&map_copy_obj
,
923 bt_private_value_borrow_value(map_obj
));
924 ok(status
== BT_VALUE_STATUS_OK
&& map_copy_obj
,
925 "bt_value_copy() succeeds");
927 ok(map_obj
!= map_copy_obj
,
928 "bt_value_copy() returns a different pointer (map)");
929 string_copy_obj
= bt_private_value_map_borrow_entry_value(map_copy_obj
,
931 ok(string_copy_obj
!= string_obj
,
932 "bt_value_copy() returns a different pointer (string)");
933 array_copy_obj
= bt_private_value_map_borrow_entry_value(map_copy_obj
,
935 ok(array_copy_obj
!= array_obj
,
936 "bt_value_copy() returns a different pointer (array)");
937 bool_copy_obj
= bt_private_value_array_borrow_element_by_index(
939 ok(bool_copy_obj
!= bool_obj
,
940 "bt_value_copy() returns a different pointer (bt_bool)");
941 integer_copy_obj
= bt_private_value_array_borrow_element_by_index(
943 ok(integer_copy_obj
!= integer_obj
,
944 "bt_value_copy() returns a different pointer (integer)");
945 real_copy_obj
= bt_private_value_array_borrow_element_by_index(
947 ok(real_copy_obj
!= real_obj
,
948 "bt_value_copy() returns a different pointer (real)");
949 null_copy_obj
= bt_private_value_array_borrow_element_by_index(
951 ok(bt_private_value_borrow_value(null_copy_obj
) == bt_value_null
,
952 "bt_value_copy() returns the same pointer (null)");
954 ok(bt_value_compare(bt_private_value_borrow_value(map_obj
),
955 bt_private_value_borrow_value(map_copy_obj
)),
956 "source and destination value objects have the same content");
958 BT_OBJECT_PUT_REF_AND_RESET(map_copy_obj
);
959 BT_OBJECT_PUT_REF_AND_RESET(bool_obj
);
960 BT_OBJECT_PUT_REF_AND_RESET(integer_obj
);
961 BT_OBJECT_PUT_REF_AND_RESET(real_obj
);
962 BT_OBJECT_PUT_REF_AND_RESET(string_obj
);
963 BT_OBJECT_PUT_REF_AND_RESET(array_obj
);
964 BT_OBJECT_PUT_REF_AND_RESET(map_obj
);
968 bt_bool
compare_map_elements(struct bt_value
*map_a
, struct bt_value
*map_b
,
971 struct bt_value
*elem_a
= NULL
;
972 struct bt_value
*elem_b
= NULL
;
975 elem_a
= bt_value_map_borrow_entry_value(map_a
, key
);
976 elem_b
= bt_value_map_borrow_entry_value(map_b
, key
);
977 equal
= bt_value_compare(elem_a
, elem_b
);
982 void test_extend(void)
984 struct bt_private_value
*base_map
= bt_private_value_map_create();
985 struct bt_private_value
*extension_map
= bt_private_value_map_create();
986 struct bt_private_value
*extended_map
= NULL
;
987 struct bt_private_value
*array
= bt_private_value_array_create();
988 enum bt_value_status status
;
991 BT_ASSERT(extension_map
);
993 status
= bt_private_value_map_insert_bool_entry(base_map
, "file",
995 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
996 status
= bt_private_value_map_insert_bool_entry(base_map
, "edit",
998 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
999 status
= bt_private_value_map_insert_integer_entry(base_map
,
1001 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1002 status
= bt_private_value_map_insert_integer_entry(base_map
, "find",
1004 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1005 status
= bt_private_value_map_insert_bool_entry(extension_map
, "edit",
1007 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1008 status
= bt_private_value_map_insert_integer_entry(extension_map
,
1010 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1011 status
= bt_private_value_map_insert_real_entry(extension_map
,
1013 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1014 status
= bt_value_map_extend(
1016 bt_private_value_borrow_value(base_map
),
1017 bt_private_value_borrow_value(extension_map
));
1018 ok(status
== BT_VALUE_STATUS_OK
&&
1019 extended_map
, "bt_value_map_extend() succeeds");
1020 ok(bt_value_map_get_size(
1021 bt_private_value_borrow_value(extended_map
)) == 5,
1022 "bt_value_map_extend() returns a map object with the correct size");
1023 ok(compare_map_elements(bt_private_value_borrow_value(base_map
),
1024 bt_private_value_borrow_value(extended_map
), "file"),
1025 "bt_value_map_extend() picks the appropriate element (file)");
1026 ok(compare_map_elements(bt_private_value_borrow_value(extension_map
),
1027 bt_private_value_borrow_value(extended_map
), "edit"),
1028 "bt_value_map_extend() picks the appropriate element (edit)");
1029 ok(compare_map_elements(bt_private_value_borrow_value(base_map
),
1030 bt_private_value_borrow_value(extended_map
), "selection"),
1031 "bt_value_map_extend() picks the appropriate element (selection)");
1032 ok(compare_map_elements(bt_private_value_borrow_value(extension_map
),
1033 bt_private_value_borrow_value(extended_map
), "find"),
1034 "bt_value_map_extend() picks the appropriate element (find)");
1035 ok(compare_map_elements(bt_private_value_borrow_value(extension_map
),
1036 bt_private_value_borrow_value(extended_map
), "project"),
1037 "bt_value_map_extend() picks the appropriate element (project)");
1039 BT_OBJECT_PUT_REF_AND_RESET(array
);
1040 BT_OBJECT_PUT_REF_AND_RESET(base_map
);
1041 BT_OBJECT_PUT_REF_AND_RESET(extension_map
);
1042 BT_OBJECT_PUT_REF_AND_RESET(extended_map
);
1047 plan_tests(NR_TESTS
);