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