Values API: split into private and public APIs
[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>
da91b29a 25#include <babeltrace/private-values.h>
25583cd0 26#include <babeltrace/assert-internal.h>
dac5c838
PP
27#include <string.h>
28#include "tap/tap.h"
29
f6ccaed9 30#define NR_TESTS 158
8bbe269d 31
dac5c838
PP
32static
33void 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");
65300d60 38 bt_object_get_ref(bt_value_null);
dac5c838 39 pass("getting bt_value_null does not cause a crash");
65300d60 40 bt_object_put_ref(bt_value_null);
dac5c838 41 pass("putting bt_value_null does not cause a crash");
dac5c838
PP
42}
43
44static
45void test_bool(void)
46{
47 int ret;
c55a9f58 48 bt_bool value;
da91b29a 49 struct bt_private_value *priv_obj;
dac5c838
PP
50 struct bt_value *obj;
51
da91b29a
PP
52 priv_obj = bt_private_value_bool_create();
53 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 54 ok(obj && bt_value_is_bool(obj),
da91b29a 55 "bt_private_value_bool_create() returns a boolean value object");
dac5c838 56
c55a9f58 57 value = BT_TRUE;
dac5c838 58 ret = bt_value_bool_get(obj, &value);
c55a9f58 59 ok(!ret && !value, "default boolean value object value is BT_FALSE");
dac5c838 60
da91b29a
PP
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");
dac5c838 64 ret = bt_value_bool_get(obj, &value);
da91b29a 65 ok(!ret && value, "bt_private_value_bool_set() works");
dac5c838 66
da91b29a 67 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
68 pass("putting an existing boolean value object does not cause a crash")
69
c55a9f58 70 value = BT_FALSE;
da91b29a
PP
71 priv_obj = bt_private_value_bool_create_init(BT_TRUE);
72 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 73 ok(obj && bt_value_is_bool(obj),
da91b29a 74 "bt_private_value_bool_create_init() returns a boolean value object");
dac5c838
PP
75 ret = bt_value_bool_get(obj, &value);
76 ok(!ret && value,
da91b29a 77 "bt_private_value_bool_create_init() sets the appropriate initial value");
dac5c838 78
da91b29a 79 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
80}
81
82static
83void test_integer(void)
84{
85 int ret;
86 int64_t value;
da91b29a 87 struct bt_private_value *priv_obj;
dac5c838
PP
88 struct bt_value *obj;
89
da91b29a
PP
90 priv_obj = bt_private_value_integer_create();
91 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 92 ok(obj && bt_value_is_integer(obj),
da91b29a 93 "bt_private_value_integer_create() returns an integer value object");
dac5c838 94
dac5c838
PP
95 value = 1961;
96 ret = bt_value_integer_get(obj, &value);
97 ok(!ret && value == 0, "default integer value object value is 0");
98
da91b29a
PP
99 ret = bt_private_integer_bool_set(priv_obj, -98765);
100 ok(!ret, "bt_private_integer_bool_set() succeeds");
dac5c838 101 ret = bt_value_integer_get(obj, &value);
da91b29a 102 ok(!ret && value == -98765, "bt_private_integer_bool_set() works");
dac5c838 103
da91b29a 104 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
105 pass("putting an existing integer value object does not cause a crash")
106
da91b29a
PP
107 priv_obj = bt_private_value_integer_create_init(321456987);
108 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 109 ok(obj && bt_value_is_integer(obj),
da91b29a 110 "bt_private_value_integer_create_init() returns an integer value object");
dac5c838
PP
111 ret = bt_value_integer_get(obj, &value);
112 ok(!ret && value == 321456987,
da91b29a 113 "bt_private_value_integer_create_init() sets the appropriate initial value");
dac5c838 114
da91b29a 115 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
116}
117
118static
a373bf69 119void test_real(void)
dac5c838
PP
120{
121 int ret;
122 double value;
da91b29a 123 struct bt_private_value *priv_obj;
dac5c838
PP
124 struct bt_value *obj;
125
da91b29a
PP
126 priv_obj = bt_private_value_real_create();
127 obj = bt_value_borrow_from_private(priv_obj);
a373bf69 128 ok(obj && bt_value_is_real(obj),
da91b29a 129 "bt_private_value_real_create() returns a real number value object");
dac5c838 130
dac5c838 131 value = 17.34;
a373bf69 132 ret = bt_value_real_get(obj, &value);
dac5c838 133 ok(!ret && value == 0.,
a373bf69 134 "default real number value object value is 0");
dac5c838 135
da91b29a
PP
136 ret = bt_private_value_real_set(priv_obj, -3.1416);
137 ok(!ret, "bt_private_value_real_set() succeeds");
a373bf69 138 ret = bt_value_real_get(obj, &value);
da91b29a 139 ok(!ret && value == -3.1416, "bt_private_value_real_set() works");
dac5c838 140
da91b29a 141 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
a373bf69 142 pass("putting an existing real number value object does not cause a crash")
dac5c838 143
da91b29a
PP
144 priv_obj = bt_private_value_real_create_init(33.1649758);
145 obj = bt_value_borrow_from_private(priv_obj);
a373bf69 146 ok(obj && bt_value_is_real(obj),
da91b29a 147 "bt_private_value_real_create_init() returns a real number value object");
a373bf69 148 ret = bt_value_real_get(obj, &value);
dac5c838 149 ok(!ret && value == 33.1649758,
da91b29a 150 "bt_private_value_real_create_init() sets the appropriate initial value");
dac5c838 151
da91b29a 152 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
153}
154
155static
156void test_string(void)
157{
158 int ret;
159 const char *value;
da91b29a 160 struct bt_private_value *priv_obj;
dac5c838
PP
161 struct bt_value *obj;
162
da91b29a
PP
163 priv_obj = bt_private_value_string_create();
164 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 165 ok(obj && bt_value_is_string(obj),
da91b29a 166 "bt_private_value_string_create() returns a string value object");
dac5c838 167
dac5c838
PP
168 ret = bt_value_string_get(obj, &value);
169 ok(!ret && value && !strcmp(value, ""),
170 "default string value object value is \"\"");
171
da91b29a
PP
172 ret = bt_private_value_string_set(priv_obj, "hello worldz");
173 ok(!ret, "bt_private_value_string_set() succeeds");
dac5c838
PP
174 ret = bt_value_string_get(obj, &value);
175 ok(!ret && value && !strcmp(value, "hello worldz"),
176 "bt_value_string_get() works");
177
da91b29a 178 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
179 pass("putting an existing string value object does not cause a crash")
180
da91b29a
PP
181 priv_obj = bt_private_value_string_create_init("initial value");
182 obj = bt_value_borrow_from_private(priv_obj);
dac5c838 183 ok(obj && bt_value_is_string(obj),
da91b29a 184 "bt_private_value_string_create_init() returns a string value object");
dac5c838
PP
185 ret = bt_value_string_get(obj, &value);
186 ok(!ret && value && !strcmp(value, "initial value"),
da91b29a 187 "bt_private_value_string_create_init() sets the appropriate initial value");
dac5c838 188
da91b29a 189 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
dac5c838
PP
190}
191
192static
193void test_array(void)
194{
195 int ret;
c55a9f58 196 bt_bool bool_value;
dac5c838 197 int64_t int_value;
a373bf69 198 double real_value;
da91b29a 199 struct bt_private_value *priv_obj;
dac5c838
PP
200 struct bt_value *obj;
201 const char *string_value;
da91b29a 202 struct bt_private_value *priv_array_obj;
dac5c838
PP
203 struct bt_value *array_obj;
204
da91b29a
PP
205 priv_array_obj = bt_private_value_array_create();
206 array_obj = bt_value_borrow_from_private(priv_array_obj);
dac5c838 207 ok(array_obj && bt_value_is_array(array_obj),
da91b29a 208 "bt_private_value_array_create() returns an array value object");
dac5c838
PP
209 ok(bt_value_array_is_empty(array_obj),
210 "initial array value object size is 0");
dac5c838 211
da91b29a
PP
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");
07208d85 227 ok(bt_value_array_get_size(array_obj) == 4,
dac5c838
PP
228 "appending an element to an array value object increment its size");
229
07208d85 230 obj = bt_value_array_borrow_element_by_index(array_obj, 0);
dac5c838 231 ok(obj && bt_value_is_integer(obj),
07208d85 232 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (integer)");
dac5c838
PP
233 ret = bt_value_integer_get(obj, &int_value);
234 ok(!ret && int_value == 345,
07208d85
PP
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);
a373bf69 237 ok(obj && bt_value_is_real(obj),
07208d85 238 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (real number)");
a373bf69
PP
239 ret = bt_value_real_get(obj, &real_value);
240 ok(!ret && real_value == -17.45,
07208d85
PP
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);
dac5c838 243 ok(obj && bt_value_is_bool(obj),
07208d85 244 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (boolean)");
dac5c838
PP
245 ret = bt_value_bool_get(obj, &bool_value);
246 ok(!ret && bool_value,
07208d85
PP
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);
dac5c838 249 ok(obj == bt_value_null,
07208d85 250 "bt_value_array_borrow_element_by_index() returns an value object with the appropriate type (null)");
dac5c838 251
da91b29a
PP
252 priv_obj = bt_private_value_integer_create_init(1001);
253 obj = bt_value_borrow_from_private(priv_obj);
25583cd0 254 BT_ASSERT(obj);
da91b29a 255 ok(!bt_private_value_array_set_element_by_index(priv_array_obj, 2, obj),
07208d85 256 "bt_value_array_set_element_by_index() succeeds");
da91b29a 257 BT_OBJECT_PUT_REF_AND_RESET(priv_obj);
07208d85 258 obj = bt_value_array_borrow_element_by_index(array_obj, 2);
dac5c838 259 ok(obj && bt_value_is_integer(obj),
07208d85 260 "bt_value_array_set_element_by_index() inserts an value object with the appropriate type");
dac5c838 261 ret = bt_value_integer_get(obj, &int_value);
25583cd0 262 BT_ASSERT(!ret);
dac5c838 263 ok(int_value == 1001,
07208d85
PP
264 "bt_value_array_set_element_by_index() inserts an value object with the appropriate value");
265
da91b29a
PP
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");
07208d85
PP
282
283 ok(bt_value_array_get_size(array_obj) == 10,
da91b29a 284 "the bt_private_value_array_append_element_*() functions increment the array value object's size");
dac5c838
PP
285 ok(!bt_value_array_is_empty(array_obj),
286 "map value object is not empty");
287
07208d85 288 obj = bt_value_array_borrow_element_by_index(array_obj, 4);
dac5c838 289 ok(obj && bt_value_is_bool(obj),
da91b29a 290 "bt_private_value_array_append_bool_element() appends a boolean value object");
dac5c838
PP
291 ret = bt_value_bool_get(obj, &bool_value);
292 ok(!ret && !bool_value,
da91b29a 293 "bt_private_value_array_append_bool_element() appends the appropriate value");
07208d85 294 obj = bt_value_array_borrow_element_by_index(array_obj, 5);
dac5c838 295 ok(obj && bt_value_is_integer(obj),
da91b29a 296 "bt_private_value_array_append_integer_element() appends an integer value object");
dac5c838
PP
297 ret = bt_value_integer_get(obj, &int_value);
298 ok(!ret && int_value == 98765,
da91b29a 299 "bt_private_value_array_append_integer_element() appends the appropriate value");
07208d85 300 obj = bt_value_array_borrow_element_by_index(array_obj, 6);
a373bf69 301 ok(obj && bt_value_is_real(obj),
da91b29a 302 "bt_private_value_array_append_real_element() appends a real number value object");
a373bf69
PP
303 ret = bt_value_real_get(obj, &real_value);
304 ok(!ret && real_value == 2.49578,
da91b29a 305 "bt_private_value_array_append_real_element() appends the appropriate value");
07208d85 306 obj = bt_value_array_borrow_element_by_index(array_obj, 7);
dac5c838 307 ok(obj && bt_value_is_string(obj),
da91b29a 308 "bt_private_value_array_append_string_element() appends a string value object");
dac5c838
PP
309 ret = bt_value_string_get(obj, &string_value);
310 ok(!ret && string_value && !strcmp(string_value, "bt_value"),
da91b29a 311 "bt_private_value_array_append_string_element() appends the appropriate value");
07208d85 312 obj = bt_value_array_borrow_element_by_index(array_obj, 8);
dac5c838 313 ok(obj && bt_value_is_array(obj),
da91b29a 314 "bt_private_value_array_append_empty_array_element() appends an array value object");
dac5c838 315 ok(bt_value_array_is_empty(obj),
da91b29a 316 "bt_private_value_array_append_empty_array_element() an empty array value object");
07208d85 317 obj = bt_value_array_borrow_element_by_index(array_obj, 9);
dac5c838 318 ok(obj && bt_value_is_map(obj),
da91b29a 319 "bt_private_value_array_append_empty_map_element() appends a map value object");
dac5c838 320 ok(bt_value_map_is_empty(obj),
da91b29a 321 "bt_private_value_array_append_empty_map_element() an empty map value object");
dac5c838 322
da91b29a 323 BT_OBJECT_PUT_REF_AND_RESET(priv_array_obj);
dac5c838
PP
324 pass("putting an existing array value object does not cause a crash")
325}
326
327static
c55a9f58 328bt_bool test_map_foreach_cb_count(const char *key, struct bt_value *object,
dac5c838
PP
329 void *data)
330{
331 int *count = data;
332
333 if (*count == 3) {
c55a9f58 334 return BT_FALSE;
dac5c838
PP
335 }
336
337 (*count)++;
338
c55a9f58 339 return BT_TRUE;
dac5c838
PP
340}
341
342struct map_foreach_checklist {
c55a9f58
PP
343 bt_bool bool1;
344 bt_bool int1;
a373bf69 345 bt_bool real1;
c55a9f58
PP
346 bt_bool null1;
347 bt_bool bool2;
348 bt_bool int2;
a373bf69 349 bt_bool real2;
c55a9f58
PP
350 bt_bool string2;
351 bt_bool array2;
352 bt_bool map2;
dac5c838
PP
353};
354
355static
c55a9f58 356bt_bool test_map_foreach_cb_check(const char *key, struct bt_value *object,
dac5c838
PP
357 void *data)
358{
359 int ret;
360 struct map_foreach_checklist *checklist = data;
361
c55a9f58 362 if (!strcmp(key, "bt_bool")) {
dac5c838 363 if (checklist->bool1) {
c55a9f58 364 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
dac5c838 365 } else {
c55a9f58 366 bt_bool val = BT_FALSE;
dac5c838
PP
367
368 ret = bt_value_bool_get(object, &val);
c55a9f58 369 ok(!ret, "test_map_foreach_cb_check(): success getting \"bt_bool\" value");
dac5c838
PP
370
371 if (val) {
c55a9f58
PP
372 pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value");
373 checklist->bool1 = BT_TRUE;
dac5c838 374 } else {
c55a9f58 375 fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value");
dac5c838
PP
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");
c55a9f58 389 checklist->int1 = BT_TRUE;
dac5c838
PP
390 } else {
391 fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value");
392 }
393 }
a373bf69
PP
394 } else if (!strcmp(key, "real")) {
395 if (checklist->real1) {
396 fail("test_map_foreach_cb_check(): duplicate key \"real\"");
dac5c838
PP
397 } else {
398 double val = 0;
399
a373bf69
PP
400 ret = bt_value_real_get(object, &val);
401 ok(!ret, "test_map_foreach_cb_check(): success getting \"real\" value");
dac5c838
PP
402
403 if (val == 5.444) {
a373bf69
PP
404 pass("test_map_foreach_cb_check(): \"real\" value object has the right value");
405 checklist->real1 = BT_TRUE;
dac5c838 406 } else {
a373bf69 407 fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value");
dac5c838
PP
408 }
409 }
410 } else if (!strcmp(key, "null")) {
411 if (checklist->null1) {
c55a9f58 412 fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\"");
dac5c838
PP
413 } else {
414 ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object");
c55a9f58 415 checklist->null1 = BT_TRUE;
dac5c838
PP
416 }
417 } else if (!strcmp(key, "bool2")) {
418 if (checklist->bool2) {
419 fail("test_map_foreach_cb_check(): duplicate key \"bool2\"");
420 } else {
c55a9f58 421 bt_bool val = BT_FALSE;
dac5c838
PP
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");
c55a9f58 428 checklist->bool2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 444 checklist->int2 = BT_TRUE;
dac5c838
PP
445 } else {
446 fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value");
447 }
448 }
a373bf69
PP
449 } else if (!strcmp(key, "real2")) {
450 if (checklist->real2) {
451 fail("test_map_foreach_cb_check(): duplicate key \"real2\"");
dac5c838
PP
452 } else {
453 double val = 0;
454
a373bf69
PP
455 ret = bt_value_real_get(object, &val);
456 ok(!ret, "test_map_foreach_cb_check(): success getting \"real2\" value");
dac5c838
PP
457
458 if (val == -49.0001) {
a373bf69
PP
459 pass("test_map_foreach_cb_check(): \"real2\" value object has the right value");
460 checklist->real2 = BT_TRUE;
dac5c838 461 } else {
a373bf69 462 fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value");
dac5c838
PP
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");
c55a9f58 476 checklist->string2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 488 checklist->array2 = BT_TRUE;
dac5c838
PP
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");
c55a9f58 497 checklist->map2 = BT_TRUE;
dac5c838
PP
498 }
499 } else {
da91b29a
PP
500 fail("test_map_foreach_cb_check(): unknown map key \"%s\"",
501 key);
dac5c838
PP
502 }
503
c55a9f58 504 return BT_TRUE;
dac5c838
PP
505}
506
507static
508void test_map(void)
509{
510 int ret;
511 int count = 0;
c55a9f58 512 bt_bool bool_value;
dac5c838 513 int64_t int_value;
a373bf69 514 double real_value;
da91b29a 515 struct bt_private_value *priv_obj;
dac5c838 516 struct bt_value *obj;
da91b29a 517 struct bt_private_value *priv_map_obj;
dac5c838
PP
518 struct bt_value *map_obj;
519 struct map_foreach_checklist checklist;
520
da91b29a
PP
521 priv_map_obj = bt_private_value_map_create();
522 map_obj = bt_value_borrow_from_private(priv_map_obj);
dac5c838 523 ok(map_obj && bt_value_is_map(map_obj),
da91b29a 524 "bt_private_value_map_create() returns a map value object");
07208d85 525 ok(bt_value_map_get_size(map_obj) == 0,
dac5c838 526 "initial map value object size is 0");
dac5c838 527
da91b29a
PP
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");
07208d85 543 ok(bt_value_map_get_size(map_obj) == 4,
dac5c838
PP
544 "inserting an element into a map value object increment its size");
545
da91b29a
PP
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");
dac5c838 551
07208d85
PP
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");
a373bf69 555 ok(obj && bt_value_is_real(obj),
07208d85 556 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)");
a373bf69
PP
557 ret = bt_value_real_get(obj, &real_value);
558 ok(!ret && real_value == 5.444,
07208d85
PP
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");
dac5c838 561 ok(obj && bt_value_is_integer(obj),
07208d85 562 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (integer)");
dac5c838
PP
563 ret = bt_value_integer_get(obj, &int_value);
564 ok(!ret && int_value == 19457,
07208d85
PP
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");
dac5c838 567 ok(obj && bt_value_is_null(obj),
07208d85
PP
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");
dac5c838 570 ok(obj && bt_value_is_bool(obj),
07208d85 571 "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)");
dac5c838
PP
572 ret = bt_value_bool_get(obj, &bool_value);
573 ok(!ret && bool_value,
07208d85
PP
574 "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)");
575
da91b29a
PP
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");
07208d85
PP
593
594 ok(bt_value_map_get_size(map_obj) == 10,
dac5c838
PP
595 "the bt_value_map_insert*() functions increment the map value object's size");
596
07208d85 597 ok(!bt_value_map_has_entry(map_obj, "hello"),
dac5c838 598 "map value object does not have key \"hello\"");
07208d85 599 ok(bt_value_map_has_entry(map_obj, "bt_bool"),
c55a9f58 600 "map value object has key \"bt_bool\"");
07208d85 601 ok(bt_value_map_has_entry(map_obj, "int"),
dac5c838 602 "map value object has key \"int\"");
07208d85 603 ok(bt_value_map_has_entry(map_obj, "real"),
a373bf69 604 "map value object has key \"real\"");
07208d85 605 ok(bt_value_map_has_entry(map_obj, "null"),
dac5c838 606 "map value object has key \"null\"");
07208d85 607 ok(bt_value_map_has_entry(map_obj, "bool2"),
dac5c838 608 "map value object has key \"bool2\"");
07208d85 609 ok(bt_value_map_has_entry(map_obj, "int2"),
dac5c838 610 "map value object has key \"int2\"");
07208d85 611 ok(bt_value_map_has_entry(map_obj, "real2"),
a373bf69 612 "map value object has key \"real2\"");
07208d85 613 ok(bt_value_map_has_entry(map_obj, "string2"),
dac5c838 614 "map value object has key \"string2\"");
07208d85 615 ok(bt_value_map_has_entry(map_obj, "array2"),
dac5c838 616 "map value object has key \"array2\"");
07208d85 617 ok(bt_value_map_has_entry(map_obj, "map2"),
dac5c838
PP
618 "map value object has key \"map2\"");
619
da91b29a
PP
620 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count,
621 &count);
f6ccaed9 622 ok(ret == BT_VALUE_STATUS_CANCELED && count == 3,
07208d85 623 "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
dac5c838
PP
624
625 memset(&checklist, 0, sizeof(checklist));
07208d85 626 ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_check,
dac5c838
PP
627 &checklist);
628 ok(ret == BT_VALUE_STATUS_OK,
07208d85 629 "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()");
a373bf69 630 ok(checklist.bool1 && checklist.int1 && checklist.real1 &&
dac5c838 631 checklist.null1 && checklist.bool2 && checklist.int2 &&
a373bf69 632 checklist.real2 && checklist.string2 &&
dac5c838 633 checklist.array2 && checklist.map2,
07208d85 634 "bt_value_map_foreach_entry() iterates over all the map value object's elements");
dac5c838 635
da91b29a 636 BT_OBJECT_PUT_REF_AND_RESET(priv_map_obj);
dac5c838
PP
637 pass("putting an existing map value object does not cause a crash")
638}
639
640static
641void test_types(void)
642{
643 test_null();
644 test_bool();
645 test_integer();
a373bf69 646 test_real();
dac5c838
PP
647 test_string();
648 test_array();
649 test_map();
650}
651
652static
653void test_compare_null(void)
654{
dac5c838
PP
655 ok(bt_value_compare(bt_value_null, bt_value_null),
656 "null value objects are equivalent");
657}
658
659static
660void test_compare_bool(void)
661{
da91b29a
PP
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);
dac5c838 668
25583cd0 669 BT_ASSERT(bool1 && bool2 && bool3);
da91b29a
PP
670 ok(!bt_value_compare(bt_value_null,
671 bt_value_borrow_from_private(bool1)),
c55a9f58 672 "cannot compare null value object and bt_bool value object");
da91b29a
PP
673 ok(!bt_value_compare(bt_value_borrow_from_private(bool1),
674 bt_value_borrow_from_private(bool2)),
c55a9f58 675 "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)");
da91b29a
PP
676 ok(bt_value_compare(bt_value_borrow_from_private(bool1),
677 bt_value_borrow_from_private(bool3)),
c55a9f58 678 "boolean value objects are equivalent (BT_FALSE and BT_FALSE)");
dac5c838 679
65300d60
PP
680 BT_OBJECT_PUT_REF_AND_RESET(bool1);
681 BT_OBJECT_PUT_REF_AND_RESET(bool2);
682 BT_OBJECT_PUT_REF_AND_RESET(bool3);
dac5c838
PP
683}
684
685static
686void test_compare_integer(void)
687{
da91b29a
PP
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);
dac5c838 694
25583cd0 695 BT_ASSERT(int1 && int2 && int3);
da91b29a
PP
696 ok(!bt_value_compare(bt_value_null,
697 bt_value_borrow_from_private(int1)),
dac5c838 698 "cannot compare null value object and integer value object");
da91b29a
PP
699 ok(!bt_value_compare(bt_value_borrow_from_private(int1),
700 bt_value_borrow_from_private(int2)),
dac5c838 701 "integer value objects are not equivalent (10 and -23)");
da91b29a
PP
702 ok(bt_value_compare(bt_value_borrow_from_private(int1),
703 bt_value_borrow_from_private(int3)),
dac5c838
PP
704 "integer value objects are equivalent (10 and 10)");
705
65300d60
PP
706 BT_OBJECT_PUT_REF_AND_RESET(int1);
707 BT_OBJECT_PUT_REF_AND_RESET(int2);
708 BT_OBJECT_PUT_REF_AND_RESET(int3);
dac5c838
PP
709}
710
711static
a373bf69 712void test_compare_real(void)
dac5c838 713{
da91b29a
PP
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);
a373bf69
PP
720
721 BT_ASSERT(real1 && real2 && real3);
722
da91b29a
PP
723 ok(!bt_value_compare(bt_value_null,
724 bt_value_borrow_from_private(real1)),
a373bf69 725 "cannot compare null value object and real number value object");
da91b29a
PP
726 ok(!bt_value_compare(bt_value_borrow_from_private(real1),
727 bt_value_borrow_from_private(real2)),
a373bf69 728 "real number value objects are not equivalent (17.38 and -14.23)");
da91b29a
PP
729 ok(bt_value_compare(bt_value_borrow_from_private(real1),
730 bt_value_borrow_from_private(real3)),
a373bf69
PP
731 "real number value objects are equivalent (17.38 and 17.38)");
732
65300d60
PP
733 BT_OBJECT_PUT_REF_AND_RESET(real1);
734 BT_OBJECT_PUT_REF_AND_RESET(real2);
735 BT_OBJECT_PUT_REF_AND_RESET(real3);
dac5c838
PP
736}
737
738static
739void test_compare_string(void)
740{
da91b29a
PP
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");
dac5c838 747
25583cd0 748 BT_ASSERT(string1 && string2 && string3);
dac5c838 749
da91b29a
PP
750 ok(!bt_value_compare(bt_value_null,
751 bt_value_borrow_from_private(string1)),
dac5c838 752 "cannot compare null value object and string value object");
da91b29a
PP
753 ok(!bt_value_compare(bt_value_borrow_from_private(string1),
754 bt_value_borrow_from_private(string2)),
dac5c838 755 "string value objects are not equivalent (\"hello\" and \"bt_value\")");
da91b29a
PP
756 ok(bt_value_compare(bt_value_borrow_from_private(string1),
757 bt_value_borrow_from_private(string3)),
dac5c838
PP
758 "string value objects are equivalent (\"hello\" and \"hello\")");
759
65300d60
PP
760 BT_OBJECT_PUT_REF_AND_RESET(string1);
761 BT_OBJECT_PUT_REF_AND_RESET(string2);
762 BT_OBJECT_PUT_REF_AND_RESET(string3);
dac5c838
PP
763}
764
765static
766void test_compare_array(void)
767{
da91b29a
PP
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;
dac5c838 772
25583cd0 773 BT_ASSERT(array1 && array2 && array3);
dac5c838 774
da91b29a
PP
775 ok(bt_value_compare(bt_value_borrow_from_private(array1),
776 bt_value_borrow_from_private(array2)),
dac5c838
PP
777 "empty array value objects are equivalent");
778
da91b29a
PP
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)),
dac5c838 806 "cannot compare null value object and array value object");
da91b29a
PP
807 ok(!bt_value_compare(bt_value_borrow_from_private(array1),
808 bt_value_borrow_from_private(array2)),
c55a9f58 809 "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])");
da91b29a
PP
810 ok(bt_value_compare(bt_value_borrow_from_private(array1),
811 bt_value_borrow_from_private(array3)),
c55a9f58 812 "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])");
dac5c838 813
65300d60
PP
814 BT_OBJECT_PUT_REF_AND_RESET(array1);
815 BT_OBJECT_PUT_REF_AND_RESET(array2);
816 BT_OBJECT_PUT_REF_AND_RESET(array3);
dac5c838
PP
817}
818
819static
820void test_compare_map(void)
821{
da91b29a
PP
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;
dac5c838 826
25583cd0 827 BT_ASSERT(map1 && map2 && map3);
dac5c838 828
da91b29a
PP
829 ok(bt_value_compare(bt_value_borrow_from_private(map1),
830 bt_value_borrow_from_private(map2)),
dac5c838
PP
831 "empty map value objects are equivalent");
832
da91b29a
PP
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)),
dac5c838 864 "cannot compare null value object and map value object");
da91b29a
PP
865 ok(!bt_value_compare(bt_value_borrow_from_private(map1),
866 bt_value_borrow_from_private(map2)),
dac5c838 867 "map value objects are not equivalent");
da91b29a
PP
868 ok(bt_value_compare(bt_value_borrow_from_private(map1),
869 bt_value_borrow_from_private(map3)),
dac5c838
PP
870 "map value objects are equivalent");
871
65300d60
PP
872 BT_OBJECT_PUT_REF_AND_RESET(map1);
873 BT_OBJECT_PUT_REF_AND_RESET(map2);
874 BT_OBJECT_PUT_REF_AND_RESET(map3);
dac5c838
PP
875}
876
877static
878void test_compare(void)
879{
dac5c838
PP
880 test_compare_null();
881 test_compare_bool();
882 test_compare_integer();
a373bf69 883 test_compare_real();
dac5c838
PP
884 test_compare_string();
885 test_compare_array();
886 test_compare_map();
887}
888
889static
890void 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,
c55a9f58 895 * and that bt_value_compare() returns BT_TRUE for the top-level
dac5c838
PP
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 */
da91b29a
PP
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();
dac5c838 915
a373bf69 916 BT_ASSERT(bool_obj && integer_obj && real_obj && string_obj &&
dac5c838
PP
917 array_obj && map_obj);
918
da91b29a
PP
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);
dac5c838 937
da91b29a 938 map_copy_obj = bt_value_copy(bt_value_borrow_from_private(map_obj));
dac5c838
PP
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)");
da91b29a
PP
944 string_copy_obj = bt_private_value_map_borrow_entry_value(map_copy_obj,
945 "string");
dac5c838
PP
946 ok(string_copy_obj != string_obj,
947 "bt_value_copy() returns a different pointer (string)");
da91b29a
PP
948 array_copy_obj = bt_private_value_map_borrow_entry_value(map_copy_obj,
949 "array");
dac5c838
PP
950 ok(array_copy_obj != array_obj,
951 "bt_value_copy() returns a different pointer (array)");
da91b29a
PP
952 bool_copy_obj = bt_private_value_array_borrow_element_by_index(
953 array_copy_obj, 0);
dac5c838 954 ok(bool_copy_obj != bool_obj,
c55a9f58 955 "bt_value_copy() returns a different pointer (bt_bool)");
da91b29a
PP
956 integer_copy_obj = bt_private_value_array_borrow_element_by_index(
957 array_copy_obj, 1);
dac5c838
PP
958 ok(integer_copy_obj != integer_obj,
959 "bt_value_copy() returns a different pointer (integer)");
da91b29a
PP
960 real_copy_obj = bt_private_value_array_borrow_element_by_index(
961 array_copy_obj, 2);
a373bf69
PP
962 ok(real_copy_obj != real_obj,
963 "bt_value_copy() returns a different pointer (real)");
da91b29a
PP
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,
dac5c838
PP
967 "bt_value_copy() returns the same pointer (null)");
968
da91b29a
PP
969 ok(bt_value_compare(bt_value_borrow_from_private(map_obj),
970 bt_value_borrow_from_private(map_copy_obj)),
dac5c838
PP
971 "source and destination value objects have the same content");
972
65300d60
PP
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);
dac5c838
PP
980}
981
b6ba7620 982static
c55a9f58 983bt_bool compare_map_elements(struct bt_value *map_a, struct bt_value *map_b,
b6ba7620
PP
984 const char *key)
985{
986 struct bt_value *elem_a = NULL;
987 struct bt_value *elem_b = NULL;
c55a9f58 988 bt_bool equal;
b6ba7620 989
07208d85
PP
990 elem_a = bt_value_map_borrow_entry_value(map_a, key);
991 elem_b = bt_value_map_borrow_entry_value(map_b, key);
b6ba7620 992 equal = bt_value_compare(elem_a, elem_b);
b6ba7620
PP
993 return equal;
994}
995
996static
997void test_extend(void)
998{
da91b29a
PP
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();
b6ba7620
PP
1003 enum bt_value_status status;
1004
25583cd0
PP
1005 BT_ASSERT(base_map);
1006 BT_ASSERT(extension_map);
1007 BT_ASSERT(array);
da91b29a
PP
1008 status = bt_private_value_map_insert_bool_entry(base_map, "file",
1009 BT_TRUE);
25583cd0 1010 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1011 status = bt_private_value_map_insert_bool_entry(base_map, "edit",
1012 BT_FALSE);
25583cd0 1013 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1014 status = bt_private_value_map_insert_integer_entry(base_map,
1015 "selection", 17);
25583cd0 1016 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1017 status = bt_private_value_map_insert_integer_entry(base_map, "find",
1018 -34);
25583cd0 1019 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1020 status = bt_private_value_map_insert_bool_entry(extension_map, "edit",
1021 BT_TRUE);
25583cd0 1022 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1023 status = bt_private_value_map_insert_integer_entry(extension_map,
1024 "find", 101);
25583cd0 1025 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1026 status = bt_private_value_map_insert_real_entry(extension_map,
1027 "project", -404);
25583cd0 1028 BT_ASSERT(status == BT_VALUE_STATUS_OK);
da91b29a
PP
1029 extended_map = bt_value_map_extend(
1030 bt_value_borrow_from_private(base_map),
1031 bt_value_borrow_from_private(extension_map));
b6ba7620 1032 ok(extended_map, "bt_value_map_extend() succeeds");
da91b29a
PP
1033 ok(bt_value_map_get_size(
1034 bt_value_borrow_from_private(extended_map)) == 5,
b6ba7620 1035 "bt_value_map_extend() returns a map object with the correct size");
da91b29a
PP
1036 ok(compare_map_elements(bt_value_borrow_from_private(base_map),
1037 bt_value_borrow_from_private(extended_map), "file"),
b6ba7620 1038 "bt_value_map_extend() picks the appropriate element (file)");
da91b29a
PP
1039 ok(compare_map_elements(bt_value_borrow_from_private(extension_map),
1040 bt_value_borrow_from_private(extended_map), "edit"),
b6ba7620 1041 "bt_value_map_extend() picks the appropriate element (edit)");
da91b29a
PP
1042 ok(compare_map_elements(bt_value_borrow_from_private(base_map),
1043 bt_value_borrow_from_private(extended_map), "selection"),
b6ba7620 1044 "bt_value_map_extend() picks the appropriate element (selection)");
da91b29a
PP
1045 ok(compare_map_elements(bt_value_borrow_from_private(extension_map),
1046 bt_value_borrow_from_private(extended_map), "find"),
b6ba7620 1047 "bt_value_map_extend() picks the appropriate element (find)");
da91b29a
PP
1048 ok(compare_map_elements(bt_value_borrow_from_private(extension_map),
1049 bt_value_borrow_from_private(extended_map), "project"),
b6ba7620
PP
1050 "bt_value_map_extend() picks the appropriate element (project)");
1051
65300d60
PP
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);
b6ba7620
PP
1056}
1057
dac5c838
PP
1058int main(void)
1059{
8bbe269d 1060 plan_tests(NR_TESTS);
dac5c838
PP
1061 test_types();
1062 test_compare();
1063 test_copy();
b6ba7620 1064 test_extend();
dac5c838
PP
1065 return 0;
1066}
This page took 0.102277 seconds and 4 git commands to generate.