4 * Babeltrace value objects tests
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; under version 2 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <babeltrace/object.h>
24 #include <babeltrace/values.h>
25 #include <babeltrace/private-values.h>
26 #include <babeltrace/assert-internal.h>
35 ok(bt_value_null
, "bt_value_null is not NULL");
36 ok(bt_value_is_null(bt_value_null
),
37 "bt_value_null is a null value object");
38 bt_object_get_ref(bt_value_null
);
39 pass("getting bt_value_null does not cause a crash");
40 bt_object_put_ref(bt_value_null
);
41 pass("putting bt_value_null does not cause a crash");
49 struct bt_private_value
*priv_obj
;
52 priv_obj
= bt_private_value_bool_create();
53 obj
= bt_value_borrow_from_private(priv_obj
);
54 ok(obj
&& bt_value_is_bool(obj
),
55 "bt_private_value_bool_create() returns a boolean value object");
58 ret
= bt_value_bool_get(obj
, &value
);
59 ok(!ret
&& !value
, "default boolean value object value is BT_FALSE");
61 BT_ASSERT(!bt_private_value_bool_set(priv_obj
, BT_FALSE
));
62 ret
= bt_private_value_bool_set(priv_obj
, BT_TRUE
);
63 ok(!ret
, "bt_private_value_bool_set() succeeds");
64 ret
= bt_value_bool_get(obj
, &value
);
65 ok(!ret
&& value
, "bt_private_value_bool_set() works");
67 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
68 pass("putting an existing boolean value object does not cause a crash")
71 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
72 obj
= bt_value_borrow_from_private(priv_obj
);
73 ok(obj
&& bt_value_is_bool(obj
),
74 "bt_private_value_bool_create_init() returns a boolean value object");
75 ret
= bt_value_bool_get(obj
, &value
);
77 "bt_private_value_bool_create_init() sets the appropriate initial value");
79 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
83 void test_integer(void)
87 struct bt_private_value
*priv_obj
;
90 priv_obj
= bt_private_value_integer_create();
91 obj
= bt_value_borrow_from_private(priv_obj
);
92 ok(obj
&& bt_value_is_integer(obj
),
93 "bt_private_value_integer_create() returns an integer value object");
96 ret
= bt_value_integer_get(obj
, &value
);
97 ok(!ret
&& value
== 0, "default integer value object value is 0");
99 ret
= bt_private_integer_bool_set(priv_obj
, -98765);
100 ok(!ret
, "bt_private_integer_bool_set() succeeds");
101 ret
= bt_value_integer_get(obj
, &value
);
102 ok(!ret
&& value
== -98765, "bt_private_integer_bool_set() works");
104 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
105 pass("putting an existing integer value object does not cause a crash")
107 priv_obj
= bt_private_value_integer_create_init(321456987);
108 obj
= bt_value_borrow_from_private(priv_obj
);
109 ok(obj
&& bt_value_is_integer(obj
),
110 "bt_private_value_integer_create_init() returns an integer value object");
111 ret
= bt_value_integer_get(obj
, &value
);
112 ok(!ret
&& value
== 321456987,
113 "bt_private_value_integer_create_init() sets the appropriate initial value");
115 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
123 struct bt_private_value
*priv_obj
;
124 struct bt_value
*obj
;
126 priv_obj
= bt_private_value_real_create();
127 obj
= bt_value_borrow_from_private(priv_obj
);
128 ok(obj
&& bt_value_is_real(obj
),
129 "bt_private_value_real_create() returns a real number value object");
132 ret
= bt_value_real_get(obj
, &value
);
133 ok(!ret
&& value
== 0.,
134 "default real number value object value is 0");
136 ret
= bt_private_value_real_set(priv_obj
, -3.1416);
137 ok(!ret
, "bt_private_value_real_set() succeeds");
138 ret
= bt_value_real_get(obj
, &value
);
139 ok(!ret
&& value
== -3.1416, "bt_private_value_real_set() works");
141 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
142 pass("putting an existing real number value object does not cause a crash")
144 priv_obj
= bt_private_value_real_create_init(33.1649758);
145 obj
= bt_value_borrow_from_private(priv_obj
);
146 ok(obj
&& bt_value_is_real(obj
),
147 "bt_private_value_real_create_init() returns a real number value object");
148 ret
= bt_value_real_get(obj
, &value
);
149 ok(!ret
&& value
== 33.1649758,
150 "bt_private_value_real_create_init() sets the appropriate initial value");
152 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
156 void test_string(void)
160 struct bt_private_value
*priv_obj
;
161 struct bt_value
*obj
;
163 priv_obj
= bt_private_value_string_create();
164 obj
= bt_value_borrow_from_private(priv_obj
);
165 ok(obj
&& bt_value_is_string(obj
),
166 "bt_private_value_string_create() returns a string value object");
168 ret
= bt_value_string_get(obj
, &value
);
169 ok(!ret
&& value
&& !strcmp(value
, ""),
170 "default string value object value is \"\"");
172 ret
= bt_private_value_string_set(priv_obj
, "hello worldz");
173 ok(!ret
, "bt_private_value_string_set() succeeds");
174 ret
= bt_value_string_get(obj
, &value
);
175 ok(!ret
&& value
&& !strcmp(value
, "hello worldz"),
176 "bt_value_string_get() works");
178 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
179 pass("putting an existing string value object does not cause a crash")
181 priv_obj
= bt_private_value_string_create_init("initial value");
182 obj
= bt_value_borrow_from_private(priv_obj
);
183 ok(obj
&& bt_value_is_string(obj
),
184 "bt_private_value_string_create_init() returns a string value object");
185 ret
= bt_value_string_get(obj
, &value
);
186 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
187 "bt_private_value_string_create_init() sets the appropriate initial value");
189 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
193 void test_array(void)
199 struct bt_private_value
*priv_obj
;
200 struct bt_value
*obj
;
201 const char *string_value
;
202 struct bt_private_value
*priv_array_obj
;
203 struct bt_value
*array_obj
;
205 priv_array_obj
= bt_private_value_array_create();
206 array_obj
= bt_value_borrow_from_private(priv_array_obj
);
207 ok(array_obj
&& bt_value_is_array(array_obj
),
208 "bt_private_value_array_create() returns an array value object");
209 ok(bt_value_array_is_empty(array_obj
),
210 "initial array value object size is 0");
212 priv_obj
= bt_private_value_integer_create_init(345);
213 obj
= bt_value_borrow_from_private(priv_obj
);
214 ret
= bt_private_value_array_append_element(priv_array_obj
, obj
);
215 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
216 priv_obj
= bt_private_value_real_create_init(-17.45);
217 obj
= bt_value_borrow_from_private(priv_obj
);
218 ret
|= bt_private_value_array_append_element(priv_array_obj
, obj
);
219 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
220 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
221 obj
= bt_value_borrow_from_private(priv_obj
);
222 ret
|= bt_private_value_array_append_element(priv_array_obj
, obj
);
223 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
224 ret
|= bt_private_value_array_append_element(priv_array_obj
,
226 ok(!ret
, "bt_private_value_array_append_element() succeeds");
227 ok(bt_value_array_get_size(array_obj
) == 4,
228 "appending an element to an array value object increment its size");
230 obj
= bt_value_array_borrow_element_by_index(array_obj
, 0);
231 ok(obj
&& bt_value_is_integer(obj
),
232 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (integer)");
233 ret
= bt_value_integer_get(obj
, &int_value
);
234 ok(!ret
&& int_value
== 345,
235 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (integer)");
236 obj
= bt_value_array_borrow_element_by_index(array_obj
, 1);
237 ok(obj
&& bt_value_is_real(obj
),
238 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
239 ret
= bt_value_real_get(obj
, &real_value
);
240 ok(!ret
&& real_value
== -17.45,
241 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
242 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
243 ok(obj
&& bt_value_is_bool(obj
),
244 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
245 ret
= bt_value_bool_get(obj
, &bool_value
);
246 ok(!ret
&& bool_value
,
247 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
248 obj
= bt_value_array_borrow_element_by_index(array_obj
, 3);
249 ok(obj
== bt_value_null
,
250 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
252 priv_obj
= bt_private_value_integer_create_init(1001);
253 obj
= bt_value_borrow_from_private(priv_obj
);
255 ok(!bt_private_value_array_set_element_by_index(priv_array_obj
, 2, obj
),
256 "bt_value_array_set_element_by_index() succeeds");
257 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
258 obj
= bt_value_array_borrow_element_by_index(array_obj
, 2);
259 ok(obj
&& bt_value_is_integer(obj
),
260 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
261 ret
= bt_value_integer_get(obj
, &int_value
);
263 ok(int_value
== 1001,
264 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
266 ret
= bt_private_value_array_append_bool_element(priv_array_obj
,
268 ok(!ret
, "bt_private_value_array_append_bool_element() succeeds");
269 ret
= bt_private_value_array_append_integer_element(priv_array_obj
,
271 ok(!ret
, "bt_private_value_array_append_integer_element() succeeds");
272 ret
= bt_private_value_array_append_real_element(priv_array_obj
,
274 ok(!ret
, "bt_private_value_array_append_real_element() succeeds");
275 ret
= bt_private_value_array_append_string_element(priv_array_obj
,
277 ok(!ret
, "bt_private_value_array_append_string_element() succeeds");
278 ret
= bt_private_value_array_append_empty_array_element(priv_array_obj
);
279 ok(!ret
, "bt_private_value_array_append_empty_array_element() succeeds");
280 ret
= bt_private_value_array_append_empty_map_element(priv_array_obj
);
281 ok(!ret
, "bt_private_value_array_append_empty_map_element() succeeds");
283 ok(bt_value_array_get_size(array_obj
) == 10,
284 "the bt_private_value_array_append_element_*() functions increment the array value object's size");
285 ok(!bt_value_array_is_empty(array_obj
),
286 "map value object is not empty");
288 obj
= bt_value_array_borrow_element_by_index(array_obj
, 4);
289 ok(obj
&& bt_value_is_bool(obj
),
290 "bt_private_value_array_append_bool_element() appends a boolean value object");
291 ret
= bt_value_bool_get(obj
, &bool_value
);
292 ok(!ret
&& !bool_value
,
293 "bt_private_value_array_append_bool_element() appends the appropriate value");
294 obj
= bt_value_array_borrow_element_by_index(array_obj
, 5);
295 ok(obj
&& bt_value_is_integer(obj
),
296 "bt_private_value_array_append_integer_element() appends an integer value object");
297 ret
= bt_value_integer_get(obj
, &int_value
);
298 ok(!ret
&& int_value
== 98765,
299 "bt_private_value_array_append_integer_element() appends the appropriate value");
300 obj
= bt_value_array_borrow_element_by_index(array_obj
, 6);
301 ok(obj
&& bt_value_is_real(obj
),
302 "bt_private_value_array_append_real_element() appends a real number value object");
303 ret
= bt_value_real_get(obj
, &real_value
);
304 ok(!ret
&& real_value
== 2.49578,
305 "bt_private_value_array_append_real_element() appends the appropriate value");
306 obj
= bt_value_array_borrow_element_by_index(array_obj
, 7);
307 ok(obj
&& bt_value_is_string(obj
),
308 "bt_private_value_array_append_string_element() appends a string value object");
309 ret
= bt_value_string_get(obj
, &string_value
);
310 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
311 "bt_private_value_array_append_string_element() appends the appropriate value");
312 obj
= bt_value_array_borrow_element_by_index(array_obj
, 8);
313 ok(obj
&& bt_value_is_array(obj
),
314 "bt_private_value_array_append_empty_array_element() appends an array value object");
315 ok(bt_value_array_is_empty(obj
),
316 "bt_private_value_array_append_empty_array_element() an empty array value object");
317 obj
= bt_value_array_borrow_element_by_index(array_obj
, 9);
318 ok(obj
&& bt_value_is_map(obj
),
319 "bt_private_value_array_append_empty_map_element() appends a map value object");
320 ok(bt_value_map_is_empty(obj
),
321 "bt_private_value_array_append_empty_map_element() an empty map value object");
323 BT_OBJECT_PUT_REF_AND_RESET(priv_array_obj
);
324 pass("putting an existing array value object does not cause a crash")
328 bt_bool
test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
342 struct map_foreach_checklist
{
356 bt_bool
test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
360 struct map_foreach_checklist
*checklist
= data
;
362 if (!strcmp(key
, "bt_bool")) {
363 if (checklist
->bool1
) {
364 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
366 bt_bool val
= BT_FALSE
;
368 ret
= bt_value_bool_get(object
, &val
);
369 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
372 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
373 checklist
->bool1
= BT_TRUE
;
375 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
378 } else if (!strcmp(key
, "int")) {
379 if (checklist
->int1
) {
380 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
384 ret
= bt_value_integer_get(object
, &val
);
385 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
388 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
389 checklist
->int1
= BT_TRUE
;
391 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
394 } else if (!strcmp(key
, "real")) {
395 if (checklist
->real1
) {
396 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
400 ret
= bt_value_real_get(object
, &val
);
401 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real\" value");
404 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
405 checklist
->real1
= BT_TRUE
;
407 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
410 } else if (!strcmp(key
, "null")) {
411 if (checklist
->null1
) {
412 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
414 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
415 checklist
->null1
= BT_TRUE
;
417 } else if (!strcmp(key
, "bool2")) {
418 if (checklist
->bool2
) {
419 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
421 bt_bool val
= BT_FALSE
;
423 ret
= bt_value_bool_get(object
, &val
);
424 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
427 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
428 checklist
->bool2
= BT_TRUE
;
430 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
433 } else if (!strcmp(key
, "int2")) {
434 if (checklist
->int2
) {
435 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
439 ret
= bt_value_integer_get(object
, &val
);
440 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
443 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
444 checklist
->int2
= BT_TRUE
;
446 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
449 } else if (!strcmp(key
, "real2")) {
450 if (checklist
->real2
) {
451 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
455 ret
= bt_value_real_get(object
, &val
);
456 ok(!ret
, "test_map_foreach_cb_check(): success getting \"real2\" value");
458 if (val
== -49.0001) {
459 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
460 checklist
->real2
= BT_TRUE
;
462 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
465 } else if (!strcmp(key
, "string2")) {
466 if (checklist
->string2
) {
467 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
471 ret
= bt_value_string_get(object
, &val
);
472 ok(!ret
, "test_map_foreach_cb_check(): success getting \"string2\" value");
474 if (val
&& !strcmp(val
, "bt_value")) {
475 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
476 checklist
->string2
= BT_TRUE
;
478 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
481 } else if (!strcmp(key
, "array2")) {
482 if (checklist
->array2
) {
483 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
485 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
486 ok(bt_value_array_is_empty(object
),
487 "test_map_foreach_cb_check(): \"array2\" value object is empty");
488 checklist
->array2
= BT_TRUE
;
490 } else if (!strcmp(key
, "map2")) {
491 if (checklist
->map2
) {
492 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
494 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
495 ok(bt_value_map_is_empty(object
),
496 "test_map_foreach_cb_check(): \"map2\" value object is empty");
497 checklist
->map2
= BT_TRUE
;
500 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
515 struct bt_private_value
*priv_obj
;
516 struct bt_value
*obj
;
517 struct bt_private_value
*priv_map_obj
;
518 struct bt_value
*map_obj
;
519 struct map_foreach_checklist checklist
;
521 priv_map_obj
= bt_private_value_map_create();
522 map_obj
= bt_value_borrow_from_private(priv_map_obj
);
523 ok(map_obj
&& bt_value_is_map(map_obj
),
524 "bt_private_value_map_create() returns a map value object");
525 ok(bt_value_map_get_size(map_obj
) == 0,
526 "initial map value object size is 0");
528 priv_obj
= bt_private_value_integer_create_init(19457);
529 obj
= bt_value_borrow_from_private(priv_obj
);
530 ret
= bt_private_value_map_insert_entry(priv_map_obj
, "int", obj
);
531 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
532 priv_obj
= bt_private_value_real_create_init(5.444);
533 obj
= bt_value_borrow_from_private(priv_obj
);
534 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "real", obj
);
535 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
536 priv_obj
= bt_private_value_bool_create();
537 obj
= bt_value_borrow_from_private(priv_obj
);
538 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "bt_bool", obj
);
539 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
540 ret
|= bt_private_value_map_insert_entry(priv_map_obj
, "null",
542 ok(!ret
, "bt_private_value_map_insert_entry() succeeds");
543 ok(bt_value_map_get_size(map_obj
) == 4,
544 "inserting an element into a map value object increment its size");
546 priv_obj
= bt_private_value_bool_create_init(BT_TRUE
);
547 obj
= bt_value_borrow_from_private(priv_obj
);
548 ret
= bt_private_value_map_insert_entry(priv_map_obj
, "bt_bool", obj
);
549 BT_OBJECT_PUT_REF_AND_RESET(priv_obj
);
550 ok(!ret
, "bt_private_value_map_insert_entry() accepts an existing key");
552 obj
= bt_value_map_borrow_entry_value(map_obj
, "life");
553 ok(!obj
, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
554 obj
= bt_value_map_borrow_entry_value(map_obj
, "real");
555 ok(obj
&& bt_value_is_real(obj
),
556 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
557 ret
= bt_value_real_get(obj
, &real_value
);
558 ok(!ret
&& real_value
== 5.444,
559 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
560 obj
= bt_value_map_borrow_entry_value(map_obj
, "int");
561 ok(obj
&& bt_value_is_integer(obj
),
562 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
563 ret
= bt_value_integer_get(obj
, &int_value
);
564 ok(!ret
&& int_value
== 19457,
565 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (integer)");
566 obj
= bt_value_map_borrow_entry_value(map_obj
, "null");
567 ok(obj
&& bt_value_is_null(obj
),
568 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
569 obj
= bt_value_map_borrow_entry_value(map_obj
, "bt_bool");
570 ok(obj
&& bt_value_is_bool(obj
),
571 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
572 ret
= bt_value_bool_get(obj
, &bool_value
);
573 ok(!ret
&& bool_value
,
574 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
576 ret
= bt_private_value_map_insert_bool_entry(priv_map_obj
, "bool2",
578 ok(!ret
, "bt_private_value_map_insert_bool_entry() succeeds");
579 ret
= bt_private_value_map_insert_integer_entry(priv_map_obj
, "int2",
581 ok(!ret
, "bt_private_value_map_insert_integer_entry() succeeds");
582 ret
= bt_private_value_map_insert_real_entry(priv_map_obj
, "real2",
584 ok(!ret
, "bt_private_value_map_insert_real_entry() succeeds");
585 ret
= bt_private_value_map_insert_string_entry(priv_map_obj
, "string2",
587 ok(!ret
, "bt_private_value_map_insert_string_entry() succeeds");
588 ret
= bt_private_value_map_insert_empty_array_entry(priv_map_obj
,
590 ok(!ret
, "bt_private_value_map_insert_empty_array_entry() succeeds");
591 ret
= bt_private_value_map_insert_empty_map_entry(priv_map_obj
, "map2");
592 ok(!ret
, "bt_private_value_map_insert_empty_map_entry() succeeds");
594 ok(bt_value_map_get_size(map_obj
) == 10,
595 "the bt_value_map_insert*() functions increment the map value object's size");
597 ok(!bt_value_map_has_entry(map_obj
, "hello"),
598 "map value object does not have key \"hello\"");
599 ok(bt_value_map_has_entry(map_obj
, "bt_bool"),
600 "map value object has key \"bt_bool\"");
601 ok(bt_value_map_has_entry(map_obj
, "int"),
602 "map value object has key \"int\"");
603 ok(bt_value_map_has_entry(map_obj
, "real"),
604 "map value object has key \"real\"");
605 ok(bt_value_map_has_entry(map_obj
, "null"),
606 "map value object has key \"null\"");
607 ok(bt_value_map_has_entry(map_obj
, "bool2"),
608 "map value object has key \"bool2\"");
609 ok(bt_value_map_has_entry(map_obj
, "int2"),
610 "map value object has key \"int2\"");
611 ok(bt_value_map_has_entry(map_obj
, "real2"),
612 "map value object has key \"real2\"");
613 ok(bt_value_map_has_entry(map_obj
, "string2"),
614 "map value object has key \"string2\"");
615 ok(bt_value_map_has_entry(map_obj
, "array2"),
616 "map value object has key \"array2\"");
617 ok(bt_value_map_has_entry(map_obj
, "map2"),
618 "map value object has key \"map2\"");
620 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_count
,
622 ok(ret
== BT_VALUE_STATUS_CANCELED
&& count
== 3,
623 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
625 memset(&checklist
, 0, sizeof(checklist
));
626 ret
= bt_value_map_foreach_entry(map_obj
, test_map_foreach_cb_check
,
628 ok(ret
== BT_VALUE_STATUS_OK
,
629 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
630 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.real1
&&
631 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
632 checklist
.real2
&& checklist
.string2
&&
633 checklist
.array2
&& checklist
.map2
,
634 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
636 BT_OBJECT_PUT_REF_AND_RESET(priv_map_obj
);
637 pass("putting an existing map value object does not cause a crash")
641 void test_types(void)
653 void test_compare_null(void)
655 ok(bt_value_compare(bt_value_null
, bt_value_null
),
656 "null value objects are equivalent");
660 void test_compare_bool(void)
662 struct bt_private_value
*bool1
=
663 bt_private_value_bool_create_init(BT_FALSE
);
664 struct bt_private_value
*bool2
=
665 bt_private_value_bool_create_init(BT_TRUE
);
666 struct bt_private_value
*bool3
=
667 bt_private_value_bool_create_init(BT_FALSE
);
669 BT_ASSERT(bool1
&& bool2
&& bool3
);
670 ok(!bt_value_compare(bt_value_null
,
671 bt_value_borrow_from_private(bool1
)),
672 "cannot compare null value object and bt_bool value object");
673 ok(!bt_value_compare(bt_value_borrow_from_private(bool1
),
674 bt_value_borrow_from_private(bool2
)),
675 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
676 ok(bt_value_compare(bt_value_borrow_from_private(bool1
),
677 bt_value_borrow_from_private(bool3
)),
678 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
680 BT_OBJECT_PUT_REF_AND_RESET(bool1
);
681 BT_OBJECT_PUT_REF_AND_RESET(bool2
);
682 BT_OBJECT_PUT_REF_AND_RESET(bool3
);
686 void test_compare_integer(void)
688 struct bt_private_value
*int1
=
689 bt_private_value_integer_create_init(10);
690 struct bt_private_value
*int2
=
691 bt_private_value_integer_create_init(-23);
692 struct bt_private_value
*int3
=
693 bt_private_value_integer_create_init(10);
695 BT_ASSERT(int1
&& int2
&& int3
);
696 ok(!bt_value_compare(bt_value_null
,
697 bt_value_borrow_from_private(int1
)),
698 "cannot compare null value object and integer value object");
699 ok(!bt_value_compare(bt_value_borrow_from_private(int1
),
700 bt_value_borrow_from_private(int2
)),
701 "integer value objects are not equivalent (10 and -23)");
702 ok(bt_value_compare(bt_value_borrow_from_private(int1
),
703 bt_value_borrow_from_private(int3
)),
704 "integer value objects are equivalent (10 and 10)");
706 BT_OBJECT_PUT_REF_AND_RESET(int1
);
707 BT_OBJECT_PUT_REF_AND_RESET(int2
);
708 BT_OBJECT_PUT_REF_AND_RESET(int3
);
712 void test_compare_real(void)
714 struct bt_private_value
*real1
=
715 bt_private_value_real_create_init(17.38);
716 struct bt_private_value
*real2
=
717 bt_private_value_real_create_init(-14.23);
718 struct bt_private_value
*real3
=
719 bt_private_value_real_create_init(17.38);
721 BT_ASSERT(real1
&& real2
&& real3
);
723 ok(!bt_value_compare(bt_value_null
,
724 bt_value_borrow_from_private(real1
)),
725 "cannot compare null value object and real number value object");
726 ok(!bt_value_compare(bt_value_borrow_from_private(real1
),
727 bt_value_borrow_from_private(real2
)),
728 "real number value objects are not equivalent (17.38 and -14.23)");
729 ok(bt_value_compare(bt_value_borrow_from_private(real1
),
730 bt_value_borrow_from_private(real3
)),
731 "real number value objects are equivalent (17.38 and 17.38)");
733 BT_OBJECT_PUT_REF_AND_RESET(real1
);
734 BT_OBJECT_PUT_REF_AND_RESET(real2
);
735 BT_OBJECT_PUT_REF_AND_RESET(real3
);
739 void test_compare_string(void)
741 struct bt_private_value
*string1
=
742 bt_private_value_string_create_init("hello");
743 struct bt_private_value
*string2
=
744 bt_private_value_string_create_init("bt_value");
745 struct bt_private_value
*string3
=
746 bt_private_value_string_create_init("hello");
748 BT_ASSERT(string1
&& string2
&& string3
);
750 ok(!bt_value_compare(bt_value_null
,
751 bt_value_borrow_from_private(string1
)),
752 "cannot compare null value object and string value object");
753 ok(!bt_value_compare(bt_value_borrow_from_private(string1
),
754 bt_value_borrow_from_private(string2
)),
755 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
756 ok(bt_value_compare(bt_value_borrow_from_private(string1
),
757 bt_value_borrow_from_private(string3
)),
758 "string value objects are equivalent (\"hello\" and \"hello\")");
760 BT_OBJECT_PUT_REF_AND_RESET(string1
);
761 BT_OBJECT_PUT_REF_AND_RESET(string2
);
762 BT_OBJECT_PUT_REF_AND_RESET(string3
);
766 void test_compare_array(void)
768 struct bt_private_value
*array1
= bt_private_value_array_create();
769 struct bt_private_value
*array2
= bt_private_value_array_create();
770 struct bt_private_value
*array3
= bt_private_value_array_create();
771 enum bt_value_status status
;
773 BT_ASSERT(array1
&& array2
&& array3
);
775 ok(bt_value_compare(bt_value_borrow_from_private(array1
),
776 bt_value_borrow_from_private(array2
)),
777 "empty array value objects are equivalent");
779 status
= bt_private_value_array_append_integer_element(array1
, 23);
780 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
781 status
= bt_private_value_array_append_real_element(array1
, 14.2);
782 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
783 status
= bt_private_value_array_append_bool_element(array1
, BT_FALSE
);
784 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
785 status
= bt_private_value_array_append_real_element(array2
, 14.2);
786 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
787 status
= bt_private_value_array_append_integer_element(array2
, 23);
788 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
789 status
= bt_private_value_array_append_bool_element(array2
, BT_FALSE
);
790 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
791 status
= bt_private_value_array_append_integer_element(array3
, 23);
792 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
793 status
= bt_private_value_array_append_real_element(array3
, 14.2);
794 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
795 status
= bt_private_value_array_append_bool_element(array3
, BT_FALSE
);
796 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
797 BT_ASSERT(bt_value_array_get_size(
798 bt_value_borrow_from_private(array1
)) == 3);
799 BT_ASSERT(bt_value_array_get_size(
800 bt_value_borrow_from_private(array2
)) == 3);
801 BT_ASSERT(bt_value_array_get_size(
802 bt_value_borrow_from_private(array3
)) == 3);
804 ok(!bt_value_compare(bt_value_null
,
805 bt_value_borrow_from_private(array1
)),
806 "cannot compare null value object and array value object");
807 ok(!bt_value_compare(bt_value_borrow_from_private(array1
),
808 bt_value_borrow_from_private(array2
)),
809 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
810 ok(bt_value_compare(bt_value_borrow_from_private(array1
),
811 bt_value_borrow_from_private(array3
)),
812 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
814 BT_OBJECT_PUT_REF_AND_RESET(array1
);
815 BT_OBJECT_PUT_REF_AND_RESET(array2
);
816 BT_OBJECT_PUT_REF_AND_RESET(array3
);
820 void test_compare_map(void)
822 struct bt_private_value
*map1
= bt_private_value_map_create();
823 struct bt_private_value
*map2
= bt_private_value_map_create();
824 struct bt_private_value
*map3
= bt_private_value_map_create();
825 enum bt_value_status status
;
827 BT_ASSERT(map1
&& map2
&& map3
);
829 ok(bt_value_compare(bt_value_borrow_from_private(map1
),
830 bt_value_borrow_from_private(map2
)),
831 "empty map value objects are equivalent");
834 status
= bt_private_value_map_insert_integer_entry(map1
, "one", 23);
835 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
836 status
= bt_private_value_map_insert_real_entry(map1
, "two", 14.2);
837 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
838 status
= bt_private_value_map_insert_bool_entry(map1
, "three",
840 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
841 status
= bt_private_value_map_insert_real_entry(map2
, "one", 14.2);
842 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
843 status
= bt_private_value_map_insert_integer_entry(map2
, "two", 23);
844 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
845 status
= bt_private_value_map_insert_bool_entry(map2
, "three",
847 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
848 status
= bt_private_value_map_insert_bool_entry(map3
, "three",
850 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
851 status
= bt_private_value_map_insert_integer_entry(map3
, "one", 23);
852 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
853 status
= bt_private_value_map_insert_real_entry(map3
, "two", 14.2);
854 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
855 BT_ASSERT(bt_value_map_get_size(
856 bt_value_borrow_from_private(map1
)) == 3);
857 BT_ASSERT(bt_value_map_get_size(
858 bt_value_borrow_from_private(map2
)) == 3);
859 BT_ASSERT(bt_value_map_get_size(
860 bt_value_borrow_from_private(map3
)) == 3);
862 ok(!bt_value_compare(bt_value_null
,
863 bt_value_borrow_from_private(map1
)),
864 "cannot compare null value object and map value object");
865 ok(!bt_value_compare(bt_value_borrow_from_private(map1
),
866 bt_value_borrow_from_private(map2
)),
867 "map value objects are not equivalent");
868 ok(bt_value_compare(bt_value_borrow_from_private(map1
),
869 bt_value_borrow_from_private(map3
)),
870 "map value objects are equivalent");
872 BT_OBJECT_PUT_REF_AND_RESET(map1
);
873 BT_OBJECT_PUT_REF_AND_RESET(map2
);
874 BT_OBJECT_PUT_REF_AND_RESET(map3
);
878 void test_compare(void)
882 test_compare_integer();
884 test_compare_string();
885 test_compare_array();
893 * Here's the deal here. If we make sure that each value object
894 * of our deep copy has a different address than its source,
895 * and that bt_value_compare() returns BT_TRUE for the top-level
896 * value object, taking into account that we test the correctness of
897 * bt_value_compare() elsewhere, then the deep copy is a
900 struct bt_private_value
*null_copy_obj
;
901 struct bt_private_value
*bool_obj
, *bool_copy_obj
;
902 struct bt_private_value
*integer_obj
, *integer_copy_obj
;
903 struct bt_private_value
*real_obj
, *real_copy_obj
;
904 struct bt_private_value
*string_obj
, *string_copy_obj
;
905 struct bt_private_value
*array_obj
, *array_copy_obj
;
906 struct bt_private_value
*map_obj
, *map_copy_obj
;
907 enum bt_value_status status
;
909 bool_obj
= bt_private_value_bool_create_init(BT_TRUE
);
910 integer_obj
= bt_private_value_integer_create_init(23);
911 real_obj
= bt_private_value_real_create_init(-3.1416);
912 string_obj
= bt_private_value_string_create_init("test");
913 array_obj
= bt_private_value_array_create();
914 map_obj
= bt_private_value_map_create();
916 BT_ASSERT(bool_obj
&& integer_obj
&& real_obj
&& string_obj
&&
917 array_obj
&& map_obj
);
919 status
= bt_private_value_array_append_element(array_obj
,
920 bt_value_borrow_from_private(bool_obj
));
921 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
922 status
= bt_private_value_array_append_element(array_obj
,
923 bt_value_borrow_from_private(integer_obj
));
924 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
925 status
= bt_private_value_array_append_element(array_obj
,
926 bt_value_borrow_from_private(real_obj
));
927 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
928 status
= bt_private_value_array_append_element(array_obj
,
930 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
931 status
= bt_private_value_map_insert_entry(map_obj
, "array",
932 bt_value_borrow_from_private(array_obj
));
933 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
934 status
= bt_private_value_map_insert_entry(map_obj
, "string",
935 bt_value_borrow_from_private(string_obj
));
936 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
938 map_copy_obj
= bt_value_copy(bt_value_borrow_from_private(map_obj
));
940 "bt_value_copy() succeeds");
942 ok(map_obj
!= map_copy_obj
,
943 "bt_value_copy() returns a different pointer (map)");
944 string_copy_obj
= bt_private_value_map_borrow_entry_value(map_copy_obj
,
946 ok(string_copy_obj
!= string_obj
,
947 "bt_value_copy() returns a different pointer (string)");
948 array_copy_obj
= bt_private_value_map_borrow_entry_value(map_copy_obj
,
950 ok(array_copy_obj
!= array_obj
,
951 "bt_value_copy() returns a different pointer (array)");
952 bool_copy_obj
= bt_private_value_array_borrow_element_by_index(
954 ok(bool_copy_obj
!= bool_obj
,
955 "bt_value_copy() returns a different pointer (bt_bool)");
956 integer_copy_obj
= bt_private_value_array_borrow_element_by_index(
958 ok(integer_copy_obj
!= integer_obj
,
959 "bt_value_copy() returns a different pointer (integer)");
960 real_copy_obj
= bt_private_value_array_borrow_element_by_index(
962 ok(real_copy_obj
!= real_obj
,
963 "bt_value_copy() returns a different pointer (real)");
964 null_copy_obj
= bt_private_value_array_borrow_element_by_index(
966 ok(bt_value_borrow_from_private(null_copy_obj
) == bt_value_null
,
967 "bt_value_copy() returns the same pointer (null)");
969 ok(bt_value_compare(bt_value_borrow_from_private(map_obj
),
970 bt_value_borrow_from_private(map_copy_obj
)),
971 "source and destination value objects have the same content");
973 BT_OBJECT_PUT_REF_AND_RESET(map_copy_obj
);
974 BT_OBJECT_PUT_REF_AND_RESET(bool_obj
);
975 BT_OBJECT_PUT_REF_AND_RESET(integer_obj
);
976 BT_OBJECT_PUT_REF_AND_RESET(real_obj
);
977 BT_OBJECT_PUT_REF_AND_RESET(string_obj
);
978 BT_OBJECT_PUT_REF_AND_RESET(array_obj
);
979 BT_OBJECT_PUT_REF_AND_RESET(map_obj
);
983 bt_bool
compare_map_elements(struct bt_value
*map_a
, struct bt_value
*map_b
,
986 struct bt_value
*elem_a
= NULL
;
987 struct bt_value
*elem_b
= NULL
;
990 elem_a
= bt_value_map_borrow_entry_value(map_a
, key
);
991 elem_b
= bt_value_map_borrow_entry_value(map_b
, key
);
992 equal
= bt_value_compare(elem_a
, elem_b
);
997 void test_extend(void)
999 struct bt_private_value
*base_map
= bt_private_value_map_create();
1000 struct bt_private_value
*extension_map
= bt_private_value_map_create();
1001 struct bt_private_value
*extended_map
= NULL
;
1002 struct bt_private_value
*array
= bt_private_value_array_create();
1003 enum bt_value_status status
;
1005 BT_ASSERT(base_map
);
1006 BT_ASSERT(extension_map
);
1008 status
= bt_private_value_map_insert_bool_entry(base_map
, "file",
1010 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1011 status
= bt_private_value_map_insert_bool_entry(base_map
, "edit",
1013 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1014 status
= bt_private_value_map_insert_integer_entry(base_map
,
1016 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1017 status
= bt_private_value_map_insert_integer_entry(base_map
, "find",
1019 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1020 status
= bt_private_value_map_insert_bool_entry(extension_map
, "edit",
1022 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1023 status
= bt_private_value_map_insert_integer_entry(extension_map
,
1025 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1026 status
= bt_private_value_map_insert_real_entry(extension_map
,
1028 BT_ASSERT(status
== BT_VALUE_STATUS_OK
);
1029 extended_map
= bt_value_map_extend(
1030 bt_value_borrow_from_private(base_map
),
1031 bt_value_borrow_from_private(extension_map
));
1032 ok(extended_map
, "bt_value_map_extend() succeeds");
1033 ok(bt_value_map_get_size(
1034 bt_value_borrow_from_private(extended_map
)) == 5,
1035 "bt_value_map_extend() returns a map object with the correct size");
1036 ok(compare_map_elements(bt_value_borrow_from_private(base_map
),
1037 bt_value_borrow_from_private(extended_map
), "file"),
1038 "bt_value_map_extend() picks the appropriate element (file)");
1039 ok(compare_map_elements(bt_value_borrow_from_private(extension_map
),
1040 bt_value_borrow_from_private(extended_map
), "edit"),
1041 "bt_value_map_extend() picks the appropriate element (edit)");
1042 ok(compare_map_elements(bt_value_borrow_from_private(base_map
),
1043 bt_value_borrow_from_private(extended_map
), "selection"),
1044 "bt_value_map_extend() picks the appropriate element (selection)");
1045 ok(compare_map_elements(bt_value_borrow_from_private(extension_map
),
1046 bt_value_borrow_from_private(extended_map
), "find"),
1047 "bt_value_map_extend() picks the appropriate element (find)");
1048 ok(compare_map_elements(bt_value_borrow_from_private(extension_map
),
1049 bt_value_borrow_from_private(extended_map
), "project"),
1050 "bt_value_map_extend() picks the appropriate element (project)");
1052 BT_OBJECT_PUT_REF_AND_RESET(array
);
1053 BT_OBJECT_PUT_REF_AND_RESET(base_map
);
1054 BT_OBJECT_PUT_REF_AND_RESET(extension_map
);
1055 BT_OBJECT_PUT_REF_AND_RESET(extended_map
);
1060 plan_tests(NR_TESTS
);