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_element(array_obj
, obj
);
199 obj
= bt_value_real_create_init(-17.45);
200 ret
|= bt_value_array_append_element(array_obj
, obj
);
202 obj
= bt_value_bool_create_init(BT_TRUE
);
203 ret
|= bt_value_array_append_element(array_obj
, obj
);
205 ret
|= bt_value_array_append_element(array_obj
, bt_value_null
);
206 ok(!ret
, "bt_value_array_append_element() succeeds");
207 ok(bt_value_array_get_size(array_obj
) == 4,
208 "appending an element to an array value object increment its size");
210 obj
= bt_value_array_borrow_element_by_index(array_obj
, 0);
211 ok(obj
&& bt_value_is_integer(obj
),
212 "bt_value_array_borrow_element_by_index() 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_borrow_element_by_index() returns an value object with the appropriate value (integer)");
216 obj
= bt_value_array_borrow_element_by_index(array_obj
, 1);
217 ok(obj
&& bt_value_is_real(obj
),
218 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
219 ret
= bt_value_real_get(obj
, &real_value
);
220 ok(!ret
&& real_value
== -17.45,
221 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
222 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
223 ok(obj
&& bt_value_is_bool(obj
),
224 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
225 ret
= bt_value_bool_get(obj
, &bool_value
);
226 ok(!ret
&& bool_value
,
227 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
228 obj
= bt_value_array_borrow_element_by_index(array_obj
, 3);
229 ok(obj
== bt_value_null
,
230 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
232 obj
= bt_value_integer_create_init(1001);
234 ok(!bt_value_array_set_element_by_index(array_obj
, 2, obj
),
235 "bt_value_array_set_element_by_index() succeeds");
237 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
238 ok(obj
&& bt_value_is_integer(obj
),
239 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
240 ret
= bt_value_integer_get(obj
, &int_value
);
242 ok(int_value
== 1001,
243 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
245 ret
= bt_value_array_append_bool_element(array_obj
, BT_FALSE
);
246 ok(!ret
, "bt_value_array_append_bool_element() succeeds");
247 ret
= bt_value_array_append_integer_element(array_obj
, 98765);
248 ok(!ret
, "bt_value_array_append_integer_element() succeeds");
249 ret
= bt_value_array_append_real_element(array_obj
, 2.49578);
250 ok(!ret
, "bt_value_array_append_real_element() succeeds");
251 ret
= bt_value_array_append_string_element(array_obj
, "bt_value");
252 ok(!ret
, "bt_value_array_append_string_element() succeeds");
253 ret
= bt_value_array_append_empty_array_element(array_obj
);
254 ok(!ret
, "bt_value_array_append_empty_array_element() succeeds");
255 ret
= bt_value_array_append_empty_map_element(array_obj
);
256 ok(!ret
, "bt_value_array_append_empty_map_element() succeeds");
258 ok(bt_value_array_get_size(array_obj
) == 10,
259 "the bt_value_array_append_element_*() functions increment the array value object's size");
260 ok(!bt_value_array_is_empty(array_obj
),
261 "map value object is not empty");
263 obj
= bt_value_array_borrow_element_by_index(array_obj
, 4);
264 ok(obj
&& bt_value_is_bool(obj
),
265 "bt_value_array_append_bool_element() appends a boolean value object");
266 ret
= bt_value_bool_get(obj
, &bool_value
);
267 ok(!ret
&& !bool_value
,
268 "bt_value_array_append_bool_element() appends the appropriate value");
269 obj
= bt_value_array_borrow_element_by_index(array_obj
, 5);
270 ok(obj
&& bt_value_is_integer(obj
),
271 "bt_value_array_append_integer_element() appends an integer value object");
272 ret
= bt_value_integer_get(obj
, &int_value
);
273 ok(!ret
&& int_value
== 98765,
274 "bt_value_array_append_integer_element() appends the appropriate value");
275 obj
= bt_value_array_borrow_element_by_index(array_obj
, 6);
276 ok(obj
&& bt_value_is_real(obj
),
277 "bt_value_array_append_real_element() appends a real number value object");
278 ret
= bt_value_real_get(obj
, &real_value
);
279 ok(!ret
&& real_value
== 2.49578,
280 "bt_value_array_append_real_element() appends the appropriate value");
281 obj
= bt_value_array_borrow_element_by_index(array_obj
, 7);
282 ok(obj
&& bt_value_is_string(obj
),
283 "bt_value_array_append_string_element() appends a string value object");
284 ret
= bt_value_string_get(obj
, &string_value
);
285 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
286 "bt_value_array_append_string_element() appends the appropriate value");
287 obj
= bt_value_array_borrow_element_by_index(array_obj
, 8);
288 ok(obj
&& bt_value_is_array(obj
),
289 "bt_value_array_append_empty_array_element() appends an array value object");
290 ok(bt_value_array_is_empty(obj
),
291 "bt_value_array_append_empty_array_element() an empty array value object");
292 obj
= bt_value_array_borrow_element_by_index(array_obj
, 9);
293 ok(obj
&& bt_value_is_map(obj
),
294 "bt_value_array_append_empty_map_element() appends a map value object");
295 ok(bt_value_map_is_empty(obj
),
296 "bt_value_array_append_empty_map_element() an empty map value object");
299 pass("putting an existing array value object does not cause a crash")
303 bt_bool
test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
317 struct map_foreach_checklist
{
331 bt_bool
test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
335 struct map_foreach_checklist
*checklist
= data
;
337 if (!strcmp(key
, "bt_bool")) {
338 if (checklist
->bool1
) {
339 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
341 bt_bool val
= BT_FALSE
;
343 ret
= bt_value_bool_get(object
, &val
);
344 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
347 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
348 checklist
->bool1
= BT_TRUE
;
350 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
353 } else if (!strcmp(key
, "int")) {
354 if (checklist
->int1
) {
355 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
359 ret
= bt_value_integer_get(object
, &val
);
360 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
363 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
364 checklist
->int1
= BT_TRUE
;
366 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
369 } else if (!strcmp(key
, "real")) {
370 if (checklist
->real1
) {
371 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
375 ret
= bt_value_real_get(object
, &val
);
376 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real\" value");
379 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
380 checklist
->real1
= BT_TRUE
;
382 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
385 } else if (!strcmp(key
, "null")) {
386 if (checklist
->null1
) {
387 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
389 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
390 checklist
->null1
= BT_TRUE
;
392 } else if (!strcmp(key
, "bool2")) {
393 if (checklist
->bool2
) {
394 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
396 bt_bool val
= BT_FALSE
;
398 ret
= bt_value_bool_get(object
, &val
);
399 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
402 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
403 checklist
->bool2
= BT_TRUE
;
405 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
408 } else if (!strcmp(key
, "int2")) {
409 if (checklist
->int2
) {
410 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
414 ret
= bt_value_integer_get(object
, &val
);
415 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
418 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
419 checklist
->int2
= BT_TRUE
;
421 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
424 } else if (!strcmp(key
, "real2")) {
425 if (checklist
->real2
) {
426 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
430 ret
= bt_value_real_get(object
, &val
);
431 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real2\" value");
433 if (val
== -49.0001) {
434 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
435 checklist
->real2
= BT_TRUE
;
437 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
440 } else if (!strcmp(key
, "string2")) {
441 if (checklist
->string2
) {
442 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
446 ret
= bt_value_string_get(object
, &val
);
447 ok(!ret
, "test_map_foreach_cb_check(): success getting \"string2\" value");
449 if (val
&& !strcmp(val
, "bt_value")) {
450 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
451 checklist
->string2
= BT_TRUE
;
453 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
456 } else if (!strcmp(key
, "array2")) {
457 if (checklist
->array2
) {
458 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
460 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
461 ok(bt_value_array_is_empty(object
),
462 "test_map_foreach_cb_check(): \"array2\" value object is empty");
463 checklist
->array2
= BT_TRUE
;
465 } else if (!strcmp(key
, "map2")) {
466 if (checklist
->map2
) {
467 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
469 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
470 ok(bt_value_map_is_empty(object
),
471 "test_map_foreach_cb_check(): \"map2\" value object is empty");
472 checklist
->map2
= BT_TRUE
;
475 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key
);
489 struct bt_value
*obj
;
490 struct bt_value
*map_obj
;
491 struct map_foreach_checklist checklist
;
493 map_obj
= bt_value_map_create();
494 ok(map_obj
&& bt_value_is_map(map_obj
),
495 "bt_value_map_create() returns a map value object");
496 ok(bt_value_map_get_size(map_obj
) == 0,
497 "initial map value object size is 0");
499 obj
= bt_value_integer_create_init(19457);
500 ret
= bt_value_map_insert_entry(map_obj
, "int", obj
);
502 obj
= bt_value_real_create_init(5.444);
503 ret
|= bt_value_map_insert_entry(map_obj
, "real", obj
);
505 obj
= bt_value_bool_create();
506 ret
|= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
508 ret
|= bt_value_map_insert_entry(map_obj
, "null", bt_value_null
);
509 ok(!ret
, "bt_value_map_insert_entry() succeeds");
510 ok(bt_value_map_get_size(map_obj
) == 4,
511 "inserting an element into a map value object increment its size");
513 obj
= bt_value_bool_create_init(BT_TRUE
);
514 ret
= bt_value_map_insert_entry(map_obj
, "bt_bool", obj
);
516 ok(!ret
, "bt_value_map_insert_entry() accepts an existing key");
518 obj
= bt_value_map_borrow_entry_value(map_obj
, "life");
519 ok(!obj
, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
520 obj
= bt_value_map_borrow_entry_value(map_obj
, "real");
521 ok(obj
&& bt_value_is_real(obj
),
522 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
523 ret
= bt_value_real_get(obj
, &real_value
);
524 ok(!ret
&& real_value
== 5.444,
525 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
526 obj
= bt_value_map_borrow_entry_value(map_obj
, "int");
527 ok(obj
&& bt_value_is_integer(obj
),
528 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
529 ret
= bt_value_integer_get(obj
, &int_value
);
530 ok(!ret
&& int_value
== 19457,
531 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (integer)");
532 obj
= bt_value_map_borrow_entry_value(map_obj
, "null");
533 ok(obj
&& bt_value_is_null(obj
),
534 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
535 obj
= bt_value_map_borrow_entry_value(map_obj
, "bt_bool");
536 ok(obj
&& bt_value_is_bool(obj
),
537 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
538 ret
= bt_value_bool_get(obj
, &bool_value
);
539 ok(!ret
&& bool_value
,
540 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
542 ret
= bt_value_map_insert_bool_entry(map_obj
, "bool2", BT_TRUE
);
543 ok(!ret
, "bt_value_map_insert_bool_entry() succeeds");
544 ret
= bt_value_map_insert_integer_entry(map_obj
, "int2", 98765);
545 ok(!ret
, "bt_value_map_insert_integer_entry() succeeds");
546 ret
= bt_value_map_insert_real_entry(map_obj
, "real2", -49.0001);
547 ok(!ret
, "bt_value_map_insert_real_entry() succeeds");
548 ret
= bt_value_map_insert_string_entry(map_obj
, "string2", "bt_value");
549 ok(!ret
, "bt_value_map_insert_string_entry() succeeds");
550 ret
= bt_value_map_insert_empty_array_entry(map_obj
, "array2");
551 ok(!ret
, "bt_value_map_insert_empty_array_entry() succeeds");
552 ret
= bt_value_map_insert_empty_map_entry(map_obj
, "map2");
553 ok(!ret
, "bt_value_map_insert_empty_map_entry() succeeds");
555 ok(bt_value_map_get_size(map_obj
) == 10,
556 "the bt_value_map_insert*() functions increment the map value object's size");
558 ok(!bt_value_map_has_entry(map_obj
, "hello"),
559 "map value object does not have key \"hello\"");
560 ok(bt_value_map_has_entry(map_obj
, "bt_bool"),
561 "map value object has key \"bt_bool\"");
562 ok(bt_value_map_has_entry(map_obj
, "int"),
563 "map value object has key \"int\"");
564 ok(bt_value_map_has_entry(map_obj
, "real"),
565 "map value object has key \"real\"");
566 ok(bt_value_map_has_entry(map_obj
, "null"),
567 "map value object has key \"null\"");
568 ok(bt_value_map_has_entry(map_obj
, "bool2"),
569 "map value object has key \"bool2\"");
570 ok(bt_value_map_has_entry(map_obj
, "int2"),
571 "map value object has key \"int2\"");
572 ok(bt_value_map_has_entry(map_obj
, "real2"),
573 "map value object has key \"real2\"");
574 ok(bt_value_map_has_entry(map_obj
, "string2"),
575 "map value object has key \"string2\"");
576 ok(bt_value_map_has_entry(map_obj
, "array2"),
577 "map value object has key \"array2\"");
578 ok(bt_value_map_has_entry(map_obj
, "map2"),
579 "map value object has key \"map2\"");
581 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_count
, &count
);
582 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
583 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
585 memset(&checklist
, 0, sizeof(checklist
));
586 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_check
,
588 ok(ret
== BT_VALUE_STATUS_OK
,
589 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
590 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.real1
&&
591 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
592 checklist
.real2
&& checklist
.string2
&&
593 checklist
.array2
&& checklist
.map2
,
594 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
597 pass("putting an existing map value object does not cause a crash")
601 void test_types(void)
613 void test_compare_null(void)
615 ok(bt_value_compare(bt_value_null
, bt_value_null
),
616 "null value objects are equivalent");
620 void test_compare_bool(void)
622 struct bt_value
*bool1
= bt_value_bool_create_init(BT_FALSE
);
623 struct bt_value
*bool2
= bt_value_bool_create_init(BT_TRUE
);
624 struct bt_value
*bool3
= bt_value_bool_create_init(BT_FALSE
);
626 BT_ASSERT(bool1
&& bool2
&& bool3
);
627 ok(!bt_value_compare(bt_value_null
, bool1
),
628 "cannot compare null value object and bt_bool value object");
629 ok(!bt_value_compare(bool1
, bool2
),
630 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
631 ok(bt_value_compare(bool1
, bool3
),
632 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
640 void test_compare_integer(void)
642 struct bt_value
*int1
= bt_value_integer_create_init(10);
643 struct bt_value
*int2
= bt_value_integer_create_init(-23);
644 struct bt_value
*int3
= bt_value_integer_create_init(10);
646 BT_ASSERT(int1
&& int2
&& int3
);
647 ok(!bt_value_compare(bt_value_null
, int1
),
648 "cannot compare null value object and integer value object");
649 ok(!bt_value_compare(int1
, int2
),
650 "integer value objects are not equivalent (10 and -23)");
651 ok(bt_value_compare(int1
, int3
),
652 "integer value objects are equivalent (10 and 10)");
660 void test_compare_real(void)
662 struct bt_value
*real1
= bt_value_real_create_init(17.38);
663 struct bt_value
*real2
= bt_value_real_create_init(-14.23);
664 struct bt_value
*real3
= bt_value_real_create_init(17.38);
666 BT_ASSERT(real1
&& real2
&& real3
);
668 ok(!bt_value_compare(bt_value_null
, real1
),
669 "cannot compare null value object and real number value object");
670 ok(!bt_value_compare(real1
, real2
),
671 "real number value objects are not equivalent (17.38 and -14.23)");
672 ok(bt_value_compare(real1
, real3
),
673 "real number value objects are equivalent (17.38 and 17.38)");
681 void test_compare_string(void)
683 struct bt_value
*string1
= bt_value_string_create_init("hello");
684 struct bt_value
*string2
= bt_value_string_create_init("bt_value");
685 struct bt_value
*string3
= bt_value_string_create_init("hello");
687 BT_ASSERT(string1
&& string2
&& string3
);
689 ok(!bt_value_compare(bt_value_null
, string1
),
690 "cannot compare null value object and string value object");
691 ok(!bt_value_compare(string1
, string2
),
692 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
693 ok(bt_value_compare(string1
, string3
),
694 "string value objects are equivalent (\"hello\" and \"hello\")");
702 void test_compare_array(void)
704 struct bt_value
*array1
= bt_value_array_create();
705 struct bt_value
*array2
= bt_value_array_create();
706 struct bt_value
*array3
= bt_value_array_create();
708 BT_ASSERT(array1
&& array2
&& array3
);
710 ok(bt_value_compare(array1
, array2
),
711 "empty array value objects are equivalent");
713 BT_ASSERT(!bt_value_array_append_integer_element(array1
, 23));
714 BT_ASSERT(!bt_value_array_append_real_element(array1
, 14.2));
715 BT_ASSERT(!bt_value_array_append_bool_element(array1
, BT_FALSE
));
716 BT_ASSERT(!bt_value_array_append_real_element(array2
, 14.2));
717 BT_ASSERT(!bt_value_array_append_integer_element(array2
, 23));
718 BT_ASSERT(!bt_value_array_append_bool_element(array2
, BT_FALSE
));
719 BT_ASSERT(!bt_value_array_append_integer_element(array3
, 23));
720 BT_ASSERT(!bt_value_array_append_real_element(array3
, 14.2));
721 BT_ASSERT(!bt_value_array_append_bool_element(array3
, BT_FALSE
));
722 BT_ASSERT(bt_value_array_get_size(array1
) == 3);
723 BT_ASSERT(bt_value_array_get_size(array2
) == 3);
724 BT_ASSERT(bt_value_array_get_size(array3
) == 3);
726 ok(!bt_value_compare(bt_value_null
, array1
),
727 "cannot compare null value object and array value object");
728 ok(!bt_value_compare(array1
, array2
),
729 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
730 ok(bt_value_compare(array1
, array3
),
731 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
739 void test_compare_map(void)
741 struct bt_value
*map1
= bt_value_map_create();
742 struct bt_value
*map2
= bt_value_map_create();
743 struct bt_value
*map3
= bt_value_map_create();
745 BT_ASSERT(map1
&& map2
&& map3
);
747 ok(bt_value_compare(map1
, map2
),
748 "empty map value objects are equivalent");
750 BT_ASSERT(!bt_value_map_insert_integer_entry(map1
, "one", 23));
751 BT_ASSERT(!bt_value_map_insert_real_entry(map1
, "two", 14.2));
752 BT_ASSERT(!bt_value_map_insert_bool_entry(map1
, "three", BT_FALSE
));
753 BT_ASSERT(!bt_value_map_insert_real_entry(map2
, "one", 14.2));
754 BT_ASSERT(!bt_value_map_insert_integer_entry(map2
, "two", 23));
755 BT_ASSERT(!bt_value_map_insert_bool_entry(map2
, "three", BT_FALSE
));
756 BT_ASSERT(!bt_value_map_insert_bool_entry(map3
, "three", BT_FALSE
));
757 BT_ASSERT(!bt_value_map_insert_integer_entry(map3
, "one", 23));
758 BT_ASSERT(!bt_value_map_insert_real_entry(map3
, "two", 14.2));
759 BT_ASSERT(bt_value_map_get_size(map1
) == 3);
760 BT_ASSERT(bt_value_map_get_size(map2
) == 3);
761 BT_ASSERT(bt_value_map_get_size(map3
) == 3);
763 ok(!bt_value_compare(bt_value_null
, map1
),
764 "cannot compare null value object and map value object");
765 ok(!bt_value_compare(map1
, map2
),
766 "map value objects are not equivalent");
767 ok(bt_value_compare(map1
, map3
),
768 "map value objects are equivalent");
776 void test_compare(void)
780 test_compare_integer();
782 test_compare_string();
783 test_compare_array();
791 * Here's the deal here. If we make sure that each value object
792 * of our deep copy has a different address than its source,
793 * and that bt_value_compare() returns BT_TRUE for the top-level
794 * value object, taking into account that we test the correctness of
795 * bt_value_compare() elsewhere, then the deep copy is a
798 struct bt_value
*null_copy_obj
;
799 struct bt_value
*bool_obj
, *bool_copy_obj
;
800 struct bt_value
*integer_obj
, *integer_copy_obj
;
801 struct bt_value
*real_obj
, *real_copy_obj
;
802 struct bt_value
*string_obj
, *string_copy_obj
;
803 struct bt_value
*array_obj
, *array_copy_obj
;
804 struct bt_value
*map_obj
, *map_copy_obj
;
806 bool_obj
= bt_value_bool_create_init(BT_TRUE
);
807 integer_obj
= bt_value_integer_create_init(23);
808 real_obj
= bt_value_real_create_init(-3.1416);
809 string_obj
= bt_value_string_create_init("test");
810 array_obj
= bt_value_array_create();
811 map_obj
= bt_value_map_create();
813 BT_ASSERT(bool_obj
&& integer_obj
&& real_obj
&& string_obj
&&
814 array_obj
&& map_obj
);
816 BT_ASSERT(!bt_value_array_append_element(array_obj
, bool_obj
));
817 BT_ASSERT(!bt_value_array_append_element(array_obj
, integer_obj
));
818 BT_ASSERT(!bt_value_array_append_element(array_obj
, real_obj
));
819 BT_ASSERT(!bt_value_array_append_element(array_obj
, bt_value_null
));
820 BT_ASSERT(!bt_value_map_insert_entry(map_obj
, "array", array_obj
));
821 BT_ASSERT(!bt_value_map_insert_entry(map_obj
, "string", string_obj
));
823 map_copy_obj
= bt_value_copy(map_obj
);
825 "bt_value_copy() succeeds");
827 ok(map_obj
!= map_copy_obj
,
828 "bt_value_copy() returns a different pointer (map)");
829 string_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
, "string");
830 ok(string_copy_obj
!= string_obj
,
831 "bt_value_copy() returns a different pointer (string)");
832 array_copy_obj
= bt_value_map_borrow_entry_value(map_copy_obj
, "array");
833 ok(array_copy_obj
!= array_obj
,
834 "bt_value_copy() returns a different pointer (array)");
835 bool_copy_obj
= bt_value_array_borrow_element_by_index(array_copy_obj
, 0);
836 ok(bool_copy_obj
!= bool_obj
,
837 "bt_value_copy() returns a different pointer (bt_bool)");
838 integer_copy_obj
= bt_value_array_borrow_element_by_index(array_copy_obj
, 1);
839 ok(integer_copy_obj
!= integer_obj
,
840 "bt_value_copy() returns a different pointer (integer)");
841 real_copy_obj
= bt_value_array_borrow_element_by_index(array_copy_obj
, 2);
842 ok(real_copy_obj
!= real_obj
,
843 "bt_value_copy() returns a different pointer (real)");
844 null_copy_obj
= bt_value_array_borrow_element_by_index(array_copy_obj
, 3);
845 ok(null_copy_obj
== bt_value_null
,
846 "bt_value_copy() returns the same pointer (null)");
848 ok(bt_value_compare(map_obj
, map_copy_obj
),
849 "source and destination value objects have the same content");
851 BT_PUT(map_copy_obj
);
861 bt_bool
compare_map_elements(struct bt_value
*map_a
, struct bt_value
*map_b
,
864 struct bt_value
*elem_a
= NULL
;
865 struct bt_value
*elem_b
= NULL
;
868 elem_a
= bt_value_map_borrow_entry_value(map_a
, key
);
869 elem_b
= bt_value_map_borrow_entry_value(map_b
, key
);
870 equal
= bt_value_compare(elem_a
, elem_b
);
875 void test_extend(void)
877 struct bt_value
*base_map
= bt_value_map_create();
878 struct bt_value
*extension_map
= bt_value_map_create();
879 struct bt_value
*extended_map
= NULL
;
880 struct bt_value
*array
= bt_value_array_create();
881 enum bt_value_status status
;
884 BT_ASSERT(extension_map
);
886 status
= bt_value_map_insert_bool_entry(base_map
, "file", BT_TRUE
);
887 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
888 status
= bt_value_map_insert_bool_entry(base_map
, "edit", BT_FALSE
);
889 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
890 status
= bt_value_map_insert_integer_entry(base_map
, "selection", 17);
891 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
892 status
= bt_value_map_insert_integer_entry(base_map
, "find", -34);
893 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
894 status
= bt_value_map_insert_bool_entry(extension_map
, "edit", BT_TRUE
);
895 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
896 status
= bt_value_map_insert_integer_entry(extension_map
, "find", 101);
897 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
898 status
= bt_value_map_insert_real_entry(extension_map
, "project", -404);
899 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
900 extended_map
= bt_value_map_extend(base_map
, extension_map
);
901 ok(extended_map
, "bt_value_map_extend() succeeds");
902 ok(bt_value_map_get_size(extended_map
) == 5,
903 "bt_value_map_extend() returns a map object with the correct size");
904 ok(compare_map_elements(base_map
, extended_map
, "file"),
905 "bt_value_map_extend() picks the appropriate element (file)");
906 ok(compare_map_elements(extension_map
, extended_map
, "edit"),
907 "bt_value_map_extend() picks the appropriate element (edit)");
908 ok(compare_map_elements(base_map
, extended_map
, "selection"),
909 "bt_value_map_extend() picks the appropriate element (selection)");
910 ok(compare_map_elements(extension_map
, extended_map
, "find"),
911 "bt_value_map_extend() picks the appropriate element (find)");
912 ok(compare_map_elements(extension_map
, extended_map
, "project"),
913 "bt_value_map_extend() picks the appropriate element (project)");
917 BT_PUT(extension_map
);
918 BT_PUT(extended_map
);
923 plan_tests(NR_TESTS
);