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