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