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/ref.h>
24 #include <babeltrace/values.h>
25 #include <babeltrace/assert-internal.h>
34 ok(bt_value_null
, "bt_value_null is not NULL");
35 ok(bt_value_is_null(bt_value_null
),
36 "bt_value_null is a null value object");
37 bt_get(bt_value_null
);
38 pass("getting bt_value_null does not cause a crash");
39 bt_put(bt_value_null
);
40 pass("putting bt_value_null does not cause a crash");
50 obj
= bt_value_bool_create();
51 ok(obj
&& bt_value_is_bool(obj
),
52 "bt_value_bool_create() returns a boolean value object");
55 ret
= bt_value_bool_get(obj
, &value
);
56 ok(!ret
&& !value
, "default boolean value object value is BT_FALSE");
58 BT_ASSERT(!bt_value_bool_set(obj
, BT_FALSE
));
59 ret
= bt_value_bool_set(obj
, BT_TRUE
);
60 ok(!ret
, "bt_value_bool_set() succeeds");
61 ret
= bt_value_bool_get(obj
, &value
);
62 ok(!ret
&& value
, "bt_value_bool_set() works");
65 pass("putting an existing boolean value object does not cause a crash")
68 obj
= bt_value_bool_create_init(BT_TRUE
);
69 ok(obj
&& bt_value_is_bool(obj
),
70 "bt_value_bool_create_init() returns a boolean value object");
71 ret
= bt_value_bool_get(obj
, &value
);
73 "bt_value_bool_create_init() sets the appropriate initial value");
79 void test_integer(void)
85 obj
= bt_value_integer_create();
86 ok(obj
&& bt_value_is_integer(obj
),
87 "bt_value_integer_create() returns an integer value object");
90 ret
= bt_value_integer_get(obj
, &value
);
91 ok(!ret
&& value
== 0, "default integer value object value is 0");
93 ret
= bt_value_integer_set(obj
, -98765);
94 ok(!ret
, "bt_value_integer_set() succeeds");
95 ret
= bt_value_integer_get(obj
, &value
);
96 ok(!ret
&& value
== -98765, "bt_value_integer_set() works");
99 pass("putting an existing integer value object does not cause a crash")
101 obj
= bt_value_integer_create_init(321456987);
102 ok(obj
&& bt_value_is_integer(obj
),
103 "bt_value_integer_create_init() returns an integer value object");
104 ret
= bt_value_integer_get(obj
, &value
);
105 ok(!ret
&& value
== 321456987,
106 "bt_value_integer_create_init() sets the appropriate initial value");
116 struct bt_value
*obj
;
118 obj
= bt_value_real_create();
119 ok(obj
&& bt_value_is_real(obj
),
120 "bt_value_real_create() returns a real number value object");
123 ret
= bt_value_real_get(obj
, &value
);
124 ok(!ret
&& value
== 0.,
125 "default real number value object value is 0");
127 ret
= bt_value_real_set(obj
, -3.1416);
128 ok(!ret
, "bt_value_real_set() succeeds");
129 ret
= bt_value_real_get(obj
, &value
);
130 ok(!ret
&& value
== -3.1416, "bt_value_real_set() works");
133 pass("putting an existing real number value object does not cause a crash")
135 obj
= bt_value_real_create_init(33.1649758);
136 ok(obj
&& bt_value_is_real(obj
),
137 "bt_value_real_create_init() returns a real number value object");
138 ret
= bt_value_real_get(obj
, &value
);
139 ok(!ret
&& value
== 33.1649758,
140 "bt_value_real_create_init() sets the appropriate initial value");
146 void test_string(void)
150 struct bt_value
*obj
;
152 obj
= bt_value_string_create();
153 ok(obj
&& bt_value_is_string(obj
),
154 "bt_value_string_create() returns a string value object");
156 ret
= bt_value_string_get(obj
, &value
);
157 ok(!ret
&& value
&& !strcmp(value
, ""),
158 "default string value object value is \"\"");
160 ret
= bt_value_string_set(obj
, "hello worldz");
161 ok(!ret
, "bt_value_string_set() succeeds");
162 ret
= bt_value_string_get(obj
, &value
);
163 ok(!ret
&& value
&& !strcmp(value
, "hello worldz"),
164 "bt_value_string_get() works");
167 pass("putting an existing string value object does not cause a crash")
169 obj
= bt_value_string_create_init("initial value");
170 ok(obj
&& bt_value_is_string(obj
),
171 "bt_value_string_create_init() returns a string value object");
172 ret
= bt_value_string_get(obj
, &value
);
173 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
174 "bt_value_string_create_init() sets the appropriate initial value");
180 void test_array(void)
186 struct bt_value
*obj
;
187 const char *string_value
;
188 struct bt_value
*array_obj
;
190 array_obj
= bt_value_array_create();
191 ok(array_obj
&& bt_value_is_array(array_obj
),
192 "bt_value_array_create() returns an array value object");
193 ok(bt_value_array_is_empty(array_obj
),
194 "initial array value object size is 0");
196 obj
= bt_value_integer_create_init(345);
197 ret
= bt_value_array_append(array_obj
, obj
);
199 obj
= bt_value_real_create_init(-17.45);
200 ret
|= bt_value_array_append(array_obj
, obj
);
202 obj
= bt_value_bool_create_init(BT_TRUE
);
203 ret
|= bt_value_array_append(array_obj
, obj
);
205 ret
|= bt_value_array_append(array_obj
, bt_value_null
);
206 ok(!ret
, "bt_value_array_append() succeeds");
207 ok(bt_value_array_size(array_obj
) == 4,
208 "appending an element to an array value object increment its size");
210 obj
= bt_value_array_get(array_obj
, 0);
211 ok(obj
&& bt_value_is_integer(obj
),
212 "bt_value_array_get() returns an value object with the appropriate type (integer)");
213 ret
= bt_value_integer_get(obj
, &int_value
);
214 ok(!ret
&& int_value
== 345,
215 "bt_value_array_get() returns an value object with the appropriate value (integer)");
217 obj
= bt_value_array_get(array_obj
, 1);
218 ok(obj
&& bt_value_is_real(obj
),
219 "bt_value_array_get() returns an value object with the appropriate type (real number)");
220 ret
= bt_value_real_get(obj
, &real_value
);
221 ok(!ret
&& real_value
== -17.45,
222 "bt_value_array_get() returns an value object with the appropriate value (real number)");
224 obj
= bt_value_array_get(array_obj
, 2);
225 ok(obj
&& bt_value_is_bool(obj
),
226 "bt_value_array_get() returns an value object with the appropriate type (boolean)");
227 ret
= bt_value_bool_get(obj
, &bool_value
);
228 ok(!ret
&& bool_value
,
229 "bt_value_array_get() returns an value object with the appropriate value (boolean)");
231 obj
= bt_value_array_get(array_obj
, 3);
232 ok(obj
== bt_value_null
,
233 "bt_value_array_get() returns an value object with the appropriate type (null)");
235 obj
= bt_value_integer_create_init(1001);
237 ok(!bt_value_array_set(array_obj
, 2, obj
),
238 "bt_value_array_set() succeeds");
240 obj
= bt_value_array_get(array_obj
, 2);
241 ok(obj
&& bt_value_is_integer(obj
),
242 "bt_value_array_set() inserts an value object with the appropriate type");
243 ret
= bt_value_integer_get(obj
, &int_value
);
245 ok(int_value
== 1001,
246 "bt_value_array_set() inserts an value object with the appropriate value");
249 ret
= bt_value_array_append_bool(array_obj
, BT_FALSE
);
250 ok(!ret
, "bt_value_array_append_bool() succeeds");
251 ret
= bt_value_array_append_integer(array_obj
, 98765);
252 ok(!ret
, "bt_value_array_append_integer() succeeds");
253 ret
= bt_value_array_append_real(array_obj
, 2.49578);
254 ok(!ret
, "bt_value_array_append_real() succeeds");
255 ret
= bt_value_array_append_string(array_obj
, "bt_value");
256 ok(!ret
, "bt_value_array_append_string() succeeds");
257 ret
= bt_value_array_append_empty_array(array_obj
);
258 ok(!ret
, "bt_value_array_append_empty_array() succeeds");
259 ret
= bt_value_array_append_empty_map(array_obj
);
260 ok(!ret
, "bt_value_array_append_empty_map() succeeds");
262 ok(bt_value_array_size(array_obj
) == 10,
263 "the bt_value_array_append_*() functions increment the array value object's size");
264 ok(!bt_value_array_is_empty(array_obj
),
265 "map value object is not empty");
267 obj
= bt_value_array_get(array_obj
, 4);
268 ok(obj
&& bt_value_is_bool(obj
),
269 "bt_value_array_append_bool() appends a boolean value object");
270 ret
= bt_value_bool_get(obj
, &bool_value
);
271 ok(!ret
&& !bool_value
,
272 "bt_value_array_append_bool() appends the appropriate value");
274 obj
= bt_value_array_get(array_obj
, 5);
275 ok(obj
&& bt_value_is_integer(obj
),
276 "bt_value_array_append_integer() appends an integer value object");
277 ret
= bt_value_integer_get(obj
, &int_value
);
278 ok(!ret
&& int_value
== 98765,
279 "bt_value_array_append_integer() appends the appropriate value");
281 obj
= bt_value_array_get(array_obj
, 6);
282 ok(obj
&& bt_value_is_real(obj
),
283 "bt_value_array_append_real() appends a real number value object");
284 ret
= bt_value_real_get(obj
, &real_value
);
285 ok(!ret
&& real_value
== 2.49578,
286 "bt_value_array_append_real() appends the appropriate value");
288 obj
= bt_value_array_get(array_obj
, 7);
289 ok(obj
&& bt_value_is_string(obj
),
290 "bt_value_array_append_string() appends a string value object");
291 ret
= bt_value_string_get(obj
, &string_value
);
292 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
293 "bt_value_array_append_string() appends the appropriate value");
295 obj
= bt_value_array_get(array_obj
, 8);
296 ok(obj
&& bt_value_is_array(obj
),
297 "bt_value_array_append_empty_array() appends an array value object");
298 ok(bt_value_array_is_empty(obj
),
299 "bt_value_array_append_empty_array() an empty array value object");
301 obj
= bt_value_array_get(array_obj
, 9);
302 ok(obj
&& bt_value_is_map(obj
),
303 "bt_value_array_append_empty_map() appends a map value object");
304 ok(bt_value_map_is_empty(obj
),
305 "bt_value_array_append_empty_map() an empty map value object");
309 pass("putting an existing array value object does not cause a crash")
313 bt_bool
test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
327 struct map_foreach_checklist
{
341 bt_bool
test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
345 struct map_foreach_checklist
*checklist
= data
;
347 if (!strcmp(key
, "bt_bool")) {
348 if (checklist
->bool1
) {
349 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
351 bt_bool val
= BT_FALSE
;
353 ret
= bt_value_bool_get(object
, &val
);
354 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
357 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
358 checklist
->bool1
= BT_TRUE
;
360 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
363 } else if (!strcmp(key
, "int")) {
364 if (checklist
->int1
) {
365 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
369 ret
= bt_value_integer_get(object
, &val
);
370 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
373 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
374 checklist
->int1
= BT_TRUE
;
376 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
379 } else if (!strcmp(key
, "real")) {
380 if (checklist
->real1
) {
381 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
385 ret
= bt_value_real_get(object
, &val
);
386 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real\" value");
389 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
390 checklist
->real1
= BT_TRUE
;
392 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
395 } else if (!strcmp(key
, "null")) {
396 if (checklist
->null1
) {
397 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
399 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
400 checklist
->null1
= BT_TRUE
;
402 } else if (!strcmp(key
, "bool2")) {
403 if (checklist
->bool2
) {
404 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
406 bt_bool val
= BT_FALSE
;
408 ret
= bt_value_bool_get(object
, &val
);
409 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
412 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
413 checklist
->bool2
= BT_TRUE
;
415 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
418 } else if (!strcmp(key
, "int2")) {
419 if (checklist
->int2
) {
420 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
424 ret
= bt_value_integer_get(object
, &val
);
425 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
428 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
429 checklist
->int2
= BT_TRUE
;
431 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
434 } else if (!strcmp(key
, "real2")) {
435 if (checklist
->real2
) {
436 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
440 ret
= bt_value_real_get(object
, &val
);
441 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real2\" value");
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 ret
= bt_value_string_get(object
, &val
);
457 ok(!ret
, "test_map_foreach_cb_check(): success getting \"string2\" value");
459 if (val
&& !strcmp(val
, "bt_value")) {
460 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
461 checklist
->string2
= BT_TRUE
;
463 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
466 } else if (!strcmp(key
, "array2")) {
467 if (checklist
->array2
) {
468 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
470 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
471 ok(bt_value_array_is_empty(object
),
472 "test_map_foreach_cb_check(): \"array2\" value object is empty");
473 checklist
->array2
= BT_TRUE
;
475 } else if (!strcmp(key
, "map2")) {
476 if (checklist
->map2
) {
477 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
479 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
480 ok(bt_value_map_is_empty(object
),
481 "test_map_foreach_cb_check(): \"map2\" value object is empty");
482 checklist
->map2
= BT_TRUE
;
485 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key
);
499 struct bt_value
*obj
;
500 struct bt_value
*map_obj
;
501 struct map_foreach_checklist checklist
;
503 map_obj
= bt_value_map_create();
504 ok(map_obj
&& bt_value_is_map(map_obj
),
505 "bt_value_map_create() returns a map value object");
506 ok(bt_value_map_size(map_obj
) == 0,
507 "initial map value object size is 0");
509 obj
= bt_value_integer_create_init(19457);
510 ret
= bt_value_map_insert(map_obj
, "int", obj
);
512 obj
= bt_value_real_create_init(5.444);
513 ret
|= bt_value_map_insert(map_obj
, "real", obj
);
515 obj
= bt_value_bool_create();
516 ret
|= bt_value_map_insert(map_obj
, "bt_bool", obj
);
518 ret
|= bt_value_map_insert(map_obj
, "null", bt_value_null
);
519 ok(!ret
, "bt_value_map_insert() succeeds");
520 ok(bt_value_map_size(map_obj
) == 4,
521 "inserting an element into a map value object increment its size");
523 obj
= bt_value_bool_create_init(BT_TRUE
);
524 ret
= bt_value_map_insert(map_obj
, "bt_bool", obj
);
526 ok(!ret
, "bt_value_map_insert() accepts an existing key");
528 obj
= bt_value_map_get(map_obj
, "life");
529 ok(!obj
, "bt_value_map_get() returns NULL with an non existing key");
530 obj
= bt_value_map_get(map_obj
, "real");
531 ok(obj
&& bt_value_is_real(obj
),
532 "bt_value_map_get() returns an value object with the appropriate type (real)");
533 ret
= bt_value_real_get(obj
, &real_value
);
534 ok(!ret
&& real_value
== 5.444,
535 "bt_value_map_get() returns an value object with the appropriate value (real)");
537 obj
= bt_value_map_get(map_obj
, "int");
538 ok(obj
&& bt_value_is_integer(obj
),
539 "bt_value_map_get() returns an value object with the appropriate type (integer)");
540 ret
= bt_value_integer_get(obj
, &int_value
);
541 ok(!ret
&& int_value
== 19457,
542 "bt_value_map_get() returns an value object with the appropriate value (integer)");
544 obj
= bt_value_map_get(map_obj
, "null");
545 ok(obj
&& bt_value_is_null(obj
),
546 "bt_value_map_get() returns an value object with the appropriate type (null)");
547 obj
= bt_value_map_get(map_obj
, "bt_bool");
548 ok(obj
&& bt_value_is_bool(obj
),
549 "bt_value_map_get() returns an value object with the appropriate type (boolean)");
550 ret
= bt_value_bool_get(obj
, &bool_value
);
551 ok(!ret
&& bool_value
,
552 "bt_value_map_get() returns an value object with the appropriate value (boolean)");
555 ret
= bt_value_map_insert_bool(map_obj
, "bool2", BT_TRUE
);
556 ok(!ret
, "bt_value_map_insert_bool() succeeds");
557 ret
= bt_value_map_insert_integer(map_obj
, "int2", 98765);
558 ok(!ret
, "bt_value_map_insert_integer() succeeds");
559 ret
= bt_value_map_insert_real(map_obj
, "real2", -49.0001);
560 ok(!ret
, "bt_value_map_insert_real() succeeds");
561 ret
= bt_value_map_insert_string(map_obj
, "string2", "bt_value");
562 ok(!ret
, "bt_value_map_insert_string() succeeds");
563 ret
= bt_value_map_insert_empty_array(map_obj
, "array2");
564 ok(!ret
, "bt_value_map_insert_empty_array() succeeds");
565 ret
= bt_value_map_insert_empty_map(map_obj
, "map2");
566 ok(!ret
, "bt_value_map_insert_empty_map() succeeds");
568 ok(bt_value_map_size(map_obj
) == 10,
569 "the bt_value_map_insert*() functions increment the map value object's size");
571 ok(!bt_value_map_has_key(map_obj
, "hello"),
572 "map value object does not have key \"hello\"");
573 ok(bt_value_map_has_key(map_obj
, "bt_bool"),
574 "map value object has key \"bt_bool\"");
575 ok(bt_value_map_has_key(map_obj
, "int"),
576 "map value object has key \"int\"");
577 ok(bt_value_map_has_key(map_obj
, "real"),
578 "map value object has key \"real\"");
579 ok(bt_value_map_has_key(map_obj
, "null"),
580 "map value object has key \"null\"");
581 ok(bt_value_map_has_key(map_obj
, "bool2"),
582 "map value object has key \"bool2\"");
583 ok(bt_value_map_has_key(map_obj
, "int2"),
584 "map value object has key \"int2\"");
585 ok(bt_value_map_has_key(map_obj
, "real2"),
586 "map value object has key \"real2\"");
587 ok(bt_value_map_has_key(map_obj
, "string2"),
588 "map value object has key \"string2\"");
589 ok(bt_value_map_has_key(map_obj
, "array2"),
590 "map value object has key \"array2\"");
591 ok(bt_value_map_has_key(map_obj
, "map2"),
592 "map value object has key \"map2\"");
594 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_count
, &count
);
595 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
596 "bt_value_map_foreach() breaks the loop when the user function returns BT_FALSE");
598 memset(&checklist
, 0, sizeof(checklist
));
599 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_check
,
601 ok(ret
== BT_VALUE_STATUS_OK
,
602 "bt_value_map_foreach() succeeds with test_map_foreach_cb_check()");
603 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.real1
&&
604 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
605 checklist
.real2
&& checklist
.string2
&&
606 checklist
.array2
&& checklist
.map2
,
607 "bt_value_map_foreach() iterates over all the map value object's elements");
610 pass("putting an existing map value object does not cause a crash")
614 void test_types(void)
626 void test_compare_null(void)
628 ok(bt_value_compare(bt_value_null
, bt_value_null
),
629 "null value objects are equivalent");
633 void test_compare_bool(void)
635 struct bt_value
*bool1
= bt_value_bool_create_init(BT_FALSE
);
636 struct bt_value
*bool2
= bt_value_bool_create_init(BT_TRUE
);
637 struct bt_value
*bool3
= bt_value_bool_create_init(BT_FALSE
);
639 BT_ASSERT(bool1
&& bool2
&& bool3
);
640 ok(!bt_value_compare(bt_value_null
, bool1
),
641 "cannot compare null value object and bt_bool value object");
642 ok(!bt_value_compare(bool1
, bool2
),
643 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
644 ok(bt_value_compare(bool1
, bool3
),
645 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
653 void test_compare_integer(void)
655 struct bt_value
*int1
= bt_value_integer_create_init(10);
656 struct bt_value
*int2
= bt_value_integer_create_init(-23);
657 struct bt_value
*int3
= bt_value_integer_create_init(10);
659 BT_ASSERT(int1
&& int2
&& int3
);
660 ok(!bt_value_compare(bt_value_null
, int1
),
661 "cannot compare null value object and integer value object");
662 ok(!bt_value_compare(int1
, int2
),
663 "integer value objects are not equivalent (10 and -23)");
664 ok(bt_value_compare(int1
, int3
),
665 "integer value objects are equivalent (10 and 10)");
673 void test_compare_real(void)
675 struct bt_value
*real1
= bt_value_real_create_init(17.38);
676 struct bt_value
*real2
= bt_value_real_create_init(-14.23);
677 struct bt_value
*real3
= bt_value_real_create_init(17.38);
679 BT_ASSERT(real1
&& real2
&& real3
);
681 ok(!bt_value_compare(bt_value_null
, real1
),
682 "cannot compare null value object and real number value object");
683 ok(!bt_value_compare(real1
, real2
),
684 "real number value objects are not equivalent (17.38 and -14.23)");
685 ok(bt_value_compare(real1
, real3
),
686 "real number value objects are equivalent (17.38 and 17.38)");
694 void test_compare_string(void)
696 struct bt_value
*string1
= bt_value_string_create_init("hello");
697 struct bt_value
*string2
= bt_value_string_create_init("bt_value");
698 struct bt_value
*string3
= bt_value_string_create_init("hello");
700 BT_ASSERT(string1
&& string2
&& string3
);
702 ok(!bt_value_compare(bt_value_null
, string1
),
703 "cannot compare null value object and string value object");
704 ok(!bt_value_compare(string1
, string2
),
705 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
706 ok(bt_value_compare(string1
, string3
),
707 "string value objects are equivalent (\"hello\" and \"hello\")");
715 void test_compare_array(void)
717 struct bt_value
*array1
= bt_value_array_create();
718 struct bt_value
*array2
= bt_value_array_create();
719 struct bt_value
*array3
= bt_value_array_create();
721 BT_ASSERT(array1
&& array2
&& array3
);
723 ok(bt_value_compare(array1
, array2
),
724 "empty array value objects are equivalent");
726 BT_ASSERT(!bt_value_array_append_integer(array1
, 23));
727 BT_ASSERT(!bt_value_array_append_real(array1
, 14.2));
728 BT_ASSERT(!bt_value_array_append_bool(array1
, BT_FALSE
));
729 BT_ASSERT(!bt_value_array_append_real(array2
, 14.2));
730 BT_ASSERT(!bt_value_array_append_integer(array2
, 23));
731 BT_ASSERT(!bt_value_array_append_bool(array2
, BT_FALSE
));
732 BT_ASSERT(!bt_value_array_append_integer(array3
, 23));
733 BT_ASSERT(!bt_value_array_append_real(array3
, 14.2));
734 BT_ASSERT(!bt_value_array_append_bool(array3
, BT_FALSE
));
735 BT_ASSERT(bt_value_array_size(array1
) == 3);
736 BT_ASSERT(bt_value_array_size(array2
) == 3);
737 BT_ASSERT(bt_value_array_size(array3
) == 3);
739 ok(!bt_value_compare(bt_value_null
, array1
),
740 "cannot compare null value object and array value object");
741 ok(!bt_value_compare(array1
, array2
),
742 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
743 ok(bt_value_compare(array1
, array3
),
744 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
752 void test_compare_map(void)
754 struct bt_value
*map1
= bt_value_map_create();
755 struct bt_value
*map2
= bt_value_map_create();
756 struct bt_value
*map3
= bt_value_map_create();
758 BT_ASSERT(map1
&& map2
&& map3
);
760 ok(bt_value_compare(map1
, map2
),
761 "empty map value objects are equivalent");
763 BT_ASSERT(!bt_value_map_insert_integer(map1
, "one", 23));
764 BT_ASSERT(!bt_value_map_insert_real(map1
, "two", 14.2));
765 BT_ASSERT(!bt_value_map_insert_bool(map1
, "three", BT_FALSE
));
766 BT_ASSERT(!bt_value_map_insert_real(map2
, "one", 14.2));
767 BT_ASSERT(!bt_value_map_insert_integer(map2
, "two", 23));
768 BT_ASSERT(!bt_value_map_insert_bool(map2
, "three", BT_FALSE
));
769 BT_ASSERT(!bt_value_map_insert_bool(map3
, "three", BT_FALSE
));
770 BT_ASSERT(!bt_value_map_insert_integer(map3
, "one", 23));
771 BT_ASSERT(!bt_value_map_insert_real(map3
, "two", 14.2));
772 BT_ASSERT(bt_value_map_size(map1
) == 3);
773 BT_ASSERT(bt_value_map_size(map2
) == 3);
774 BT_ASSERT(bt_value_map_size(map3
) == 3);
776 ok(!bt_value_compare(bt_value_null
, map1
),
777 "cannot compare null value object and map value object");
778 ok(!bt_value_compare(map1
, map2
),
779 "map value objects are not equivalent");
780 ok(bt_value_compare(map1
, map3
),
781 "map value objects are equivalent");
789 void test_compare(void)
793 test_compare_integer();
795 test_compare_string();
796 test_compare_array();
804 * Here's the deal here. If we make sure that each value object
805 * of our deep copy has a different address than its source,
806 * and that bt_value_compare() returns BT_TRUE for the top-level
807 * value object, taking into account that we test the correctness of
808 * bt_value_compare() elsewhere, then the deep copy is a
811 struct bt_value
*null_copy_obj
;
812 struct bt_value
*bool_obj
, *bool_copy_obj
;
813 struct bt_value
*integer_obj
, *integer_copy_obj
;
814 struct bt_value
*real_obj
, *real_copy_obj
;
815 struct bt_value
*string_obj
, *string_copy_obj
;
816 struct bt_value
*array_obj
, *array_copy_obj
;
817 struct bt_value
*map_obj
, *map_copy_obj
;
819 bool_obj
= bt_value_bool_create_init(BT_TRUE
);
820 integer_obj
= bt_value_integer_create_init(23);
821 real_obj
= bt_value_real_create_init(-3.1416);
822 string_obj
= bt_value_string_create_init("test");
823 array_obj
= bt_value_array_create();
824 map_obj
= bt_value_map_create();
826 BT_ASSERT(bool_obj
&& integer_obj
&& real_obj
&& string_obj
&&
827 array_obj
&& map_obj
);
829 BT_ASSERT(!bt_value_array_append(array_obj
, bool_obj
));
830 BT_ASSERT(!bt_value_array_append(array_obj
, integer_obj
));
831 BT_ASSERT(!bt_value_array_append(array_obj
, real_obj
));
832 BT_ASSERT(!bt_value_array_append(array_obj
, bt_value_null
));
833 BT_ASSERT(!bt_value_map_insert(map_obj
, "array", array_obj
));
834 BT_ASSERT(!bt_value_map_insert(map_obj
, "string", string_obj
));
836 map_copy_obj
= bt_value_copy(map_obj
);
838 "bt_value_copy() succeeds");
840 ok(map_obj
!= map_copy_obj
,
841 "bt_value_copy() returns a different pointer (map)");
842 string_copy_obj
= bt_value_map_get(map_copy_obj
, "string");
843 ok(string_copy_obj
!= string_obj
,
844 "bt_value_copy() returns a different pointer (string)");
845 array_copy_obj
= bt_value_map_get(map_copy_obj
, "array");
846 ok(array_copy_obj
!= array_obj
,
847 "bt_value_copy() returns a different pointer (array)");
848 bool_copy_obj
= bt_value_array_get(array_copy_obj
, 0);
849 ok(bool_copy_obj
!= bool_obj
,
850 "bt_value_copy() returns a different pointer (bt_bool)");
851 integer_copy_obj
= bt_value_array_get(array_copy_obj
, 1);
852 ok(integer_copy_obj
!= integer_obj
,
853 "bt_value_copy() returns a different pointer (integer)");
854 real_copy_obj
= bt_value_array_get(array_copy_obj
, 2);
855 ok(real_copy_obj
!= real_obj
,
856 "bt_value_copy() returns a different pointer (real)");
857 null_copy_obj
= bt_value_array_get(array_copy_obj
, 3);
858 ok(null_copy_obj
== bt_value_null
,
859 "bt_value_copy() returns the same pointer (null)");
861 ok(bt_value_compare(map_obj
, map_copy_obj
),
862 "source and destination value objects have the same content");
864 BT_PUT(bool_copy_obj
);
865 BT_PUT(integer_copy_obj
);
866 BT_PUT(real_copy_obj
);
867 BT_PUT(string_copy_obj
);
868 BT_PUT(array_copy_obj
);
869 BT_PUT(map_copy_obj
);
879 bt_bool
compare_map_elements(struct bt_value
*map_a
, struct bt_value
*map_b
,
882 struct bt_value
*elem_a
= NULL
;
883 struct bt_value
*elem_b
= NULL
;
886 elem_a
= bt_value_map_get(map_a
, key
);
887 elem_b
= bt_value_map_get(map_b
, key
);
888 equal
= bt_value_compare(elem_a
, elem_b
);
896 void test_extend(void)
898 struct bt_value
*base_map
= bt_value_map_create();
899 struct bt_value
*extension_map
= bt_value_map_create();
900 struct bt_value
*extended_map
= NULL
;
901 struct bt_value
*array
= bt_value_array_create();
902 enum bt_value_status status
;
905 BT_ASSERT(extension_map
);
907 status
= bt_value_map_insert_bool(base_map
, "file", BT_TRUE
);
908 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
909 status
= bt_value_map_insert_bool(base_map
, "edit", BT_FALSE
);
910 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
911 status
= bt_value_map_insert_integer(base_map
, "selection", 17);
912 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
913 status
= bt_value_map_insert_integer(base_map
, "find", -34);
914 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
915 status
= bt_value_map_insert_bool(extension_map
, "edit", BT_TRUE
);
916 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
917 status
= bt_value_map_insert_integer(extension_map
, "find", 101);
918 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
919 status
= bt_value_map_insert_real(extension_map
, "project", -404);
920 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
921 extended_map
= bt_value_map_extend(base_map
, extension_map
);
922 ok(extended_map
, "bt_value_map_extend() succeeds");
923 ok(bt_value_map_size(extended_map
) == 5,
924 "bt_value_map_extend() returns a map object with the correct size");
925 ok(compare_map_elements(base_map
, extended_map
, "file"),
926 "bt_value_map_extend() picks the appropriate element (file)");
927 ok(compare_map_elements(extension_map
, extended_map
, "edit"),
928 "bt_value_map_extend() picks the appropriate element (edit)");
929 ok(compare_map_elements(base_map
, extended_map
, "selection"),
930 "bt_value_map_extend() picks the appropriate element (selection)");
931 ok(compare_map_elements(extension_map
, extended_map
, "find"),
932 "bt_value_map_extend() picks the appropriate element (find)");
933 ok(compare_map_elements(extension_map
, extended_map
, "project"),
934 "bt_value_map_extend() picks the appropriate element (project)");
938 BT_PUT(extension_map
);
939 BT_PUT(extended_map
);
944 plan_tests(NR_TESTS
);