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 <babeltrace2/babeltrace.h>
24 #include "common/assert.h"
33 ok(bt_value_null
, "bt_value_null is not NULL");
34 ok(bt_value_is_null(bt_value_null
),
35 "bt_value_null is a null value object");
36 bt_value_get_ref(bt_value_null
);
37 pass("getting bt_value_null does not cause a crash");
38 bt_value_put_ref(bt_value_null
);
39 pass("putting bt_value_null does not cause a crash");
48 obj
= bt_value_bool_create();
49 ok(obj
&& bt_value_is_bool(obj
),
50 "bt_value_bool_create() returns a boolean value object");
53 value
= bt_value_bool_get(obj
);
54 ok(!value
, "default boolean value object value is BT_FALSE");
56 bt_value_bool_set(obj
, BT_FALSE
);
57 bt_value_bool_set(obj
, BT_TRUE
);
58 value
= bt_value_bool_get(obj
);
59 ok(value
, "bt_value_bool_set() works");
61 BT_VALUE_PUT_REF_AND_RESET(obj
);
62 pass("putting an existing boolean value object does not cause a crash")
65 obj
= bt_value_bool_create_init(BT_TRUE
);
66 ok(obj
&& bt_value_is_bool(obj
),
67 "bt_value_bool_create_init() returns a boolean value object");
68 value
= bt_value_bool_get(obj
);
70 "bt_value_bool_create_init() sets the appropriate initial value");
72 BT_VALUE_PUT_REF_AND_RESET(obj
);
76 void test_unsigned_integer(void)
81 obj
= bt_value_unsigned_integer_create();
82 ok(obj
&& bt_value_is_unsigned_integer(obj
),
83 "bt_value_unsigned_integer_create() returns an unsigned integer value object");
86 value
= bt_value_unsigned_integer_get(obj
);
87 ok(value
== 0, "default unsigned integer value object value is 0");
89 bt_value_unsigned_integer_set(obj
, 98765);
90 value
= bt_value_unsigned_integer_get(obj
);
91 ok(value
== 98765, "bt_value_unsigned_integer_bool_set() works");
93 BT_VALUE_PUT_REF_AND_RESET(obj
);
94 pass("putting an existing unsigned integer value object does not cause a crash")
96 obj
= bt_value_unsigned_integer_create_init(321456987);
97 ok(obj
&& bt_value_is_unsigned_integer(obj
),
98 "bt_value_unsigned_integer_create_init() returns an unsigned integer value object");
99 value
= bt_value_unsigned_integer_get(obj
);
100 ok(value
== 321456987,
101 "bt_value_unsigned_integer_create_init() sets the appropriate initial value");
103 BT_VALUE_PUT_REF_AND_RESET(obj
);
107 void test_signed_integer(void)
112 obj
= bt_value_signed_integer_create();
113 ok(obj
&& bt_value_is_signed_integer(obj
),
114 "bt_value_signed_integer_create() returns a signed integer value object");
117 value
= bt_value_signed_integer_get(obj
);
118 ok(value
== 0, "default signed integer value object value is 0");
120 bt_value_signed_integer_set(obj
, 98765);
121 value
= bt_value_signed_integer_get(obj
);
122 ok(value
== 98765, "bt_value_signed_integer_bool_set() works");
124 BT_VALUE_PUT_REF_AND_RESET(obj
);
125 pass("putting an existing signed integer value object does not cause a crash")
127 obj
= bt_value_signed_integer_create_init(-321456987);
128 ok(obj
&& bt_value_is_signed_integer(obj
),
129 "bt_value_signed_integer_create_init() returns a signed integer value object");
130 value
= bt_value_signed_integer_get(obj
);
131 ok(value
== -321456987,
132 "bt_value_signed_integer_create_init() sets the appropriate initial value");
134 BT_VALUE_PUT_REF_AND_RESET(obj
);
143 obj
= bt_value_real_create();
144 ok(obj
&& bt_value_is_real(obj
),
145 "bt_value_real_create() returns a real number value object");
148 value
= bt_value_real_get(obj
);
150 "default real number value object value is 0");
152 bt_value_real_set(obj
, -3.1416);
153 value
= bt_value_real_get(obj
);
154 ok(value
== -3.1416, "bt_value_real_set() works");
156 BT_VALUE_PUT_REF_AND_RESET(obj
);
157 pass("putting an existing real number value object does not cause a crash")
159 obj
= bt_value_real_create_init(33.1649758);
160 ok(obj
&& bt_value_is_real(obj
),
161 "bt_value_real_create_init() returns a real number value object");
162 value
= bt_value_real_get(obj
);
163 ok(value
== 33.1649758,
164 "bt_value_real_create_init() sets the appropriate initial value");
166 BT_VALUE_PUT_REF_AND_RESET(obj
);
170 void test_string(void)
175 obj
= bt_value_string_create();
176 ok(obj
&& bt_value_is_string(obj
),
177 "bt_value_string_create() returns a string value object");
179 value
= bt_value_string_get(obj
);
180 ok(value
&& !strcmp(value
, ""),
181 "default string value object value is \"\"");
183 bt_value_string_set(obj
, "hello worldz");
184 value
= bt_value_string_get(obj
);
185 ok(value
&& !strcmp(value
, "hello worldz"),
186 "bt_value_string_get() works");
188 BT_VALUE_PUT_REF_AND_RESET(obj
);
189 pass("putting an existing string value object does not cause a crash")
191 obj
= bt_value_string_create_init("initial value");
192 ok(obj
&& bt_value_is_string(obj
),
193 "bt_value_string_create_init() returns a string value object");
194 value
= bt_value_string_get(obj
);
195 ok(value
&& !strcmp(value
, "initial value"),
196 "bt_value_string_create_init() sets the appropriate initial value");
198 BT_VALUE_PUT_REF_AND_RESET(obj
);
202 void test_array(void)
209 const char *string_value
;
212 array_obj
= bt_value_array_create();
213 ok(array_obj
&& bt_value_is_array(array_obj
),
214 "bt_value_array_create() returns an array value object");
215 ok(bt_value_array_is_empty(array_obj
),
216 "initial array value object size is 0");
218 obj
= bt_value_unsigned_integer_create_init(345);
219 ret
= bt_value_array_append_element(array_obj
, obj
);
220 BT_VALUE_PUT_REF_AND_RESET(obj
);
221 obj
= bt_value_signed_integer_create_init(-507);
222 ret
|= bt_value_array_append_element(array_obj
, obj
);
223 BT_VALUE_PUT_REF_AND_RESET(obj
);
224 obj
= bt_value_real_create_init(-17.45);
225 ret
|= bt_value_array_append_element(array_obj
, obj
);
226 BT_VALUE_PUT_REF_AND_RESET(obj
);
227 obj
= bt_value_bool_create_init(BT_TRUE
);
228 ret
|= bt_value_array_append_element(array_obj
, obj
);
229 BT_VALUE_PUT_REF_AND_RESET(obj
);
230 ret
|= bt_value_array_append_element(array_obj
,
232 ok(!ret
, "bt_value_array_append_element() succeeds");
233 ok(bt_value_array_get_size(array_obj
) == 5,
234 "appending an element to an array value object increment its size");
236 obj
= bt_value_array_borrow_element_by_index(array_obj
, 0);
237 ok(obj
&& bt_value_is_unsigned_integer(obj
),
238 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (unsigned integer)");
239 int_value
= bt_value_unsigned_integer_get(obj
);
241 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (unsigned integer)");
242 obj
= bt_value_array_borrow_element_by_index(array_obj
, 1);
243 ok(obj
&& bt_value_is_signed_integer(obj
),
244 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (signed integer)");
245 int_value
= bt_value_signed_integer_get(obj
);
246 ok(int_value
== -507,
247 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (signed integer)");
248 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
249 ok(obj
&& bt_value_is_real(obj
),
250 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
251 real_value
= bt_value_real_get(obj
);
252 ok(real_value
== -17.45,
253 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
254 obj
= bt_value_array_borrow_element_by_index(array_obj
, 3);
255 ok(obj
&& bt_value_is_bool(obj
),
256 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
257 bool_value
= bt_value_bool_get(obj
);
259 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
260 obj
= bt_value_array_borrow_element_by_index(array_obj
, 4);
261 ok(obj
== bt_value_null
,
262 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
264 obj
= bt_value_signed_integer_create_init(1001);
266 ok(!bt_value_array_set_element_by_index(array_obj
, 2, obj
),
267 "bt_value_array_set_element_by_index() succeeds");
268 BT_VALUE_PUT_REF_AND_RESET(obj
);
269 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
270 ok(obj
&& bt_value_is_signed_integer(obj
),
271 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
272 int_value
= bt_value_signed_integer_get(obj
);
274 ok(int_value
== 1001,
275 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
277 ret
= bt_value_array_append_bool_element(array_obj
,
279 ok(!ret
, "bt_value_array_append_bool_element() succeeds");
280 ret
= bt_value_array_append_unsigned_integer_element(array_obj
,
282 ok(!ret
, "bt_value_array_append_unsigned_integer_element() succeeds");
283 ret
= bt_value_array_append_signed_integer_element(array_obj
,
285 ok(!ret
, "bt_value_array_append_signed_integer_element() succeeds");
286 ret
= bt_value_array_append_real_element(array_obj
,
288 ok(!ret
, "bt_value_array_append_real_element() succeeds");
289 ret
= bt_value_array_append_string_element(array_obj
,
291 ok(!ret
, "bt_value_array_append_string_element() succeeds");
292 ret
= bt_value_array_append_empty_array_element(array_obj
);
293 ok(!ret
, "bt_value_array_append_empty_array_element() succeeds");
294 ret
= bt_value_array_append_empty_map_element(array_obj
);
295 ok(!ret
, "bt_value_array_append_empty_map_element() succeeds");
297 ok(bt_value_array_get_size(array_obj
) == 12,
298 "the bt_value_array_append_element_*() functions increment the array value object's size");
299 ok(!bt_value_array_is_empty(array_obj
),
300 "map value object is not empty");
302 obj
= bt_value_array_borrow_element_by_index(array_obj
, 5);
303 ok(obj
&& bt_value_is_bool(obj
),
304 "bt_value_array_append_bool_element() appends a boolean value object");
305 bool_value
= bt_value_bool_get(obj
);
307 "bt_value_array_append_bool_element() appends the appropriate value");
308 obj
= bt_value_array_borrow_element_by_index(array_obj
, 6);
309 ok(obj
&& bt_value_is_unsigned_integer(obj
),
310 "bt_value_array_append_unsigned_integer_element() appends an unsigned integer value object");
311 int_value
= bt_value_unsigned_integer_get(obj
);
312 ok(int_value
== 98765,
313 "bt_value_array_append_unsigned_integer_element() appends the appropriate value");
314 obj
= bt_value_array_borrow_element_by_index(array_obj
, 7);
315 ok(obj
&& bt_value_is_signed_integer(obj
),
316 "bt_value_array_append_signed_integer_element() appends a signed integer value object");
317 int_value
= bt_value_signed_integer_get(obj
);
318 ok(int_value
== -10101,
319 "bt_value_array_append_signed_integer_element() appends the appropriate value");
320 obj
= bt_value_array_borrow_element_by_index(array_obj
, 8);
321 ok(obj
&& bt_value_is_real(obj
),
322 "bt_value_array_append_real_element() appends a real number value object");
323 real_value
= bt_value_real_get(obj
);
324 ok(real_value
== 2.49578,
325 "bt_value_array_append_real_element() appends the appropriate value");
326 obj
= bt_value_array_borrow_element_by_index(array_obj
, 9);
327 ok(obj
&& bt_value_is_string(obj
),
328 "bt_value_array_append_string_element() appends a string value object");
329 string_value
= bt_value_string_get(obj
);
330 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
331 "bt_value_array_append_string_element() appends the appropriate value");
332 obj
= bt_value_array_borrow_element_by_index(array_obj
, 10);
333 ok(obj
&& bt_value_is_array(obj
),
334 "bt_value_array_append_empty_array_element() appends an array value object");
335 ok(bt_value_array_is_empty(obj
),
336 "bt_value_array_append_empty_array_element() an empty array value object");
337 obj
= bt_value_array_borrow_element_by_index(array_obj
, 11);
338 ok(obj
&& bt_value_is_map(obj
),
339 "bt_value_array_append_empty_map_element() appends a map value object");
340 ok(bt_value_map_is_empty(obj
),
341 "bt_value_array_append_empty_map_element() an empty map value object");
343 BT_VALUE_PUT_REF_AND_RESET(array_obj
);
344 pass("putting an existing array value object does not cause a crash")
348 bt_bool
test_map_foreach_cb_count(const char *key
, bt_value
*object
,
362 struct map_foreach_checklist
{
377 bt_bool
test_map_foreach_cb_check(const char *key
, bt_value
*object
,
380 struct map_foreach_checklist
*checklist
= data
;
382 if (!strcmp(key
, "bt_bool")) {
383 if (checklist
->bool1
) {
384 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
386 bt_bool val
= BT_FALSE
;
388 val
= bt_value_bool_get(object
);
391 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
392 checklist
->bool1
= BT_TRUE
;
394 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
397 } else if (!strcmp(key
, "uint")) {
398 if (checklist
->uint
) {
399 fail("test_map_foreach_cb_check(): duplicate key \"uint\"");
403 val
= bt_value_unsigned_integer_get(object
);
406 pass("test_map_foreach_cb_check(): \"uint\" value object has the right value");
407 checklist
->uint
= BT_TRUE
;
409 fail("test_map_foreach_cb_check(): \"uint\" value object has the wrong value");
412 } else if (!strcmp(key
, "int")) {
413 if (checklist
->int1
) {
414 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
418 val
= bt_value_signed_integer_get(object
);
421 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
422 checklist
->int1
= BT_TRUE
;
424 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
427 } else if (!strcmp(key
, "real")) {
428 if (checklist
->real1
) {
429 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
433 val
= bt_value_real_get(object
);
436 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
437 checklist
->real1
= BT_TRUE
;
439 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
442 } else if (!strcmp(key
, "null")) {
443 if (checklist
->null1
) {
444 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
446 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
447 checklist
->null1
= BT_TRUE
;
449 } else if (!strcmp(key
, "bool2")) {
450 if (checklist
->bool2
) {
451 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
453 bt_bool val
= BT_FALSE
;
455 val
= bt_value_bool_get(object
);
458 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
459 checklist
->bool2
= BT_TRUE
;
461 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
464 } else if (!strcmp(key
, "int2")) {
465 if (checklist
->int2
) {
466 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
470 val
= bt_value_signed_integer_get(object
);
473 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
474 checklist
->int2
= BT_TRUE
;
476 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
479 } else if (!strcmp(key
, "real2")) {
480 if (checklist
->real2
) {
481 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
485 val
= bt_value_real_get(object
);
487 if (val
== -49.0001) {
488 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
489 checklist
->real2
= BT_TRUE
;
491 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
494 } else if (!strcmp(key
, "string2")) {
495 if (checklist
->string2
) {
496 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
500 val
= bt_value_string_get(object
);
502 if (val
&& !strcmp(val
, "bt_value")) {
503 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
504 checklist
->string2
= BT_TRUE
;
506 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
509 } else if (!strcmp(key
, "array2")) {
510 if (checklist
->array2
) {
511 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
513 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
514 ok(bt_value_array_is_empty(object
),
515 "test_map_foreach_cb_check(): \"array2\" value object is empty");
516 checklist
->array2
= BT_TRUE
;
518 } else if (!strcmp(key
, "map2")) {
519 if (checklist
->map2
) {
520 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
522 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
523 ok(bt_value_map_is_empty(object
),
524 "test_map_foreach_cb_check(): \"map2\" value object is empty");
525 checklist
->map2
= BT_TRUE
;
528 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
545 struct map_foreach_checklist checklist
;
547 map_obj
= bt_value_map_create();
548 ok(map_obj
&& bt_value_is_map(map_obj
),
549 "bt_value_map_create() returns a map value object");
550 ok(bt_value_map_get_size(map_obj
) == 0,
551 "initial map value object size is 0");
553 obj
= bt_value_unsigned_integer_create_init(19457);
554 ret
= bt_value_map_insert_entry(map_obj
, "uint", obj
);
555 BT_VALUE_PUT_REF_AND_RESET(obj
);
556 obj
= bt_value_signed_integer_create_init(-12345);
557 ret
|= bt_value_map_insert_entry(map_obj
, "int", obj
);
558 BT_VALUE_PUT_REF_AND_RESET(obj
);
559 obj
= bt_value_real_create_init(5.444);
560 ret
|= bt_value_map_insert_entry(map_obj
, "real", obj
);
561 BT_VALUE_PUT_REF_AND_RESET(obj
);
562 obj
= bt_value_bool_create();
563 ret
|= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
564 BT_VALUE_PUT_REF_AND_RESET(obj
);
565 ret
|= bt_value_map_insert_entry(map_obj
, "null",
567 ok(!ret
, "bt_value_map_insert_entry() succeeds");
568 ok(bt_value_map_get_size(map_obj
) == 5,
569 "inserting an element into a map value object increment its size");
571 obj
= bt_value_bool_create_init(BT_TRUE
);
572 ret
= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
573 BT_VALUE_PUT_REF_AND_RESET(obj
);
574 ok(!ret
, "bt_value_map_insert_entry() accepts an existing key");
576 obj
= bt_value_map_borrow_entry_value(map_obj
, "life");
577 ok(!obj
, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
578 obj
= bt_value_map_borrow_entry_value(map_obj
, "real");
579 ok(obj
&& bt_value_is_real(obj
),
580 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
581 real_value
= bt_value_real_get(obj
);
582 ok(real_value
== 5.444,
583 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
584 obj
= bt_value_map_borrow_entry_value(map_obj
, "uint");
585 ok(obj
&& bt_value_is_unsigned_integer(obj
),
586 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (unsigned integer)");
587 int_value
= bt_value_unsigned_integer_get(obj
);
588 ok(int_value
== 19457,
589 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (unsigned integer)");
590 obj
= bt_value_map_borrow_entry_value(map_obj
, "int");
591 ok(obj
&& bt_value_is_signed_integer(obj
),
592 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (signed integer)");
593 int_value
= bt_value_signed_integer_get(obj
);
594 ok(int_value
== -12345,
595 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (signed integer)");
596 obj
= bt_value_map_borrow_entry_value(map_obj
, "null");
597 ok(obj
&& bt_value_is_null(obj
),
598 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
599 obj
= bt_value_map_borrow_entry_value(map_obj
, "bt_bool");
600 ok(obj
&& bt_value_is_bool(obj
),
601 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
602 bool_value
= bt_value_bool_get(obj
);
604 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
606 ret
= bt_value_map_insert_bool_entry(map_obj
, "bool2",
608 ok(!ret
, "bt_value_map_insert_bool_entry() succeeds");
609 ret
= bt_value_map_insert_signed_integer_entry(map_obj
, "int2",
611 ok(!ret
, "bt_value_map_insert_signed_integer_entry() succeeds");
612 ret
= bt_value_map_insert_real_entry(map_obj
, "real2",
614 ok(!ret
, "bt_value_map_insert_real_entry() succeeds");
615 ret
= bt_value_map_insert_string_entry(map_obj
, "string2",
617 ok(!ret
, "bt_value_map_insert_string_entry() succeeds");
618 ret
= bt_value_map_insert_empty_array_entry(map_obj
,
620 ok(!ret
, "bt_value_map_insert_empty_array_entry() succeeds");
621 ret
= bt_value_map_insert_empty_map_entry(map_obj
, "map2");
622 ok(!ret
, "bt_value_map_insert_empty_map_entry() succeeds");
624 ok(bt_value_map_get_size(map_obj
) == 11,
625 "the bt_value_map_insert*() functions increment the map value object's size");
627 ok(!bt_value_map_has_entry(map_obj
, "hello"),
628 "map value object does not have key \"hello\"");
629 ok(bt_value_map_has_entry(map_obj
, "bt_bool"),
630 "map value object has key \"bt_bool\"");
631 ok(bt_value_map_has_entry(map_obj
, "uint"),
632 "map value object has key \"uint\"");
633 ok(bt_value_map_has_entry(map_obj
, "int"),
634 "map value object has key \"int\"");
635 ok(bt_value_map_has_entry(map_obj
, "real"),
636 "map value object has key \"real\"");
637 ok(bt_value_map_has_entry(map_obj
, "null"),
638 "map value object has key \"null\"");
639 ok(bt_value_map_has_entry(map_obj
, "bool2"),
640 "map value object has key \"bool2\"");
641 ok(bt_value_map_has_entry(map_obj
, "int2"),
642 "map value object has key \"int2\"");
643 ok(bt_value_map_has_entry(map_obj
, "real2"),
644 "map value object has key \"real2\"");
645 ok(bt_value_map_has_entry(map_obj
, "string2"),
646 "map value object has key \"string2\"");
647 ok(bt_value_map_has_entry(map_obj
, "array2"),
648 "map value object has key \"array2\"");
649 ok(bt_value_map_has_entry(map_obj
, "map2"),
650 "map value object has key \"map2\"");
652 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_count
,
654 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
655 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
657 memset(&checklist
, 0, sizeof(checklist
));
658 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_check
,
660 ok(ret
== BT_VALUE_STATUS_OK
,
661 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
662 ok(checklist
.bool1
&& checklist
.uint
&& checklist
.int1
&&
663 checklist
.real1
&& checklist
.null1
&& checklist
.bool2
&&
664 checklist
.int2
&& checklist
.real2
&& checklist
.string2
&&
665 checklist
.array2
&& checklist
.map2
,
666 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
668 BT_VALUE_PUT_REF_AND_RESET(map_obj
);
669 pass("putting an existing map value object does not cause a crash")
673 void test_types(void)
677 test_unsigned_integer();
678 test_signed_integer();
686 void test_compare_null(void)
688 ok(bt_value_compare(bt_value_null
, bt_value_null
),
689 "null value objects are equivalent");
693 void test_compare_bool(void)
696 bt_value_bool_create_init(BT_FALSE
);
698 bt_value_bool_create_init(BT_TRUE
);
700 bt_value_bool_create_init(BT_FALSE
);
702 BT_ASSERT(bool1
&& bool2
&& bool3
);
703 ok(!bt_value_compare(bt_value_null
,
705 "cannot compare null value object and bt_bool value object");
706 ok(!bt_value_compare(bool1
,
708 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
709 ok(bt_value_compare(bool1
,
711 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
713 BT_VALUE_PUT_REF_AND_RESET(bool1
);
714 BT_VALUE_PUT_REF_AND_RESET(bool2
);
715 BT_VALUE_PUT_REF_AND_RESET(bool3
);
719 void test_compare_unsigned_integer(void)
722 bt_value_unsigned_integer_create_init(10);
724 bt_value_unsigned_integer_create_init(23);
726 bt_value_unsigned_integer_create_init(10);
728 BT_ASSERT(int1
&& int2
&& int3
);
729 ok(!bt_value_compare(bt_value_null
,
731 "cannot compare null value object and unsigned integer value object");
732 ok(!bt_value_compare(int1
, int2
),
733 "unsigned integer value objects are not equivalent (10 and 23)");
734 ok(bt_value_compare(int1
, int3
),
735 "unsigned integer value objects are equivalent (10 and 10)");
737 BT_VALUE_PUT_REF_AND_RESET(int1
);
738 BT_VALUE_PUT_REF_AND_RESET(int2
);
739 BT_VALUE_PUT_REF_AND_RESET(int3
);
742 void test_compare_signed_integer(void)
745 bt_value_signed_integer_create_init(10);
747 bt_value_signed_integer_create_init(-23);
749 bt_value_signed_integer_create_init(10);
751 BT_ASSERT(int1
&& int2
&& int3
);
752 ok(!bt_value_compare(bt_value_null
,
754 "cannot compare null value object and signed integer value object");
755 ok(!bt_value_compare(int1
, int2
),
756 "signed integer value objects are not equivalent (10 and -23)");
757 ok(bt_value_compare(int1
, int3
),
758 "signed integer value objects are equivalent (10 and 10)");
760 BT_VALUE_PUT_REF_AND_RESET(int1
);
761 BT_VALUE_PUT_REF_AND_RESET(int2
);
762 BT_VALUE_PUT_REF_AND_RESET(int3
);
766 void test_compare_real(void)
769 bt_value_real_create_init(17.38);
771 bt_value_real_create_init(-14.23);
773 bt_value_real_create_init(17.38);
775 BT_ASSERT(real1
&& real2
&& real3
);
777 ok(!bt_value_compare(bt_value_null
,
779 "cannot compare null value object and real number value object");
780 ok(!bt_value_compare(real1
,
782 "real number value objects are not equivalent (17.38 and -14.23)");
783 ok(bt_value_compare(real1
,
785 "real number value objects are equivalent (17.38 and 17.38)");
787 BT_VALUE_PUT_REF_AND_RESET(real1
);
788 BT_VALUE_PUT_REF_AND_RESET(real2
);
789 BT_VALUE_PUT_REF_AND_RESET(real3
);
793 void test_compare_string(void)
796 bt_value_string_create_init("hello");
798 bt_value_string_create_init("bt_value");
800 bt_value_string_create_init("hello");
802 BT_ASSERT(string1
&& string2
&& string3
);
804 ok(!bt_value_compare(bt_value_null
,
806 "cannot compare null value object and string value object");
807 ok(!bt_value_compare(string1
,
809 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
810 ok(bt_value_compare(string1
,
812 "string value objects are equivalent (\"hello\" and \"hello\")");
814 BT_VALUE_PUT_REF_AND_RESET(string1
);
815 BT_VALUE_PUT_REF_AND_RESET(string2
);
816 BT_VALUE_PUT_REF_AND_RESET(string3
);
820 void test_compare_array(void)
822 bt_value
*array1
= bt_value_array_create();
823 bt_value
*array2
= bt_value_array_create();
824 bt_value
*array3
= bt_value_array_create();
825 bt_value_status status
;
827 BT_ASSERT(array1
&& array2
&& array3
);
829 ok(bt_value_compare(array1
, array2
),
830 "empty array value objects are equivalent");
832 status
= bt_value_array_append_signed_integer_element(array1
, 23);
833 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
834 status
= bt_value_array_append_real_element(array1
, 14.2);
835 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
836 status
= bt_value_array_append_bool_element(array1
, BT_FALSE
);
837 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
838 status
= bt_value_array_append_real_element(array2
, 14.2);
839 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
840 status
= bt_value_array_append_signed_integer_element(array2
, 23);
841 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
842 status
= bt_value_array_append_bool_element(array2
, BT_FALSE
);
843 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
844 status
= bt_value_array_append_signed_integer_element(array3
, 23);
845 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
846 status
= bt_value_array_append_real_element(array3
, 14.2);
847 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
848 status
= bt_value_array_append_bool_element(array3
, BT_FALSE
);
849 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
850 BT_ASSERT(bt_value_array_get_size(array1
) == 3);
851 BT_ASSERT(bt_value_array_get_size(array2
) == 3);
852 BT_ASSERT(bt_value_array_get_size(array3
) == 3);
854 ok(!bt_value_compare(bt_value_null
,
856 "cannot compare null value object and array value object");
857 ok(!bt_value_compare(array1
,
859 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
860 ok(bt_value_compare(array1
,
862 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
864 BT_VALUE_PUT_REF_AND_RESET(array1
);
865 BT_VALUE_PUT_REF_AND_RESET(array2
);
866 BT_VALUE_PUT_REF_AND_RESET(array3
);
870 void test_compare_map(void)
872 bt_value
*map1
= bt_value_map_create();
873 bt_value
*map2
= bt_value_map_create();
874 bt_value
*map3
= bt_value_map_create();
875 bt_value_status status
;
877 BT_ASSERT(map1
&& map2
&& map3
);
879 ok(bt_value_compare(map1
,
881 "empty map value objects are equivalent");
884 status
= bt_value_map_insert_signed_integer_entry(map1
, "one", 23);
885 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
886 status
= bt_value_map_insert_real_entry(map1
, "two", 14.2);
887 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
888 status
= bt_value_map_insert_bool_entry(map1
, "three",
890 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
891 status
= bt_value_map_insert_real_entry(map2
, "one", 14.2);
892 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
893 status
= bt_value_map_insert_signed_integer_entry(map2
, "two", 23);
894 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
895 status
= bt_value_map_insert_bool_entry(map2
, "three",
897 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
898 status
= bt_value_map_insert_bool_entry(map3
, "three",
900 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
901 status
= bt_value_map_insert_signed_integer_entry(map3
, "one", 23);
902 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
903 status
= bt_value_map_insert_real_entry(map3
, "two", 14.2);
904 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
905 BT_ASSERT(bt_value_map_get_size(map1
) == 3);
906 BT_ASSERT(bt_value_map_get_size(map2
) == 3);
907 BT_ASSERT(bt_value_map_get_size(map3
) == 3);
909 ok(!bt_value_compare(bt_value_null
,
911 "cannot compare null value object and map value object");
912 ok(!bt_value_compare(map1
,
914 "map value objects are not equivalent");
915 ok(bt_value_compare(map1
,
917 "map value objects are equivalent");
919 BT_VALUE_PUT_REF_AND_RESET(map1
);
920 BT_VALUE_PUT_REF_AND_RESET(map2
);
921 BT_VALUE_PUT_REF_AND_RESET(map3
);
925 void test_compare(void)
929 test_compare_unsigned_integer();
930 test_compare_signed_integer();
932 test_compare_string();
933 test_compare_array();
941 * Here's the deal here. If we make sure that each value object
942 * of our deep copy has a different address than its source, and
943 * that bt_value_compare() returns BT_TRUE for the top-level
944 * value object, taking into account that we test the
945 * correctness of bt_value_compare() elsewhere, then the deep
948 bt_value
*null_copy_obj
;
949 bt_value
*bool_obj
, *bool_copy_obj
;
950 bt_value
*unsigned_integer_obj
, *unsigned_integer_copy_obj
;
951 bt_value
*signed_integer_obj
, *signed_integer_copy_obj
;
952 bt_value
*real_obj
, *real_copy_obj
;
953 bt_value
*string_obj
, *string_copy_obj
;
954 bt_value
*array_obj
, *array_copy_obj
;
955 bt_value
*map_obj
, *map_copy_obj
;
956 bt_value_status status
;
958 bool_obj
= bt_value_bool_create_init(BT_TRUE
);
959 unsigned_integer_obj
= bt_value_unsigned_integer_create_init(23);
960 signed_integer_obj
= bt_value_signed_integer_create_init(-47);
961 real_obj
= bt_value_real_create_init(-3.1416);
962 string_obj
= bt_value_string_create_init("test");
963 array_obj
= bt_value_array_create();
964 map_obj
= bt_value_map_create();
966 BT_ASSERT(bool_obj
&& unsigned_integer_obj
&& signed_integer_obj
&&
967 real_obj
&& string_obj
&& array_obj
&& map_obj
);
969 status
= bt_value_array_append_element(array_obj
, bool_obj
);
970 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
971 status
= bt_value_array_append_element(array_obj
, unsigned_integer_obj
);
972 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
973 status
= bt_value_array_append_element(array_obj
, signed_integer_obj
);
974 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
975 status
= bt_value_array_append_element(array_obj
, real_obj
);
976 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
977 status
= bt_value_array_append_element(array_obj
, bt_value_null
);
978 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
979 status
= bt_value_map_insert_entry(map_obj
, "array", array_obj
);
980 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
981 status
= bt_value_map_insert_entry(map_obj
, "string", string_obj
);
982 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
984 status
= bt_value_copy(map_obj
, &map_copy_obj
);
985 ok(status
== BT_VALUE_STATUS_OK
&& map_copy_obj
,
986 "bt_value_copy() succeeds");
988 ok(map_obj
!= map_copy_obj
,
989 "bt_value_copy() returns a different pointer (map)");
990 string_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
,
992 ok(string_copy_obj
!= string_obj
,
993 "bt_value_copy() returns a different pointer (string)");
994 array_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
,
996 ok(array_copy_obj
!= array_obj
,
997 "bt_value_copy() returns a different pointer (array)");
998 bool_copy_obj
= bt_value_array_borrow_element_by_index(
1000 ok(bool_copy_obj
!= bool_obj
,
1001 "bt_value_copy() returns a different pointer (bool)");
1002 unsigned_integer_copy_obj
= bt_value_array_borrow_element_by_index(
1004 ok(unsigned_integer_copy_obj
!= unsigned_integer_obj
,
1005 "bt_value_copy() returns a different pointer (unsigned integer)");
1006 signed_integer_copy_obj
= bt_value_array_borrow_element_by_index(
1008 ok(signed_integer_copy_obj
!= signed_integer_obj
,
1009 "bt_value_copy() returns a different pointer (signed integer)");
1010 real_copy_obj
= bt_value_array_borrow_element_by_index(
1012 ok(real_copy_obj
!= real_obj
,
1013 "bt_value_copy() returns a different pointer (real)");
1014 null_copy_obj
= bt_value_array_borrow_element_by_index(
1016 ok(null_copy_obj
== bt_value_null
,
1017 "bt_value_copy() returns the same pointer (null)");
1019 ok(bt_value_compare(map_obj
, map_copy_obj
),
1020 "source and destination value objects have the same content");
1022 BT_VALUE_PUT_REF_AND_RESET(map_copy_obj
);
1023 BT_VALUE_PUT_REF_AND_RESET(bool_obj
);
1024 BT_VALUE_PUT_REF_AND_RESET(unsigned_integer_obj
);
1025 BT_VALUE_PUT_REF_AND_RESET(signed_integer_obj
);
1026 BT_VALUE_PUT_REF_AND_RESET(real_obj
);
1027 BT_VALUE_PUT_REF_AND_RESET(string_obj
);
1028 BT_VALUE_PUT_REF_AND_RESET(array_obj
);
1029 BT_VALUE_PUT_REF_AND_RESET(map_obj
);
1033 bt_bool
compare_map_elements(const bt_value
*map_a
, const bt_value
*map_b
,
1036 const bt_value
*elem_a
= NULL
;
1037 const bt_value
*elem_b
= NULL
;
1040 elem_a
= bt_value_map_borrow_entry_value_const(map_a
, key
);
1041 elem_b
= bt_value_map_borrow_entry_value_const(map_b
, key
);
1042 equal
= bt_value_compare(elem_a
, elem_b
);
1047 void test_extend(void)
1049 bt_value
*base_map
= bt_value_map_create();
1050 bt_value
*extension_map
= bt_value_map_create();
1051 bt_value
*extended_map
= NULL
;
1052 bt_value
*array
= bt_value_array_create();
1053 bt_value_status status
;
1055 BT_ASSERT(base_map
);
1056 BT_ASSERT(extension_map
);
1058 status
= bt_value_map_insert_bool_entry(base_map
, "file",
1060 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1061 status
= bt_value_map_insert_bool_entry(base_map
, "edit",
1063 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1064 status
= bt_value_map_insert_signed_integer_entry(base_map
,
1066 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1067 status
= bt_value_map_insert_signed_integer_entry(base_map
, "find",
1069 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1070 status
= bt_value_map_insert_bool_entry(extension_map
, "edit",
1072 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1073 status
= bt_value_map_insert_signed_integer_entry(extension_map
,
1075 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1076 status
= bt_value_map_insert_real_entry(extension_map
,
1078 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1079 status
= bt_value_map_extend(base_map
, extension_map
, &extended_map
);
1080 ok(status
== BT_VALUE_STATUS_OK
&&
1081 extended_map
, "bt_value_map_extend() succeeds");
1082 ok(bt_value_map_get_size(extended_map
) == 5,
1083 "bt_value_map_extend() returns a map object with the correct size");
1084 ok(compare_map_elements(base_map
,
1085 extended_map
, "file"),
1086 "bt_value_map_extend() picks the appropriate element (file)");
1087 ok(compare_map_elements(extension_map
,
1088 extended_map
, "edit"),
1089 "bt_value_map_extend() picks the appropriate element (edit)");
1090 ok(compare_map_elements(base_map
,
1091 extended_map
, "selection"),
1092 "bt_value_map_extend() picks the appropriate element (selection)");
1093 ok(compare_map_elements(extension_map
,
1094 extended_map
, "find"),
1095 "bt_value_map_extend() picks the appropriate element (find)");
1096 ok(compare_map_elements(extension_map
,
1097 extended_map
, "project"),
1098 "bt_value_map_extend() picks the appropriate element (project)");
1100 BT_VALUE_PUT_REF_AND_RESET(array
);
1101 BT_VALUE_PUT_REF_AND_RESET(base_map
);
1102 BT_VALUE_PUT_REF_AND_RESET(extension_map
);
1103 BT_VALUE_PUT_REF_AND_RESET(extended_map
);
1108 plan_tests(NR_TESTS
);