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