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