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