d16c67a5b213244d2f7c6b09cf061669ed65e465
[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 <babeltrace2/babeltrace.h>
24 #include "common/assert.h"
25 #include <string.h>
26 #include "tap/tap.h"
27
28 #define NR_TESTS 166
29
30 static
31 void test_null(void)
32 {
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_value_get_ref(bt_value_null);
37 pass("getting bt_value_null does not cause a crash");
38 bt_value_put_ref(bt_value_null);
39 pass("putting bt_value_null does not cause a crash");
40 }
41
42 static
43 void test_bool(void)
44 {
45 bt_bool value;
46 bt_value *obj;
47
48 obj = bt_value_bool_create();
49 ok(obj && bt_value_is_bool(obj),
50 "bt_value_bool_create() returns a boolean value object");
51
52 value = BT_TRUE;
53 value = bt_value_bool_get(obj);
54 ok(!value, "default boolean value object value is BT_FALSE");
55
56 bt_value_bool_set(obj, BT_FALSE);
57 bt_value_bool_set(obj, BT_TRUE);
58 value = bt_value_bool_get(obj);
59 ok(value, "bt_value_bool_set() works");
60
61 BT_VALUE_PUT_REF_AND_RESET(obj);
62 pass("putting an existing boolean value object does not cause a crash")
63
64 value = BT_FALSE;
65 obj = bt_value_bool_create_init(BT_TRUE);
66 ok(obj && bt_value_is_bool(obj),
67 "bt_value_bool_create_init() returns a boolean value object");
68 value = bt_value_bool_get(obj);
69 ok(value,
70 "bt_value_bool_create_init() sets the appropriate initial value");
71
72 BT_VALUE_PUT_REF_AND_RESET(obj);
73 }
74
75 static
76 void test_unsigned_integer(void)
77 {
78 uint64_t value;
79 bt_value *obj;
80
81 obj = bt_value_unsigned_integer_create();
82 ok(obj && bt_value_is_unsigned_integer(obj),
83 "bt_value_unsigned_integer_create() returns an unsigned integer value object");
84
85 value = 1961;
86 value = bt_value_unsigned_integer_get(obj);
87 ok(value == 0, "default unsigned integer value object value is 0");
88
89 bt_value_unsigned_integer_set(obj, 98765);
90 value = bt_value_unsigned_integer_get(obj);
91 ok(value == 98765, "bt_value_unsigned_integer_bool_set() works");
92
93 BT_VALUE_PUT_REF_AND_RESET(obj);
94 pass("putting an existing unsigned integer value object does not cause a crash")
95
96 obj = bt_value_unsigned_integer_create_init(321456987);
97 ok(obj && bt_value_is_unsigned_integer(obj),
98 "bt_value_unsigned_integer_create_init() returns an unsigned integer value object");
99 value = bt_value_unsigned_integer_get(obj);
100 ok(value == 321456987,
101 "bt_value_unsigned_integer_create_init() sets the appropriate initial value");
102
103 BT_VALUE_PUT_REF_AND_RESET(obj);
104 }
105
106 static
107 void test_signed_integer(void)
108 {
109 int64_t value;
110 bt_value *obj;
111
112 obj = bt_value_signed_integer_create();
113 ok(obj && bt_value_is_signed_integer(obj),
114 "bt_value_signed_integer_create() returns a signed integer value object");
115
116 value = 1961;
117 value = bt_value_signed_integer_get(obj);
118 ok(value == 0, "default signed integer value object value is 0");
119
120 bt_value_signed_integer_set(obj, 98765);
121 value = bt_value_signed_integer_get(obj);
122 ok(value == 98765, "bt_value_signed_integer_bool_set() works");
123
124 BT_VALUE_PUT_REF_AND_RESET(obj);
125 pass("putting an existing signed integer value object does not cause a crash")
126
127 obj = bt_value_signed_integer_create_init(-321456987);
128 ok(obj && bt_value_is_signed_integer(obj),
129 "bt_value_signed_integer_create_init() returns a signed integer value object");
130 value = bt_value_signed_integer_get(obj);
131 ok(value == -321456987,
132 "bt_value_signed_integer_create_init() sets the appropriate initial value");
133
134 BT_VALUE_PUT_REF_AND_RESET(obj);
135 }
136
137 static
138 void test_real(void)
139 {
140 double value;
141 bt_value *obj;
142
143 obj = bt_value_real_create();
144 ok(obj && bt_value_is_real(obj),
145 "bt_value_real_create() returns a real number value object");
146
147 value = 17.34;
148 value = bt_value_real_get(obj);
149 ok(value == 0.,
150 "default real number value object value is 0");
151
152 bt_value_real_set(obj, -3.1416);
153 value = bt_value_real_get(obj);
154 ok(value == -3.1416, "bt_value_real_set() works");
155
156 BT_VALUE_PUT_REF_AND_RESET(obj);
157 pass("putting an existing real number value object does not cause a crash")
158
159 obj = bt_value_real_create_init(33.1649758);
160 ok(obj && bt_value_is_real(obj),
161 "bt_value_real_create_init() returns a real number value object");
162 value = bt_value_real_get(obj);
163 ok(value == 33.1649758,
164 "bt_value_real_create_init() sets the appropriate initial value");
165
166 BT_VALUE_PUT_REF_AND_RESET(obj);
167 }
168
169 static
170 void test_string(void)
171 {
172 const char *value;
173 bt_value *obj;
174
175 obj = bt_value_string_create();
176 ok(obj && bt_value_is_string(obj),
177 "bt_value_string_create() returns a string value object");
178
179 value = bt_value_string_get(obj);
180 ok(value && !strcmp(value, ""),
181 "default string value object value is \"\"");
182
183 bt_value_string_set(obj, "hello worldz");
184 value = bt_value_string_get(obj);
185 ok(value && !strcmp(value, "hello worldz"),
186 "bt_value_string_get() works");
187
188 BT_VALUE_PUT_REF_AND_RESET(obj);
189 pass("putting an existing string value object does not cause a crash")
190
191 obj = bt_value_string_create_init("initial value");
192 ok(obj && bt_value_is_string(obj),
193 "bt_value_string_create_init() returns a string value object");
194 value = bt_value_string_get(obj);
195 ok(value && !strcmp(value, "initial value"),
196 "bt_value_string_create_init() sets the appropriate initial value");
197
198 BT_VALUE_PUT_REF_AND_RESET(obj);
199 }
200
201 static
202 void test_array(void)
203 {
204 int ret;
205 bt_bool bool_value;
206 int64_t int_value;
207 double real_value;
208 bt_value *obj;
209 const char *string_value;
210 bt_value *array_obj;
211
212 array_obj = bt_value_array_create();
213 ok(array_obj && bt_value_is_array(array_obj),
214 "bt_value_array_create() returns an array value object");
215 ok(bt_value_array_is_empty(array_obj),
216 "initial array value object size is 0");
217
218 obj = bt_value_unsigned_integer_create_init(345);
219 ret = bt_value_array_append_element(array_obj, obj);
220 BT_VALUE_PUT_REF_AND_RESET(obj);
221 obj = bt_value_signed_integer_create_init(-507);
222 ret |= bt_value_array_append_element(array_obj, obj);
223 BT_VALUE_PUT_REF_AND_RESET(obj);
224 obj = bt_value_real_create_init(-17.45);
225 ret |= bt_value_array_append_element(array_obj, obj);
226 BT_VALUE_PUT_REF_AND_RESET(obj);
227 obj = bt_value_bool_create_init(BT_TRUE);
228 ret |= bt_value_array_append_element(array_obj, obj);
229 BT_VALUE_PUT_REF_AND_RESET(obj);
230 ret |= bt_value_array_append_element(array_obj,
231 bt_value_null);
232 ok(!ret, "bt_value_array_append_element() succeeds");
233 ok(bt_value_array_get_size(array_obj) == 5,
234 "appending an element to an array value object increment its size");
235
236 obj = bt_value_array_borrow_element_by_index(array_obj, 0);
237 ok(obj && bt_value_is_unsigned_integer(obj),
238 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (unsigned integer)");
239 int_value = bt_value_unsigned_integer_get(obj);
240 ok(int_value == 345,
241 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (unsigned integer)");
242 obj = bt_value_array_borrow_element_by_index(array_obj, 1);
243 ok(obj && bt_value_is_signed_integer(obj),
244 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (signed integer)");
245 int_value = bt_value_signed_integer_get(obj);
246 ok(int_value == -507,
247 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (signed integer)");
248 obj = bt_value_array_borrow_element_by_index(array_obj, 2);
249 ok(obj && bt_value_is_real(obj),
250 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
251 real_value = bt_value_real_get(obj);
252 ok(real_value == -17.45,
253 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (real number)");
254 obj = bt_value_array_borrow_element_by_index(array_obj, 3);
255 ok(obj && bt_value_is_bool(obj),
256 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
257 bool_value = bt_value_bool_get(obj);
258 ok(bool_value,
259 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate value (boolean)");
260 obj = bt_value_array_borrow_element_by_index(array_obj, 4);
261 ok(obj == bt_value_null,
262 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
263
264 obj = bt_value_signed_integer_create_init(1001);
265 BT_ASSERT(obj);
266 ok(!bt_value_array_set_element_by_index(array_obj, 2, obj),
267 "bt_value_array_set_element_by_index() succeeds");
268 BT_VALUE_PUT_REF_AND_RESET(obj);
269 obj = bt_value_array_borrow_element_by_index(array_obj, 2);
270 ok(obj && bt_value_is_signed_integer(obj),
271 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
272 int_value = bt_value_signed_integer_get(obj);
273 BT_ASSERT(!ret);
274 ok(int_value == 1001,
275 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
276
277 ret = bt_value_array_append_bool_element(array_obj,
278 BT_FALSE);
279 ok(!ret, "bt_value_array_append_bool_element() succeeds");
280 ret = bt_value_array_append_unsigned_integer_element(array_obj,
281 98765);
282 ok(!ret, "bt_value_array_append_unsigned_integer_element() succeeds");
283 ret = bt_value_array_append_signed_integer_element(array_obj,
284 -10101);
285 ok(!ret, "bt_value_array_append_signed_integer_element() succeeds");
286 ret = bt_value_array_append_real_element(array_obj,
287 2.49578);
288 ok(!ret, "bt_value_array_append_real_element() succeeds");
289 ret = bt_value_array_append_string_element(array_obj,
290 "bt_value");
291 ok(!ret, "bt_value_array_append_string_element() succeeds");
292 ret = bt_value_array_append_empty_array_element(array_obj);
293 ok(!ret, "bt_value_array_append_empty_array_element() succeeds");
294 ret = bt_value_array_append_empty_map_element(array_obj);
295 ok(!ret, "bt_value_array_append_empty_map_element() succeeds");
296
297 ok(bt_value_array_get_size(array_obj) == 12,
298 "the bt_value_array_append_element_*() functions increment the array value object's size");
299 ok(!bt_value_array_is_empty(array_obj),
300 "map value object is not empty");
301
302 obj = bt_value_array_borrow_element_by_index(array_obj, 5);
303 ok(obj && bt_value_is_bool(obj),
304 "bt_value_array_append_bool_element() appends a boolean value object");
305 bool_value = bt_value_bool_get(obj);
306 ok(!bool_value,
307 "bt_value_array_append_bool_element() appends the appropriate value");
308 obj = bt_value_array_borrow_element_by_index(array_obj, 6);
309 ok(obj && bt_value_is_unsigned_integer(obj),
310 "bt_value_array_append_unsigned_integer_element() appends an unsigned integer value object");
311 int_value = bt_value_unsigned_integer_get(obj);
312 ok(int_value == 98765,
313 "bt_value_array_append_unsigned_integer_element() appends the appropriate value");
314 obj = bt_value_array_borrow_element_by_index(array_obj, 7);
315 ok(obj && bt_value_is_signed_integer(obj),
316 "bt_value_array_append_signed_integer_element() appends a signed integer value object");
317 int_value = bt_value_signed_integer_get(obj);
318 ok(int_value == -10101,
319 "bt_value_array_append_signed_integer_element() appends the appropriate value");
320 obj = bt_value_array_borrow_element_by_index(array_obj, 8);
321 ok(obj && bt_value_is_real(obj),
322 "bt_value_array_append_real_element() appends a real number value object");
323 real_value = bt_value_real_get(obj);
324 ok(real_value == 2.49578,
325 "bt_value_array_append_real_element() appends the appropriate value");
326 obj = bt_value_array_borrow_element_by_index(array_obj, 9);
327 ok(obj && bt_value_is_string(obj),
328 "bt_value_array_append_string_element() appends a string value object");
329 string_value = bt_value_string_get(obj);
330 ok(!ret && string_value && !strcmp(string_value, "bt_value"),
331 "bt_value_array_append_string_element() appends the appropriate value");
332 obj = bt_value_array_borrow_element_by_index(array_obj, 10);
333 ok(obj && bt_value_is_array(obj),
334 "bt_value_array_append_empty_array_element() appends an array value object");
335 ok(bt_value_array_is_empty(obj),
336 "bt_value_array_append_empty_array_element() an empty array value object");
337 obj = bt_value_array_borrow_element_by_index(array_obj, 11);
338 ok(obj && bt_value_is_map(obj),
339 "bt_value_array_append_empty_map_element() appends a map value object");
340 ok(bt_value_map_is_empty(obj),
341 "bt_value_array_append_empty_map_element() an empty map value object");
342
343 BT_VALUE_PUT_REF_AND_RESET(array_obj);
344 pass("putting an existing array value object does not cause a crash")
345 }
346
347 static
348 bt_bool test_map_foreach_cb_count(const char *key, bt_value *object,
349 void *data)
350 {
351 int *count = data;
352
353 if (*count == 3) {
354 return BT_FALSE;
355 }
356
357 (*count)++;
358
359 return BT_TRUE;
360 }
361
362 struct map_foreach_checklist {
363 bt_bool bool1;
364 bt_bool uint;
365 bt_bool int1;
366 bt_bool real1;
367 bt_bool null1;
368 bt_bool bool2;
369 bt_bool int2;
370 bt_bool real2;
371 bt_bool string2;
372 bt_bool array2;
373 bt_bool map2;
374 };
375
376 static
377 bt_bool test_map_foreach_cb_check(const char *key, bt_value *object,
378 void *data)
379 {
380 struct map_foreach_checklist *checklist = data;
381
382 if (!strcmp(key, "bt_bool")) {
383 if (checklist->bool1) {
384 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
385 } else {
386 bt_bool val = BT_FALSE;
387
388 val = bt_value_bool_get(object);
389
390 if (val) {
391 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
392 checklist->bool1 = BT_TRUE;
393 } else {
394 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
395 }
396 }
397 } else if (!strcmp(key, "uint")) {
398 if (checklist->uint) {
399 fail("test_map_foreach_cb_check(): duplicate key \"uint\"");
400 } else {
401 uint64_t val = 0;
402
403 val = bt_value_unsigned_integer_get(object);
404
405 if (val == 19457) {
406 pass("test_map_foreach_cb_check(): \"uint\" value object has the right value");
407 checklist->uint = BT_TRUE;
408 } else {
409 fail("test_map_foreach_cb_check(): \"uint\" value object has the wrong value");
410 }
411 }
412 } else if (!strcmp(key, "int")) {
413 if (checklist->int1) {
414 fail("test_map_foreach_cb_check(): duplicate key \"int\"");
415 } else {
416 int64_t val = 0;
417
418 val = bt_value_signed_integer_get(object);
419
420 if (val == -12345) {
421 pass("test_map_foreach_cb_check(): \"int\" value object has the right value");
422 checklist->int1 = BT_TRUE;
423 } else {
424 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
425 }
426 }
427 } else if (!strcmp(key, "real")) {
428 if (checklist->real1) {
429 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
430 } else {
431 double val = 0;
432
433 val = bt_value_real_get(object);
434
435 if (val == 5.444) {
436 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
437 checklist->real1 = BT_TRUE;
438 } else {
439 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
440 }
441 }
442 } else if (!strcmp(key, "null")) {
443 if (checklist->null1) {
444 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
445 } else {
446 ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object");
447 checklist->null1 = BT_TRUE;
448 }
449 } else if (!strcmp(key, "bool2")) {
450 if (checklist->bool2) {
451 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
452 } else {
453 bt_bool val = BT_FALSE;
454
455 val = bt_value_bool_get(object);
456
457 if (val) {
458 pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value");
459 checklist->bool2 = BT_TRUE;
460 } else {
461 fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value");
462 }
463 }
464 } else if (!strcmp(key, "int2")) {
465 if (checklist->int2) {
466 fail("test_map_foreach_cb_check(): duplicate key \"int2\"");
467 } else {
468 int64_t val = 0;
469
470 val = bt_value_signed_integer_get(object);
471
472 if (val == 98765) {
473 pass("test_map_foreach_cb_check(): \"int2\" value object has the right value");
474 checklist->int2 = BT_TRUE;
475 } else {
476 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
477 }
478 }
479 } else if (!strcmp(key, "real2")) {
480 if (checklist->real2) {
481 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
482 } else {
483 double val = 0;
484
485 val = bt_value_real_get(object);
486
487 if (val == -49.0001) {
488 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
489 checklist->real2 = BT_TRUE;
490 } else {
491 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
492 }
493 }
494 } else if (!strcmp(key, "string2")) {
495 if (checklist->string2) {
496 fail("test_map_foreach_cb_check(): duplicate key \"string2\"");
497 } else {
498 const char *val;
499
500 val = bt_value_string_get(object);
501
502 if (val && !strcmp(val, "bt_value")) {
503 pass("test_map_foreach_cb_check(): \"string2\" value object has the right value");
504 checklist->string2 = BT_TRUE;
505 } else {
506 fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value");
507 }
508 }
509 } else if (!strcmp(key, "array2")) {
510 if (checklist->array2) {
511 fail("test_map_foreach_cb_check(): duplicate key \"array2\"");
512 } else {
513 ok(bt_value_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" value object");
514 ok(bt_value_array_is_empty(object),
515 "test_map_foreach_cb_check(): \"array2\" value object is empty");
516 checklist->array2 = BT_TRUE;
517 }
518 } else if (!strcmp(key, "map2")) {
519 if (checklist->map2) {
520 fail("test_map_foreach_cb_check(): duplicate key \"map2\"");
521 } else {
522 ok(bt_value_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" value object");
523 ok(bt_value_map_is_empty(object),
524 "test_map_foreach_cb_check(): \"map2\" value object is empty");
525 checklist->map2 = BT_TRUE;
526 }
527 } else {
528 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
529 key);
530 }
531
532 return BT_TRUE;
533 }
534
535 static
536 void test_map(void)
537 {
538 int ret;
539 int count = 0;
540 bt_bool bool_value;
541 int64_t int_value;
542 double real_value;
543 bt_value *obj;
544 bt_value *map_obj;
545 struct map_foreach_checklist checklist;
546
547 map_obj = bt_value_map_create();
548 ok(map_obj && bt_value_is_map(map_obj),
549 "bt_value_map_create() returns a map value object");
550 ok(bt_value_map_get_size(map_obj) == 0,
551 "initial map value object size is 0");
552
553 obj = bt_value_unsigned_integer_create_init(19457);
554 ret = bt_value_map_insert_entry(map_obj, "uint", obj);
555 BT_VALUE_PUT_REF_AND_RESET(obj);
556 obj = bt_value_signed_integer_create_init(-12345);
557 ret |= bt_value_map_insert_entry(map_obj, "int", obj);
558 BT_VALUE_PUT_REF_AND_RESET(obj);
559 obj = bt_value_real_create_init(5.444);
560 ret |= bt_value_map_insert_entry(map_obj, "real", obj);
561 BT_VALUE_PUT_REF_AND_RESET(obj);
562 obj = bt_value_bool_create();
563 ret |= bt_value_map_insert_entry(map_obj, "bt_bool", obj);
564 BT_VALUE_PUT_REF_AND_RESET(obj);
565 ret |= bt_value_map_insert_entry(map_obj, "null",
566 bt_value_null);
567 ok(!ret, "bt_value_map_insert_entry() succeeds");
568 ok(bt_value_map_get_size(map_obj) == 5,
569 "inserting an element into a map value object increment its size");
570
571 obj = bt_value_bool_create_init(BT_TRUE);
572 ret = bt_value_map_insert_entry(map_obj, "bt_bool", obj);
573 BT_VALUE_PUT_REF_AND_RESET(obj);
574 ok(!ret, "bt_value_map_insert_entry() accepts an existing key");
575
576 obj = bt_value_map_borrow_entry_value(map_obj, "life");
577 ok(!obj, "bt_value_map_borrow_entry_value() returns NULL with an non existing key");
578 obj = bt_value_map_borrow_entry_value(map_obj, "real");
579 ok(obj && bt_value_is_real(obj),
580 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
581 real_value = bt_value_real_get(obj);
582 ok(real_value == 5.444,
583 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)");
584 obj = bt_value_map_borrow_entry_value(map_obj, "uint");
585 ok(obj && bt_value_is_unsigned_integer(obj),
586 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (unsigned integer)");
587 int_value = bt_value_unsigned_integer_get(obj);
588 ok(int_value == 19457,
589 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (unsigned integer)");
590 obj = bt_value_map_borrow_entry_value(map_obj, "int");
591 ok(obj && bt_value_is_signed_integer(obj),
592 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (signed integer)");
593 int_value = bt_value_signed_integer_get(obj);
594 ok(int_value == -12345,
595 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (signed integer)");
596 obj = bt_value_map_borrow_entry_value(map_obj, "null");
597 ok(obj && bt_value_is_null(obj),
598 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)");
599 obj = bt_value_map_borrow_entry_value(map_obj, "bt_bool");
600 ok(obj && bt_value_is_bool(obj),
601 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
602 bool_value = bt_value_bool_get(obj);
603 ok(bool_value,
604 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
605
606 ret = bt_value_map_insert_bool_entry(map_obj, "bool2",
607 BT_TRUE);
608 ok(!ret, "bt_value_map_insert_bool_entry() succeeds");
609 ret = bt_value_map_insert_signed_integer_entry(map_obj, "int2",
610 98765);
611 ok(!ret, "bt_value_map_insert_signed_integer_entry() succeeds");
612 ret = bt_value_map_insert_real_entry(map_obj, "real2",
613 -49.0001);
614 ok(!ret, "bt_value_map_insert_real_entry() succeeds");
615 ret = bt_value_map_insert_string_entry(map_obj, "string2",
616 "bt_value");
617 ok(!ret, "bt_value_map_insert_string_entry() succeeds");
618 ret = bt_value_map_insert_empty_array_entry(map_obj,
619 "array2");
620 ok(!ret, "bt_value_map_insert_empty_array_entry() succeeds");
621 ret = bt_value_map_insert_empty_map_entry(map_obj, "map2");
622 ok(!ret, "bt_value_map_insert_empty_map_entry() succeeds");
623
624 ok(bt_value_map_get_size(map_obj) == 11,
625 "the bt_value_map_insert*() functions increment the map value object's size");
626
627 ok(!bt_value_map_has_entry(map_obj, "hello"),
628 "map value object does not have key \"hello\"");
629 ok(bt_value_map_has_entry(map_obj, "bt_bool"),
630 "map value object has key \"bt_bool\"");
631 ok(bt_value_map_has_entry(map_obj, "uint"),
632 "map value object has key \"uint\"");
633 ok(bt_value_map_has_entry(map_obj, "int"),
634 "map value object has key \"int\"");
635 ok(bt_value_map_has_entry(map_obj, "real"),
636 "map value object has key \"real\"");
637 ok(bt_value_map_has_entry(map_obj, "null"),
638 "map value object has key \"null\"");
639 ok(bt_value_map_has_entry(map_obj, "bool2"),
640 "map value object has key \"bool2\"");
641 ok(bt_value_map_has_entry(map_obj, "int2"),
642 "map value object has key \"int2\"");
643 ok(bt_value_map_has_entry(map_obj, "real2"),
644 "map value object has key \"real2\"");
645 ok(bt_value_map_has_entry(map_obj, "string2"),
646 "map value object has key \"string2\"");
647 ok(bt_value_map_has_entry(map_obj, "array2"),
648 "map value object has key \"array2\"");
649 ok(bt_value_map_has_entry(map_obj, "map2"),
650 "map value object has key \"map2\"");
651
652 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count,
653 &count);
654 ok(ret == BT_VALUE_STATUS_CANCELED && count == 3,
655 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
656
657 memset(&checklist, 0, sizeof(checklist));
658 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_check,
659 &checklist);
660 ok(ret == BT_VALUE_STATUS_OK,
661 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
662 ok(checklist.bool1 && checklist.uint && checklist.int1 &&
663 checklist.real1 && checklist.null1 && checklist.bool2 &&
664 checklist.int2 && checklist.real2 && checklist.string2 &&
665 checklist.array2 && checklist.map2,
666 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
667
668 BT_VALUE_PUT_REF_AND_RESET(map_obj);
669 pass("putting an existing map value object does not cause a crash")
670 }
671
672 static
673 void test_types(void)
674 {
675 test_null();
676 test_bool();
677 test_unsigned_integer();
678 test_signed_integer();
679 test_real();
680 test_string();
681 test_array();
682 test_map();
683 }
684
685 static
686 void test_compare_null(void)
687 {
688 ok(bt_value_compare(bt_value_null, bt_value_null),
689 "null value objects are equivalent");
690 }
691
692 static
693 void test_compare_bool(void)
694 {
695 bt_value *bool1 =
696 bt_value_bool_create_init(BT_FALSE);
697 bt_value *bool2 =
698 bt_value_bool_create_init(BT_TRUE);
699 bt_value *bool3 =
700 bt_value_bool_create_init(BT_FALSE);
701
702 BT_ASSERT(bool1 && bool2 && bool3);
703 ok(!bt_value_compare(bt_value_null,
704 bool1),
705 "cannot compare null value object and bt_bool value object");
706 ok(!bt_value_compare(bool1,
707 bool2),
708 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
709 ok(bt_value_compare(bool1,
710 bool3),
711 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
712
713 BT_VALUE_PUT_REF_AND_RESET(bool1);
714 BT_VALUE_PUT_REF_AND_RESET(bool2);
715 BT_VALUE_PUT_REF_AND_RESET(bool3);
716 }
717
718 static
719 void test_compare_unsigned_integer(void)
720 {
721 bt_value *int1 =
722 bt_value_unsigned_integer_create_init(10);
723 bt_value *int2 =
724 bt_value_unsigned_integer_create_init(23);
725 bt_value *int3 =
726 bt_value_unsigned_integer_create_init(10);
727
728 BT_ASSERT(int1 && int2 && int3);
729 ok(!bt_value_compare(bt_value_null,
730 int1),
731 "cannot compare null value object and unsigned integer value object");
732 ok(!bt_value_compare(int1, int2),
733 "unsigned integer value objects are not equivalent (10 and 23)");
734 ok(bt_value_compare(int1, int3),
735 "unsigned integer value objects are equivalent (10 and 10)");
736
737 BT_VALUE_PUT_REF_AND_RESET(int1);
738 BT_VALUE_PUT_REF_AND_RESET(int2);
739 BT_VALUE_PUT_REF_AND_RESET(int3);
740 }
741
742 void test_compare_signed_integer(void)
743 {
744 bt_value *int1 =
745 bt_value_signed_integer_create_init(10);
746 bt_value *int2 =
747 bt_value_signed_integer_create_init(-23);
748 bt_value *int3 =
749 bt_value_signed_integer_create_init(10);
750
751 BT_ASSERT(int1 && int2 && int3);
752 ok(!bt_value_compare(bt_value_null,
753 int1),
754 "cannot compare null value object and signed integer value object");
755 ok(!bt_value_compare(int1, int2),
756 "signed integer value objects are not equivalent (10 and -23)");
757 ok(bt_value_compare(int1, int3),
758 "signed integer value objects are equivalent (10 and 10)");
759
760 BT_VALUE_PUT_REF_AND_RESET(int1);
761 BT_VALUE_PUT_REF_AND_RESET(int2);
762 BT_VALUE_PUT_REF_AND_RESET(int3);
763 }
764
765 static
766 void test_compare_real(void)
767 {
768 bt_value *real1 =
769 bt_value_real_create_init(17.38);
770 bt_value *real2 =
771 bt_value_real_create_init(-14.23);
772 bt_value *real3 =
773 bt_value_real_create_init(17.38);
774
775 BT_ASSERT(real1 && real2 && real3);
776
777 ok(!bt_value_compare(bt_value_null,
778 real1),
779 "cannot compare null value object and real number value object");
780 ok(!bt_value_compare(real1,
781 real2),
782 "real number value objects are not equivalent (17.38 and -14.23)");
783 ok(bt_value_compare(real1,
784 real3),
785 "real number value objects are equivalent (17.38 and 17.38)");
786
787 BT_VALUE_PUT_REF_AND_RESET(real1);
788 BT_VALUE_PUT_REF_AND_RESET(real2);
789 BT_VALUE_PUT_REF_AND_RESET(real3);
790 }
791
792 static
793 void test_compare_string(void)
794 {
795 bt_value *string1 =
796 bt_value_string_create_init("hello");
797 bt_value *string2 =
798 bt_value_string_create_init("bt_value");
799 bt_value *string3 =
800 bt_value_string_create_init("hello");
801
802 BT_ASSERT(string1 && string2 && string3);
803
804 ok(!bt_value_compare(bt_value_null,
805 string1),
806 "cannot compare null value object and string value object");
807 ok(!bt_value_compare(string1,
808 string2),
809 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
810 ok(bt_value_compare(string1,
811 string3),
812 "string value objects are equivalent (\"hello\" and \"hello\")");
813
814 BT_VALUE_PUT_REF_AND_RESET(string1);
815 BT_VALUE_PUT_REF_AND_RESET(string2);
816 BT_VALUE_PUT_REF_AND_RESET(string3);
817 }
818
819 static
820 void test_compare_array(void)
821 {
822 bt_value *array1 = bt_value_array_create();
823 bt_value *array2 = bt_value_array_create();
824 bt_value *array3 = bt_value_array_create();
825 bt_value_status status;
826
827 BT_ASSERT(array1 && array2 && array3);
828
829 ok(bt_value_compare(array1, array2),
830 "empty array value objects are equivalent");
831
832 status = bt_value_array_append_signed_integer_element(array1, 23);
833 BT_ASSERT(status == BT_VALUE_STATUS_OK);
834 status = bt_value_array_append_real_element(array1, 14.2);
835 BT_ASSERT(status == BT_VALUE_STATUS_OK);
836 status = bt_value_array_append_bool_element(array1, BT_FALSE);
837 BT_ASSERT(status == BT_VALUE_STATUS_OK);
838 status = bt_value_array_append_real_element(array2, 14.2);
839 BT_ASSERT(status == BT_VALUE_STATUS_OK);
840 status = bt_value_array_append_signed_integer_element(array2, 23);
841 BT_ASSERT(status == BT_VALUE_STATUS_OK);
842 status = bt_value_array_append_bool_element(array2, BT_FALSE);
843 BT_ASSERT(status == BT_VALUE_STATUS_OK);
844 status = bt_value_array_append_signed_integer_element(array3, 23);
845 BT_ASSERT(status == BT_VALUE_STATUS_OK);
846 status = bt_value_array_append_real_element(array3, 14.2);
847 BT_ASSERT(status == BT_VALUE_STATUS_OK);
848 status = bt_value_array_append_bool_element(array3, BT_FALSE);
849 BT_ASSERT(status == BT_VALUE_STATUS_OK);
850 BT_ASSERT(bt_value_array_get_size(array1) == 3);
851 BT_ASSERT(bt_value_array_get_size(array2) == 3);
852 BT_ASSERT(bt_value_array_get_size(array3) == 3);
853
854 ok(!bt_value_compare(bt_value_null,
855 array1),
856 "cannot compare null value object and array value object");
857 ok(!bt_value_compare(array1,
858 array2),
859 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
860 ok(bt_value_compare(array1,
861 array3),
862 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
863
864 BT_VALUE_PUT_REF_AND_RESET(array1);
865 BT_VALUE_PUT_REF_AND_RESET(array2);
866 BT_VALUE_PUT_REF_AND_RESET(array3);
867 }
868
869 static
870 void test_compare_map(void)
871 {
872 bt_value *map1 = bt_value_map_create();
873 bt_value *map2 = bt_value_map_create();
874 bt_value *map3 = bt_value_map_create();
875 bt_value_status status;
876
877 BT_ASSERT(map1 && map2 && map3);
878
879 ok(bt_value_compare(map1,
880 map2),
881 "empty map value objects are equivalent");
882
883
884 status = bt_value_map_insert_signed_integer_entry(map1, "one", 23);
885 BT_ASSERT(status == BT_VALUE_STATUS_OK);
886 status = bt_value_map_insert_real_entry(map1, "two", 14.2);
887 BT_ASSERT(status == BT_VALUE_STATUS_OK);
888 status = bt_value_map_insert_bool_entry(map1, "three",
889 BT_FALSE);
890 BT_ASSERT(status == BT_VALUE_STATUS_OK);
891 status = bt_value_map_insert_real_entry(map2, "one", 14.2);
892 BT_ASSERT(status == BT_VALUE_STATUS_OK);
893 status = bt_value_map_insert_signed_integer_entry(map2, "two", 23);
894 BT_ASSERT(status == BT_VALUE_STATUS_OK);
895 status = bt_value_map_insert_bool_entry(map2, "three",
896 BT_FALSE);
897 BT_ASSERT(status == BT_VALUE_STATUS_OK);
898 status = bt_value_map_insert_bool_entry(map3, "three",
899 BT_FALSE);
900 BT_ASSERT(status == BT_VALUE_STATUS_OK);
901 status = bt_value_map_insert_signed_integer_entry(map3, "one", 23);
902 BT_ASSERT(status == BT_VALUE_STATUS_OK);
903 status = bt_value_map_insert_real_entry(map3, "two", 14.2);
904 BT_ASSERT(status == BT_VALUE_STATUS_OK);
905 BT_ASSERT(bt_value_map_get_size(map1) == 3);
906 BT_ASSERT(bt_value_map_get_size(map2) == 3);
907 BT_ASSERT(bt_value_map_get_size(map3) == 3);
908
909 ok(!bt_value_compare(bt_value_null,
910 map1),
911 "cannot compare null value object and map value object");
912 ok(!bt_value_compare(map1,
913 map2),
914 "map value objects are not equivalent");
915 ok(bt_value_compare(map1,
916 map3),
917 "map value objects are equivalent");
918
919 BT_VALUE_PUT_REF_AND_RESET(map1);
920 BT_VALUE_PUT_REF_AND_RESET(map2);
921 BT_VALUE_PUT_REF_AND_RESET(map3);
922 }
923
924 static
925 void test_compare(void)
926 {
927 test_compare_null();
928 test_compare_bool();
929 test_compare_unsigned_integer();
930 test_compare_signed_integer();
931 test_compare_real();
932 test_compare_string();
933 test_compare_array();
934 test_compare_map();
935 }
936
937 static
938 void test_copy(void)
939 {
940 /*
941 * Here's the deal here. If we make sure that each value object
942 * of our deep copy has a different address than its source, and
943 * that bt_value_compare() returns BT_TRUE for the top-level
944 * value object, taking into account that we test the
945 * correctness of bt_value_compare() elsewhere, then the deep
946 * copy is a success.
947 */
948 bt_value *null_copy_obj;
949 bt_value *bool_obj, *bool_copy_obj;
950 bt_value *unsigned_integer_obj, *unsigned_integer_copy_obj;
951 bt_value *signed_integer_obj, *signed_integer_copy_obj;
952 bt_value *real_obj, *real_copy_obj;
953 bt_value *string_obj, *string_copy_obj;
954 bt_value *array_obj, *array_copy_obj;
955 bt_value *map_obj, *map_copy_obj;
956 bt_value_status status;
957
958 bool_obj = bt_value_bool_create_init(BT_TRUE);
959 unsigned_integer_obj = bt_value_unsigned_integer_create_init(23);
960 signed_integer_obj = bt_value_signed_integer_create_init(-47);
961 real_obj = bt_value_real_create_init(-3.1416);
962 string_obj = bt_value_string_create_init("test");
963 array_obj = bt_value_array_create();
964 map_obj = bt_value_map_create();
965
966 BT_ASSERT(bool_obj && unsigned_integer_obj && signed_integer_obj &&
967 real_obj && string_obj && array_obj && map_obj);
968
969 status = bt_value_array_append_element(array_obj, bool_obj);
970 BT_ASSERT(status == BT_VALUE_STATUS_OK);
971 status = bt_value_array_append_element(array_obj, unsigned_integer_obj);
972 BT_ASSERT(status == BT_VALUE_STATUS_OK);
973 status = bt_value_array_append_element(array_obj, signed_integer_obj);
974 BT_ASSERT(status == BT_VALUE_STATUS_OK);
975 status = bt_value_array_append_element(array_obj, real_obj);
976 BT_ASSERT(status == BT_VALUE_STATUS_OK);
977 status = bt_value_array_append_element(array_obj, bt_value_null);
978 BT_ASSERT(status == BT_VALUE_STATUS_OK);
979 status = bt_value_map_insert_entry(map_obj, "array", array_obj);
980 BT_ASSERT(status == BT_VALUE_STATUS_OK);
981 status = bt_value_map_insert_entry(map_obj, "string", string_obj);
982 BT_ASSERT(status == BT_VALUE_STATUS_OK);
983
984 status = bt_value_copy(map_obj, &map_copy_obj);
985 ok(status == BT_VALUE_STATUS_OK && map_copy_obj,
986 "bt_value_copy() succeeds");
987
988 ok(map_obj != map_copy_obj,
989 "bt_value_copy() returns a different pointer (map)");
990 string_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj,
991 "string");
992 ok(string_copy_obj != string_obj,
993 "bt_value_copy() returns a different pointer (string)");
994 array_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj,
995 "array");
996 ok(array_copy_obj != array_obj,
997 "bt_value_copy() returns a different pointer (array)");
998 bool_copy_obj = bt_value_array_borrow_element_by_index(
999 array_copy_obj, 0);
1000 ok(bool_copy_obj != bool_obj,
1001 "bt_value_copy() returns a different pointer (bool)");
1002 unsigned_integer_copy_obj = bt_value_array_borrow_element_by_index(
1003 array_copy_obj, 1);
1004 ok(unsigned_integer_copy_obj != unsigned_integer_obj,
1005 "bt_value_copy() returns a different pointer (unsigned integer)");
1006 signed_integer_copy_obj = bt_value_array_borrow_element_by_index(
1007 array_copy_obj, 2);
1008 ok(signed_integer_copy_obj != signed_integer_obj,
1009 "bt_value_copy() returns a different pointer (signed integer)");
1010 real_copy_obj = bt_value_array_borrow_element_by_index(
1011 array_copy_obj, 3);
1012 ok(real_copy_obj != real_obj,
1013 "bt_value_copy() returns a different pointer (real)");
1014 null_copy_obj = bt_value_array_borrow_element_by_index(
1015 array_copy_obj, 4);
1016 ok(null_copy_obj == bt_value_null,
1017 "bt_value_copy() returns the same pointer (null)");
1018
1019 ok(bt_value_compare(map_obj, map_copy_obj),
1020 "source and destination value objects have the same content");
1021
1022 BT_VALUE_PUT_REF_AND_RESET(map_copy_obj);
1023 BT_VALUE_PUT_REF_AND_RESET(bool_obj);
1024 BT_VALUE_PUT_REF_AND_RESET(unsigned_integer_obj);
1025 BT_VALUE_PUT_REF_AND_RESET(signed_integer_obj);
1026 BT_VALUE_PUT_REF_AND_RESET(real_obj);
1027 BT_VALUE_PUT_REF_AND_RESET(string_obj);
1028 BT_VALUE_PUT_REF_AND_RESET(array_obj);
1029 BT_VALUE_PUT_REF_AND_RESET(map_obj);
1030 }
1031
1032 static
1033 bt_bool compare_map_elements(const bt_value *map_a, const bt_value *map_b,
1034 const char *key)
1035 {
1036 const bt_value *elem_a = NULL;
1037 const bt_value *elem_b = NULL;
1038 bt_bool equal;
1039
1040 elem_a = bt_value_map_borrow_entry_value_const(map_a, key);
1041 elem_b = bt_value_map_borrow_entry_value_const(map_b, key);
1042 equal = bt_value_compare(elem_a, elem_b);
1043 return equal;
1044 }
1045
1046 static
1047 void test_extend(void)
1048 {
1049 bt_value *base_map = bt_value_map_create();
1050 bt_value *extension_map = bt_value_map_create();
1051 bt_value *extended_map = NULL;
1052 bt_value *array = bt_value_array_create();
1053 bt_value_status status;
1054
1055 BT_ASSERT(base_map);
1056 BT_ASSERT(extension_map);
1057 BT_ASSERT(array);
1058 status = bt_value_map_insert_bool_entry(base_map, "file",
1059 BT_TRUE);
1060 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1061 status = bt_value_map_insert_bool_entry(base_map, "edit",
1062 BT_FALSE);
1063 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1064 status = bt_value_map_insert_signed_integer_entry(base_map,
1065 "selection", 17);
1066 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1067 status = bt_value_map_insert_signed_integer_entry(base_map, "find",
1068 -34);
1069 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1070 status = bt_value_map_insert_bool_entry(extension_map, "edit",
1071 BT_TRUE);
1072 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1073 status = bt_value_map_insert_signed_integer_entry(extension_map,
1074 "find", 101);
1075 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1076 status = bt_value_map_insert_real_entry(extension_map,
1077 "project", -404);
1078 BT_ASSERT(status == BT_VALUE_STATUS_OK);
1079 status = bt_value_map_extend(base_map, extension_map, &extended_map);
1080 ok(status == BT_VALUE_STATUS_OK &&
1081 extended_map, "bt_value_map_extend() succeeds");
1082 ok(bt_value_map_get_size(extended_map) == 5,
1083 "bt_value_map_extend() returns a map object with the correct size");
1084 ok(compare_map_elements(base_map,
1085 extended_map, "file"),
1086 "bt_value_map_extend() picks the appropriate element (file)");
1087 ok(compare_map_elements(extension_map,
1088 extended_map, "edit"),
1089 "bt_value_map_extend() picks the appropriate element (edit)");
1090 ok(compare_map_elements(base_map,
1091 extended_map, "selection"),
1092 "bt_value_map_extend() picks the appropriate element (selection)");
1093 ok(compare_map_elements(extension_map,
1094 extended_map, "find"),
1095 "bt_value_map_extend() picks the appropriate element (find)");
1096 ok(compare_map_elements(extension_map,
1097 extended_map, "project"),
1098 "bt_value_map_extend() picks the appropriate element (project)");
1099
1100 BT_VALUE_PUT_REF_AND_RESET(array);
1101 BT_VALUE_PUT_REF_AND_RESET(base_map);
1102 BT_VALUE_PUT_REF_AND_RESET(extension_map);
1103 BT_VALUE_PUT_REF_AND_RESET(extended_map);
1104 }
1105
1106 int main(void)
1107 {
1108 plan_tests(NR_TESTS);
1109 test_types();
1110 test_compare();
1111 test_copy();
1112 test_extend();
1113 return 0;
1114 }
This page took 0.052524 seconds and 3 git commands to generate.