Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / tests / lib / test_bt_values.c
CommitLineData
dac5c838
PP
1/*
2 * test_bt_values.c
3 *
705b853d 4 * Babeltrace value objects tests
dac5c838
PP
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
65300d60 23#include <babeltrace/object.h>
dac5c838 24#include <babeltrace/values.h>
25583cd0 25#include <babeltrace/assert-internal.h>
dac5c838
PP
26#include <string.h>
27#include "tap/tap.h"
28
f6ccaed9 29#define NR_TESTS 158
8bbe269d 30
dac5c838
PP
31static
32void 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");
65300d60 37 bt_object_get_ref(bt_value_null);
dac5c838 38 pass("getting bt_value_null does not cause a crash");
65300d60 39 bt_object_put_ref(bt_value_null);
dac5c838 40 pass("putting bt_value_null does not cause a crash");
dac5c838
PP
41}
42
43static
44void test_bool(void)
45{
46 int ret;
c55a9f58 47 bt_bool value;
dac5c838
PP
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
c55a9f58 54 value = BT_TRUE;
dac5c838 55 ret = bt_value_bool_get(obj, &value);
c55a9f58 56 ok(!ret && !value, "default boolean value object value is BT_FALSE");
dac5c838 57
25583cd0 58 BT_ASSERT(!bt_value_bool_set(obj, BT_FALSE));
c55a9f58 59 ret = bt_value_bool_set(obj, BT_TRUE);
dac5c838
PP
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
65300d60 64 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
65 pass("putting an existing boolean value object does not cause a crash")
66
c55a9f58
PP
67 value = BT_FALSE;
68 obj = bt_value_bool_create_init(BT_TRUE);
dac5c838
PP
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
65300d60 75 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
76}
77
78static
79void 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
dac5c838
PP
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
65300d60 98 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
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
65300d60 108 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
109}
110
111static
a373bf69 112void test_real(void)
dac5c838
PP
113{
114 int ret;
115 double value;
116 struct bt_value *obj;
117
a373bf69
PP
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");
dac5c838 121
dac5c838 122 value = 17.34;
a373bf69 123 ret = bt_value_real_get(obj, &value);
dac5c838 124 ok(!ret && value == 0.,
a373bf69 125 "default real number value object value is 0");
dac5c838 126
a373bf69
PP
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");
dac5c838 131
65300d60 132 BT_OBJECT_PUT_REF_AND_RESET(obj);
a373bf69 133 pass("putting an existing real number value object does not cause a crash")
dac5c838 134
a373bf69
PP
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);
dac5c838 139 ok(!ret && value == 33.1649758,
a373bf69 140 "bt_value_real_create_init() sets the appropriate initial value");
dac5c838 141
65300d60 142 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
143}
144
145static
146void 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
dac5c838
PP
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
65300d60 166 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
167 pass("putting an existing string value object does not cause a crash")
168
dac5c838
PP
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
65300d60 176 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838
PP
177}
178
179static
180void test_array(void)
181{
182 int ret;
c55a9f58 183 bt_bool bool_value;
dac5c838 184 int64_t int_value;
a373bf69 185 double real_value;
dac5c838
PP
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");
dac5c838
PP
193 ok(bt_value_array_is_empty(array_obj),
194 "initial array value object size is 0");
dac5c838
PP
195
196 obj = bt_value_integer_create_init(345);
07208d85 197 ret = bt_value_array_append_element(array_obj, obj);
65300d60 198 BT_OBJECT_PUT_REF_AND_RESET(obj);
a373bf69 199 obj = bt_value_real_create_init(-17.45);
07208d85 200 ret |= bt_value_array_append_element(array_obj, obj);
65300d60 201 BT_OBJECT_PUT_REF_AND_RESET(obj);
c55a9f58 202 obj = bt_value_bool_create_init(BT_TRUE);
07208d85 203 ret |= bt_value_array_append_element(array_obj, obj);
65300d60 204 BT_OBJECT_PUT_REF_AND_RESET(obj);
07208d85
PP
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,
dac5c838
PP
208 "appending an element to an array value object increment its size");
209
07208d85 210 obj = bt_value_array_borrow_element_by_index(array_obj, 0);
dac5c838 211 ok(obj && bt_value_is_integer(obj),
07208d85 212 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (integer)");
dac5c838
PP
213 ret = bt_value_integer_get(obj, &int_value);
214 ok(!ret && int_value == 345,
07208d85
PP
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);
a373bf69 217 ok(obj && bt_value_is_real(obj),
07208d85 218 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
a373bf69
PP
219 ret = bt_value_real_get(obj, &real_value);
220 ok(!ret && real_value == -17.45,
07208d85
PP
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);
dac5c838 223 ok(obj && bt_value_is_bool(obj),
07208d85 224 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
dac5c838
PP
225 ret = bt_value_bool_get(obj, &bool_value);
226 ok(!ret && bool_value,
07208d85
PP
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);
dac5c838 229 ok(obj == bt_value_null,
07208d85 230 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
dac5c838 231
dac5c838 232 obj = bt_value_integer_create_init(1001);
25583cd0 233 BT_ASSERT(obj);
07208d85
PP
234 ok(!bt_value_array_set_element_by_index(array_obj, 2, obj),
235 "bt_value_array_set_element_by_index() succeeds");
65300d60 236 BT_OBJECT_PUT_REF_AND_RESET(obj);
07208d85 237 obj = bt_value_array_borrow_element_by_index(array_obj, 2);
dac5c838 238 ok(obj && bt_value_is_integer(obj),
07208d85 239 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
dac5c838 240 ret = bt_value_integer_get(obj, &int_value);
25583cd0 241 BT_ASSERT(!ret);
dac5c838 242 ok(int_value == 1001,
07208d85
PP
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");
dac5c838
PP
260 ok(!bt_value_array_is_empty(array_obj),
261 "map value object is not empty");
262
07208d85 263 obj = bt_value_array_borrow_element_by_index(array_obj, 4);
dac5c838 264 ok(obj && bt_value_is_bool(obj),
07208d85 265 "bt_value_array_append_bool_element() appends a boolean value object");
dac5c838
PP
266 ret = bt_value_bool_get(obj, &bool_value);
267 ok(!ret && !bool_value,
07208d85
PP
268 "bt_value_array_append_bool_element() appends the appropriate value");
269 obj = bt_value_array_borrow_element_by_index(array_obj, 5);
dac5c838 270 ok(obj && bt_value_is_integer(obj),
07208d85 271 "bt_value_array_append_integer_element() appends an integer value object");
dac5c838
PP
272 ret = bt_value_integer_get(obj, &int_value);
273 ok(!ret && int_value == 98765,
07208d85
PP
274 "bt_value_array_append_integer_element() appends the appropriate value");
275 obj = bt_value_array_borrow_element_by_index(array_obj, 6);
a373bf69 276 ok(obj && bt_value_is_real(obj),
07208d85 277 "bt_value_array_append_real_element() appends a real number value object");
a373bf69
PP
278 ret = bt_value_real_get(obj, &real_value);
279 ok(!ret && real_value == 2.49578,
07208d85
PP
280 "bt_value_array_append_real_element() appends the appropriate value");
281 obj = bt_value_array_borrow_element_by_index(array_obj, 7);
dac5c838 282 ok(obj && bt_value_is_string(obj),
07208d85 283 "bt_value_array_append_string_element() appends a string value object");
dac5c838
PP
284 ret = bt_value_string_get(obj, &string_value);
285 ok(!ret && string_value && !strcmp(string_value, "bt_value"),
07208d85
PP
286 "bt_value_array_append_string_element() appends the appropriate value");
287 obj = bt_value_array_borrow_element_by_index(array_obj, 8);
dac5c838 288 ok(obj && bt_value_is_array(obj),
07208d85 289 "bt_value_array_append_empty_array_element() appends an array value object");
dac5c838 290 ok(bt_value_array_is_empty(obj),
07208d85
PP
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);
dac5c838 293 ok(obj && bt_value_is_map(obj),
07208d85 294 "bt_value_array_append_empty_map_element() appends a map value object");
dac5c838 295 ok(bt_value_map_is_empty(obj),
07208d85 296 "bt_value_array_append_empty_map_element() an empty map value object");
dac5c838 297
65300d60 298 BT_OBJECT_PUT_REF_AND_RESET(array_obj);
dac5c838
PP
299 pass("putting an existing array value object does not cause a crash")
300}
301
302static
c55a9f58 303bt_bool test_map_foreach_cb_count(const char *key, struct bt_value *object,
dac5c838
PP
304 void *data)
305{
306 int *count = data;
307
308 if (*count == 3) {
c55a9f58 309 return BT_FALSE;
dac5c838
PP
310 }
311
312 (*count)++;
313
c55a9f58 314 return BT_TRUE;
dac5c838
PP
315}
316
317struct map_foreach_checklist {
c55a9f58
PP
318 bt_bool bool1;
319 bt_bool int1;
a373bf69 320 bt_bool real1;
c55a9f58
PP
321 bt_bool null1;
322 bt_bool bool2;
323 bt_bool int2;
a373bf69 324 bt_bool real2;
c55a9f58
PP
325 bt_bool string2;
326 bt_bool array2;
327 bt_bool map2;
dac5c838
PP
328};
329
330static
c55a9f58 331bt_bool test_map_foreach_cb_check(const char *key, struct bt_value *object,
dac5c838
PP
332 void *data)
333{
334 int ret;
335 struct map_foreach_checklist *checklist = data;
336
c55a9f58 337 if (!strcmp(key, "bt_bool")) {
dac5c838 338 if (checklist->bool1) {
c55a9f58 339 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
dac5c838 340 } else {
c55a9f58 341 bt_bool val = BT_FALSE;
dac5c838
PP
342
343 ret = bt_value_bool_get(object, &val);
c55a9f58 344 ok(!ret, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
dac5c838
PP
345
346 if (val) {
c55a9f58
PP
347 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
348 checklist->bool1 = BT_TRUE;
dac5c838 349 } else {
c55a9f58 350 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
dac5c838
PP
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");
c55a9f58 364 checklist->int1 = BT_TRUE;
dac5c838
PP
365 } else {
366 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
367 }
368 }
a373bf69
PP
369 } else if (!strcmp(key, "real")) {
370 if (checklist->real1) {
371 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
dac5c838
PP
372 } else {
373 double val = 0;
374
a373bf69
PP
375 ret = bt_value_real_get(object, &val);
376 ok(!ret, "test_map_foreach_cb_check(): success getting \"real\" value");
dac5c838
PP
377
378 if (val == 5.444) {
a373bf69
PP
379 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
380 checklist->real1 = BT_TRUE;
dac5c838 381 } else {
a373bf69 382 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
dac5c838
PP
383 }
384 }
385 } else if (!strcmp(key, "null")) {
386 if (checklist->null1) {
c55a9f58 387 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
dac5c838
PP
388 } else {
389 ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object");
c55a9f58 390 checklist->null1 = BT_TRUE;
dac5c838
PP
391 }
392 } else if (!strcmp(key, "bool2")) {
393 if (checklist->bool2) {
394 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
395 } else {
c55a9f58 396 bt_bool val = BT_FALSE;
dac5c838
PP
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");
c55a9f58 403 checklist->bool2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 419 checklist->int2 = BT_TRUE;
dac5c838
PP
420 } else {
421 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
422 }
423 }
a373bf69
PP
424 } else if (!strcmp(key, "real2")) {
425 if (checklist->real2) {
426 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
dac5c838
PP
427 } else {
428 double val = 0;
429
a373bf69
PP
430 ret = bt_value_real_get(object, &val);
431 ok(!ret, "test_map_foreach_cb_check(): success getting \"real2\" value");
dac5c838
PP
432
433 if (val == -49.0001) {
a373bf69
PP
434 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
435 checklist->real2 = BT_TRUE;
dac5c838 436 } else {
a373bf69 437 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
dac5c838
PP
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");
c55a9f58 451 checklist->string2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 463 checklist->array2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 472 checklist->map2 = BT_TRUE;
dac5c838
PP
473 }
474 } else {
475 fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key);
476 }
477
c55a9f58 478 return BT_TRUE;
dac5c838
PP
479}
480
481static
482void test_map(void)
483{
484 int ret;
485 int count = 0;
c55a9f58 486 bt_bool bool_value;
dac5c838 487 int64_t int_value;
a373bf69 488 double real_value;
dac5c838
PP
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");
07208d85 496 ok(bt_value_map_get_size(map_obj) == 0,
dac5c838 497 "initial map value object size is 0");
dac5c838
PP
498
499 obj = bt_value_integer_create_init(19457);
07208d85 500 ret = bt_value_map_insert_entry(map_obj, "int", obj);
65300d60 501 BT_OBJECT_PUT_REF_AND_RESET(obj);
a373bf69 502 obj = bt_value_real_create_init(5.444);
07208d85 503 ret |= bt_value_map_insert_entry(map_obj, "real", obj);
65300d60 504 BT_OBJECT_PUT_REF_AND_RESET(obj);
dac5c838 505 obj = bt_value_bool_create();
07208d85 506 ret |= bt_value_map_insert_entry(map_obj, "bt_bool", obj);
65300d60 507 BT_OBJECT_PUT_REF_AND_RESET(obj);
07208d85
PP
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,
dac5c838
PP
511 "inserting an element into a map value object increment its size");
512
c55a9f58 513 obj = bt_value_bool_create_init(BT_TRUE);
07208d85 514 ret = bt_value_map_insert_entry(map_obj, "bt_bool", obj);
65300d60 515 BT_OBJECT_PUT_REF_AND_RESET(obj);
07208d85 516 ok(!ret, "bt_value_map_insert_entry() accepts an existing key");
dac5c838 517
07208d85
PP
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");
a373bf69 521 ok(obj && bt_value_is_real(obj),
07208d85 522 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
a373bf69
PP
523 ret = bt_value_real_get(obj, &real_value);
524 ok(!ret && real_value == 5.444,
07208d85
PP
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");
dac5c838 527 ok(obj && bt_value_is_integer(obj),
07208d85 528 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
dac5c838
PP
529 ret = bt_value_integer_get(obj, &int_value);
530 ok(!ret && int_value == 19457,
07208d85
PP
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");
dac5c838 533 ok(obj && bt_value_is_null(obj),
07208d85
PP
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");
dac5c838 536 ok(obj && bt_value_is_bool(obj),
07208d85 537 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
dac5c838
PP
538 ret = bt_value_bool_get(obj, &bool_value);
539 ok(!ret && bool_value,
07208d85
PP
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,
dac5c838
PP
556 "the bt_value_map_insert*() functions increment the map value object's size");
557
07208d85 558 ok(!bt_value_map_has_entry(map_obj, "hello"),
dac5c838 559 "map value object does not have key \"hello\"");
07208d85 560 ok(bt_value_map_has_entry(map_obj, "bt_bool"),
c55a9f58 561 "map value object has key \"bt_bool\"");
07208d85 562 ok(bt_value_map_has_entry(map_obj, "int"),
dac5c838 563 "map value object has key \"int\"");
07208d85 564 ok(bt_value_map_has_entry(map_obj, "real"),
a373bf69 565 "map value object has key \"real\"");
07208d85 566 ok(bt_value_map_has_entry(map_obj, "null"),
dac5c838 567 "map value object has key \"null\"");
07208d85 568 ok(bt_value_map_has_entry(map_obj, "bool2"),
dac5c838 569 "map value object has key \"bool2\"");
07208d85 570 ok(bt_value_map_has_entry(map_obj, "int2"),
dac5c838 571 "map value object has key \"int2\"");
07208d85 572 ok(bt_value_map_has_entry(map_obj, "real2"),
a373bf69 573 "map value object has key \"real2\"");
07208d85 574 ok(bt_value_map_has_entry(map_obj, "string2"),
dac5c838 575 "map value object has key \"string2\"");
07208d85 576 ok(bt_value_map_has_entry(map_obj, "array2"),
dac5c838 577 "map value object has key \"array2\"");
07208d85 578 ok(bt_value_map_has_entry(map_obj, "map2"),
dac5c838
PP
579 "map value object has key \"map2\"");
580
07208d85 581 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count, &count);
f6ccaed9 582 ok(ret == BT_VALUE_STATUS_CANCELED && count == 3,
07208d85 583 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
dac5c838
PP
584
585 memset(&checklist, 0, sizeof(checklist));
07208d85 586 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_check,
dac5c838
PP
587 &checklist);
588 ok(ret == BT_VALUE_STATUS_OK,
07208d85 589 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
a373bf69 590 ok(checklist.bool1 && checklist.int1 && checklist.real1 &&
dac5c838 591 checklist.null1 && checklist.bool2 && checklist.int2 &&
a373bf69 592 checklist.real2 && checklist.string2 &&
dac5c838 593 checklist.array2 && checklist.map2,
07208d85 594 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
dac5c838 595
65300d60 596 BT_OBJECT_PUT_REF_AND_RESET(map_obj);
dac5c838
PP
597 pass("putting an existing map value object does not cause a crash")
598}
599
600static
601void test_types(void)
602{
603 test_null();
604 test_bool();
605 test_integer();
a373bf69 606 test_real();
dac5c838
PP
607 test_string();
608 test_array();
609 test_map();
610}
611
612static
613void test_compare_null(void)
614{
dac5c838
PP
615 ok(bt_value_compare(bt_value_null, bt_value_null),
616 "null value objects are equivalent");
617}
618
619static
620void test_compare_bool(void)
621{
c55a9f58
PP
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);
dac5c838 625
25583cd0 626 BT_ASSERT(bool1 && bool2 && bool3);
dac5c838 627 ok(!bt_value_compare(bt_value_null, bool1),
c55a9f58 628 "cannot compare null value object and bt_bool value object");
dac5c838 629 ok(!bt_value_compare(bool1, bool2),
c55a9f58 630 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
dac5c838 631 ok(bt_value_compare(bool1, bool3),
c55a9f58 632 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
dac5c838 633
65300d60
PP
634 BT_OBJECT_PUT_REF_AND_RESET(bool1);
635 BT_OBJECT_PUT_REF_AND_RESET(bool2);
636 BT_OBJECT_PUT_REF_AND_RESET(bool3);
dac5c838
PP
637}
638
639static
640void 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
25583cd0 646 BT_ASSERT(int1 && int2 && int3);
dac5c838
PP
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
65300d60
PP
654 BT_OBJECT_PUT_REF_AND_RESET(int1);
655 BT_OBJECT_PUT_REF_AND_RESET(int2);
656 BT_OBJECT_PUT_REF_AND_RESET(int3);
dac5c838
PP
657}
658
659static
a373bf69 660void test_compare_real(void)
dac5c838 661{
a373bf69
PP
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
65300d60
PP
675 BT_OBJECT_PUT_REF_AND_RESET(real1);
676 BT_OBJECT_PUT_REF_AND_RESET(real2);
677 BT_OBJECT_PUT_REF_AND_RESET(real3);
dac5c838
PP
678}
679
680static
681void 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
25583cd0 687 BT_ASSERT(string1 && string2 && string3);
dac5c838
PP
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
65300d60
PP
696 BT_OBJECT_PUT_REF_AND_RESET(string1);
697 BT_OBJECT_PUT_REF_AND_RESET(string2);
698 BT_OBJECT_PUT_REF_AND_RESET(string3);
dac5c838
PP
699}
700
701static
702void 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
25583cd0 708 BT_ASSERT(array1 && array2 && array3);
dac5c838
PP
709
710 ok(bt_value_compare(array1, array2),
711 "empty array value objects are equivalent");
712
07208d85
PP
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);
dac5c838
PP
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),
c55a9f58 729 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
dac5c838 730 ok(bt_value_compare(array1, array3),
c55a9f58 731 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
dac5c838 732
65300d60
PP
733 BT_OBJECT_PUT_REF_AND_RESET(array1);
734 BT_OBJECT_PUT_REF_AND_RESET(array2);
735 BT_OBJECT_PUT_REF_AND_RESET(array3);
dac5c838
PP
736}
737
738static
739void 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
25583cd0 745 BT_ASSERT(map1 && map2 && map3);
dac5c838
PP
746
747 ok(bt_value_compare(map1, map2),
748 "empty map value objects are equivalent");
749
07208d85
PP
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);
dac5c838
PP
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
65300d60
PP
770 BT_OBJECT_PUT_REF_AND_RESET(map1);
771 BT_OBJECT_PUT_REF_AND_RESET(map2);
772 BT_OBJECT_PUT_REF_AND_RESET(map3);
dac5c838
PP
773}
774
775static
776void test_compare(void)
777{
dac5c838
PP
778 test_compare_null();
779 test_compare_bool();
780 test_compare_integer();
a373bf69 781 test_compare_real();
dac5c838
PP
782 test_compare_string();
783 test_compare_array();
784 test_compare_map();
785}
786
787static
788void 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,
c55a9f58 793 * and that bt_value_compare() returns BT_TRUE for the top-level
dac5c838
PP
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;
a373bf69 801 struct bt_value *real_obj, *real_copy_obj;
dac5c838
PP
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
c55a9f58 806 bool_obj = bt_value_bool_create_init(BT_TRUE);
dac5c838 807 integer_obj = bt_value_integer_create_init(23);
a373bf69 808 real_obj = bt_value_real_create_init(-3.1416);
dac5c838
PP
809 string_obj = bt_value_string_create_init("test");
810 array_obj = bt_value_array_create();
811 map_obj = bt_value_map_create();
812
a373bf69 813 BT_ASSERT(bool_obj && integer_obj && real_obj && string_obj &&
dac5c838
PP
814 array_obj && map_obj);
815
07208d85
PP
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));
dac5c838 822
dac5c838
PP
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)");
07208d85 829 string_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj, "string");
dac5c838
PP
830 ok(string_copy_obj != string_obj,
831 "bt_value_copy() returns a different pointer (string)");
07208d85 832 array_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj, "array");
dac5c838
PP
833 ok(array_copy_obj != array_obj,
834 "bt_value_copy() returns a different pointer (array)");
07208d85 835 bool_copy_obj = bt_value_array_borrow_element_by_index(array_copy_obj, 0);
dac5c838 836 ok(bool_copy_obj != bool_obj,
c55a9f58 837 "bt_value_copy() returns a different pointer (bt_bool)");
07208d85 838 integer_copy_obj = bt_value_array_borrow_element_by_index(array_copy_obj, 1);
dac5c838
PP
839 ok(integer_copy_obj != integer_obj,
840 "bt_value_copy() returns a different pointer (integer)");
07208d85 841 real_copy_obj = bt_value_array_borrow_element_by_index(array_copy_obj, 2);
a373bf69
PP
842 ok(real_copy_obj != real_obj,
843 "bt_value_copy() returns a different pointer (real)");
07208d85 844 null_copy_obj = bt_value_array_borrow_element_by_index(array_copy_obj, 3);
dac5c838
PP
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
65300d60
PP
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);
dac5c838
PP
858}
859
b6ba7620 860static
c55a9f58 861bt_bool compare_map_elements(struct bt_value *map_a, struct bt_value *map_b,
b6ba7620
PP
862 const char *key)
863{
864 struct bt_value *elem_a = NULL;
865 struct bt_value *elem_b = NULL;
c55a9f58 866 bt_bool equal;
b6ba7620 867
07208d85
PP
868 elem_a = bt_value_map_borrow_entry_value(map_a, key);
869 elem_b = bt_value_map_borrow_entry_value(map_b, key);
b6ba7620 870 equal = bt_value_compare(elem_a, elem_b);
b6ba7620
PP
871 return equal;
872}
873
874static
875void 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
25583cd0
PP
883 BT_ASSERT(base_map);
884 BT_ASSERT(extension_map);
885 BT_ASSERT(array);
07208d85 886 status = bt_value_map_insert_bool_entry(base_map, "file", BT_TRUE);
25583cd0 887 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 888 status = bt_value_map_insert_bool_entry(base_map, "edit", BT_FALSE);
25583cd0 889 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 890 status = bt_value_map_insert_integer_entry(base_map, "selection", 17);
25583cd0 891 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 892 status = bt_value_map_insert_integer_entry(base_map, "find", -34);
25583cd0 893 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 894 status = bt_value_map_insert_bool_entry(extension_map, "edit", BT_TRUE);
25583cd0 895 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 896 status = bt_value_map_insert_integer_entry(extension_map, "find", 101);
25583cd0 897 BT_ASSERT(status == BT_VALUE_STATUS_OK);
07208d85 898 status = bt_value_map_insert_real_entry(extension_map, "project", -404);
25583cd0 899 BT_ASSERT(status == BT_VALUE_STATUS_OK);
b6ba7620
PP
900 extended_map = bt_value_map_extend(base_map, extension_map);
901 ok(extended_map, "bt_value_map_extend() succeeds");
07208d85 902 ok(bt_value_map_get_size(extended_map) == 5,
b6ba7620
PP
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
65300d60
PP
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);
b6ba7620
PP
919}
920
dac5c838
PP
921int main(void)
922{
8bbe269d 923 plan_tests(NR_TESTS);
dac5c838
PP
924 test_types();
925 test_compare();
926 test_copy();
b6ba7620 927 test_extend();
dac5c838
PP
928 return 0;
929}
This page took 0.086603 seconds and 4 git commands to generate.