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