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/values.h>
33 ok(bt_value_null
, "bt_value_null is not NULL");
34 ok(bt_value_is_null(bt_value_null
),
35 "bt_value_null is a null value object");
36 bt_get(bt_value_null
);
37 pass("getting bt_value_null does not cause a crash");
38 bt_put(bt_value_null
);
39 pass("putting bt_value_null does not cause a crash");
42 pass("getting NULL does not cause a crash");
44 pass("putting NULL does not cause a crash");
46 ok(bt_value_get_type(NULL
) == BT_VALUE_TYPE_UNKNOWN
,
47 "bt_value_get_type(NULL) returns BT_VALUE_TYPE_UNKNOWN");
57 obj
= bt_value_bool_create();
58 ok(obj
&& bt_value_is_bool(obj
),
59 "bt_value_bool_create() returns a boolean value object");
62 ret
= bt_value_bool_get(obj
, &value
);
63 ok(!ret
&& !value
, "default boolean value object value is false");
65 ret
= bt_value_bool_set(NULL
, true);
66 ok(ret
== BT_VALUE_STATUS_INVAL
,
67 "bt_value_bool_set() fails with an value object set to NULL");
68 ret
= bt_value_bool_get(NULL
, &value
);
69 ok(ret
== BT_VALUE_STATUS_INVAL
,
70 "bt_value_bool_get() fails with an value object set to NULL");
71 ret
= bt_value_bool_get(obj
, NULL
);
72 ok(ret
== BT_VALUE_STATUS_INVAL
,
73 "bt_value_bool_get() fails with a return value set to NULL");
75 assert(!bt_value_bool_set(obj
, false));
76 ret
= bt_value_bool_set(obj
, true);
77 ok(!ret
, "bt_value_bool_set() succeeds");
78 ret
= bt_value_bool_get(obj
, &value
);
79 ok(!ret
&& value
, "bt_value_bool_set() works");
82 pass("putting an existing boolean value object does not cause a crash")
85 obj
= bt_value_bool_create_init(true);
86 ok(obj
&& bt_value_is_bool(obj
),
87 "bt_value_bool_create_init() returns a boolean value object");
88 ret
= bt_value_bool_get(obj
, &value
);
90 "bt_value_bool_create_init() sets the appropriate initial value");
92 assert(!bt_value_freeze(obj
));
93 ok(bt_value_bool_set(obj
, false) == BT_VALUE_STATUS_FROZEN
,
94 "bt_value_bool_set() cannot be called on a frozen boolean value object");
96 ret
= bt_value_bool_get(obj
, &value
);
98 "bt_value_bool_set() does not alter a frozen floating point number value object");
104 void test_integer(void)
108 struct bt_value
*obj
;
110 obj
= bt_value_integer_create();
111 ok(obj
&& bt_value_is_integer(obj
),
112 "bt_value_integer_create() returns an integer value object");
114 ret
= bt_value_integer_set(NULL
, -12345);
115 ok(ret
== BT_VALUE_STATUS_INVAL
,
116 "bt_value_integer_set() fails with an value object set to NULL");
117 ret
= bt_value_integer_get(NULL
, &value
);
118 ok(ret
== BT_VALUE_STATUS_INVAL
,
119 "bt_value_integer_get() fails with an value object set to NULL");
120 ret
= bt_value_integer_get(obj
, NULL
);
121 ok(ret
== BT_VALUE_STATUS_INVAL
,
122 "bt_value_integer_get() fails with a return value set to NULL");
125 ret
= bt_value_integer_get(obj
, &value
);
126 ok(!ret
&& value
== 0, "default integer value object value is 0");
128 ret
= bt_value_integer_set(obj
, -98765);
129 ok(!ret
, "bt_value_integer_set() succeeds");
130 ret
= bt_value_integer_get(obj
, &value
);
131 ok(!ret
&& value
== -98765, "bt_value_integer_set() works");
134 pass("putting an existing integer value object does not cause a crash")
136 obj
= bt_value_integer_create_init(321456987);
137 ok(obj
&& bt_value_is_integer(obj
),
138 "bt_value_integer_create_init() returns an integer value object");
139 ret
= bt_value_integer_get(obj
, &value
);
140 ok(!ret
&& value
== 321456987,
141 "bt_value_integer_create_init() sets the appropriate initial value");
143 assert(!bt_value_freeze(obj
));
144 ok(bt_value_integer_set(obj
, 18276) == BT_VALUE_STATUS_FROZEN
,
145 "bt_value_integer_set() cannot be called on a frozen integer value object");
147 ret
= bt_value_integer_get(obj
, &value
);
148 ok(!ret
&& value
== 321456987,
149 "bt_value_integer_set() does not alter a frozen integer value object");
155 void test_float(void)
159 struct bt_value
*obj
;
161 obj
= bt_value_float_create();
162 ok(obj
&& bt_value_is_float(obj
),
163 "bt_value_float_create() returns a floating point number value object");
165 ret
= bt_value_float_set(NULL
, 1.2345);
166 ok(ret
== BT_VALUE_STATUS_INVAL
,
167 "bt_value_float_set() fails with an value object set to NULL");
168 ret
= bt_value_float_get(NULL
, &value
);
169 ok(ret
== BT_VALUE_STATUS_INVAL
,
170 "bt_value_float_get() fails with an value object set to NULL");
171 ret
= bt_value_float_get(obj
, NULL
);
172 ok(ret
== BT_VALUE_STATUS_INVAL
,
173 "bt_value_float_get() fails with a return value set to NULL");
176 ret
= bt_value_float_get(obj
, &value
);
177 ok(!ret
&& value
== 0.,
178 "default floating point number value object value is 0");
180 ret
= bt_value_float_set(obj
, -3.1416);
181 ok(!ret
, "bt_value_float_set() succeeds");
182 ret
= bt_value_float_get(obj
, &value
);
183 ok(!ret
&& value
== -3.1416, "bt_value_float_set() works");
186 pass("putting an existing floating point number value object does not cause a crash")
188 obj
= bt_value_float_create_init(33.1649758);
189 ok(obj
&& bt_value_is_float(obj
),
190 "bt_value_float_create_init() returns a floating point number value object");
191 ret
= bt_value_float_get(obj
, &value
);
192 ok(!ret
&& value
== 33.1649758,
193 "bt_value_float_create_init() sets the appropriate initial value");
195 assert(!bt_value_freeze(obj
));
196 ok(bt_value_float_set(obj
, 17.88) == BT_VALUE_STATUS_FROZEN
,
197 "bt_value_float_set() fails with a frozen floating point number value object");
199 ret
= bt_value_float_get(obj
, &value
);
200 ok(!ret
&& value
== 33.1649758,
201 "bt_value_float_set() does not alter a frozen floating point number value object");
207 void test_string(void)
211 struct bt_value
*obj
;
213 obj
= bt_value_string_create();
214 ok(obj
&& bt_value_is_string(obj
),
215 "bt_value_string_create() returns a string value object");
217 ret
= bt_value_string_set(NULL
, "hoho");
218 ok(ret
== BT_VALUE_STATUS_INVAL
,
219 "bt_value_string_set() fails with an value object set to NULL");
220 ret
= bt_value_string_set(obj
, NULL
);
221 ok(ret
== BT_VALUE_STATUS_INVAL
,
222 "bt_value_string_set() fails with a value set to NULL");
223 ret
= bt_value_string_get(NULL
, &value
);
224 ok(ret
== BT_VALUE_STATUS_INVAL
,
225 "bt_value_string_get() fails with an value object set to NULL");
226 ret
= bt_value_string_get(obj
, NULL
);
227 ok(ret
== BT_VALUE_STATUS_INVAL
,
228 "bt_value_string_get() fails with a return value set to NULL");
230 ret
= bt_value_string_get(obj
, &value
);
231 ok(!ret
&& value
&& !strcmp(value
, ""),
232 "default string value object value is \"\"");
234 ret
= bt_value_string_set(obj
, "hello worldz");
235 ok(!ret
, "bt_value_string_set() succeeds");
236 ret
= bt_value_string_get(obj
, &value
);
237 ok(!ret
&& value
&& !strcmp(value
, "hello worldz"),
238 "bt_value_string_get() works");
241 pass("putting an existing string value object does not cause a crash")
243 obj
= bt_value_string_create_init(NULL
);
244 ok(!obj
, "bt_value_string_create_init() fails with an initial value set to NULL");
245 obj
= bt_value_string_create_init("initial value");
246 ok(obj
&& bt_value_is_string(obj
),
247 "bt_value_string_create_init() returns a string value object");
248 ret
= bt_value_string_get(obj
, &value
);
249 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
250 "bt_value_string_create_init() sets the appropriate initial value");
252 assert(!bt_value_freeze(obj
));
253 ok(bt_value_string_set(obj
, "new value") == BT_VALUE_STATUS_FROZEN
,
254 "bt_value_string_set() fails with a frozen string value object");
256 ret
= bt_value_string_get(obj
, &value
);
257 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
258 "bt_value_string_set() does not alter a frozen string value object");
264 void test_array(void)
270 struct bt_value
*obj
;
271 const char *string_value
;
272 struct bt_value
*array_obj
;
274 array_obj
= bt_value_array_create();
275 ok(array_obj
&& bt_value_is_array(array_obj
),
276 "bt_value_array_create() returns an array value object");
277 ok(bt_value_array_is_empty(NULL
) == false,
278 "bt_value_array_is_empty() returns false with an value object set to NULL");
279 ok(bt_value_array_is_empty(array_obj
),
280 "initial array value object size is 0");
281 ok(bt_value_array_size(NULL
) == BT_VALUE_STATUS_INVAL
,
282 "bt_value_array_size() fails with an array value object set to NULL");
284 ok(bt_value_array_append(NULL
, bt_value_null
)
285 == BT_VALUE_STATUS_INVAL
,
286 "bt_value_array_append() fails with an array value object set to NULL");
287 ok(bt_value_array_append(array_obj
, NULL
) == BT_VALUE_STATUS_INVAL
,
288 "bt_value_array_append() fails with a value set to NULL");
290 obj
= bt_value_integer_create_init(345);
291 ret
= bt_value_array_append(array_obj
, obj
);
293 obj
= bt_value_float_create_init(-17.45);
294 ret
|= bt_value_array_append(array_obj
, obj
);
296 obj
= bt_value_bool_create_init(true);
297 ret
|= bt_value_array_append(array_obj
, obj
);
299 ret
|= bt_value_array_append(array_obj
, bt_value_null
);
300 ok(!ret
, "bt_value_array_append() succeeds");
301 ok(bt_value_array_size(array_obj
) == 4,
302 "appending an element to an array value object increment its size");
304 obj
= bt_value_array_get(array_obj
, 4);
305 ok(!obj
, "getting an array value object's element at an index equal to its size fails");
306 obj
= bt_value_array_get(array_obj
, 5);
307 ok(!obj
, "getting an array value object's element at a larger index fails");
309 obj
= bt_value_array_get(NULL
, 2);
310 ok(!obj
, "bt_value_array_get() fails with an array value object set to NULL");
312 obj
= bt_value_array_get(array_obj
, 0);
313 ok(obj
&& bt_value_is_integer(obj
),
314 "bt_value_array_get() returns an value object with the appropriate type (integer)");
315 ret
= bt_value_integer_get(obj
, &int_value
);
316 ok(!ret
&& int_value
== 345,
317 "bt_value_array_get() returns an value object with the appropriate value (integer)");
319 obj
= bt_value_array_get(array_obj
, 1);
320 ok(obj
&& bt_value_is_float(obj
),
321 "bt_value_array_get() returns an value object with the appropriate type (floating point number)");
322 ret
= bt_value_float_get(obj
, &float_value
);
323 ok(!ret
&& float_value
== -17.45,
324 "bt_value_array_get() returns an value object with the appropriate value (floating point number)");
326 obj
= bt_value_array_get(array_obj
, 2);
327 ok(obj
&& bt_value_is_bool(obj
),
328 "bt_value_array_get() returns an value object with the appropriate type (boolean)");
329 ret
= bt_value_bool_get(obj
, &bool_value
);
330 ok(!ret
&& bool_value
,
331 "bt_value_array_get() returns an value object with the appropriate value (boolean)");
333 obj
= bt_value_array_get(array_obj
, 3);
334 ok(obj
== bt_value_null
,
335 "bt_value_array_get() returns an value object with the appropriate type (null)");
337 ok(bt_value_array_set(NULL
, 0, bt_value_null
) ==
338 BT_VALUE_STATUS_INVAL
,
339 "bt_value_array_set() fails with an array value object set to NULL");
340 ok(bt_value_array_set(array_obj
, 0, NULL
) == BT_VALUE_STATUS_INVAL
,
341 "bt_value_array_set() fails with an element value object set to NULL");
342 ok(bt_value_array_set(array_obj
, 4, bt_value_null
) ==
343 BT_VALUE_STATUS_INVAL
,
344 "bt_value_array_set() fails with an invalid index");
345 obj
= bt_value_integer_create_init(1001);
347 ok(!bt_value_array_set(array_obj
, 2, obj
),
348 "bt_value_array_set() succeeds");
350 obj
= bt_value_array_get(array_obj
, 2);
351 ok(obj
&& bt_value_is_integer(obj
),
352 "bt_value_array_set() inserts an value object with the appropriate type");
353 ret
= bt_value_integer_get(obj
, &int_value
);
355 ok(int_value
== 1001,
356 "bt_value_array_set() inserts an value object with the appropriate value");
359 ret
= bt_value_array_append_bool(array_obj
, false);
360 ok(!ret
, "bt_value_array_append_bool() succeeds");
361 ok(bt_value_array_append_bool(NULL
, true) == BT_VALUE_STATUS_INVAL
,
362 "bt_value_array_append_bool() fails with an array value object set to NULL");
363 ret
= bt_value_array_append_integer(array_obj
, 98765);
364 ok(!ret
, "bt_value_array_append_integer() succeeds");
365 ok(bt_value_array_append_integer(NULL
, 18765) ==
366 BT_VALUE_STATUS_INVAL
,
367 "bt_value_array_append_integer() fails with an array value object set to NULL");
368 ret
= bt_value_array_append_float(array_obj
, 2.49578);
369 ok(!ret
, "bt_value_array_append_float() succeeds");
370 ok(bt_value_array_append_float(NULL
, 1.49578) ==
371 BT_VALUE_STATUS_INVAL
,
372 "bt_value_array_append_float() fails with an array value object set to NULL");
373 ret
= bt_value_array_append_string(array_obj
, "bt_value");
374 ok(!ret
, "bt_value_array_append_string() succeeds");
375 ok(bt_value_array_append_string(NULL
, "bt_obj") ==
376 BT_VALUE_STATUS_INVAL
,
377 "bt_value_array_append_string() fails with an array value object set to NULL");
378 ret
= bt_value_array_append_empty_array(array_obj
);
379 ok(!ret
, "bt_value_array_append_empty_array() succeeds");
380 ok(bt_value_array_append_empty_array(NULL
) == BT_VALUE_STATUS_INVAL
,
381 "bt_value_array_append_empty_array() fails with an array value object set to NULL");
382 ret
= bt_value_array_append_empty_map(array_obj
);
383 ok(!ret
, "bt_value_array_append_empty_map() succeeds");
384 ok(bt_value_array_append_empty_map(NULL
) == BT_VALUE_STATUS_INVAL
,
385 "bt_value_array_append_empty_map() fails with an array value object set to NULL");
387 ok(bt_value_array_size(array_obj
) == 10,
388 "the bt_value_array_append_*() functions increment the array value object's size");
389 ok(!bt_value_array_is_empty(array_obj
),
390 "map value object is not empty");
392 obj
= bt_value_array_get(array_obj
, 4);
393 ok(obj
&& bt_value_is_bool(obj
),
394 "bt_value_array_append_bool() appends a boolean value object");
395 ret
= bt_value_bool_get(obj
, &bool_value
);
396 ok(!ret
&& !bool_value
,
397 "bt_value_array_append_bool() appends the appropriate value");
399 obj
= bt_value_array_get(array_obj
, 5);
400 ok(obj
&& bt_value_is_integer(obj
),
401 "bt_value_array_append_integer() appends an integer value object");
402 ret
= bt_value_integer_get(obj
, &int_value
);
403 ok(!ret
&& int_value
== 98765,
404 "bt_value_array_append_integer() appends the appropriate value");
406 obj
= bt_value_array_get(array_obj
, 6);
407 ok(obj
&& bt_value_is_float(obj
),
408 "bt_value_array_append_float() appends a floating point number value object");
409 ret
= bt_value_float_get(obj
, &float_value
);
410 ok(!ret
&& float_value
== 2.49578,
411 "bt_value_array_append_float() appends the appropriate value");
413 obj
= bt_value_array_get(array_obj
, 7);
414 ok(obj
&& bt_value_is_string(obj
),
415 "bt_value_array_append_string() appends a string value object");
416 ret
= bt_value_string_get(obj
, &string_value
);
417 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
418 "bt_value_array_append_string() appends the appropriate value");
420 obj
= bt_value_array_get(array_obj
, 8);
421 ok(obj
&& bt_value_is_array(obj
),
422 "bt_value_array_append_empty_array() appends an array value object");
423 ok(bt_value_array_is_empty(obj
),
424 "bt_value_array_append_empty_array() an empty array value object");
426 obj
= bt_value_array_get(array_obj
, 9);
427 ok(obj
&& bt_value_is_map(obj
),
428 "bt_value_array_append_empty_map() appends a map value object");
429 ok(bt_value_map_is_empty(obj
),
430 "bt_value_array_append_empty_map() an empty map value object");
433 assert(!bt_value_freeze(array_obj
));
434 ok(bt_value_array_append(array_obj
, bt_value_null
) ==
435 BT_VALUE_STATUS_FROZEN
,
436 "bt_value_array_append() fails with a frozen array value object");
437 ok(bt_value_array_append_bool(array_obj
, false) ==
438 BT_VALUE_STATUS_FROZEN
,
439 "bt_value_array_append_bool() fails with a frozen array value object");
440 ok(bt_value_array_append_integer(array_obj
, 23) ==
441 BT_VALUE_STATUS_FROZEN
,
442 "bt_value_array_append_integer() fails with a frozen array value object");
443 ok(bt_value_array_append_float(array_obj
, 2.34) ==
444 BT_VALUE_STATUS_FROZEN
,
445 "bt_value_array_append_float() fails with a frozen array value object");
446 ok(bt_value_array_append_string(array_obj
, "yayayayaya") ==
447 BT_VALUE_STATUS_FROZEN
,
448 "bt_value_array_append_string() fails with a frozen array value object");
449 ok(bt_value_array_append_empty_array(array_obj
) ==
450 BT_VALUE_STATUS_FROZEN
,
451 "bt_value_array_append_empty_array() fails with a frozen array value object");
452 ok(bt_value_array_append_empty_map(array_obj
) ==
453 BT_VALUE_STATUS_FROZEN
,
454 "bt_value_array_append_empty_map() fails with a frozen array value object");
455 ok(bt_value_array_set(array_obj
, 2, bt_value_null
) ==
456 BT_VALUE_STATUS_FROZEN
,
457 "bt_value_array_set() fails with a frozen array value object");
458 ok(bt_value_array_size(array_obj
) == 10,
459 "appending to a frozen array value object does not change its size");
461 obj
= bt_value_array_get(array_obj
, 1);
463 ok(bt_value_float_set(obj
, 14.52) == BT_VALUE_STATUS_FROZEN
,
464 "freezing an array value object also freezes its elements");
468 pass("putting an existing array value object does not cause a crash")
472 bool test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
486 struct map_foreach_checklist
{
500 bool test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
504 struct map_foreach_checklist
*checklist
= data
;
506 if (!strcmp(key
, "bool")) {
507 if (checklist
->bool1
) {
508 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
512 ret
= bt_value_bool_get(object
, &val
);
513 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool\" value");
516 pass("test_map_foreach_cb_check(): \"bool\" value object has the right value");
517 checklist
->bool1
= true;
519 fail("test_map_foreach_cb_check(): \"bool\" value object has the wrong value");
522 } else if (!strcmp(key
, "int")) {
523 if (checklist
->int1
) {
524 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
528 ret
= bt_value_integer_get(object
, &val
);
529 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
532 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
533 checklist
->int1
= true;
535 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
538 } else if (!strcmp(key
, "float")) {
539 if (checklist
->float1
) {
540 fail("test_map_foreach_cb_check(): duplicate key \"float\"");
544 ret
= bt_value_float_get(object
, &val
);
545 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float\" value");
548 pass("test_map_foreach_cb_check(): \"float\" value object has the right value");
549 checklist
->float1
= true;
551 fail("test_map_foreach_cb_check(): \"float\" value object has the wrong value");
554 } else if (!strcmp(key
, "null")) {
555 if (checklist
->null1
) {
556 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
558 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
559 checklist
->null1
= true;
561 } else if (!strcmp(key
, "bool2")) {
562 if (checklist
->bool2
) {
563 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
567 ret
= bt_value_bool_get(object
, &val
);
568 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
571 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
572 checklist
->bool2
= true;
574 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
577 } else if (!strcmp(key
, "int2")) {
578 if (checklist
->int2
) {
579 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
583 ret
= bt_value_integer_get(object
, &val
);
584 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
587 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
588 checklist
->int2
= true;
590 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
593 } else if (!strcmp(key
, "float2")) {
594 if (checklist
->float2
) {
595 fail("test_map_foreach_cb_check(): duplicate key \"float2\"");
599 ret
= bt_value_float_get(object
, &val
);
600 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float2\" value");
602 if (val
== -49.0001) {
603 pass("test_map_foreach_cb_check(): \"float2\" value object has the right value");
604 checklist
->float2
= true;
606 fail("test_map_foreach_cb_check(): \"float2\" value object has the wrong value");
609 } else if (!strcmp(key
, "string2")) {
610 if (checklist
->string2
) {
611 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
615 ret
= bt_value_string_get(object
, &val
);
616 ok(!ret
, "test_map_foreach_cb_check(): success getting \"string2\" value");
618 if (val
&& !strcmp(val
, "bt_value")) {
619 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
620 checklist
->string2
= true;
622 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
625 } else if (!strcmp(key
, "array2")) {
626 if (checklist
->array2
) {
627 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
629 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
630 ok(bt_value_array_is_empty(object
),
631 "test_map_foreach_cb_check(): \"array2\" value object is empty");
632 checklist
->array2
= true;
634 } else if (!strcmp(key
, "map2")) {
635 if (checklist
->map2
) {
636 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
638 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
639 ok(bt_value_map_is_empty(object
),
640 "test_map_foreach_cb_check(): \"map2\" value object is empty");
641 checklist
->map2
= true;
644 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key
);
658 struct bt_value
*obj
;
659 struct bt_value
*map_obj
;
660 struct map_foreach_checklist checklist
;
662 map_obj
= bt_value_map_create();
663 ok(map_obj
&& bt_value_is_map(map_obj
),
664 "bt_value_map_create() returns a map value object");
665 ok(bt_value_map_size(map_obj
) == 0,
666 "initial map value object size is 0");
667 ok(bt_value_map_size(NULL
) == BT_VALUE_STATUS_INVAL
,
668 "bt_value_map_size() fails with a map value object set to NULL");
670 ok(bt_value_map_insert(NULL
, "hello", bt_value_null
) ==
671 BT_VALUE_STATUS_INVAL
,
672 "bt_value_array_insert() fails with a map value object set to NULL");
673 ok(bt_value_map_insert(map_obj
, NULL
, bt_value_null
) ==
674 BT_VALUE_STATUS_INVAL
,
675 "bt_value_array_insert() fails with a key set to NULL");
676 ok(bt_value_map_insert(map_obj
, "yeah", NULL
) ==
677 BT_VALUE_STATUS_INVAL
,
678 "bt_value_array_insert() fails with an element value object set to NULL");
680 obj
= bt_value_integer_create_init(19457);
681 ret
= bt_value_map_insert(map_obj
, "int", obj
);
683 obj
= bt_value_float_create_init(5.444);
684 ret
|= bt_value_map_insert(map_obj
, "float", obj
);
686 obj
= bt_value_bool_create();
687 ret
|= bt_value_map_insert(map_obj
, "bool", obj
);
689 ret
|= bt_value_map_insert(map_obj
, "null", bt_value_null
);
690 ok(!ret
, "bt_value_map_insert() succeeds");
691 ok(bt_value_map_size(map_obj
) == 4,
692 "inserting an element into a map value object increment its size");
694 obj
= bt_value_bool_create_init(true);
695 ret
= bt_value_map_insert(map_obj
, "bool", obj
);
697 ok(!ret
, "bt_value_map_insert() accepts an existing key");
699 obj
= bt_value_map_get(map_obj
, NULL
);
700 ok(!obj
, "bt_value_map_get() fails with a key set to NULL");
701 obj
= bt_value_map_get(NULL
, "bool");
702 ok(!obj
, "bt_value_map_get() fails with a map value object set to NULL");
704 obj
= bt_value_map_get(map_obj
, "life");
705 ok(!obj
, "bt_value_map_get() fails with an non existing key");
706 obj
= bt_value_map_get(map_obj
, "float");
707 ok(obj
&& bt_value_is_float(obj
),
708 "bt_value_map_get() returns an value object with the appropriate type (float)");
709 ret
= bt_value_float_get(obj
, &float_value
);
710 ok(!ret
&& float_value
== 5.444,
711 "bt_value_map_get() returns an value object with the appropriate value (float)");
713 obj
= bt_value_map_get(map_obj
, "int");
714 ok(obj
&& bt_value_is_integer(obj
),
715 "bt_value_map_get() returns an value object with the appropriate type (integer)");
716 ret
= bt_value_integer_get(obj
, &int_value
);
717 ok(!ret
&& int_value
== 19457,
718 "bt_value_map_get() returns an value object with the appropriate value (integer)");
720 obj
= bt_value_map_get(map_obj
, "null");
721 ok(obj
&& bt_value_is_null(obj
),
722 "bt_value_map_get() returns an value object with the appropriate type (null)");
723 obj
= bt_value_map_get(map_obj
, "bool");
724 ok(obj
&& bt_value_is_bool(obj
),
725 "bt_value_map_get() returns an value object with the appropriate type (boolean)");
726 ret
= bt_value_bool_get(obj
, &bool_value
);
727 ok(!ret
&& bool_value
,
728 "bt_value_map_get() returns an value object with the appropriate value (boolean)");
731 ret
= bt_value_map_insert_bool(map_obj
, "bool2", true);
732 ok(!ret
, "bt_value_map_insert_bool() succeeds");
733 ok(bt_value_map_insert_bool(NULL
, "bool2", false) ==
734 BT_VALUE_STATUS_INVAL
,
735 "bt_value_map_insert_bool() fails with a map value object set to NULL");
736 ret
= bt_value_map_insert_integer(map_obj
, "int2", 98765);
737 ok(!ret
, "bt_value_map_insert_integer() succeeds");
738 ok(bt_value_map_insert_integer(NULL
, "int2", 1001) ==
739 BT_VALUE_STATUS_INVAL
,
740 "bt_value_map_insert_integer() fails with a map value object set to NULL");
741 ret
= bt_value_map_insert_float(map_obj
, "float2", -49.0001);
742 ok(!ret
, "bt_value_map_insert_float() succeeds");
743 ok(bt_value_map_insert_float(NULL
, "float2", 495) ==
744 BT_VALUE_STATUS_INVAL
,
745 "bt_value_map_insert_float() fails with a map value object set to NULL");
746 ret
= bt_value_map_insert_string(map_obj
, "string2", "bt_value");
747 ok(!ret
, "bt_value_map_insert_string() succeeds");
748 ok(bt_value_map_insert_string(NULL
, "string2", "bt_obj") ==
749 BT_VALUE_STATUS_INVAL
,
750 "bt_value_map_insert_string() fails with a map value object set to NULL");
751 ret
= bt_value_map_insert_empty_array(map_obj
, "array2");
752 ok(!ret
, "bt_value_map_insert_empty_array() succeeds");
753 ok(bt_value_map_insert_empty_array(NULL
, "array2") == BT_VALUE_STATUS_INVAL
,
754 "bt_value_map_insert_empty_array() fails with a map value object set to NULL");
755 ret
= bt_value_map_insert_empty_map(map_obj
, "map2");
756 ok(!ret
, "bt_value_map_insert_empty_map() succeeds");
757 ok(bt_value_map_insert_empty_map(NULL
, "map2") == BT_VALUE_STATUS_INVAL
,
758 "bt_value_map_insert_empty_map() fails with a map value object set to NULL");
760 ok(bt_value_map_size(map_obj
) == 10,
761 "the bt_value_map_insert*() functions increment the map value object's size");
763 ok(!bt_value_map_has_key(map_obj
, "hello"),
764 "map value object does not have key \"hello\"");
765 ok(bt_value_map_has_key(map_obj
, "bool"),
766 "map value object has key \"bool\"");
767 ok(bt_value_map_has_key(map_obj
, "int"),
768 "map value object has key \"int\"");
769 ok(bt_value_map_has_key(map_obj
, "float"),
770 "map value object has key \"float\"");
771 ok(bt_value_map_has_key(map_obj
, "null"),
772 "map value object has key \"null\"");
773 ok(bt_value_map_has_key(map_obj
, "bool2"),
774 "map value object has key \"bool2\"");
775 ok(bt_value_map_has_key(map_obj
, "int2"),
776 "map value object has key \"int2\"");
777 ok(bt_value_map_has_key(map_obj
, "float2"),
778 "map value object has key \"float2\"");
779 ok(bt_value_map_has_key(map_obj
, "string2"),
780 "map value object has key \"string2\"");
781 ok(bt_value_map_has_key(map_obj
, "array2"),
782 "map value object has key \"array2\"");
783 ok(bt_value_map_has_key(map_obj
, "map2"),
784 "map value object has key \"map2\"");
786 ok(bt_value_map_foreach(NULL
, test_map_foreach_cb_count
, &count
) ==
787 BT_VALUE_STATUS_INVAL
,
788 "bt_value_map_foreach() fails with a map value object set to NULL");
789 ok(bt_value_map_foreach(map_obj
, NULL
, &count
) ==
790 BT_VALUE_STATUS_INVAL
,
791 "bt_value_map_foreach() fails with a user function set to NULL");
792 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_count
, &count
);
793 ok(ret
== BT_VALUE_STATUS_CANCELLED
&& count
== 3,
794 "bt_value_map_foreach() breaks the loop when the user function returns false");
796 memset(&checklist
, 0, sizeof(checklist
));
797 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_check
,
799 ok(ret
== BT_VALUE_STATUS_OK
,
800 "bt_value_map_foreach() succeeds with test_map_foreach_cb_check()");
801 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.float1
&&
802 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
803 checklist
.float2
&& checklist
.string2
&&
804 checklist
.array2
&& checklist
.map2
,
805 "bt_value_map_foreach() iterates over all the map value object's elements");
807 assert(!bt_value_freeze(map_obj
));
808 ok(bt_value_map_insert(map_obj
, "allo", bt_value_null
) ==
809 BT_VALUE_STATUS_FROZEN
,
810 "bt_value_map_insert() fails with a frozen map value object");
811 ok(bt_value_map_insert_bool(map_obj
, "duh", false) ==
812 BT_VALUE_STATUS_FROZEN
,
813 "bt_value_map_insert_bool() fails with a frozen array value object");
814 ok(bt_value_map_insert_integer(map_obj
, "duh", 23) ==
815 BT_VALUE_STATUS_FROZEN
,
816 "bt_value_map_insert_integer() fails with a frozen array value object");
817 ok(bt_value_map_insert_float(map_obj
, "duh", 2.34) ==
818 BT_VALUE_STATUS_FROZEN
,
819 "bt_value_map_insert_float() fails with a frozen array value object");
820 ok(bt_value_map_insert_string(map_obj
, "duh", "yayayayaya") ==
821 BT_VALUE_STATUS_FROZEN
,
822 "bt_value_map_insert_string() fails with a frozen array value object");
823 ok(bt_value_map_insert_empty_array(map_obj
, "duh") ==
824 BT_VALUE_STATUS_FROZEN
,
825 "bt_value_map_insert_empty_array() fails with a frozen array value object");
826 ok(bt_value_map_insert_empty_map(map_obj
, "duh") ==
827 BT_VALUE_STATUS_FROZEN
,
828 "bt_value_map_insert_empty_map() fails with a frozen array value object");
829 ok(bt_value_map_size(map_obj
) == 10,
830 "appending to a frozen map value object does not change its size");
833 pass("putting an existing map value object does not cause a crash")
837 void test_types(void)
849 void test_compare_null(void)
851 ok(!bt_value_compare(bt_value_null
, NULL
),
852 "cannot compare null value object and NULL");
853 ok(!bt_value_compare(NULL
, bt_value_null
),
854 "cannot compare NULL and null value object");
855 ok(bt_value_compare(bt_value_null
, bt_value_null
),
856 "null value objects are equivalent");
860 void test_compare_bool(void)
862 struct bt_value
*bool1
= bt_value_bool_create_init(false);
863 struct bt_value
*bool2
= bt_value_bool_create_init(true);
864 struct bt_value
*bool3
= bt_value_bool_create_init(false);
866 assert(bool1
&& bool2
&& bool3
);
867 ok(!bt_value_compare(bt_value_null
, bool1
),
868 "cannot compare null value object and bool value object");
869 ok(!bt_value_compare(bool1
, bool2
),
870 "integer value objects are not equivalent (false and true)");
871 ok(bt_value_compare(bool1
, bool3
),
872 "integer value objects are equivalent (false and false)");
880 void test_compare_integer(void)
882 struct bt_value
*int1
= bt_value_integer_create_init(10);
883 struct bt_value
*int2
= bt_value_integer_create_init(-23);
884 struct bt_value
*int3
= bt_value_integer_create_init(10);
886 assert(int1
&& int2
&& int3
);
887 ok(!bt_value_compare(bt_value_null
, int1
),
888 "cannot compare null value object and integer value object");
889 ok(!bt_value_compare(int1
, int2
),
890 "integer value objects are not equivalent (10 and -23)");
891 ok(bt_value_compare(int1
, int3
),
892 "integer value objects are equivalent (10 and 10)");
900 void test_compare_float(void)
902 struct bt_value
*float1
= bt_value_float_create_init(17.38);
903 struct bt_value
*float2
= bt_value_float_create_init(-14.23);
904 struct bt_value
*float3
= bt_value_float_create_init(17.38);
906 assert(float1
&& float2
&& float3
);
908 ok(!bt_value_compare(bt_value_null
, float1
),
909 "cannot compare null value object and floating point number value object");
910 ok(!bt_value_compare(float1
, float2
),
911 "floating point number value objects are not equivalent (17.38 and -14.23)");
912 ok(bt_value_compare(float1
, float3
),
913 "floating point number value objects are equivalent (17.38 and 17.38)");
921 void test_compare_string(void)
923 struct bt_value
*string1
= bt_value_string_create_init("hello");
924 struct bt_value
*string2
= bt_value_string_create_init("bt_value");
925 struct bt_value
*string3
= bt_value_string_create_init("hello");
927 assert(string1
&& string2
&& string3
);
929 ok(!bt_value_compare(bt_value_null
, string1
),
930 "cannot compare null value object and string value object");
931 ok(!bt_value_compare(string1
, string2
),
932 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
933 ok(bt_value_compare(string1
, string3
),
934 "string value objects are equivalent (\"hello\" and \"hello\")");
942 void test_compare_array(void)
944 struct bt_value
*array1
= bt_value_array_create();
945 struct bt_value
*array2
= bt_value_array_create();
946 struct bt_value
*array3
= bt_value_array_create();
948 assert(array1
&& array2
&& array3
);
950 ok(bt_value_compare(array1
, array2
),
951 "empty array value objects are equivalent");
953 assert(!bt_value_array_append_integer(array1
, 23));
954 assert(!bt_value_array_append_float(array1
, 14.2));
955 assert(!bt_value_array_append_bool(array1
, false));
956 assert(!bt_value_array_append_float(array2
, 14.2));
957 assert(!bt_value_array_append_integer(array2
, 23));
958 assert(!bt_value_array_append_bool(array2
, false));
959 assert(!bt_value_array_append_integer(array3
, 23));
960 assert(!bt_value_array_append_float(array3
, 14.2));
961 assert(!bt_value_array_append_bool(array3
, false));
962 assert(bt_value_array_size(array1
) == 3);
963 assert(bt_value_array_size(array2
) == 3);
964 assert(bt_value_array_size(array3
) == 3);
966 ok(!bt_value_compare(bt_value_null
, array1
),
967 "cannot compare null value object and array value object");
968 ok(!bt_value_compare(array1
, array2
),
969 "array value objects are not equivalent ([23, 14.2, false] and [14.2, 23, false])");
970 ok(bt_value_compare(array1
, array3
),
971 "array value objects are equivalent ([23, 14.2, false] and [23, 14.2, false])");
979 void test_compare_map(void)
981 struct bt_value
*map1
= bt_value_map_create();
982 struct bt_value
*map2
= bt_value_map_create();
983 struct bt_value
*map3
= bt_value_map_create();
985 assert(map1
&& map2
&& map3
);
987 ok(bt_value_compare(map1
, map2
),
988 "empty map value objects are equivalent");
990 assert(!bt_value_map_insert_integer(map1
, "one", 23));
991 assert(!bt_value_map_insert_float(map1
, "two", 14.2));
992 assert(!bt_value_map_insert_bool(map1
, "three", false));
993 assert(!bt_value_map_insert_float(map2
, "one", 14.2));
994 assert(!bt_value_map_insert_integer(map2
, "two", 23));
995 assert(!bt_value_map_insert_bool(map2
, "three", false));
996 assert(!bt_value_map_insert_bool(map3
, "three", false));
997 assert(!bt_value_map_insert_integer(map3
, "one", 23));
998 assert(!bt_value_map_insert_float(map3
, "two", 14.2));
999 assert(bt_value_map_size(map1
) == 3);
1000 assert(bt_value_map_size(map2
) == 3);
1001 assert(bt_value_map_size(map3
) == 3);
1003 ok(!bt_value_compare(bt_value_null
, map1
),
1004 "cannot compare null value object and map value object");
1005 ok(!bt_value_compare(map1
, map2
),
1006 "map value objects are not equivalent");
1007 ok(bt_value_compare(map1
, map3
),
1008 "map value objects are equivalent");
1016 void test_compare(void)
1018 ok(!bt_value_compare(NULL
, NULL
), "cannot compare NULL and NULL");
1019 test_compare_null();
1020 test_compare_bool();
1021 test_compare_integer();
1022 test_compare_float();
1023 test_compare_string();
1024 test_compare_array();
1029 void test_copy(void)
1032 * Here's the deal here. If we make sure that each value object
1033 * of our deep copy has a different address than its source,
1034 * and that bt_value_compare() returns true for the top-level
1035 * value object, taking into account that we test the correctness of
1036 * bt_value_compare() elsewhere, then the deep copy is a
1039 struct bt_value
*null_copy_obj
;
1040 struct bt_value
*bool_obj
, *bool_copy_obj
;
1041 struct bt_value
*integer_obj
, *integer_copy_obj
;
1042 struct bt_value
*float_obj
, *float_copy_obj
;
1043 struct bt_value
*string_obj
, *string_copy_obj
;
1044 struct bt_value
*array_obj
, *array_copy_obj
;
1045 struct bt_value
*map_obj
, *map_copy_obj
;
1047 bool_obj
= bt_value_bool_create_init(true);
1048 integer_obj
= bt_value_integer_create_init(23);
1049 float_obj
= bt_value_float_create_init(-3.1416);
1050 string_obj
= bt_value_string_create_init("test");
1051 array_obj
= bt_value_array_create();
1052 map_obj
= bt_value_map_create();
1054 assert(bool_obj
&& integer_obj
&& float_obj
&& string_obj
&&
1055 array_obj
&& map_obj
);
1057 assert(!bt_value_array_append(array_obj
, bool_obj
));
1058 assert(!bt_value_array_append(array_obj
, integer_obj
));
1059 assert(!bt_value_array_append(array_obj
, float_obj
));
1060 assert(!bt_value_array_append(array_obj
, bt_value_null
));
1061 assert(!bt_value_map_insert(map_obj
, "array", array_obj
));
1062 assert(!bt_value_map_insert(map_obj
, "string", string_obj
));
1064 map_copy_obj
= bt_value_copy(NULL
);
1066 "bt_value_copy() fails with a source value object set to NULL");
1068 map_copy_obj
= bt_value_copy(map_obj
);
1070 "bt_value_copy() succeeds");
1072 ok(map_obj
!= map_copy_obj
,
1073 "bt_value_copy() returns a different pointer (map)");
1074 string_copy_obj
= bt_value_map_get(map_copy_obj
, "string");
1075 ok(string_copy_obj
!= string_obj
,
1076 "bt_value_copy() returns a different pointer (string)");
1077 array_copy_obj
= bt_value_map_get(map_copy_obj
, "array");
1078 ok(array_copy_obj
!= array_obj
,
1079 "bt_value_copy() returns a different pointer (array)");
1080 bool_copy_obj
= bt_value_array_get(array_copy_obj
, 0);
1081 ok(bool_copy_obj
!= bool_obj
,
1082 "bt_value_copy() returns a different pointer (bool)");
1083 integer_copy_obj
= bt_value_array_get(array_copy_obj
, 1);
1084 ok(integer_copy_obj
!= integer_obj
,
1085 "bt_value_copy() returns a different pointer (integer)");
1086 float_copy_obj
= bt_value_array_get(array_copy_obj
, 2);
1087 ok(float_copy_obj
!= float_obj
,
1088 "bt_value_copy() returns a different pointer (float)");
1089 null_copy_obj
= bt_value_array_get(array_copy_obj
, 3);
1090 ok(null_copy_obj
== bt_value_null
,
1091 "bt_value_copy() returns the same pointer (null)");
1093 ok(bt_value_compare(map_obj
, map_copy_obj
),
1094 "source and destination value objects have the same content");
1096 BT_PUT(bool_copy_obj
);
1097 BT_PUT(integer_copy_obj
);
1098 BT_PUT(float_copy_obj
);
1099 BT_PUT(string_copy_obj
);
1100 BT_PUT(array_copy_obj
);
1101 BT_PUT(map_copy_obj
);
1103 BT_PUT(integer_obj
);
1111 void test_macros(void)
1113 struct bt_value
*obj
= bt_value_bool_create();
1114 struct bt_value
*src
;
1115 struct bt_value
*dst
= NULL
;
1119 ok(!obj
, "BT_PUT() resets the variable to NULL");
1121 obj
= bt_value_bool_create();
1125 ok(!src
, "BT_MOVE() resets the source variable to NULL");
1126 ok(dst
== obj
, "BT_MOVE() moves the ownership");
1132 void test_freeze(void)
1134 struct bt_value
*obj
;
1136 ok(bt_value_freeze(NULL
) == BT_VALUE_STATUS_INVAL
,
1137 "bt_value_freeze() fails with an value object set to NULL");
1138 ok(!bt_value_freeze(bt_value_null
),
1139 "bt_value_freeze() succeeds with a null value object");
1141 ok(!bt_value_is_frozen(NULL
), "NULL is not frozen");
1142 ok(bt_value_is_frozen(bt_value_null
),
1143 "the null singleton is frozen");
1144 obj
= bt_value_integer_create();
1146 ok(!bt_value_is_frozen(obj
),
1147 "bt_value_is_frozen() returns false with a fresh value object");
1148 assert(!bt_value_freeze(obj
));
1149 ok(!bt_value_freeze(obj
),
1150 "bt_value_freeze() passes with a frozen value object");
1151 ok(bt_value_is_frozen(obj
),
1152 "bt_value_is_frozen() returns true with a frozen value object");
1159 plan_tests(NR_TESTS
);