tests: use BT_ASSERT() instead of assert()
[babeltrace.git] / tests / lib / test_bt_values.c
1 /*
2 * test_bt_values.c
3 *
4 * Babeltrace value objects tests
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 *
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.
12 *
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.
17 *
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.
21 */
22
23 #include <babeltrace/ref.h>
24 #include <babeltrace/values.h>
25 #include <babeltrace/assert-internal.h>
26 #include <string.h>
27 #include "tap/tap.h"
28
29 #define NR_TESTS 158
30
31 static
32 void test_null(void)
33 {
34 ok(bt_value_null, "bt_value_null is not NULL");
35 ok(bt_value_is_null(bt_value_null),
36 "bt_value_null is a null value object");
37 bt_get(bt_value_null);
38 pass("getting bt_value_null does not cause a crash");
39 bt_put(bt_value_null);
40 pass("putting bt_value_null does not cause a crash");
41 }
42
43 static
44 void test_bool(void)
45 {
46 int ret;
47 bt_bool value;
48 struct bt_value *obj;
49
50 obj = bt_value_bool_create();
51 ok(obj && bt_value_is_bool(obj),
52 "bt_value_bool_create() returns a boolean value object");
53
54 value = BT_TRUE;
55 ret = bt_value_bool_get(obj, &value);
56 ok(!ret && !value, "default boolean value object value is BT_FALSE");
57
58 BT_ASSERT(!bt_value_bool_set(obj, BT_FALSE));
59 ret = bt_value_bool_set(obj, BT_TRUE);
60 ok(!ret, "bt_value_bool_set() succeeds");
61 ret = bt_value_bool_get(obj, &value);
62 ok(!ret && value, "bt_value_bool_set() works");
63
64 BT_PUT(obj);
65 pass("putting an existing boolean value object does not cause a crash")
66
67 value = BT_FALSE;
68 obj = bt_value_bool_create_init(BT_TRUE);
69 ok(obj && bt_value_is_bool(obj),
70 "bt_value_bool_create_init() returns a boolean value object");
71 ret = bt_value_bool_get(obj, &value);
72 ok(!ret && value,
73 "bt_value_bool_create_init() sets the appropriate initial value");
74
75 BT_PUT(obj);
76 }
77
78 static
79 void test_integer(void)
80 {
81 int ret;
82 int64_t value;
83 struct bt_value *obj;
84
85 obj = bt_value_integer_create();
86 ok(obj && bt_value_is_integer(obj),
87 "bt_value_integer_create() returns an integer value object");
88
89 value = 1961;
90 ret = bt_value_integer_get(obj, &value);
91 ok(!ret && value == 0, "default integer value object value is 0");
92
93 ret = bt_value_integer_set(obj, -98765);
94 ok(!ret, "bt_value_integer_set() succeeds");
95 ret = bt_value_integer_get(obj, &value);
96 ok(!ret && value == -98765, "bt_value_integer_set() works");
97
98 BT_PUT(obj);
99 pass("putting an existing integer value object does not cause a crash")
100
101 obj = bt_value_integer_create_init(321456987);
102 ok(obj && bt_value_is_integer(obj),
103 "bt_value_integer_create_init() returns an integer value object");
104 ret = bt_value_integer_get(obj, &value);
105 ok(!ret && value == 321456987,
106 "bt_value_integer_create_init() sets the appropriate initial value");
107
108 BT_PUT(obj);
109 }
110
111 static
112 void test_float(void)
113 {
114 int ret;
115 double value;
116 struct bt_value *obj;
117
118 obj = bt_value_float_create();
119 ok(obj && bt_value_is_float(obj),
120 "bt_value_float_create() returns a floating point number value object");
121
122 value = 17.34;
123 ret = bt_value_float_get(obj, &value);
124 ok(!ret && value == 0.,
125 "default floating point number value object value is 0");
126
127 ret = bt_value_float_set(obj, -3.1416);
128 ok(!ret, "bt_value_float_set() succeeds");
129 ret = bt_value_float_get(obj, &value);
130 ok(!ret && value == -3.1416, "bt_value_float_set() works");
131
132 BT_PUT(obj);
133 pass("putting an existing floating point number value object does not cause a crash")
134
135 obj = bt_value_float_create_init(33.1649758);
136 ok(obj && bt_value_is_float(obj),
137 "bt_value_float_create_init() returns a floating point number value object");
138 ret = bt_value_float_get(obj, &value);
139 ok(!ret && value == 33.1649758,
140 "bt_value_float_create_init() sets the appropriate initial value");
141
142 BT_PUT(obj);
143 }
144
145 static
146 void test_string(void)
147 {
148 int ret;
149 const char *value;
150 struct bt_value *obj;
151
152 obj = bt_value_string_create();
153 ok(obj && bt_value_is_string(obj),
154 "bt_value_string_create() returns a string value object");
155
156 ret = bt_value_string_get(obj, &value);
157 ok(!ret && value && !strcmp(value, ""),
158 "default string value object value is \"\"");
159
160 ret = bt_value_string_set(obj, "hello worldz");
161 ok(!ret, "bt_value_string_set() succeeds");
162 ret = bt_value_string_get(obj, &value);
163 ok(!ret && value && !strcmp(value, "hello worldz"),
164 "bt_value_string_get() works");
165
166 BT_PUT(obj);
167 pass("putting an existing string value object does not cause a crash")
168
169 obj = bt_value_string_create_init("initial value");
170 ok(obj && bt_value_is_string(obj),
171 "bt_value_string_create_init() returns a string value object");
172 ret = bt_value_string_get(obj, &value);
173 ok(!ret && value && !strcmp(value, "initial value"),
174 "bt_value_string_create_init() sets the appropriate initial value");
175
176 BT_PUT(obj);
177 }
178
179 static
180 void test_array(void)
181 {
182 int ret;
183 bt_bool bool_value;
184 int64_t int_value;
185 double float_value;
186 struct bt_value *obj;
187 const char *string_value;
188 struct bt_value *array_obj;
189
190 array_obj = bt_value_array_create();
191 ok(array_obj && bt_value_is_array(array_obj),
192 "bt_value_array_create() returns an array value object");
193 ok(bt_value_array_is_empty(array_obj),
194 "initial array value object size is 0");
195
196 obj = bt_value_integer_create_init(345);
197 ret = bt_value_array_append(array_obj, obj);
198 BT_PUT(obj);
199 obj = bt_value_float_create_init(-17.45);
200 ret |= bt_value_array_append(array_obj, obj);
201 BT_PUT(obj);
202 obj = bt_value_bool_create_init(BT_TRUE);
203 ret |= bt_value_array_append(array_obj, obj);
204 BT_PUT(obj);
205 ret |= bt_value_array_append(array_obj, bt_value_null);
206 ok(!ret, "bt_value_array_append() succeeds");
207 ok(bt_value_array_size(array_obj) == 4,
208 "appending an element to an array value object increment its size");
209
210 obj = bt_value_array_get(array_obj, 0);
211 ok(obj && bt_value_is_integer(obj),
212 "bt_value_array_get() returns an value object with the appropriate type (integer)");
213 ret = bt_value_integer_get(obj, &int_value);
214 ok(!ret && int_value == 345,
215 "bt_value_array_get() returns an value object with the appropriate value (integer)");
216 BT_PUT(obj);
217 obj = bt_value_array_get(array_obj, 1);
218 ok(obj && bt_value_is_float(obj),
219 "bt_value_array_get() returns an value object with the appropriate type (floating point number)");
220 ret = bt_value_float_get(obj, &float_value);
221 ok(!ret && float_value == -17.45,
222 "bt_value_array_get() returns an value object with the appropriate value (floating point number)");
223 BT_PUT(obj);
224 obj = bt_value_array_get(array_obj, 2);
225 ok(obj && bt_value_is_bool(obj),
226 "bt_value_array_get() returns an value object with the appropriate type (boolean)");
227 ret = bt_value_bool_get(obj, &bool_value);
228 ok(!ret && bool_value,
229 "bt_value_array_get() returns an value object with the appropriate value (boolean)");
230 BT_PUT(obj);
231 obj = bt_value_array_get(array_obj, 3);
232 ok(obj == bt_value_null,
233 "bt_value_array_get() returns an value object with the appropriate type (null)");
234
235 obj = bt_value_integer_create_init(1001);
236 BT_ASSERT(obj);
237 ok(!bt_value_array_set(array_obj, 2, obj),
238 "bt_value_array_set() succeeds");
239 BT_PUT(obj);
240 obj = bt_value_array_get(array_obj, 2);
241 ok(obj && bt_value_is_integer(obj),
242 "bt_value_array_set() inserts an value object with the appropriate type");
243 ret = bt_value_integer_get(obj, &int_value);
244 BT_ASSERT(!ret);
245 ok(int_value == 1001,
246 "bt_value_array_set() inserts an value object with the appropriate value");
247 BT_PUT(obj);
248
249 ret = bt_value_array_append_bool(array_obj, BT_FALSE);
250 ok(!ret, "bt_value_array_append_bool() succeeds");
251 ret = bt_value_array_append_integer(array_obj, 98765);
252 ok(!ret, "bt_value_array_append_integer() succeeds");
253 ret = bt_value_array_append_float(array_obj, 2.49578);
254 ok(!ret, "bt_value_array_append_float() succeeds");
255 ret = bt_value_array_append_string(array_obj, "bt_value");
256 ok(!ret, "bt_value_array_append_string() succeeds");
257 ret = bt_value_array_append_empty_array(array_obj);
258 ok(!ret, "bt_value_array_append_empty_array() succeeds");
259 ret = bt_value_array_append_empty_map(array_obj);
260 ok(!ret, "bt_value_array_append_empty_map() succeeds");
261
262 ok(bt_value_array_size(array_obj) == 10,
263 "the bt_value_array_append_*() functions increment the array value object's size");
264 ok(!bt_value_array_is_empty(array_obj),
265 "map value object is not empty");
266
267 obj = bt_value_array_get(array_obj, 4);
268 ok(obj && bt_value_is_bool(obj),
269 "bt_value_array_append_bool() appends a boolean value object");
270 ret = bt_value_bool_get(obj, &bool_value);
271 ok(!ret && !bool_value,
272 "bt_value_array_append_bool() appends the appropriate value");
273 BT_PUT(obj);
274 obj = bt_value_array_get(array_obj, 5);
275 ok(obj && bt_value_is_integer(obj),
276 "bt_value_array_append_integer() appends an integer value object");
277 ret = bt_value_integer_get(obj, &int_value);
278 ok(!ret && int_value == 98765,
279 "bt_value_array_append_integer() appends the appropriate value");
280 BT_PUT(obj);
281 obj = bt_value_array_get(array_obj, 6);
282 ok(obj && bt_value_is_float(obj),
283 "bt_value_array_append_float() appends a floating point number value object");
284 ret = bt_value_float_get(obj, &float_value);
285 ok(!ret && float_value == 2.49578,
286 "bt_value_array_append_float() appends the appropriate value");
287 BT_PUT(obj);
288 obj = bt_value_array_get(array_obj, 7);
289 ok(obj && bt_value_is_string(obj),
290 "bt_value_array_append_string() appends a string value object");
291 ret = bt_value_string_get(obj, &string_value);
292 ok(!ret && string_value && !strcmp(string_value, "bt_value"),
293 "bt_value_array_append_string() appends the appropriate value");
294 BT_PUT(obj);
295 obj = bt_value_array_get(array_obj, 8);
296 ok(obj && bt_value_is_array(obj),
297 "bt_value_array_append_empty_array() appends an array value object");
298 ok(bt_value_array_is_empty(obj),
299 "bt_value_array_append_empty_array() an empty array value object");
300 BT_PUT(obj);
301 obj = bt_value_array_get(array_obj, 9);
302 ok(obj && bt_value_is_map(obj),
303 "bt_value_array_append_empty_map() appends a map value object");
304 ok(bt_value_map_is_empty(obj),
305 "bt_value_array_append_empty_map() an empty map value object");
306 BT_PUT(obj);
307
308 BT_PUT(array_obj);
309 pass("putting an existing array value object does not cause a crash")
310 }
311
312 static
313 bt_bool test_map_foreach_cb_count(const char *key, struct bt_value *object,
314 void *data)
315 {
316 int *count = data;
317
318 if (*count == 3) {
319 return BT_FALSE;
320 }
321
322 (*count)++;
323
324 return BT_TRUE;
325 }
326
327 struct map_foreach_checklist {
328 bt_bool bool1;
329 bt_bool int1;
330 bt_bool float1;
331 bt_bool null1;
332 bt_bool bool2;
333 bt_bool int2;
334 bt_bool float2;
335 bt_bool string2;
336 bt_bool array2;
337 bt_bool map2;
338 };
339
340 static
341 bt_bool test_map_foreach_cb_check(const char *key, struct bt_value *object,
342 void *data)
343 {
344 int ret;
345 struct map_foreach_checklist *checklist = data;
346
347 if (!strcmp(key, "bt_bool")) {
348 if (checklist->bool1) {
349 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
350 } else {
351 bt_bool val = BT_FALSE;
352
353 ret = bt_value_bool_get(object, &val);
354 ok(!ret, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
355
356 if (val) {
357 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
358 checklist->bool1 = BT_TRUE;
359 } else {
360 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
361 }
362 }
363 } else if (!strcmp(key, "int")) {
364 if (checklist->int1) {
365 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
366 } else {
367 int64_t val = 0;
368
369 ret = bt_value_integer_get(object, &val);
370 ok(!ret, "test_map_foreach_cb_check(): success getting \"int\" value");
371
372 if (val == 19457) {
373 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
374 checklist->int1 = BT_TRUE;
375 } else {
376 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
377 }
378 }
379 } else if (!strcmp(key, "float")) {
380 if (checklist->float1) {
381 fail("test_map_foreach_cb_check(): duplicate key \"float\"");
382 } else {
383 double val = 0;
384
385 ret = bt_value_float_get(object, &val);
386 ok(!ret, "test_map_foreach_cb_check(): success getting \"float\" value");
387
388 if (val == 5.444) {
389 pass("test_map_foreach_cb_check(): \"float\" value object has the right value");
390 checklist->float1 = BT_TRUE;
391 } else {
392 fail("test_map_foreach_cb_check(): \"float\" value object has the wrong value");
393 }
394 }
395 } else if (!strcmp(key, "null")) {
396 if (checklist->null1) {
397 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
398 } else {
399 ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object");
400 checklist->null1 = BT_TRUE;
401 }
402 } else if (!strcmp(key, "bool2")) {
403 if (checklist->bool2) {
404 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
405 } else {
406 bt_bool val = BT_FALSE;
407
408 ret = bt_value_bool_get(object, &val);
409 ok(!ret, "test_map_foreach_cb_check(): success getting \"bool2\" value");
410
411 if (val) {
412 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
413 checklist->bool2 = BT_TRUE;
414 } else {
415 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
416 }
417 }
418 } else if (!strcmp(key, "int2")) {
419 if (checklist->int2) {
420 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
421 } else {
422 int64_t val = 0;
423
424 ret = bt_value_integer_get(object, &val);
425 ok(!ret, "test_map_foreach_cb_check(): success getting \"int2\" value");
426
427 if (val == 98765) {
428 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
429 checklist->int2 = BT_TRUE;
430 } else {
431 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
432 }
433 }
434 } else if (!strcmp(key, "float2")) {
435 if (checklist->float2) {
436 fail("test_map_foreach_cb_check(): duplicate key \"float2\"");
437 } else {
438 double val = 0;
439
440 ret = bt_value_float_get(object, &val);
441 ok(!ret, "test_map_foreach_cb_check(): success getting \"float2\" value");
442
443 if (val == -49.0001) {
444 pass("test_map_foreach_cb_check(): \"float2\" value object has the right value");
445 checklist->float2 = BT_TRUE;
446 } else {
447 fail("test_map_foreach_cb_check(): \"float2\" value object has the wrong value");
448 }
449 }
450 } else if (!strcmp(key, "string2")) {
451 if (checklist->string2) {
452 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
453 } else {
454 const char *val;
455
456 ret = bt_value_string_get(object, &val);
457 ok(!ret, "test_map_foreach_cb_check(): success getting \"string2\" value");
458
459 if (val && !strcmp(val, "bt_value")) {
460 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
461 checklist->string2 = BT_TRUE;
462 } else {
463 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
464 }
465 }
466 } else if (!strcmp(key, "array2")) {
467 if (checklist->array2) {
468 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
469 } else {
470 ok(bt_value_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" value object");
471 ok(bt_value_array_is_empty(object),
472 "test_map_foreach_cb_check(): \"array2\" value object is empty");
473 checklist->array2 = BT_TRUE;
474 }
475 } else if (!strcmp(key, "map2")) {
476 if (checklist->map2) {
477 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
478 } else {
479 ok(bt_value_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" value object");
480 ok(bt_value_map_is_empty(object),
481 "test_map_foreach_cb_check(): \"map2\" value object is empty");
482 checklist->map2 = BT_TRUE;
483 }
484 } else {
485 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key);
486 }
487
488 return BT_TRUE;
489 }
490
491 static
492 void test_map(void)
493 {
494 int ret;
495 int count = 0;
496 bt_bool bool_value;
497 int64_t int_value;
498 double float_value;
499 struct bt_value *obj;
500 struct bt_value *map_obj;
501 struct map_foreach_checklist checklist;
502
503 map_obj = bt_value_map_create();
504 ok(map_obj && bt_value_is_map(map_obj),
505 "bt_value_map_create() returns a map value object");
506 ok(bt_value_map_size(map_obj) == 0,
507 "initial map value object size is 0");
508
509 obj = bt_value_integer_create_init(19457);
510 ret = bt_value_map_insert(map_obj, "int", obj);
511 BT_PUT(obj);
512 obj = bt_value_float_create_init(5.444);
513 ret |= bt_value_map_insert(map_obj, "float", obj);
514 BT_PUT(obj);
515 obj = bt_value_bool_create();
516 ret |= bt_value_map_insert(map_obj, "bt_bool", obj);
517 BT_PUT(obj);
518 ret |= bt_value_map_insert(map_obj, "null", bt_value_null);
519 ok(!ret, "bt_value_map_insert() succeeds");
520 ok(bt_value_map_size(map_obj) == 4,
521 "inserting an element into a map value object increment its size");
522
523 obj = bt_value_bool_create_init(BT_TRUE);
524 ret = bt_value_map_insert(map_obj, "bt_bool", obj);
525 BT_PUT(obj);
526 ok(!ret, "bt_value_map_insert() accepts an existing key");
527
528 obj = bt_value_map_get(map_obj, "life");
529 ok(!obj, "bt_value_map_get() returns NULL with an non existing key");
530 obj = bt_value_map_get(map_obj, "float");
531 ok(obj && bt_value_is_float(obj),
532 "bt_value_map_get() returns an value object with the appropriate type (float)");
533 ret = bt_value_float_get(obj, &float_value);
534 ok(!ret && float_value == 5.444,
535 "bt_value_map_get() returns an value object with the appropriate value (float)");
536 BT_PUT(obj);
537 obj = bt_value_map_get(map_obj, "int");
538 ok(obj && bt_value_is_integer(obj),
539 "bt_value_map_get() returns an value object with the appropriate type (integer)");
540 ret = bt_value_integer_get(obj, &int_value);
541 ok(!ret && int_value == 19457,
542 "bt_value_map_get() returns an value object with the appropriate value (integer)");
543 BT_PUT(obj);
544 obj = bt_value_map_get(map_obj, "null");
545 ok(obj && bt_value_is_null(obj),
546 "bt_value_map_get() returns an value object with the appropriate type (null)");
547 obj = bt_value_map_get(map_obj, "bt_bool");
548 ok(obj && bt_value_is_bool(obj),
549 "bt_value_map_get() returns an value object with the appropriate type (boolean)");
550 ret = bt_value_bool_get(obj, &bool_value);
551 ok(!ret && bool_value,
552 "bt_value_map_get() returns an value object with the appropriate value (boolean)");
553 BT_PUT(obj);
554
555 ret = bt_value_map_insert_bool(map_obj, "bool2", BT_TRUE);
556 ok(!ret, "bt_value_map_insert_bool() succeeds");
557 ret = bt_value_map_insert_integer(map_obj, "int2", 98765);
558 ok(!ret, "bt_value_map_insert_integer() succeeds");
559 ret = bt_value_map_insert_float(map_obj, "float2", -49.0001);
560 ok(!ret, "bt_value_map_insert_float() succeeds");
561 ret = bt_value_map_insert_string(map_obj, "string2", "bt_value");
562 ok(!ret, "bt_value_map_insert_string() succeeds");
563 ret = bt_value_map_insert_empty_array(map_obj, "array2");
564 ok(!ret, "bt_value_map_insert_empty_array() succeeds");
565 ret = bt_value_map_insert_empty_map(map_obj, "map2");
566 ok(!ret, "bt_value_map_insert_empty_map() succeeds");
567
568 ok(bt_value_map_size(map_obj) == 10,
569 "the bt_value_map_insert*() functions increment the map value object's size");
570
571 ok(!bt_value_map_has_key(map_obj, "hello"),
572 "map value object does not have key \"hello\"");
573 ok(bt_value_map_has_key(map_obj, "bt_bool"),
574 "map value object has key \"bt_bool\"");
575 ok(bt_value_map_has_key(map_obj, "int"),
576 "map value object has key \"int\"");
577 ok(bt_value_map_has_key(map_obj, "float"),
578 "map value object has key \"float\"");
579 ok(bt_value_map_has_key(map_obj, "null"),
580 "map value object has key \"null\"");
581 ok(bt_value_map_has_key(map_obj, "bool2"),
582 "map value object has key \"bool2\"");
583 ok(bt_value_map_has_key(map_obj, "int2"),
584 "map value object has key \"int2\"");
585 ok(bt_value_map_has_key(map_obj, "float2"),
586 "map value object has key \"float2\"");
587 ok(bt_value_map_has_key(map_obj, "string2"),
588 "map value object has key \"string2\"");
589 ok(bt_value_map_has_key(map_obj, "array2"),
590 "map value object has key \"array2\"");
591 ok(bt_value_map_has_key(map_obj, "map2"),
592 "map value object has key \"map2\"");
593
594 ret = bt_value_map_foreach(map_obj, test_map_foreach_cb_count, &count);
595 ok(ret == BT_VALUE_STATUS_CANCELED && count == 3,
596 "bt_value_map_foreach() breaks the loop when the user function returns BT_FALSE");
597
598 memset(&checklist, 0, sizeof(checklist));
599 ret = bt_value_map_foreach(map_obj, test_map_foreach_cb_check,
600 &checklist);
601 ok(ret == BT_VALUE_STATUS_OK,
602 "bt_value_map_foreach() succeeds with test_map_foreach_cb_check()");
603 ok(checklist.bool1 && checklist.int1 && checklist.float1 &&
604 checklist.null1 && checklist.bool2 && checklist.int2 &&
605 checklist.float2 && checklist.string2 &&
606 checklist.array2 && checklist.map2,
607 "bt_value_map_foreach() iterates over all the map value object's elements");
608
609 BT_PUT(map_obj);
610 pass("putting an existing map value object does not cause a crash")
611 }
612
613 static
614 void test_types(void)
615 {
616 test_null();
617 test_bool();
618 test_integer();
619 test_float();
620 test_string();
621 test_array();
622 test_map();
623 }
624
625 static
626 void test_compare_null(void)
627 {
628 ok(bt_value_compare(bt_value_null, bt_value_null),
629 "null value objects are equivalent");
630 }
631
632 static
633 void test_compare_bool(void)
634 {
635 struct bt_value *bool1 = bt_value_bool_create_init(BT_FALSE);
636 struct bt_value *bool2 = bt_value_bool_create_init(BT_TRUE);
637 struct bt_value *bool3 = bt_value_bool_create_init(BT_FALSE);
638
639 BT_ASSERT(bool1 && bool2 && bool3);
640 ok(!bt_value_compare(bt_value_null, bool1),
641 "cannot compare null value object and bt_bool value object");
642 ok(!bt_value_compare(bool1, bool2),
643 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
644 ok(bt_value_compare(bool1, bool3),
645 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
646
647 BT_PUT(bool1);
648 BT_PUT(bool2);
649 BT_PUT(bool3);
650 }
651
652 static
653 void test_compare_integer(void)
654 {
655 struct bt_value *int1 = bt_value_integer_create_init(10);
656 struct bt_value *int2 = bt_value_integer_create_init(-23);
657 struct bt_value *int3 = bt_value_integer_create_init(10);
658
659 BT_ASSERT(int1 && int2 && int3);
660 ok(!bt_value_compare(bt_value_null, int1),
661 "cannot compare null value object and integer value object");
662 ok(!bt_value_compare(int1, int2),
663 "integer value objects are not equivalent (10 and -23)");
664 ok(bt_value_compare(int1, int3),
665 "integer value objects are equivalent (10 and 10)");
666
667 BT_PUT(int1);
668 BT_PUT(int2);
669 BT_PUT(int3);
670 }
671
672 static
673 void test_compare_float(void)
674 {
675 struct bt_value *float1 = bt_value_float_create_init(17.38);
676 struct bt_value *float2 = bt_value_float_create_init(-14.23);
677 struct bt_value *float3 = bt_value_float_create_init(17.38);
678
679 BT_ASSERT(float1 && float2 && float3);
680
681 ok(!bt_value_compare(bt_value_null, float1),
682 "cannot compare null value object and floating point number value object");
683 ok(!bt_value_compare(float1, float2),
684 "floating point number value objects are not equivalent (17.38 and -14.23)");
685 ok(bt_value_compare(float1, float3),
686 "floating point number value objects are equivalent (17.38 and 17.38)");
687
688 BT_PUT(float1);
689 BT_PUT(float2);
690 BT_PUT(float3);
691 }
692
693 static
694 void test_compare_string(void)
695 {
696 struct bt_value *string1 = bt_value_string_create_init("hello");
697 struct bt_value *string2 = bt_value_string_create_init("bt_value");
698 struct bt_value *string3 = bt_value_string_create_init("hello");
699
700 BT_ASSERT(string1 && string2 && string3);
701
702 ok(!bt_value_compare(bt_value_null, string1),
703 "cannot compare null value object and string value object");
704 ok(!bt_value_compare(string1, string2),
705 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
706 ok(bt_value_compare(string1, string3),
707 "string value objects are equivalent (\"hello\" and \"hello\")");
708
709 BT_PUT(string1);
710 BT_PUT(string2);
711 BT_PUT(string3);
712 }
713
714 static
715 void test_compare_array(void)
716 {
717 struct bt_value *array1 = bt_value_array_create();
718 struct bt_value *array2 = bt_value_array_create();
719 struct bt_value *array3 = bt_value_array_create();
720
721 BT_ASSERT(array1 && array2 && array3);
722
723 ok(bt_value_compare(array1, array2),
724 "empty array value objects are equivalent");
725
726 BT_ASSERT(!bt_value_array_append_integer(array1, 23));
727 BT_ASSERT(!bt_value_array_append_float(array1, 14.2));
728 BT_ASSERT(!bt_value_array_append_bool(array1, BT_FALSE));
729 BT_ASSERT(!bt_value_array_append_float(array2, 14.2));
730 BT_ASSERT(!bt_value_array_append_integer(array2, 23));
731 BT_ASSERT(!bt_value_array_append_bool(array2, BT_FALSE));
732 BT_ASSERT(!bt_value_array_append_integer(array3, 23));
733 BT_ASSERT(!bt_value_array_append_float(array3, 14.2));
734 BT_ASSERT(!bt_value_array_append_bool(array3, BT_FALSE));
735 BT_ASSERT(bt_value_array_size(array1) == 3);
736 BT_ASSERT(bt_value_array_size(array2) == 3);
737 BT_ASSERT(bt_value_array_size(array3) == 3);
738
739 ok(!bt_value_compare(bt_value_null, array1),
740 "cannot compare null value object and array value object");
741 ok(!bt_value_compare(array1, array2),
742 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
743 ok(bt_value_compare(array1, array3),
744 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
745
746 BT_PUT(array1);
747 BT_PUT(array2);
748 BT_PUT(array3);
749 }
750
751 static
752 void test_compare_map(void)
753 {
754 struct bt_value *map1 = bt_value_map_create();
755 struct bt_value *map2 = bt_value_map_create();
756 struct bt_value *map3 = bt_value_map_create();
757
758 BT_ASSERT(map1 && map2 && map3);
759
760 ok(bt_value_compare(map1, map2),
761 "empty map value objects are equivalent");
762
763 BT_ASSERT(!bt_value_map_insert_integer(map1, "one", 23));
764 BT_ASSERT(!bt_value_map_insert_float(map1, "two", 14.2));
765 BT_ASSERT(!bt_value_map_insert_bool(map1, "three", BT_FALSE));
766 BT_ASSERT(!bt_value_map_insert_float(map2, "one", 14.2));
767 BT_ASSERT(!bt_value_map_insert_integer(map2, "two", 23));
768 BT_ASSERT(!bt_value_map_insert_bool(map2, "three", BT_FALSE));
769 BT_ASSERT(!bt_value_map_insert_bool(map3, "three", BT_FALSE));
770 BT_ASSERT(!bt_value_map_insert_integer(map3, "one", 23));
771 BT_ASSERT(!bt_value_map_insert_float(map3, "two", 14.2));
772 BT_ASSERT(bt_value_map_size(map1) == 3);
773 BT_ASSERT(bt_value_map_size(map2) == 3);
774 BT_ASSERT(bt_value_map_size(map3) == 3);
775
776 ok(!bt_value_compare(bt_value_null, map1),
777 "cannot compare null value object and map value object");
778 ok(!bt_value_compare(map1, map2),
779 "map value objects are not equivalent");
780 ok(bt_value_compare(map1, map3),
781 "map value objects are equivalent");
782
783 BT_PUT(map1);
784 BT_PUT(map2);
785 BT_PUT(map3);
786 }
787
788 static
789 void test_compare(void)
790 {
791 test_compare_null();
792 test_compare_bool();
793 test_compare_integer();
794 test_compare_float();
795 test_compare_string();
796 test_compare_array();
797 test_compare_map();
798 }
799
800 static
801 void test_copy(void)
802 {
803 /*
804 * Here's the deal here. If we make sure that each value object
805 * of our deep copy has a different address than its source,
806 * and that bt_value_compare() returns BT_TRUE for the top-level
807 * value object, taking into account that we test the correctness of
808 * bt_value_compare() elsewhere, then the deep copy is a
809 * success.
810 */
811 struct bt_value *null_copy_obj;
812 struct bt_value *bool_obj, *bool_copy_obj;
813 struct bt_value *integer_obj, *integer_copy_obj;
814 struct bt_value *float_obj, *float_copy_obj;
815 struct bt_value *string_obj, *string_copy_obj;
816 struct bt_value *array_obj, *array_copy_obj;
817 struct bt_value *map_obj, *map_copy_obj;
818
819 bool_obj = bt_value_bool_create_init(BT_TRUE);
820 integer_obj = bt_value_integer_create_init(23);
821 float_obj = bt_value_float_create_init(-3.1416);
822 string_obj = bt_value_string_create_init("test");
823 array_obj = bt_value_array_create();
824 map_obj = bt_value_map_create();
825
826 BT_ASSERT(bool_obj && integer_obj && float_obj && string_obj &&
827 array_obj && map_obj);
828
829 BT_ASSERT(!bt_value_array_append(array_obj, bool_obj));
830 BT_ASSERT(!bt_value_array_append(array_obj, integer_obj));
831 BT_ASSERT(!bt_value_array_append(array_obj, float_obj));
832 BT_ASSERT(!bt_value_array_append(array_obj, bt_value_null));
833 BT_ASSERT(!bt_value_map_insert(map_obj, "array", array_obj));
834 BT_ASSERT(!bt_value_map_insert(map_obj, "string", string_obj));
835
836 map_copy_obj = bt_value_copy(map_obj);
837 ok(map_copy_obj,
838 "bt_value_copy() succeeds");
839
840 ok(map_obj != map_copy_obj,
841 "bt_value_copy() returns a different pointer (map)");
842 string_copy_obj = bt_value_map_get(map_copy_obj, "string");
843 ok(string_copy_obj != string_obj,
844 "bt_value_copy() returns a different pointer (string)");
845 array_copy_obj = bt_value_map_get(map_copy_obj, "array");
846 ok(array_copy_obj != array_obj,
847 "bt_value_copy() returns a different pointer (array)");
848 bool_copy_obj = bt_value_array_get(array_copy_obj, 0);
849 ok(bool_copy_obj != bool_obj,
850 "bt_value_copy() returns a different pointer (bt_bool)");
851 integer_copy_obj = bt_value_array_get(array_copy_obj, 1);
852 ok(integer_copy_obj != integer_obj,
853 "bt_value_copy() returns a different pointer (integer)");
854 float_copy_obj = bt_value_array_get(array_copy_obj, 2);
855 ok(float_copy_obj != float_obj,
856 "bt_value_copy() returns a different pointer (float)");
857 null_copy_obj = bt_value_array_get(array_copy_obj, 3);
858 ok(null_copy_obj == bt_value_null,
859 "bt_value_copy() returns the same pointer (null)");
860
861 ok(bt_value_compare(map_obj, map_copy_obj),
862 "source and destination value objects have the same content");
863
864 BT_PUT(bool_copy_obj);
865 BT_PUT(integer_copy_obj);
866 BT_PUT(float_copy_obj);
867 BT_PUT(string_copy_obj);
868 BT_PUT(array_copy_obj);
869 BT_PUT(map_copy_obj);
870 BT_PUT(bool_obj);
871 BT_PUT(integer_obj);
872 BT_PUT(float_obj);
873 BT_PUT(string_obj);
874 BT_PUT(array_obj);
875 BT_PUT(map_obj);
876 }
877
878 static
879 bt_bool compare_map_elements(struct bt_value *map_a, struct bt_value *map_b,
880 const char *key)
881 {
882 struct bt_value *elem_a = NULL;
883 struct bt_value *elem_b = NULL;
884 bt_bool equal;
885
886 elem_a = bt_value_map_get(map_a, key);
887 elem_b = bt_value_map_get(map_b, key);
888 equal = bt_value_compare(elem_a, elem_b);
889 BT_PUT(elem_a);
890 BT_PUT(elem_b);
891
892 return equal;
893 }
894
895 static
896 void test_extend(void)
897 {
898 struct bt_value *base_map = bt_value_map_create();
899 struct bt_value *extension_map = bt_value_map_create();
900 struct bt_value *extended_map = NULL;
901 struct bt_value *array = bt_value_array_create();
902 enum bt_value_status status;
903
904 BT_ASSERT(base_map);
905 BT_ASSERT(extension_map);
906 BT_ASSERT(array);
907 status = bt_value_map_insert_bool(base_map, "file", BT_TRUE);
908 BT_ASSERT(status == BT_VALUE_STATUS_OK);
909 status = bt_value_map_insert_bool(base_map, "edit", BT_FALSE);
910 BT_ASSERT(status == BT_VALUE_STATUS_OK);
911 status = bt_value_map_insert_integer(base_map, "selection", 17);
912 BT_ASSERT(status == BT_VALUE_STATUS_OK);
913 status = bt_value_map_insert_integer(base_map, "find", -34);
914 BT_ASSERT(status == BT_VALUE_STATUS_OK);
915 status = bt_value_map_insert_bool(extension_map, "edit", BT_TRUE);
916 BT_ASSERT(status == BT_VALUE_STATUS_OK);
917 status = bt_value_map_insert_integer(extension_map, "find", 101);
918 BT_ASSERT(status == BT_VALUE_STATUS_OK);
919 status = bt_value_map_insert_float(extension_map, "project", -404);
920 BT_ASSERT(status == BT_VALUE_STATUS_OK);
921 extended_map = bt_value_map_extend(base_map, extension_map);
922 ok(extended_map, "bt_value_map_extend() succeeds");
923 ok(bt_value_map_size(extended_map) == 5,
924 "bt_value_map_extend() returns a map object with the correct size");
925 ok(compare_map_elements(base_map, extended_map, "file"),
926 "bt_value_map_extend() picks the appropriate element (file)");
927 ok(compare_map_elements(extension_map, extended_map, "edit"),
928 "bt_value_map_extend() picks the appropriate element (edit)");
929 ok(compare_map_elements(base_map, extended_map, "selection"),
930 "bt_value_map_extend() picks the appropriate element (selection)");
931 ok(compare_map_elements(extension_map, extended_map, "find"),
932 "bt_value_map_extend() picks the appropriate element (find)");
933 ok(compare_map_elements(extension_map, extended_map, "project"),
934 "bt_value_map_extend() picks the appropriate element (project)");
935
936 BT_PUT(array);
937 BT_PUT(base_map);
938 BT_PUT(extension_map);
939 BT_PUT(extended_map);
940 }
941
942 int main(void)
943 {
944 plan_tests(NR_TESTS);
945 test_types();
946 test_compare();
947 test_copy();
948 test_extend();
949 return 0;
950 }
This page took 0.050642 seconds and 5 git commands to generate.