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/babeltrace.h>
24 #include <babeltrace/assert-internal.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_object_get_ref(bt_value_null
);
37 pass("getting bt_value_null does not cause a crash");
38 bt_object_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_OBJECT_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_OBJECT_PUT_REF_AND_RESET(obj
);
76 void test_integer(void)
81 obj
= bt_value_integer_create();
82 ok(obj
&& bt_value_is_integer(obj
),
83 "bt_value_integer_create() returns an integer value object");
86 value
= bt_value_integer_get(obj
);
87 ok(value
== 0, "default integer value object value is 0");
89 bt_value_integer_set(obj
, -98765);
90 value
= bt_value_integer_get(obj
);
91 ok(value
== -98765, "bt_private_integer_bool_set() works");
93 BT_OBJECT_PUT_REF_AND_RESET(obj
);
94 pass("putting an existing integer value object does not cause a crash")
96 obj
= bt_value_integer_create_init(321456987);
97 ok(obj
&& bt_value_is_integer(obj
),
98 "bt_value_integer_create_init() returns an integer value object");
99 value
= bt_value_integer_get(obj
);
100 ok(value
== 321456987,
101 "bt_value_integer_create_init() sets the appropriate initial value");
103 BT_OBJECT_PUT_REF_AND_RESET(obj
);
110 struct bt_value
*obj
;
112 obj
= bt_value_real_create();
113 ok(obj
&& bt_value_is_real(obj
),
114 "bt_value_real_create() returns a real number value object");
117 value
= bt_value_real_get(obj
);
119 "default real number value object value is 0");
121 bt_value_real_set(obj
, -3.1416);
122 value
= bt_value_real_get(obj
);
123 ok(value
== -3.1416, "bt_value_real_set() works");
125 BT_OBJECT_PUT_REF_AND_RESET(obj
);
126 pass("putting an existing real number value object does not cause a crash")
128 obj
= bt_value_real_create_init(33.1649758);
129 ok(obj
&& bt_value_is_real(obj
),
130 "bt_value_real_create_init() returns a real number value object");
131 value
= bt_value_real_get(obj
);
132 ok(value
== 33.1649758,
133 "bt_value_real_create_init() sets the appropriate initial value");
135 BT_OBJECT_PUT_REF_AND_RESET(obj
);
139 void test_string(void)
142 struct bt_value
*obj
;
144 obj
= bt_value_string_create();
145 ok(obj
&& bt_value_is_string(obj
),
146 "bt_value_string_create() returns a string value object");
148 value
= bt_value_string_get(obj
);
149 ok(value
&& !strcmp(value
, ""),
150 "default string value object value is \"\"");
152 bt_value_string_set(obj
, "hello worldz");
153 value
= bt_value_string_get(obj
);
154 ok(value
&& !strcmp(value
, "hello worldz"),
155 "bt_value_string_get() works");
157 BT_OBJECT_PUT_REF_AND_RESET(obj
);
158 pass("putting an existing string value object does not cause a crash")
160 obj
= bt_value_string_create_init("initial value");
161 ok(obj
&& bt_value_is_string(obj
),
162 "bt_value_string_create_init() returns a string value object");
163 value
= bt_value_string_get(obj
);
164 ok(value
&& !strcmp(value
, "initial value"),
165 "bt_value_string_create_init() sets the appropriate initial value");
167 BT_OBJECT_PUT_REF_AND_RESET(obj
);
171 void test_array(void)
177 struct bt_value
*obj
;
178 const char *string_value
;
179 struct bt_value
*array_obj
;
181 array_obj
= bt_value_array_create();
182 ok(array_obj
&& bt_value_is_array(array_obj
),
183 "bt_value_array_create() returns an array value object");
184 ok(bt_value_array_is_empty(array_obj
),
185 "initial array value object size is 0");
187 obj
= bt_value_integer_create_init(345);
188 ret
= bt_value_array_append_element(array_obj
, obj
);
189 BT_OBJECT_PUT_REF_AND_RESET(obj
);
190 obj
= bt_value_real_create_init(-17.45);
191 ret
|= bt_value_array_append_element(array_obj
, obj
);
192 BT_OBJECT_PUT_REF_AND_RESET(obj
);
193 obj
= bt_value_bool_create_init(BT_TRUE
);
194 ret
|= bt_value_array_append_element(array_obj
, obj
);
195 BT_OBJECT_PUT_REF_AND_RESET(obj
);
196 ret
|= bt_value_array_append_element(array_obj
,
198 ok(!ret
, "bt_value_array_append_element() succeeds");
199 ok(bt_value_array_get_size(array_obj
) == 4,
200 "appending an element to an array value object increment its size");
202 obj
= bt_value_array_borrow_element_by_index(array_obj
, 0);
203 ok(obj
&& bt_value_is_integer(obj
),
204 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (integer)");
205 int_value
= bt_value_integer_get(obj
);
207 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (integer)");
208 obj
= bt_value_array_borrow_element_by_index(array_obj
, 1);
209 ok(obj
&& bt_value_is_real(obj
),
210 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
211 real_value
= bt_value_real_get(obj
);
212 ok(real_value
== -17.45,
213 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
214 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
215 ok(obj
&& bt_value_is_bool(obj
),
216 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
217 bool_value
= bt_value_bool_get(obj
);
219 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
220 obj
= bt_value_array_borrow_element_by_index(array_obj
, 3);
221 ok(obj
== bt_value_null
,
222 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
224 obj
= bt_value_integer_create_init(1001);
226 ok(!bt_value_array_set_element_by_index(array_obj
, 2, obj
),
227 "bt_value_array_set_element_by_index() succeeds");
228 BT_OBJECT_PUT_REF_AND_RESET(obj
);
229 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
230 ok(obj
&& bt_value_is_integer(obj
),
231 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
232 int_value
= bt_value_integer_get(obj
);
234 ok(int_value
== 1001,
235 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
237 ret
= bt_value_array_append_bool_element(array_obj
,
239 ok(!ret
, "bt_value_array_append_bool_element() succeeds");
240 ret
= bt_value_array_append_integer_element(array_obj
,
242 ok(!ret
, "bt_value_array_append_integer_element() succeeds");
243 ret
= bt_value_array_append_real_element(array_obj
,
245 ok(!ret
, "bt_value_array_append_real_element() succeeds");
246 ret
= bt_value_array_append_string_element(array_obj
,
248 ok(!ret
, "bt_value_array_append_string_element() succeeds");
249 ret
= bt_value_array_append_empty_array_element(array_obj
);
250 ok(!ret
, "bt_value_array_append_empty_array_element() succeeds");
251 ret
= bt_value_array_append_empty_map_element(array_obj
);
252 ok(!ret
, "bt_value_array_append_empty_map_element() succeeds");
254 ok(bt_value_array_get_size(array_obj
) == 10,
255 "the bt_value_array_append_element_*() functions increment the array value object's size");
256 ok(!bt_value_array_is_empty(array_obj
),
257 "map value object is not empty");
259 obj
= bt_value_array_borrow_element_by_index(array_obj
, 4);
260 ok(obj
&& bt_value_is_bool(obj
),
261 "bt_value_array_append_bool_element() appends a boolean value object");
262 bool_value
= bt_value_bool_get(obj
);
264 "bt_value_array_append_bool_element() appends the appropriate value");
265 obj
= bt_value_array_borrow_element_by_index(array_obj
, 5);
266 ok(obj
&& bt_value_is_integer(obj
),
267 "bt_value_array_append_integer_element() appends an integer value object");
268 int_value
= bt_value_integer_get(obj
);
269 ok(int_value
== 98765,
270 "bt_value_array_append_integer_element() appends the appropriate value");
271 obj
= bt_value_array_borrow_element_by_index(array_obj
, 6);
272 ok(obj
&& bt_value_is_real(obj
),
273 "bt_value_array_append_real_element() appends a real number value object");
274 real_value
= bt_value_real_get(obj
);
275 ok(real_value
== 2.49578,
276 "bt_value_array_append_real_element() appends the appropriate value");
277 obj
= bt_value_array_borrow_element_by_index(array_obj
, 7);
278 ok(obj
&& bt_value_is_string(obj
),
279 "bt_value_array_append_string_element() appends a string value object");
280 string_value
= bt_value_string_get(obj
);
281 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
282 "bt_value_array_append_string_element() appends the appropriate value");
283 obj
= bt_value_array_borrow_element_by_index(array_obj
, 8);
284 ok(obj
&& bt_value_is_array(obj
),
285 "bt_value_array_append_empty_array_element() appends an array value object");
286 ok(bt_value_array_is_empty(obj
),
287 "bt_value_array_append_empty_array_element() an empty array value object");
288 obj
= bt_value_array_borrow_element_by_index(array_obj
, 9);
289 ok(obj
&& bt_value_is_map(obj
),
290 "bt_value_array_append_empty_map_element() appends a map value object");
291 ok(bt_value_map_is_empty(obj
),
292 "bt_value_array_append_empty_map_element() an empty map value object");
294 BT_OBJECT_PUT_REF_AND_RESET(array_obj
);
295 pass("putting an existing array value object does not cause a crash")
299 bt_bool
test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
313 struct map_foreach_checklist
{
327 bt_bool
test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
330 struct map_foreach_checklist
*checklist
= data
;
332 if (!strcmp(key
, "bt_bool")) {
333 if (checklist
->bool1
) {
334 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
336 bt_bool val
= BT_FALSE
;
338 val
= bt_value_bool_get(object
);
341 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
342 checklist
->bool1
= BT_TRUE
;
344 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
347 } else if (!strcmp(key
, "int")) {
348 if (checklist
->int1
) {
349 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
353 val
= bt_value_integer_get(object
);
356 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
357 checklist
->int1
= BT_TRUE
;
359 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
362 } else if (!strcmp(key
, "real")) {
363 if (checklist
->real1
) {
364 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
368 val
= bt_value_real_get(object
);
371 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
372 checklist
->real1
= BT_TRUE
;
374 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
377 } else if (!strcmp(key
, "null")) {
378 if (checklist
->null1
) {
379 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
381 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
382 checklist
->null1
= BT_TRUE
;
384 } else if (!strcmp(key
, "bool2")) {
385 if (checklist
->bool2
) {
386 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
388 bt_bool val
= BT_FALSE
;
390 val
= bt_value_bool_get(object
);
393 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
394 checklist
->bool2
= BT_TRUE
;
396 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
399 } else if (!strcmp(key
, "int2")) {
400 if (checklist
->int2
) {
401 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
405 val
= bt_value_integer_get(object
);
408 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
409 checklist
->int2
= BT_TRUE
;
411 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
414 } else if (!strcmp(key
, "real2")) {
415 if (checklist
->real2
) {
416 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
420 val
= bt_value_real_get(object
);
422 if (val
== -49.0001) {
423 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
424 checklist
->real2
= BT_TRUE
;
426 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
429 } else if (!strcmp(key
, "string2")) {
430 if (checklist
->string2
) {
431 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
435 val
= bt_value_string_get(object
);
437 if (val
&& !strcmp(val
, "bt_value")) {
438 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
439 checklist
->string2
= BT_TRUE
;
441 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
444 } else if (!strcmp(key
, "array2")) {
445 if (checklist
->array2
) {
446 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
448 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
449 ok(bt_value_array_is_empty(object
),
450 "test_map_foreach_cb_check(): \"array2\" value object is empty");
451 checklist
->array2
= BT_TRUE
;
453 } else if (!strcmp(key
, "map2")) {
454 if (checklist
->map2
) {
455 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
457 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
458 ok(bt_value_map_is_empty(object
),
459 "test_map_foreach_cb_check(): \"map2\" value object is empty");
460 checklist
->map2
= BT_TRUE
;
463 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
478 struct bt_value
*obj
;
479 struct bt_value
*map_obj
;
480 struct map_foreach_checklist checklist
;
482 map_obj
= bt_value_map_create();
483 ok(map_obj
&& bt_value_is_map(map_obj
),
484 "bt_value_map_create() returns a map value object");
485 ok(bt_value_map_get_size(map_obj
) == 0,
486 "initial map value object size is 0");
488 obj
= bt_value_integer_create_init(19457);
489 ret
= bt_value_map_insert_entry(map_obj
, "int", obj
);
490 BT_OBJECT_PUT_REF_AND_RESET(obj
);
491 obj
= bt_value_real_create_init(5.444);
492 ret
|= bt_value_map_insert_entry(map_obj
, "real", obj
);
493 BT_OBJECT_PUT_REF_AND_RESET(obj
);
494 obj
= bt_value_bool_create();
495 ret
|= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
496 BT_OBJECT_PUT_REF_AND_RESET(obj
);
497 ret
|= bt_value_map_insert_entry(map_obj
, "null",
499 ok(!ret
, "bt_value_map_insert_entry() succeeds");
500 ok(bt_value_map_get_size(map_obj
) == 4,
501 "inserting an element into a map value object increment its size");
503 obj
= bt_value_bool_create_init(BT_TRUE
);
504 ret
= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
505 BT_OBJECT_PUT_REF_AND_RESET(obj
);
506 ok(!ret
, "bt_value_map_insert_entry() accepts an existing key");
508 obj
= bt_value_map_borrow_entry_value(map_obj
, "life");
509 ok(!obj
, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
510 obj
= bt_value_map_borrow_entry_value(map_obj
, "real");
511 ok(obj
&& bt_value_is_real(obj
),
512 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
513 real_value
= bt_value_real_get(obj
);
514 ok(real_value
== 5.444,
515 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
516 obj
= bt_value_map_borrow_entry_value(map_obj
, "int");
517 ok(obj
&& bt_value_is_integer(obj
),
518 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
519 int_value
= bt_value_integer_get(obj
);
520 ok(int_value
== 19457,
521 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (integer)");
522 obj
= bt_value_map_borrow_entry_value(map_obj
, "null");
523 ok(obj
&& bt_value_is_null(obj
),
524 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
525 obj
= bt_value_map_borrow_entry_value(map_obj
, "bt_bool");
526 ok(obj
&& bt_value_is_bool(obj
),
527 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
528 bool_value
= bt_value_bool_get(obj
);
530 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
532 ret
= bt_value_map_insert_bool_entry(map_obj
, "bool2",
534 ok(!ret
, "bt_value_map_insert_bool_entry() succeeds");
535 ret
= bt_value_map_insert_integer_entry(map_obj
, "int2",
537 ok(!ret
, "bt_value_map_insert_integer_entry() succeeds");
538 ret
= bt_value_map_insert_real_entry(map_obj
, "real2",
540 ok(!ret
, "bt_value_map_insert_real_entry() succeeds");
541 ret
= bt_value_map_insert_string_entry(map_obj
, "string2",
543 ok(!ret
, "bt_value_map_insert_string_entry() succeeds");
544 ret
= bt_value_map_insert_empty_array_entry(map_obj
,
546 ok(!ret
, "bt_value_map_insert_empty_array_entry() succeeds");
547 ret
= bt_value_map_insert_empty_map_entry(map_obj
, "map2");
548 ok(!ret
, "bt_value_map_insert_empty_map_entry() succeeds");
550 ok(bt_value_map_get_size(map_obj
) == 10,
551 "the bt_value_map_insert*() functions increment the map value object's size");
553 ok(!bt_value_map_has_entry(map_obj
, "hello"),
554 "map value object does not have key \"hello\"");
555 ok(bt_value_map_has_entry(map_obj
, "bt_bool"),
556 "map value object has key \"bt_bool\"");
557 ok(bt_value_map_has_entry(map_obj
, "int"),
558 "map value object has key \"int\"");
559 ok(bt_value_map_has_entry(map_obj
, "real"),
560 "map value object has key \"real\"");
561 ok(bt_value_map_has_entry(map_obj
, "null"),
562 "map value object has key \"null\"");
563 ok(bt_value_map_has_entry(map_obj
, "bool2"),
564 "map value object has key \"bool2\"");
565 ok(bt_value_map_has_entry(map_obj
, "int2"),
566 "map value object has key \"int2\"");
567 ok(bt_value_map_has_entry(map_obj
, "real2"),
568 "map value object has key \"real2\"");
569 ok(bt_value_map_has_entry(map_obj
, "string2"),
570 "map value object has key \"string2\"");
571 ok(bt_value_map_has_entry(map_obj
, "array2"),
572 "map value object has key \"array2\"");
573 ok(bt_value_map_has_entry(map_obj
, "map2"),
574 "map value object has key \"map2\"");
576 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_count
,
578 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
579 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
581 memset(&checklist
, 0, sizeof(checklist
));
582 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_check
,
584 ok(ret
== BT_VALUE_STATUS_OK
,
585 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
586 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.real1
&&
587 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
588 checklist
.real2
&& checklist
.string2
&&
589 checklist
.array2
&& checklist
.map2
,
590 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
592 BT_OBJECT_PUT_REF_AND_RESET(map_obj
);
593 pass("putting an existing map value object does not cause a crash")
597 void test_types(void)
609 void test_compare_null(void)
611 ok(bt_value_compare(bt_value_null
, bt_value_null
),
612 "null value objects are equivalent");
616 void test_compare_bool(void)
618 struct bt_value
*bool1
=
619 bt_value_bool_create_init(BT_FALSE
);
620 struct bt_value
*bool2
=
621 bt_value_bool_create_init(BT_TRUE
);
622 struct bt_value
*bool3
=
623 bt_value_bool_create_init(BT_FALSE
);
625 BT_ASSERT(bool1
&& bool2
&& bool3
);
626 ok(!bt_value_compare(bt_value_null
,
628 "cannot compare null value object and bt_bool value object");
629 ok(!bt_value_compare(bool1
,
631 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
632 ok(bt_value_compare(bool1
,
634 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
636 BT_OBJECT_PUT_REF_AND_RESET(bool1
);
637 BT_OBJECT_PUT_REF_AND_RESET(bool2
);
638 BT_OBJECT_PUT_REF_AND_RESET(bool3
);
642 void test_compare_integer(void)
644 struct bt_value
*int1
=
645 bt_value_integer_create_init(10);
646 struct bt_value
*int2
=
647 bt_value_integer_create_init(-23);
648 struct bt_value
*int3
=
649 bt_value_integer_create_init(10);
651 BT_ASSERT(int1
&& int2
&& int3
);
652 ok(!bt_value_compare(bt_value_null
,
654 "cannot compare null value object and integer value object");
655 ok(!bt_value_compare(int1
,
657 "integer value objects are not equivalent (10 and -23)");
658 ok(bt_value_compare(int1
,
660 "integer value objects are equivalent (10 and 10)");
662 BT_OBJECT_PUT_REF_AND_RESET(int1
);
663 BT_OBJECT_PUT_REF_AND_RESET(int2
);
664 BT_OBJECT_PUT_REF_AND_RESET(int3
);
668 void test_compare_real(void)
670 struct bt_value
*real1
=
671 bt_value_real_create_init(17.38);
672 struct bt_value
*real2
=
673 bt_value_real_create_init(-14.23);
674 struct bt_value
*real3
=
675 bt_value_real_create_init(17.38);
677 BT_ASSERT(real1
&& real2
&& real3
);
679 ok(!bt_value_compare(bt_value_null
,
681 "cannot compare null value object and real number value object");
682 ok(!bt_value_compare(real1
,
684 "real number value objects are not equivalent (17.38 and -14.23)");
685 ok(bt_value_compare(real1
,
687 "real number value objects are equivalent (17.38 and 17.38)");
689 BT_OBJECT_PUT_REF_AND_RESET(real1
);
690 BT_OBJECT_PUT_REF_AND_RESET(real2
);
691 BT_OBJECT_PUT_REF_AND_RESET(real3
);
695 void test_compare_string(void)
697 struct bt_value
*string1
=
698 bt_value_string_create_init("hello");
699 struct bt_value
*string2
=
700 bt_value_string_create_init("bt_value");
701 struct bt_value
*string3
=
702 bt_value_string_create_init("hello");
704 BT_ASSERT(string1
&& string2
&& string3
);
706 ok(!bt_value_compare(bt_value_null
,
708 "cannot compare null value object and string value object");
709 ok(!bt_value_compare(string1
,
711 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
712 ok(bt_value_compare(string1
,
714 "string value objects are equivalent (\"hello\" and \"hello\")");
716 BT_OBJECT_PUT_REF_AND_RESET(string1
);
717 BT_OBJECT_PUT_REF_AND_RESET(string2
);
718 BT_OBJECT_PUT_REF_AND_RESET(string3
);
722 void test_compare_array(void)
724 struct bt_value
*array1
= bt_value_array_create();
725 struct bt_value
*array2
= bt_value_array_create();
726 struct bt_value
*array3
= bt_value_array_create();
727 enum bt_value_status status
;
729 BT_ASSERT(array1
&& array2
&& array3
);
731 ok(bt_value_compare(array1
,
733 "empty array value objects are equivalent");
735 status
= bt_value_array_append_integer_element(array1
, 23);
736 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
737 status
= bt_value_array_append_real_element(array1
, 14.2);
738 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
739 status
= bt_value_array_append_bool_element(array1
, BT_FALSE
);
740 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
741 status
= bt_value_array_append_real_element(array2
, 14.2);
742 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
743 status
= bt_value_array_append_integer_element(array2
, 23);
744 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
745 status
= bt_value_array_append_bool_element(array2
, BT_FALSE
);
746 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
747 status
= bt_value_array_append_integer_element(array3
, 23);
748 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
749 status
= bt_value_array_append_real_element(array3
, 14.2);
750 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
751 status
= bt_value_array_append_bool_element(array3
, BT_FALSE
);
752 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
753 BT_ASSERT(bt_value_array_get_size(array1
) == 3);
754 BT_ASSERT(bt_value_array_get_size(array2
) == 3);
755 BT_ASSERT(bt_value_array_get_size(array3
) == 3);
757 ok(!bt_value_compare(bt_value_null
,
759 "cannot compare null value object and array value object");
760 ok(!bt_value_compare(array1
,
762 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
763 ok(bt_value_compare(array1
,
765 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
767 BT_OBJECT_PUT_REF_AND_RESET(array1
);
768 BT_OBJECT_PUT_REF_AND_RESET(array2
);
769 BT_OBJECT_PUT_REF_AND_RESET(array3
);
773 void test_compare_map(void)
775 struct bt_value
*map1
= bt_value_map_create();
776 struct bt_value
*map2
= bt_value_map_create();
777 struct bt_value
*map3
= bt_value_map_create();
778 enum bt_value_status status
;
780 BT_ASSERT(map1
&& map2
&& map3
);
782 ok(bt_value_compare(map1
,
784 "empty map value objects are equivalent");
787 status
= bt_value_map_insert_integer_entry(map1
, "one", 23);
788 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
789 status
= bt_value_map_insert_real_entry(map1
, "two", 14.2);
790 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
791 status
= bt_value_map_insert_bool_entry(map1
, "three",
793 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
794 status
= bt_value_map_insert_real_entry(map2
, "one", 14.2);
795 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
796 status
= bt_value_map_insert_integer_entry(map2
, "two", 23);
797 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
798 status
= bt_value_map_insert_bool_entry(map2
, "three",
800 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
801 status
= bt_value_map_insert_bool_entry(map3
, "three",
803 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
804 status
= bt_value_map_insert_integer_entry(map3
, "one", 23);
805 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
806 status
= bt_value_map_insert_real_entry(map3
, "two", 14.2);
807 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
808 BT_ASSERT(bt_value_map_get_size(map1
) == 3);
809 BT_ASSERT(bt_value_map_get_size(map2
) == 3);
810 BT_ASSERT(bt_value_map_get_size(map3
) == 3);
812 ok(!bt_value_compare(bt_value_null
,
814 "cannot compare null value object and map value object");
815 ok(!bt_value_compare(map1
,
817 "map value objects are not equivalent");
818 ok(bt_value_compare(map1
,
820 "map value objects are equivalent");
822 BT_OBJECT_PUT_REF_AND_RESET(map1
);
823 BT_OBJECT_PUT_REF_AND_RESET(map2
);
824 BT_OBJECT_PUT_REF_AND_RESET(map3
);
828 void test_compare(void)
832 test_compare_integer();
834 test_compare_string();
835 test_compare_array();
843 * Here's the deal here. If we make sure that each value object
844 * of our deep copy has a different address than its source,
845 * and that bt_value_compare() returns BT_TRUE for the top-level
846 * value object, taking into account that we test the correctness of
847 * bt_value_compare() elsewhere, then the deep copy is a
850 struct bt_value
*null_copy_obj
;
851 struct bt_value
*bool_obj
, *bool_copy_obj
;
852 struct bt_value
*integer_obj
, *integer_copy_obj
;
853 struct bt_value
*real_obj
, *real_copy_obj
;
854 struct bt_value
*string_obj
, *string_copy_obj
;
855 struct bt_value
*array_obj
, *array_copy_obj
;
856 struct bt_value
*map_obj
, *map_copy_obj
;
857 enum bt_value_status status
;
859 bool_obj
= bt_value_bool_create_init(BT_TRUE
);
860 integer_obj
= bt_value_integer_create_init(23);
861 real_obj
= bt_value_real_create_init(-3.1416);
862 string_obj
= bt_value_string_create_init("test");
863 array_obj
= bt_value_array_create();
864 map_obj
= bt_value_map_create();
866 BT_ASSERT(bool_obj
&& integer_obj
&& real_obj
&& string_obj
&&
867 array_obj
&& map_obj
);
869 status
= bt_value_array_append_element(array_obj
,
871 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
872 status
= bt_value_array_append_element(array_obj
,
874 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
875 status
= bt_value_array_append_element(array_obj
,
877 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
878 status
= bt_value_array_append_element(array_obj
,
880 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
881 status
= bt_value_map_insert_entry(map_obj
, "array",
883 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
884 status
= bt_value_map_insert_entry(map_obj
, "string",
886 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
888 status
= bt_value_copy(&map_copy_obj
,
890 ok(status
== BT_VALUE_STATUS_OK
&& map_copy_obj
,
891 "bt_value_copy() succeeds");
893 ok(map_obj
!= map_copy_obj
,
894 "bt_value_copy() returns a different pointer (map)");
895 string_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
,
897 ok(string_copy_obj
!= string_obj
,
898 "bt_value_copy() returns a different pointer (string)");
899 array_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
,
901 ok(array_copy_obj
!= array_obj
,
902 "bt_value_copy() returns a different pointer (array)");
903 bool_copy_obj
= bt_value_array_borrow_element_by_index(
905 ok(bool_copy_obj
!= bool_obj
,
906 "bt_value_copy() returns a different pointer (bt_bool)");
907 integer_copy_obj
= bt_value_array_borrow_element_by_index(
909 ok(integer_copy_obj
!= integer_obj
,
910 "bt_value_copy() returns a different pointer (integer)");
911 real_copy_obj
= bt_value_array_borrow_element_by_index(
913 ok(real_copy_obj
!= real_obj
,
914 "bt_value_copy() returns a different pointer (real)");
915 null_copy_obj
= bt_value_array_borrow_element_by_index(
917 ok(null_copy_obj
== bt_value_null
,
918 "bt_value_copy() returns the same pointer (null)");
920 ok(bt_value_compare(map_obj
,
922 "source and destination value objects have the same content");
924 BT_OBJECT_PUT_REF_AND_RESET(map_copy_obj
);
925 BT_OBJECT_PUT_REF_AND_RESET(bool_obj
);
926 BT_OBJECT_PUT_REF_AND_RESET(integer_obj
);
927 BT_OBJECT_PUT_REF_AND_RESET(real_obj
);
928 BT_OBJECT_PUT_REF_AND_RESET(string_obj
);
929 BT_OBJECT_PUT_REF_AND_RESET(array_obj
);
930 BT_OBJECT_PUT_REF_AND_RESET(map_obj
);
934 bt_bool
compare_map_elements(const struct bt_value
*map_a
, const struct bt_value
*map_b
,
937 const struct bt_value
*elem_a
= NULL
;
938 const struct bt_value
*elem_b
= NULL
;
941 elem_a
= bt_value_map_borrow_entry_value_const(map_a
, key
);
942 elem_b
= bt_value_map_borrow_entry_value_const(map_b
, key
);
943 equal
= bt_value_compare(elem_a
, elem_b
);
948 void test_extend(void)
950 struct bt_value
*base_map
= bt_value_map_create();
951 struct bt_value
*extension_map
= bt_value_map_create();
952 struct bt_value
*extended_map
= NULL
;
953 struct bt_value
*array
= bt_value_array_create();
954 enum bt_value_status status
;
957 BT_ASSERT(extension_map
);
959 status
= bt_value_map_insert_bool_entry(base_map
, "file",
961 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
962 status
= bt_value_map_insert_bool_entry(base_map
, "edit",
964 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
965 status
= bt_value_map_insert_integer_entry(base_map
,
967 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
968 status
= bt_value_map_insert_integer_entry(base_map
, "find",
970 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
971 status
= bt_value_map_insert_bool_entry(extension_map
, "edit",
973 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
974 status
= bt_value_map_insert_integer_entry(extension_map
,
976 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
977 status
= bt_value_map_insert_real_entry(extension_map
,
979 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
980 status
= bt_value_map_extend(base_map
, extension_map
, &extended_map
);
981 ok(status
== BT_VALUE_STATUS_OK
&&
982 extended_map
, "bt_value_map_extend() succeeds");
983 ok(bt_value_map_get_size(extended_map
) == 5,
984 "bt_value_map_extend() returns a map object with the correct size");
985 ok(compare_map_elements(base_map
,
986 extended_map
, "file"),
987 "bt_value_map_extend() picks the appropriate element (file)");
988 ok(compare_map_elements(extension_map
,
989 extended_map
, "edit"),
990 "bt_value_map_extend() picks the appropriate element (edit)");
991 ok(compare_map_elements(base_map
,
992 extended_map
, "selection"),
993 "bt_value_map_extend() picks the appropriate element (selection)");
994 ok(compare_map_elements(extension_map
,
995 extended_map
, "find"),
996 "bt_value_map_extend() picks the appropriate element (find)");
997 ok(compare_map_elements(extension_map
,
998 extended_map
, "project"),
999 "bt_value_map_extend() picks the appropriate element (project)");
1001 BT_OBJECT_PUT_REF_AND_RESET(array
);
1002 BT_OBJECT_PUT_REF_AND_RESET(base_map
);
1003 BT_OBJECT_PUT_REF_AND_RESET(extension_map
);
1004 BT_OBJECT_PUT_REF_AND_RESET(extended_map
);
1009 plan_tests(NR_TESTS
);