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