4 * Babeltrace value 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>
31 ok(bt_value_null
, "bt_value_null is not NULL");
32 ok(bt_value_is_null(bt_value_null
),
33 "bt_value_null is a null value object");
34 bt_get(bt_value_null
);
35 pass("getting bt_value_null does not cause a crash");
36 bt_put(bt_value_null
);
37 pass("putting bt_value_null does not cause a crash");
40 pass("getting NULL does not cause a crash");
42 pass("putting NULL does not cause a crash");
44 ok(bt_value_get_type(NULL
) == BT_VALUE_TYPE_UNKNOWN
,
45 "bt_value_get_type(NULL) returns BT_VALUE_TYPE_UNKNOWN");
55 obj
= bt_value_bool_create();
56 ok(obj
&& bt_value_is_bool(obj
),
57 "bt_value_bool_create() returns a boolean value object");
60 ret
= bt_value_bool_get(obj
, &value
);
61 ok(!ret
&& !value
, "default boolean value object value is false");
63 ret
= bt_value_bool_set(NULL
, true);
64 ok(ret
== BT_VALUE_STATUS_INVAL
,
65 "bt_value_bool_set() fails with an value object set to NULL");
66 ret
= bt_value_bool_get(NULL
, &value
);
67 ok(ret
== BT_VALUE_STATUS_INVAL
,
68 "bt_value_bool_get() fails with an value object set to NULL");
69 ret
= bt_value_bool_get(obj
, NULL
);
70 ok(ret
== BT_VALUE_STATUS_INVAL
,
71 "bt_value_bool_get() fails with a return value set to NULL");
73 assert(!bt_value_bool_set(obj
, false));
74 ret
= bt_value_bool_set(obj
, true);
75 ok(!ret
, "bt_value_bool_set() succeeds");
76 ret
= bt_value_bool_get(obj
, &value
);
77 ok(!ret
&& value
, "bt_value_bool_set() works");
80 pass("putting an existing boolean value object does not cause a crash")
83 obj
= bt_value_bool_create_init(true);
84 ok(obj
&& bt_value_is_bool(obj
),
85 "bt_value_bool_create_init() returns a boolean value object");
86 ret
= bt_value_bool_get(obj
, &value
);
88 "bt_value_bool_create_init() sets the appropriate initial value");
90 assert(!bt_value_freeze(obj
));
91 ok(bt_value_bool_set(obj
, false) == BT_VALUE_STATUS_FROZEN
,
92 "bt_value_bool_set() cannot be called on a frozen boolean value object");
94 ret
= bt_value_bool_get(obj
, &value
);
96 "bt_value_bool_set() does not alter a frozen floating point number value object");
102 void test_integer(void)
106 struct bt_value
*obj
;
108 obj
= bt_value_integer_create();
109 ok(obj
&& bt_value_is_integer(obj
),
110 "bt_value_integer_create() returns an integer value object");
112 ret
= bt_value_integer_set(NULL
, -12345);
113 ok(ret
== BT_VALUE_STATUS_INVAL
,
114 "bt_value_integer_set() fails with an value object set to NULL");
115 ret
= bt_value_integer_get(NULL
, &value
);
116 ok(ret
== BT_VALUE_STATUS_INVAL
,
117 "bt_value_integer_get() fails with an value object set to NULL");
118 ret
= bt_value_integer_get(obj
, NULL
);
119 ok(ret
== BT_VALUE_STATUS_INVAL
,
120 "bt_value_integer_get() fails with a return value set to NULL");
123 ret
= bt_value_integer_get(obj
, &value
);
124 ok(!ret
&& value
== 0, "default integer value object value is 0");
126 ret
= bt_value_integer_set(obj
, -98765);
127 ok(!ret
, "bt_value_integer_set() succeeds");
128 ret
= bt_value_integer_get(obj
, &value
);
129 ok(!ret
&& value
== -98765, "bt_value_integer_set() works");
132 pass("putting an existing integer value object does not cause a crash")
134 obj
= bt_value_integer_create_init(321456987);
135 ok(obj
&& bt_value_is_integer(obj
),
136 "bt_value_integer_create_init() returns an integer value object");
137 ret
= bt_value_integer_get(obj
, &value
);
138 ok(!ret
&& value
== 321456987,
139 "bt_value_integer_create_init() sets the appropriate initial value");
141 assert(!bt_value_freeze(obj
));
142 ok(bt_value_integer_set(obj
, 18276) == BT_VALUE_STATUS_FROZEN
,
143 "bt_value_integer_set() cannot be called on a frozen integer value object");
145 ret
= bt_value_integer_get(obj
, &value
);
146 ok(!ret
&& value
== 321456987,
147 "bt_value_integer_set() does not alter a frozen integer value object");
153 void test_float(void)
157 struct bt_value
*obj
;
159 obj
= bt_value_float_create();
160 ok(obj
&& bt_value_is_float(obj
),
161 "bt_value_float_create() returns a floating point number value object");
163 ret
= bt_value_float_set(NULL
, 1.2345);
164 ok(ret
== BT_VALUE_STATUS_INVAL
,
165 "bt_value_float_set() fails with an value object set to NULL");
166 ret
= bt_value_float_get(NULL
, &value
);
167 ok(ret
== BT_VALUE_STATUS_INVAL
,
168 "bt_value_float_get() fails with an value object set to NULL");
169 ret
= bt_value_float_get(obj
, NULL
);
170 ok(ret
== BT_VALUE_STATUS_INVAL
,
171 "bt_value_float_get() fails with a return value set to NULL");
174 ret
= bt_value_float_get(obj
, &value
);
175 ok(!ret
&& value
== 0.,
176 "default floating point number value object value is 0");
178 ret
= bt_value_float_set(obj
, -3.1416);
179 ok(!ret
, "bt_value_float_set() succeeds");
180 ret
= bt_value_float_get(obj
, &value
);
181 ok(!ret
&& value
== -3.1416, "bt_value_float_set() works");
184 pass("putting an existing floating point number value object does not cause a crash")
186 obj
= bt_value_float_create_init(33.1649758);
187 ok(obj
&& bt_value_is_float(obj
),
188 "bt_value_float_create_init() returns a floating point number value object");
189 ret
= bt_value_float_get(obj
, &value
);
190 ok(!ret
&& value
== 33.1649758,
191 "bt_value_float_create_init() sets the appropriate initial value");
193 assert(!bt_value_freeze(obj
));
194 ok(bt_value_float_set(obj
, 17.88) == BT_VALUE_STATUS_FROZEN
,
195 "bt_value_float_set() fails with a frozen floating point number value object");
197 ret
= bt_value_float_get(obj
, &value
);
198 ok(!ret
&& value
== 33.1649758,
199 "bt_value_float_set() does not alter a frozen floating point number value object");
205 void test_string(void)
209 struct bt_value
*obj
;
211 obj
= bt_value_string_create();
212 ok(obj
&& bt_value_is_string(obj
),
213 "bt_value_string_create() returns a string value object");
215 ret
= bt_value_string_set(NULL
, "hoho");
216 ok(ret
== BT_VALUE_STATUS_INVAL
,
217 "bt_value_string_set() fails with an value object set to NULL");
218 ret
= bt_value_string_set(obj
, NULL
);
219 ok(ret
== BT_VALUE_STATUS_INVAL
,
220 "bt_value_string_set() fails with a value set to NULL");
221 ret
= bt_value_string_get(NULL
, &value
);
222 ok(ret
== BT_VALUE_STATUS_INVAL
,
223 "bt_value_string_get() fails with an value object set to NULL");
224 ret
= bt_value_string_get(obj
, NULL
);
225 ok(ret
== BT_VALUE_STATUS_INVAL
,
226 "bt_value_string_get() fails with a return value set to NULL");
228 ret
= bt_value_string_get(obj
, &value
);
229 ok(!ret
&& value
&& !strcmp(value
, ""),
230 "default string value object value is \"\"");
232 ret
= bt_value_string_set(obj
, "hello worldz");
233 ok(!ret
, "bt_value_string_set() succeeds");
234 ret
= bt_value_string_get(obj
, &value
);
235 ok(!ret
&& value
&& !strcmp(value
, "hello worldz"),
236 "bt_value_string_get() works");
239 pass("putting an existing string value object does not cause a crash")
241 obj
= bt_value_string_create_init(NULL
);
242 ok(!obj
, "bt_value_string_create_init() fails with an initial value set to NULL");
243 obj
= bt_value_string_create_init("initial value");
244 ok(obj
&& bt_value_is_string(obj
),
245 "bt_value_string_create_init() returns a string value object");
246 ret
= bt_value_string_get(obj
, &value
);
247 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
248 "bt_value_string_create_init() sets the appropriate initial value");
250 assert(!bt_value_freeze(obj
));
251 ok(bt_value_string_set(obj
, "new value") == BT_VALUE_STATUS_FROZEN
,
252 "bt_value_string_set() fails with a frozen string value object");
254 ret
= bt_value_string_get(obj
, &value
);
255 ok(!ret
&& value
&& !strcmp(value
, "initial value"),
256 "bt_value_string_set() does not alter a frozen string value object");
262 void test_array(void)
268 struct bt_value
*obj
;
269 const char *string_value
;
270 struct bt_value
*array_obj
;
272 array_obj
= bt_value_array_create();
273 ok(array_obj
&& bt_value_is_array(array_obj
),
274 "bt_value_array_create() returns an array value object");
275 ok(bt_value_array_is_empty(NULL
) == false,
276 "bt_value_array_is_empty() returns false with an value object set to NULL");
277 ok(bt_value_array_is_empty(array_obj
),
278 "initial array value object size is 0");
279 ok(bt_value_array_size(NULL
) == BT_VALUE_STATUS_INVAL
,
280 "bt_value_array_size() fails with an array value object set to NULL");
282 ok(bt_value_array_append(NULL
, bt_value_null
)
283 == BT_VALUE_STATUS_INVAL
,
284 "bt_value_array_append() fails with an array value object set to NULL");
285 ok(bt_value_array_append(array_obj
, NULL
) == BT_VALUE_STATUS_INVAL
,
286 "bt_value_array_append() fails with a value set to NULL");
288 obj
= bt_value_integer_create_init(345);
289 ret
= bt_value_array_append(array_obj
, obj
);
291 obj
= bt_value_float_create_init(-17.45);
292 ret
|= bt_value_array_append(array_obj
, obj
);
294 obj
= bt_value_bool_create_init(true);
295 ret
|= bt_value_array_append(array_obj
, obj
);
297 ret
|= bt_value_array_append(array_obj
, bt_value_null
);
298 ok(!ret
, "bt_value_array_append() succeeds");
299 ok(bt_value_array_size(array_obj
) == 4,
300 "appending an element to an array value object increment its size");
302 obj
= bt_value_array_get(array_obj
, 4);
303 ok(!obj
, "getting an array value object's element at an index equal to its size fails");
304 obj
= bt_value_array_get(array_obj
, 5);
305 ok(!obj
, "getting an array value object's element at a larger index fails");
307 obj
= bt_value_array_get(NULL
, 2);
308 ok(!obj
, "bt_value_array_get() fails with an array value object set to NULL");
310 obj
= bt_value_array_get(array_obj
, 0);
311 ok(obj
&& bt_value_is_integer(obj
),
312 "bt_value_array_get() returns an value object with the appropriate type (integer)");
313 ret
= bt_value_integer_get(obj
, &int_value
);
314 ok(!ret
&& int_value
== 345,
315 "bt_value_array_get() returns an value object with the appropriate value (integer)");
317 obj
= bt_value_array_get(array_obj
, 1);
318 ok(obj
&& bt_value_is_float(obj
),
319 "bt_value_array_get() returns an value object with the appropriate type (floating point number)");
320 ret
= bt_value_float_get(obj
, &float_value
);
321 ok(!ret
&& float_value
== -17.45,
322 "bt_value_array_get() returns an value object with the appropriate value (floating point number)");
324 obj
= bt_value_array_get(array_obj
, 2);
325 ok(obj
&& bt_value_is_bool(obj
),
326 "bt_value_array_get() returns an value object with the appropriate type (boolean)");
327 ret
= bt_value_bool_get(obj
, &bool_value
);
328 ok(!ret
&& bool_value
,
329 "bt_value_array_get() returns an value object with the appropriate value (boolean)");
331 obj
= bt_value_array_get(array_obj
, 3);
332 ok(obj
== bt_value_null
,
333 "bt_value_array_get() returns an value object with the appropriate type (null)");
335 ok(bt_value_array_set(NULL
, 0, bt_value_null
) ==
336 BT_VALUE_STATUS_INVAL
,
337 "bt_value_array_set() fails with an array value object set to NULL");
338 ok(bt_value_array_set(array_obj
, 0, NULL
) == BT_VALUE_STATUS_INVAL
,
339 "bt_value_array_set() fails with an element value object set to NULL");
340 ok(bt_value_array_set(array_obj
, 4, bt_value_null
) ==
341 BT_VALUE_STATUS_INVAL
,
342 "bt_value_array_set() fails with an invalid index");
343 obj
= bt_value_integer_create_init(1001);
345 ok(!bt_value_array_set(array_obj
, 2, obj
),
346 "bt_value_array_set() succeeds");
348 obj
= bt_value_array_get(array_obj
, 2);
349 ok(obj
&& bt_value_is_integer(obj
),
350 "bt_value_array_set() inserts an value object with the appropriate type");
351 ret
= bt_value_integer_get(obj
, &int_value
);
353 ok(int_value
== 1001,
354 "bt_value_array_set() inserts an value object with the appropriate value");
357 ret
= bt_value_array_append_bool(array_obj
, false);
358 ok(!ret
, "bt_value_array_append_bool() succeeds");
359 ok(bt_value_array_append_bool(NULL
, true) == BT_VALUE_STATUS_INVAL
,
360 "bt_value_array_append_bool() fails with an array value object set to NULL");
361 ret
= bt_value_array_append_integer(array_obj
, 98765);
362 ok(!ret
, "bt_value_array_append_integer() succeeds");
363 ok(bt_value_array_append_integer(NULL
, 18765) ==
364 BT_VALUE_STATUS_INVAL
,
365 "bt_value_array_append_integer() fails with an array value object set to NULL");
366 ret
= bt_value_array_append_float(array_obj
, 2.49578);
367 ok(!ret
, "bt_value_array_append_float() succeeds");
368 ok(bt_value_array_append_float(NULL
, 1.49578) ==
369 BT_VALUE_STATUS_INVAL
,
370 "bt_value_array_append_float() fails with an array value object set to NULL");
371 ret
= bt_value_array_append_string(array_obj
, "bt_value");
372 ok(!ret
, "bt_value_array_append_string() succeeds");
373 ok(bt_value_array_append_string(NULL
, "bt_obj") ==
374 BT_VALUE_STATUS_INVAL
,
375 "bt_value_array_append_string() fails with an array value object set to NULL");
376 ret
= bt_value_array_append_array(array_obj
);
377 ok(!ret
, "bt_value_array_append_array() succeeds");
378 ok(bt_value_array_append_array(NULL
) == BT_VALUE_STATUS_INVAL
,
379 "bt_value_array_append_array() fails with an array value object set to NULL");
380 ret
= bt_value_array_append_map(array_obj
);
381 ok(!ret
, "bt_value_array_append_map() succeeds");
382 ok(bt_value_array_append_map(NULL
) == BT_VALUE_STATUS_INVAL
,
383 "bt_value_array_append_map() fails with an array value object set to NULL");
385 ok(bt_value_array_size(array_obj
) == 10,
386 "the bt_value_array_append_*() functions increment the array value object's size");
387 ok(!bt_value_array_is_empty(array_obj
),
388 "map value object is not empty");
390 obj
= bt_value_array_get(array_obj
, 4);
391 ok(obj
&& bt_value_is_bool(obj
),
392 "bt_value_array_append_bool() appends a boolean value object");
393 ret
= bt_value_bool_get(obj
, &bool_value
);
394 ok(!ret
&& !bool_value
,
395 "bt_value_array_append_bool() appends the appropriate value");
397 obj
= bt_value_array_get(array_obj
, 5);
398 ok(obj
&& bt_value_is_integer(obj
),
399 "bt_value_array_append_integer() appends an integer value object");
400 ret
= bt_value_integer_get(obj
, &int_value
);
401 ok(!ret
&& int_value
== 98765,
402 "bt_value_array_append_integer() appends the appropriate value");
404 obj
= bt_value_array_get(array_obj
, 6);
405 ok(obj
&& bt_value_is_float(obj
),
406 "bt_value_array_append_float() appends a floating point number value object");
407 ret
= bt_value_float_get(obj
, &float_value
);
408 ok(!ret
&& float_value
== 2.49578,
409 "bt_value_array_append_float() appends the appropriate value");
411 obj
= bt_value_array_get(array_obj
, 7);
412 ok(obj
&& bt_value_is_string(obj
),
413 "bt_value_array_append_string() appends a string value object");
414 ret
= bt_value_string_get(obj
, &string_value
);
415 ok(!ret
&& string_value
&& !strcmp(string_value
, "bt_value"),
416 "bt_value_array_append_string() appends the appropriate value");
418 obj
= bt_value_array_get(array_obj
, 8);
419 ok(obj
&& bt_value_is_array(obj
),
420 "bt_value_array_append_array() appends an array value object");
421 ok(bt_value_array_is_empty(obj
),
422 "bt_value_array_append_array() an empty array value object");
424 obj
= bt_value_array_get(array_obj
, 9);
425 ok(obj
&& bt_value_is_map(obj
),
426 "bt_value_array_append_map() appends a map value object");
427 ok(bt_value_map_is_empty(obj
),
428 "bt_value_array_append_map() an empty map value object");
431 assert(!bt_value_freeze(array_obj
));
432 ok(bt_value_array_append(array_obj
, bt_value_null
) ==
433 BT_VALUE_STATUS_FROZEN
,
434 "bt_value_array_append() fails with a frozen array value object");
435 ok(bt_value_array_append_bool(array_obj
, false) ==
436 BT_VALUE_STATUS_FROZEN
,
437 "bt_value_array_append_bool() fails with a frozen array value object");
438 ok(bt_value_array_append_integer(array_obj
, 23) ==
439 BT_VALUE_STATUS_FROZEN
,
440 "bt_value_array_append_integer() fails with a frozen array value object");
441 ok(bt_value_array_append_float(array_obj
, 2.34) ==
442 BT_VALUE_STATUS_FROZEN
,
443 "bt_value_array_append_float() fails with a frozen array value object");
444 ok(bt_value_array_append_string(array_obj
, "yayayayaya") ==
445 BT_VALUE_STATUS_FROZEN
,
446 "bt_value_array_append_string() fails with a frozen array value object");
447 ok(bt_value_array_append_array(array_obj
) ==
448 BT_VALUE_STATUS_FROZEN
,
449 "bt_value_array_append_array() fails with a frozen array value object");
450 ok(bt_value_array_append_map(array_obj
) ==
451 BT_VALUE_STATUS_FROZEN
,
452 "bt_value_array_append_map() fails with a frozen array value object");
453 ok(bt_value_array_set(array_obj
, 2, bt_value_null
) ==
454 BT_VALUE_STATUS_FROZEN
,
455 "bt_value_array_set() fails with a frozen array value object");
456 ok(bt_value_array_size(array_obj
) == 10,
457 "appending to a frozen array value object does not change its size");
459 obj
= bt_value_array_get(array_obj
, 1);
461 ok(bt_value_float_set(obj
, 14.52) == BT_VALUE_STATUS_FROZEN
,
462 "freezing an array value object also freezes its elements");
466 pass("putting an existing array value object does not cause a crash")
470 bool test_map_foreach_cb_count(const char *key
, struct bt_value
*object
,
484 struct map_foreach_checklist
{
498 bool test_map_foreach_cb_check(const char *key
, struct bt_value
*object
,
502 struct map_foreach_checklist
*checklist
= data
;
504 if (!strcmp(key
, "bool")) {
505 if (checklist
->bool1
) {
506 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
510 ret
= bt_value_bool_get(object
, &val
);
511 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool\" value");
514 pass("test_map_foreach_cb_check(): \"bool\" value object has the right value");
515 checklist
->bool1
= true;
517 fail("test_map_foreach_cb_check(): \"bool\" value object has the wrong value");
520 } else if (!strcmp(key
, "int")) {
521 if (checklist
->int1
) {
522 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
526 ret
= bt_value_integer_get(object
, &val
);
527 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int\" value");
530 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
531 checklist
->int1
= true;
533 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
536 } else if (!strcmp(key
, "float")) {
537 if (checklist
->float1
) {
538 fail("test_map_foreach_cb_check(): duplicate key \"float\"");
542 ret
= bt_value_float_get(object
, &val
);
543 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float\" value");
546 pass("test_map_foreach_cb_check(): \"float\" value object has the right value");
547 checklist
->float1
= true;
549 fail("test_map_foreach_cb_check(): \"float\" value object has the wrong value");
552 } else if (!strcmp(key
, "null")) {
553 if (checklist
->null1
) {
554 fail("test_map_foreach_cb_check(): duplicate key \"bool\"");
556 ok(bt_value_is_null(object
), "test_map_foreach_cb_check(): success getting \"null\" value object");
557 checklist
->null1
= true;
559 } else if (!strcmp(key
, "bool2")) {
560 if (checklist
->bool2
) {
561 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
565 ret
= bt_value_bool_get(object
, &val
);
566 ok(!ret
, "test_map_foreach_cb_check(): success getting \"bool2\" value");
569 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
570 checklist
->bool2
= true;
572 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
575 } else if (!strcmp(key
, "int2")) {
576 if (checklist
->int2
) {
577 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
581 ret
= bt_value_integer_get(object
, &val
);
582 ok(!ret
, "test_map_foreach_cb_check(): success getting \"int2\" value");
585 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
586 checklist
->int2
= true;
588 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
591 } else if (!strcmp(key
, "float2")) {
592 if (checklist
->float2
) {
593 fail("test_map_foreach_cb_check(): duplicate key \"float2\"");
597 ret
= bt_value_float_get(object
, &val
);
598 ok(!ret
, "test_map_foreach_cb_check(): success getting \"float2\" value");
600 if (val
== -49.0001) {
601 pass("test_map_foreach_cb_check(): \"float2\" value object has the right value");
602 checklist
->float2
= true;
604 fail("test_map_foreach_cb_check(): \"float2\" value object has the wrong value");
607 } else if (!strcmp(key
, "string2")) {
608 if (checklist
->string2
) {
609 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
613 ret
= bt_value_string_get(object
, &val
);
614 ok(!ret
, "test_map_foreach_cb_check(): success getting \"string2\" value");
616 if (val
&& !strcmp(val
, "bt_value")) {
617 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
618 checklist
->string2
= true;
620 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
623 } else if (!strcmp(key
, "array2")) {
624 if (checklist
->array2
) {
625 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
627 ok(bt_value_is_array(object
), "test_map_foreach_cb_check(): success getting \"array2\" value object");
628 ok(bt_value_array_is_empty(object
),
629 "test_map_foreach_cb_check(): \"array2\" value object is empty");
630 checklist
->array2
= true;
632 } else if (!strcmp(key
, "map2")) {
633 if (checklist
->map2
) {
634 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
636 ok(bt_value_is_map(object
), "test_map_foreach_cb_check(): success getting \"map2\" value object");
637 ok(bt_value_map_is_empty(object
),
638 "test_map_foreach_cb_check(): \"map2\" value object is empty");
639 checklist
->map2
= true;
642 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key
);
656 struct bt_value
*obj
;
657 struct bt_value
*map_obj
;
658 struct map_foreach_checklist checklist
;
660 map_obj
= bt_value_map_create();
661 ok(map_obj
&& bt_value_is_map(map_obj
),
662 "bt_value_map_create() returns a map value object");
663 ok(bt_value_map_size(map_obj
) == 0,
664 "initial map value object size is 0");
665 ok(bt_value_map_size(NULL
) == BT_VALUE_STATUS_INVAL
,
666 "bt_value_map_size() fails with a map value object set to NULL");
668 ok(bt_value_map_insert(NULL
, "hello", bt_value_null
) ==
669 BT_VALUE_STATUS_INVAL
,
670 "bt_value_array_insert() fails with a map value object set to NULL");
671 ok(bt_value_map_insert(map_obj
, NULL
, bt_value_null
) ==
672 BT_VALUE_STATUS_INVAL
,
673 "bt_value_array_insert() fails with a key set to NULL");
674 ok(bt_value_map_insert(map_obj
, "yeah", NULL
) ==
675 BT_VALUE_STATUS_INVAL
,
676 "bt_value_array_insert() fails with an element value object set to NULL");
678 obj
= bt_value_integer_create_init(19457);
679 ret
= bt_value_map_insert(map_obj
, "int", obj
);
681 obj
= bt_value_float_create_init(5.444);
682 ret
|= bt_value_map_insert(map_obj
, "float", obj
);
684 obj
= bt_value_bool_create();
685 ret
|= bt_value_map_insert(map_obj
, "bool", obj
);
687 ret
|= bt_value_map_insert(map_obj
, "null", bt_value_null
);
688 ok(!ret
, "bt_value_map_insert() succeeds");
689 ok(bt_value_map_size(map_obj
) == 4,
690 "inserting an element into a map value object increment its size");
692 obj
= bt_value_bool_create_init(true);
693 ret
= bt_value_map_insert(map_obj
, "bool", obj
);
695 ok(!ret
, "bt_value_map_insert() accepts an existing key");
697 obj
= bt_value_map_get(map_obj
, NULL
);
698 ok(!obj
, "bt_value_map_get() fails with a key set to NULL");
699 obj
= bt_value_map_get(NULL
, "bool");
700 ok(!obj
, "bt_value_map_get() fails with a map value object set to NULL");
702 obj
= bt_value_map_get(map_obj
, "life");
703 ok(!obj
, "bt_value_map_get() fails with an non existing key");
704 obj
= bt_value_map_get(map_obj
, "float");
705 ok(obj
&& bt_value_is_float(obj
),
706 "bt_value_map_get() returns an value object with the appropriate type (float)");
707 ret
= bt_value_float_get(obj
, &float_value
);
708 ok(!ret
&& float_value
== 5.444,
709 "bt_value_map_get() returns an value object with the appropriate value (float)");
711 obj
= bt_value_map_get(map_obj
, "int");
712 ok(obj
&& bt_value_is_integer(obj
),
713 "bt_value_map_get() returns an value object with the appropriate type (integer)");
714 ret
= bt_value_integer_get(obj
, &int_value
);
715 ok(!ret
&& int_value
== 19457,
716 "bt_value_map_get() returns an value object with the appropriate value (integer)");
718 obj
= bt_value_map_get(map_obj
, "null");
719 ok(obj
&& bt_value_is_null(obj
),
720 "bt_value_map_get() returns an value object with the appropriate type (null)");
721 obj
= bt_value_map_get(map_obj
, "bool");
722 ok(obj
&& bt_value_is_bool(obj
),
723 "bt_value_map_get() returns an value object with the appropriate type (boolean)");
724 ret
= bt_value_bool_get(obj
, &bool_value
);
725 ok(!ret
&& bool_value
,
726 "bt_value_map_get() returns an value object with the appropriate value (boolean)");
729 ret
= bt_value_map_insert_bool(map_obj
, "bool2", true);
730 ok(!ret
, "bt_value_map_insert_bool() succeeds");
731 ok(bt_value_map_insert_bool(NULL
, "bool2", false) ==
732 BT_VALUE_STATUS_INVAL
,
733 "bt_value_map_insert_bool() fails with a map value object set to NULL");
734 ret
= bt_value_map_insert_integer(map_obj
, "int2", 98765);
735 ok(!ret
, "bt_value_map_insert_integer() succeeds");
736 ok(bt_value_map_insert_integer(NULL
, "int2", 1001) ==
737 BT_VALUE_STATUS_INVAL
,
738 "bt_value_map_insert_integer() fails with a map value object set to NULL");
739 ret
= bt_value_map_insert_float(map_obj
, "float2", -49.0001);
740 ok(!ret
, "bt_value_map_insert_float() succeeds");
741 ok(bt_value_map_insert_float(NULL
, "float2", 495) ==
742 BT_VALUE_STATUS_INVAL
,
743 "bt_value_map_insert_float() fails with a map value object set to NULL");
744 ret
= bt_value_map_insert_string(map_obj
, "string2", "bt_value");
745 ok(!ret
, "bt_value_map_insert_string() succeeds");
746 ok(bt_value_map_insert_string(NULL
, "string2", "bt_obj") ==
747 BT_VALUE_STATUS_INVAL
,
748 "bt_value_map_insert_string() fails with a map value object set to NULL");
749 ret
= bt_value_map_insert_array(map_obj
, "array2");
750 ok(!ret
, "bt_value_map_insert_array() succeeds");
751 ok(bt_value_map_insert_array(NULL
, "array2") == BT_VALUE_STATUS_INVAL
,
752 "bt_value_map_insert_array() fails with a map value object set to NULL");
753 ret
= bt_value_map_insert_map(map_obj
, "map2");
754 ok(!ret
, "bt_value_map_insert_map() succeeds");
755 ok(bt_value_map_insert_map(NULL
, "map2") == BT_VALUE_STATUS_INVAL
,
756 "bt_value_map_insert_map() fails with a map value object set to NULL");
758 ok(bt_value_map_size(map_obj
) == 10,
759 "the bt_value_map_insert*() functions increment the map value object's size");
761 ok(!bt_value_map_has_key(map_obj
, "hello"),
762 "map value object does not have key \"hello\"");
763 ok(bt_value_map_has_key(map_obj
, "bool"),
764 "map value object has key \"bool\"");
765 ok(bt_value_map_has_key(map_obj
, "int"),
766 "map value object has key \"int\"");
767 ok(bt_value_map_has_key(map_obj
, "float"),
768 "map value object has key \"float\"");
769 ok(bt_value_map_has_key(map_obj
, "null"),
770 "map value object has key \"null\"");
771 ok(bt_value_map_has_key(map_obj
, "bool2"),
772 "map value object has key \"bool2\"");
773 ok(bt_value_map_has_key(map_obj
, "int2"),
774 "map value object has key \"int2\"");
775 ok(bt_value_map_has_key(map_obj
, "float2"),
776 "map value object has key \"float2\"");
777 ok(bt_value_map_has_key(map_obj
, "string2"),
778 "map value object has key \"string2\"");
779 ok(bt_value_map_has_key(map_obj
, "array2"),
780 "map value object has key \"array2\"");
781 ok(bt_value_map_has_key(map_obj
, "map2"),
782 "map value object has key \"map2\"");
784 ok(bt_value_map_foreach(NULL
, test_map_foreach_cb_count
, &count
) ==
785 BT_VALUE_STATUS_INVAL
,
786 "bt_value_map_foreach() fails with a map value object set to NULL");
787 ok(bt_value_map_foreach(map_obj
, NULL
, &count
) ==
788 BT_VALUE_STATUS_INVAL
,
789 "bt_value_map_foreach() fails with a user function set to NULL");
790 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_count
, &count
);
791 ok(ret
== BT_VALUE_STATUS_CANCELLED
&& count
== 3,
792 "bt_value_map_foreach() breaks the loop when the user function returns false");
794 memset(&checklist
, 0, sizeof(checklist
));
795 ret
= bt_value_map_foreach(map_obj
, test_map_foreach_cb_check
,
797 ok(ret
== BT_VALUE_STATUS_OK
,
798 "bt_value_map_foreach() succeeds with test_map_foreach_cb_check()");
799 ok(checklist
.bool1
&& checklist
.int1
&& checklist
.float1
&&
800 checklist
.null1
&& checklist
.bool2
&& checklist
.int2
&&
801 checklist
.float2
&& checklist
.string2
&&
802 checklist
.array2
&& checklist
.map2
,
803 "bt_value_map_foreach() iterates over all the map value object's elements");
805 assert(!bt_value_freeze(map_obj
));
806 ok(bt_value_map_insert(map_obj
, "allo", bt_value_null
) ==
807 BT_VALUE_STATUS_FROZEN
,
808 "bt_value_map_insert() fails with a frozen map value object");
809 ok(bt_value_map_insert_bool(map_obj
, "duh", false) ==
810 BT_VALUE_STATUS_FROZEN
,
811 "bt_value_map_insert_bool() fails with a frozen array value object");
812 ok(bt_value_map_insert_integer(map_obj
, "duh", 23) ==
813 BT_VALUE_STATUS_FROZEN
,
814 "bt_value_map_insert_integer() fails with a frozen array value object");
815 ok(bt_value_map_insert_float(map_obj
, "duh", 2.34) ==
816 BT_VALUE_STATUS_FROZEN
,
817 "bt_value_map_insert_float() fails with a frozen array value object");
818 ok(bt_value_map_insert_string(map_obj
, "duh", "yayayayaya") ==
819 BT_VALUE_STATUS_FROZEN
,
820 "bt_value_map_insert_string() fails with a frozen array value object");
821 ok(bt_value_map_insert_array(map_obj
, "duh") ==
822 BT_VALUE_STATUS_FROZEN
,
823 "bt_value_map_insert_array() fails with a frozen array value object");
824 ok(bt_value_map_insert_map(map_obj
, "duh") ==
825 BT_VALUE_STATUS_FROZEN
,
826 "bt_value_map_insert_map() fails with a frozen array value object");
827 ok(bt_value_map_size(map_obj
) == 10,
828 "appending to a frozen map value object does not change its size");
831 pass("putting an existing map value object does not cause a crash")
835 void test_types(void)
847 void test_compare_null(void)
849 ok(!bt_value_compare(bt_value_null
, NULL
),
850 "cannot compare null value object and NULL");
851 ok(!bt_value_compare(NULL
, bt_value_null
),
852 "cannot compare NULL and null value object");
853 ok(bt_value_compare(bt_value_null
, bt_value_null
),
854 "null value objects are equivalent");
858 void test_compare_bool(void)
860 struct bt_value
*bool1
= bt_value_bool_create_init(false);
861 struct bt_value
*bool2
= bt_value_bool_create_init(true);
862 struct bt_value
*bool3
= bt_value_bool_create_init(false);
864 assert(bool1
&& bool2
&& bool3
);
865 ok(!bt_value_compare(bt_value_null
, bool1
),
866 "cannot compare null value object and bool value object");
867 ok(!bt_value_compare(bool1
, bool2
),
868 "integer value objects are not equivalent (false and true)");
869 ok(bt_value_compare(bool1
, bool3
),
870 "integer value objects are equivalent (false and false)");
878 void test_compare_integer(void)
880 struct bt_value
*int1
= bt_value_integer_create_init(10);
881 struct bt_value
*int2
= bt_value_integer_create_init(-23);
882 struct bt_value
*int3
= bt_value_integer_create_init(10);
884 assert(int1
&& int2
&& int3
);
885 ok(!bt_value_compare(bt_value_null
, int1
),
886 "cannot compare null value object and integer value object");
887 ok(!bt_value_compare(int1
, int2
),
888 "integer value objects are not equivalent (10 and -23)");
889 ok(bt_value_compare(int1
, int3
),
890 "integer value objects are equivalent (10 and 10)");
898 void test_compare_float(void)
900 struct bt_value
*float1
= bt_value_float_create_init(17.38);
901 struct bt_value
*float2
= bt_value_float_create_init(-14.23);
902 struct bt_value
*float3
= bt_value_float_create_init(17.38);
904 assert(float1
&& float2
&& float3
);
906 ok(!bt_value_compare(bt_value_null
, float1
),
907 "cannot compare null value object and floating point number value object");
908 ok(!bt_value_compare(float1
, float2
),
909 "floating point number value objects are not equivalent (17.38 and -14.23)");
910 ok(bt_value_compare(float1
, float3
),
911 "floating point number value objects are equivalent (17.38 and 17.38)");
919 void test_compare_string(void)
921 struct bt_value
*string1
= bt_value_string_create_init("hello");
922 struct bt_value
*string2
= bt_value_string_create_init("bt_value");
923 struct bt_value
*string3
= bt_value_string_create_init("hello");
925 assert(string1
&& string2
&& string3
);
927 ok(!bt_value_compare(bt_value_null
, string1
),
928 "cannot compare null value object and string value object");
929 ok(!bt_value_compare(string1
, string2
),
930 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
931 ok(bt_value_compare(string1
, string3
),
932 "string value objects are equivalent (\"hello\" and \"hello\")");
940 void test_compare_array(void)
942 struct bt_value
*array1
= bt_value_array_create();
943 struct bt_value
*array2
= bt_value_array_create();
944 struct bt_value
*array3
= bt_value_array_create();
946 assert(array1
&& array2
&& array3
);
948 ok(bt_value_compare(array1
, array2
),
949 "empty array value objects are equivalent");
951 assert(!bt_value_array_append_integer(array1
, 23));
952 assert(!bt_value_array_append_float(array1
, 14.2));
953 assert(!bt_value_array_append_bool(array1
, false));
954 assert(!bt_value_array_append_float(array2
, 14.2));
955 assert(!bt_value_array_append_integer(array2
, 23));
956 assert(!bt_value_array_append_bool(array2
, false));
957 assert(!bt_value_array_append_integer(array3
, 23));
958 assert(!bt_value_array_append_float(array3
, 14.2));
959 assert(!bt_value_array_append_bool(array3
, false));
960 assert(bt_value_array_size(array1
) == 3);
961 assert(bt_value_array_size(array2
) == 3);
962 assert(bt_value_array_size(array3
) == 3);
964 ok(!bt_value_compare(bt_value_null
, array1
),
965 "cannot compare null value object and array value object");
966 ok(!bt_value_compare(array1
, array2
),
967 "array value objects are not equivalent ([23, 14.2, false] and [14.2, 23, false])");
968 ok(bt_value_compare(array1
, array3
),
969 "array value objects are equivalent ([23, 14.2, false] and [23, 14.2, false])");
977 void test_compare_map(void)
979 struct bt_value
*map1
= bt_value_map_create();
980 struct bt_value
*map2
= bt_value_map_create();
981 struct bt_value
*map3
= bt_value_map_create();
983 assert(map1
&& map2
&& map3
);
985 ok(bt_value_compare(map1
, map2
),
986 "empty map value objects are equivalent");
988 assert(!bt_value_map_insert_integer(map1
, "one", 23));
989 assert(!bt_value_map_insert_float(map1
, "two", 14.2));
990 assert(!bt_value_map_insert_bool(map1
, "three", false));
991 assert(!bt_value_map_insert_float(map2
, "one", 14.2));
992 assert(!bt_value_map_insert_integer(map2
, "two", 23));
993 assert(!bt_value_map_insert_bool(map2
, "three", false));
994 assert(!bt_value_map_insert_bool(map3
, "three", false));
995 assert(!bt_value_map_insert_integer(map3
, "one", 23));
996 assert(!bt_value_map_insert_float(map3
, "two", 14.2));
997 assert(bt_value_map_size(map1
) == 3);
998 assert(bt_value_map_size(map2
) == 3);
999 assert(bt_value_map_size(map3
) == 3);
1001 ok(!bt_value_compare(bt_value_null
, map1
),
1002 "cannot compare null value object and map value object");
1003 ok(!bt_value_compare(map1
, map2
),
1004 "map value objects are not equivalent");
1005 ok(bt_value_compare(map1
, map3
),
1006 "map value objects are equivalent");
1014 void test_compare(void)
1016 ok(!bt_value_compare(NULL
, NULL
), "cannot compare NULL and NULL");
1017 test_compare_null();
1018 test_compare_bool();
1019 test_compare_integer();
1020 test_compare_float();
1021 test_compare_string();
1022 test_compare_array();
1027 void test_copy(void)
1030 * Here's the deal here. If we make sure that each value object
1031 * of our deep copy has a different address than its source,
1032 * and that bt_value_compare() returns true for the top-level
1033 * value object, taking into account that we test the correctness of
1034 * bt_value_compare() elsewhere, then the deep copy is a
1037 struct bt_value
*null_copy_obj
;
1038 struct bt_value
*bool_obj
, *bool_copy_obj
;
1039 struct bt_value
*integer_obj
, *integer_copy_obj
;
1040 struct bt_value
*float_obj
, *float_copy_obj
;
1041 struct bt_value
*string_obj
, *string_copy_obj
;
1042 struct bt_value
*array_obj
, *array_copy_obj
;
1043 struct bt_value
*map_obj
, *map_copy_obj
;
1045 bool_obj
= bt_value_bool_create_init(true);
1046 integer_obj
= bt_value_integer_create_init(23);
1047 float_obj
= bt_value_float_create_init(-3.1416);
1048 string_obj
= bt_value_string_create_init("test");
1049 array_obj
= bt_value_array_create();
1050 map_obj
= bt_value_map_create();
1052 assert(bool_obj
&& integer_obj
&& float_obj
&& string_obj
&&
1053 array_obj
&& map_obj
);
1055 assert(!bt_value_array_append(array_obj
, bool_obj
));
1056 assert(!bt_value_array_append(array_obj
, integer_obj
));
1057 assert(!bt_value_array_append(array_obj
, float_obj
));
1058 assert(!bt_value_array_append(array_obj
, bt_value_null
));
1059 assert(!bt_value_map_insert(map_obj
, "array", array_obj
));
1060 assert(!bt_value_map_insert(map_obj
, "string", string_obj
));
1062 map_copy_obj
= bt_value_copy(NULL
);
1064 "bt_value_copy() fails with a source value object set to NULL");
1066 map_copy_obj
= bt_value_copy(map_obj
);
1068 "bt_value_copy() succeeds");
1070 ok(map_obj
!= map_copy_obj
,
1071 "bt_value_copy() returns a different pointer (map)");
1072 string_copy_obj
= bt_value_map_get(map_copy_obj
, "string");
1073 ok(string_copy_obj
!= string_obj
,
1074 "bt_value_copy() returns a different pointer (string)");
1075 array_copy_obj
= bt_value_map_get(map_copy_obj
, "array");
1076 ok(array_copy_obj
!= array_obj
,
1077 "bt_value_copy() returns a different pointer (array)");
1078 bool_copy_obj
= bt_value_array_get(array_copy_obj
, 0);
1079 ok(bool_copy_obj
!= bool_obj
,
1080 "bt_value_copy() returns a different pointer (bool)");
1081 integer_copy_obj
= bt_value_array_get(array_copy_obj
, 1);
1082 ok(integer_copy_obj
!= integer_obj
,
1083 "bt_value_copy() returns a different pointer (integer)");
1084 float_copy_obj
= bt_value_array_get(array_copy_obj
, 2);
1085 ok(float_copy_obj
!= float_obj
,
1086 "bt_value_copy() returns a different pointer (float)");
1087 null_copy_obj
= bt_value_array_get(array_copy_obj
, 3);
1088 ok(null_copy_obj
== bt_value_null
,
1089 "bt_value_copy() returns the same pointer (null)");
1091 ok(bt_value_compare(map_obj
, map_copy_obj
),
1092 "source and destination value objects have the same content");
1094 BT_PUT(bool_copy_obj
);
1095 BT_PUT(integer_copy_obj
);
1096 BT_PUT(float_copy_obj
);
1097 BT_PUT(string_copy_obj
);
1098 BT_PUT(array_copy_obj
);
1099 BT_PUT(map_copy_obj
);
1101 BT_PUT(integer_obj
);
1109 void test_macros(void)
1111 struct bt_value
*obj
= bt_value_bool_create();
1112 struct bt_value
*src
;
1113 struct bt_value
*dst
;
1117 ok(!obj
, "BT_PUT() resets the variable to NULL");
1119 obj
= bt_value_bool_create();
1123 ok(!src
, "BT_MOVE() resets the source variable to NULL");
1124 ok(dst
== obj
, "BT_MOVE() moves the ownership");
1130 void test_freeze(void)
1132 struct bt_value
*obj
;
1134 ok(bt_value_freeze(NULL
) == BT_VALUE_STATUS_INVAL
,
1135 "bt_value_freeze() fails with an value object set to NULL");
1136 ok(!bt_value_freeze(bt_value_null
),
1137 "bt_value_freeze() succeeds with a null value object");
1139 ok(!bt_value_is_frozen(NULL
), "NULL is not frozen");
1140 ok(bt_value_is_frozen(bt_value_null
),
1141 "the null singleton is frozen");
1142 obj
= bt_value_integer_create();
1144 ok(!bt_value_is_frozen(obj
),
1145 "bt_value_is_frozen() returns false with a fresh value object");
1146 assert(!bt_value_freeze(obj
));
1147 ok(!bt_value_freeze(obj
),
1148 "bt_value_freeze() passes with a frozen value object");
1149 ok(bt_value_is_frozen(obj
),
1150 "bt_value_is_frozen() returns true with a frozen value object");