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 | ||
06a0c632 JG |
460 | obj = bt_object_array_get(array_obj, 1); |
461 | assert(obj); | |
a40a567e PP |
462 | ok(bt_object_float_set(obj, 14.52) == BT_OBJECT_STATUS_FROZEN, |
463 | "freezing an array object also freezes its elements"); | |
464 | BT_OBJECT_PUT(obj); | |
465 | ||
4f29b187 PP |
466 | BT_OBJECT_PUT(array_obj); |
467 | pass("putting an existing array object does not cause a crash") | |
468 | } | |
469 | ||
470 | static | |
471 | bool test_map_foreach_cb_count(const char *key, struct bt_object *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 | ||
485 | struct 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 | ||
498 | static | |
499 | bool test_map_foreach_cb_check(const char *key, struct bt_object *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_object_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\" object has the right value"); | |
516 | checklist->bool1 = true; | |
a40a567e PP |
517 | } else { |
518 | fail("test_map_foreach_cb_check(): \"bool\" object has the wrong value"); | |
4f29b187 PP |
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_object_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\" object has the right value"); | |
532 | checklist->int1 = true; | |
a40a567e PP |
533 | } else { |
534 | fail("test_map_foreach_cb_check(): \"int\" object has the wrong value"); | |
4f29b187 PP |
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_object_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\" object has the right value"); | |
548 | checklist->float1 = true; | |
a40a567e PP |
549 | } else { |
550 | fail("test_map_foreach_cb_check(): \"float\" object has the wrong value"); | |
4f29b187 PP |
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_object_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" 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_object_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\" object has the right value"); | |
571 | checklist->bool2 = true; | |
a40a567e PP |
572 | } else { |
573 | fail("test_map_foreach_cb_check(): \"bool2\" object has the wrong value"); | |
4f29b187 PP |
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_object_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\" object has the right value"); | |
587 | checklist->int2 = true; | |
a40a567e PP |
588 | } else { |
589 | fail("test_map_foreach_cb_check(): \"int2\" object has the wrong value"); | |
4f29b187 PP |
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_object_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\" object has the right value"); | |
603 | checklist->float2 = true; | |
a40a567e PP |
604 | } else { |
605 | fail("test_map_foreach_cb_check(): \"float2\" object has the wrong value"); | |
4f29b187 PP |
606 | } |
607 | } | |
608 | } else if (!strcmp(key, "string2")) { | |
609 | if (checklist->string2) { | |
610 | fail("test_map_foreach_cb_check(): duplicate key \"string2\""); | |
611 | } else { | |
a40a567e | 612 | const char *val; |
4f29b187 | 613 | |
a40a567e PP |
614 | ret = bt_object_string_get(object, &val); |
615 | ok(!ret, "test_map_foreach_cb_check(): success getting \"string2\" value"); | |
4f29b187 PP |
616 | |
617 | if (val && !strcmp(val, "bt_object")) { | |
618 | pass("test_map_foreach_cb_check(): \"string2\" object has the right value"); | |
619 | checklist->string2 = true; | |
a40a567e PP |
620 | } else { |
621 | fail("test_map_foreach_cb_check(): \"string2\" object has the wrong value"); | |
4f29b187 PP |
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_object_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" object"); | |
629 | ok(bt_object_array_is_empty(object), | |
630 | "test_map_foreach_cb_check(): \"array2\" 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_object_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" object"); | |
638 | ok(bt_object_map_is_empty(object), | |
639 | "test_map_foreach_cb_check(): \"map2\" object is empty"); | |
640 | checklist->map2 = true; | |
641 | } | |
642 | } else { | |
a40a567e | 643 | fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key); |
4f29b187 PP |
644 | } |
645 | ||
646 | return true; | |
647 | } | |
648 | ||
649 | static | |
650 | void 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_object *obj; | |
658 | struct bt_object *map_obj; | |
659 | struct map_foreach_checklist checklist; | |
660 | ||
661 | map_obj = bt_object_map_create(); | |
662 | ok(map_obj && bt_object_is_map(map_obj), | |
663 | "bt_object_map_create() returns a map object"); | |
664 | ok(bt_object_map_size(map_obj) == 0, | |
665 | "initial map object size is 0"); | |
a40a567e | 666 | ok(bt_object_map_size(NULL) == BT_OBJECT_STATUS_INVAL, |
4f29b187 PP |
667 | "bt_object_map_size() fails with a map object set to NULL"); |
668 | ||
a40a567e PP |
669 | ok(bt_object_map_insert(NULL, "hello", bt_object_null) == |
670 | BT_OBJECT_STATUS_INVAL, | |
4f29b187 | 671 | "bt_object_array_insert() fails with a map object set to NULL"); |
a40a567e PP |
672 | ok(bt_object_map_insert(map_obj, NULL, bt_object_null) == |
673 | BT_OBJECT_STATUS_INVAL, | |
4f29b187 | 674 | "bt_object_array_insert() fails with a key set to NULL"); |
a40a567e PP |
675 | ok(bt_object_map_insert(map_obj, "yeah", NULL) == |
676 | BT_OBJECT_STATUS_INVAL, | |
4f29b187 PP |
677 | "bt_object_array_insert() fails with an element object set to NULL"); |
678 | ||
679 | obj = bt_object_integer_create_init(19457); | |
680 | ret = bt_object_map_insert(map_obj, "int", obj); | |
681 | BT_OBJECT_PUT(obj); | |
682 | obj = bt_object_float_create_init(5.444); | |
683 | ret |= bt_object_map_insert(map_obj, "float", obj); | |
684 | BT_OBJECT_PUT(obj); | |
685 | obj = bt_object_bool_create(); | |
686 | ret |= bt_object_map_insert(map_obj, "bool", obj); | |
687 | BT_OBJECT_PUT(obj); | |
688 | ret |= bt_object_map_insert(map_obj, "null", bt_object_null); | |
689 | ok(!ret, "bt_object_map_insert() succeeds"); | |
690 | ok(bt_object_map_size(map_obj) == 4, | |
691 | "inserting an element into a map object increment its size"); | |
692 | ||
693 | obj = bt_object_bool_create_init(true); | |
694 | ret = bt_object_map_insert(map_obj, "bool", obj); | |
695 | BT_OBJECT_PUT(obj); | |
696 | ok(!ret, "bt_object_map_insert() accepts an existing key"); | |
697 | ||
698 | obj = bt_object_map_get(map_obj, NULL); | |
699 | ok(!obj, "bt_object_map_get() fails with a key set to NULL"); | |
700 | obj = bt_object_map_get(NULL, "bool"); | |
701 | ok(!obj, "bt_object_map_get() fails with a map object set to NULL"); | |
702 | ||
703 | obj = bt_object_map_get(map_obj, "life"); | |
704 | ok(!obj, "bt_object_map_get() fails with an non existing key"); | |
705 | obj = bt_object_map_get(map_obj, "float"); | |
706 | ok(obj && bt_object_is_float(obj), | |
707 | "bt_object_map_get() returns an object with the appropriate type (float)"); | |
708 | ret = bt_object_float_get(obj, &float_value); | |
709 | ok(!ret && float_value == 5.444, | |
710 | "bt_object_map_get() returns an object with the appropriate value (float)"); | |
711 | BT_OBJECT_PUT(obj); | |
712 | obj = bt_object_map_get(map_obj, "int"); | |
713 | ok(obj && bt_object_is_integer(obj), | |
714 | "bt_object_map_get() returns an object with the appropriate type (integer)"); | |
715 | ret = bt_object_integer_get(obj, &int_value); | |
716 | ok(!ret && int_value == 19457, | |
717 | "bt_object_map_get() returns an object with the appropriate value (integer)"); | |
718 | BT_OBJECT_PUT(obj); | |
719 | obj = bt_object_map_get(map_obj, "null"); | |
720 | ok(obj && bt_object_is_null(obj), | |
721 | "bt_object_map_get() returns an object with the appropriate type (null)"); | |
722 | obj = bt_object_map_get(map_obj, "bool"); | |
723 | ok(obj && bt_object_is_bool(obj), | |
724 | "bt_object_map_get() returns an object with the appropriate type (boolean)"); | |
725 | ret = bt_object_bool_get(obj, &bool_value); | |
726 | ok(!ret && bool_value, | |
727 | "bt_object_map_get() returns an object with the appropriate value (boolean)"); | |
728 | BT_OBJECT_PUT(obj); | |
729 | ||
730 | ret = bt_object_map_insert_bool(map_obj, "bool2", true); | |
731 | ok(!ret, "bt_object_map_insert_bool() succeeds"); | |
a40a567e PP |
732 | ok(bt_object_map_insert_bool(NULL, "bool2", false) == |
733 | BT_OBJECT_STATUS_INVAL, | |
734 | "bt_object_map_insert_bool() fails with a map object set to NULL"); | |
4f29b187 PP |
735 | ret = bt_object_map_insert_integer(map_obj, "int2", 98765); |
736 | ok(!ret, "bt_object_map_insert_integer() succeeds"); | |
a40a567e PP |
737 | ok(bt_object_map_insert_integer(NULL, "int2", 1001) == |
738 | BT_OBJECT_STATUS_INVAL, | |
739 | "bt_object_map_insert_integer() fails with a map object set to NULL"); | |
4f29b187 PP |
740 | ret = bt_object_map_insert_float(map_obj, "float2", -49.0001); |
741 | ok(!ret, "bt_object_map_insert_float() succeeds"); | |
a40a567e PP |
742 | ok(bt_object_map_insert_float(NULL, "float2", 495) == |
743 | BT_OBJECT_STATUS_INVAL, | |
744 | "bt_object_map_insert_float() fails with a map object set to NULL"); | |
4f29b187 PP |
745 | ret = bt_object_map_insert_string(map_obj, "string2", "bt_object"); |
746 | ok(!ret, "bt_object_map_insert_string() succeeds"); | |
a40a567e PP |
747 | ok(bt_object_map_insert_string(NULL, "string2", "bt_obj") == |
748 | BT_OBJECT_STATUS_INVAL, | |
749 | "bt_object_map_insert_string() fails with a map object set to NULL"); | |
4f29b187 PP |
750 | ret = bt_object_map_insert_array(map_obj, "array2"); |
751 | ok(!ret, "bt_object_map_insert_array() succeeds"); | |
a40a567e PP |
752 | ok(bt_object_map_insert_array(NULL, "array2") == BT_OBJECT_STATUS_INVAL, |
753 | "bt_object_map_insert_array() fails with a map object set to NULL"); | |
4f29b187 PP |
754 | ret = bt_object_map_insert_map(map_obj, "map2"); |
755 | ok(!ret, "bt_object_map_insert_map() succeeds"); | |
a40a567e PP |
756 | ok(bt_object_map_insert_map(NULL, "map2") == BT_OBJECT_STATUS_INVAL, |
757 | "bt_object_map_insert_map() fails with a map object set to NULL"); | |
4f29b187 PP |
758 | |
759 | ok(bt_object_map_size(map_obj) == 10, | |
760 | "the bt_object_map_insert*() functions increment the map object's size"); | |
761 | ||
762 | ok(!bt_object_map_has_key(map_obj, "hello"), | |
763 | "map object does not have key \"hello\""); | |
764 | ok(bt_object_map_has_key(map_obj, "bool"), | |
765 | "map object has key \"bool\""); | |
766 | ok(bt_object_map_has_key(map_obj, "int"), | |
767 | "map object has key \"int\""); | |
768 | ok(bt_object_map_has_key(map_obj, "float"), | |
769 | "map object has key \"float\""); | |
770 | ok(bt_object_map_has_key(map_obj, "null"), | |
771 | "map object has key \"null\""); | |
772 | ok(bt_object_map_has_key(map_obj, "bool2"), | |
773 | "map object has key \"bool2\""); | |
774 | ok(bt_object_map_has_key(map_obj, "int2"), | |
775 | "map object has key \"int2\""); | |
776 | ok(bt_object_map_has_key(map_obj, "float2"), | |
777 | "map object has key \"float2\""); | |
778 | ok(bt_object_map_has_key(map_obj, "string2"), | |
779 | "map object has key \"string2\""); | |
780 | ok(bt_object_map_has_key(map_obj, "array2"), | |
781 | "map object has key \"array2\""); | |
782 | ok(bt_object_map_has_key(map_obj, "map2"), | |
783 | "map object has key \"map2\""); | |
784 | ||
a40a567e PP |
785 | ok(bt_object_map_foreach(NULL, test_map_foreach_cb_count, &count) == |
786 | BT_OBJECT_STATUS_INVAL, | |
4a512e75 | 787 | "bt_object_map_foreach() fails with a map object set to NULL"); |
a40a567e PP |
788 | ok(bt_object_map_foreach(map_obj, NULL, &count) == |
789 | BT_OBJECT_STATUS_INVAL, | |
4a512e75 | 790 | "bt_object_map_foreach() fails with a user function set to NULL"); |
4f29b187 | 791 | ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_count, &count); |
4a512e75 | 792 | ok(ret == BT_OBJECT_STATUS_CANCELLED && count == 3, |
4f29b187 PP |
793 | "bt_object_map_foreach() breaks the loop when the user function returns false"); |
794 | ||
795 | memset(&checklist, 0, sizeof(checklist)); | |
796 | ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_check, | |
797 | &checklist); | |
4a512e75 PP |
798 | ok(ret == BT_OBJECT_STATUS_OK, |
799 | "bt_object_map_foreach() succeeds with test_map_foreach_cb_check()"); | |
4f29b187 PP |
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_object_map_foreach() iterates over all the map object's elements"); | |
805 | ||
a40a567e PP |
806 | assert(!bt_object_freeze(map_obj)); |
807 | ok(bt_object_map_insert(map_obj, "allo", bt_object_null) == | |
808 | BT_OBJECT_STATUS_FROZEN, | |
809 | "bt_object_map_insert() fails with a frozen map object"); | |
810 | ok(bt_object_map_insert_bool(map_obj, "duh", false) == | |
811 | BT_OBJECT_STATUS_FROZEN, | |
812 | "bt_object_map_insert_bool() fails with a frozen array object"); | |
813 | ok(bt_object_map_insert_integer(map_obj, "duh", 23) == | |
814 | BT_OBJECT_STATUS_FROZEN, | |
815 | "bt_object_map_insert_integer() fails with a frozen array object"); | |
816 | ok(bt_object_map_insert_float(map_obj, "duh", 2.34) == | |
817 | BT_OBJECT_STATUS_FROZEN, | |
818 | "bt_object_map_insert_float() fails with a frozen array object"); | |
819 | ok(bt_object_map_insert_string(map_obj, "duh", "yayayayaya") == | |
820 | BT_OBJECT_STATUS_FROZEN, | |
821 | "bt_object_map_insert_string() fails with a frozen array object"); | |
822 | ok(bt_object_map_insert_array(map_obj, "duh") == | |
823 | BT_OBJECT_STATUS_FROZEN, | |
824 | "bt_object_map_insert_array() fails with a frozen array object"); | |
825 | ok(bt_object_map_insert_map(map_obj, "duh") == | |
826 | BT_OBJECT_STATUS_FROZEN, | |
827 | "bt_object_map_insert_map() fails with a frozen array object"); | |
828 | ok(bt_object_map_size(map_obj) == 10, | |
829 | "appending to a frozen map object does not change its size"); | |
830 | ||
4f29b187 PP |
831 | BT_OBJECT_PUT(map_obj); |
832 | pass("putting an existing map object does not cause a crash") | |
833 | } | |
834 | ||
835 | static | |
836 | void 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 | ||
847 | static | |
848 | void test_compare_null(void) | |
849 | { | |
850 | ok(!bt_object_compare(bt_object_null, NULL), | |
851 | "cannot compare null object and NULL"); | |
852 | ok(!bt_object_compare(NULL, bt_object_null), | |
853 | "cannot compare NULL and null object"); | |
854 | ok(bt_object_compare(bt_object_null, bt_object_null), | |
855 | "null objects are equivalent"); | |
856 | } | |
857 | ||
858 | static | |
859 | void test_compare_bool(void) | |
860 | { | |
861 | struct bt_object *bool1 = bt_object_bool_create_init(false); | |
862 | struct bt_object *bool2 = bt_object_bool_create_init(true); | |
863 | struct bt_object *bool3 = bt_object_bool_create_init(false); | |
864 | ||
865 | assert(bool1 && bool2 && bool3); | |
866 | ok(!bt_object_compare(bt_object_null, bool1), | |
867 | "cannot compare null object and bool object"); | |
868 | ok(!bt_object_compare(bool1, bool2), | |
869 | "integer objects are not equivalent (false and true)"); | |
870 | ok(bt_object_compare(bool1, bool3), | |
871 | "integer objects are equivalent (false and false)"); | |
872 | ||
873 | BT_OBJECT_PUT(bool1); | |
874 | BT_OBJECT_PUT(bool2); | |
875 | BT_OBJECT_PUT(bool3); | |
876 | } | |
877 | ||
878 | static | |
879 | void test_compare_integer(void) | |
880 | { | |
881 | struct bt_object *int1 = bt_object_integer_create_init(10); | |
882 | struct bt_object *int2 = bt_object_integer_create_init(-23); | |
883 | struct bt_object *int3 = bt_object_integer_create_init(10); | |
884 | ||
885 | assert(int1 && int2 && int3); | |
886 | ok(!bt_object_compare(bt_object_null, int1), | |
887 | "cannot compare null object and integer object"); | |
888 | ok(!bt_object_compare(int1, int2), | |
889 | "integer objects are not equivalent (10 and -23)"); | |
890 | ok(bt_object_compare(int1, int3), | |
891 | "integer objects are equivalent (10 and 10)"); | |
892 | ||
893 | BT_OBJECT_PUT(int1); | |
894 | BT_OBJECT_PUT(int2); | |
895 | BT_OBJECT_PUT(int3); | |
896 | } | |
897 | ||
898 | static | |
899 | void test_compare_float(void) | |
900 | { | |
901 | struct bt_object *float1 = bt_object_float_create_init(17.38); | |
902 | struct bt_object *float2 = bt_object_float_create_init(-14.23); | |
903 | struct bt_object *float3 = bt_object_float_create_init(17.38); | |
904 | ||
905 | assert(float1 && float2 && float3); | |
906 | ||
907 | ok(!bt_object_compare(bt_object_null, float1), | |
908 | "cannot compare null object and floating point number object"); | |
909 | ok(!bt_object_compare(float1, float2), | |
910 | "floating point number objects are not equivalent (17.38 and -14.23)"); | |
911 | ok(bt_object_compare(float1, float3), | |
912 | "floating point number objects are equivalent (17.38 and 17.38)"); | |
913 | ||
914 | BT_OBJECT_PUT(float1); | |
915 | BT_OBJECT_PUT(float2); | |
916 | BT_OBJECT_PUT(float3); | |
917 | } | |
918 | ||
919 | static | |
920 | void test_compare_string(void) | |
921 | { | |
922 | struct bt_object *string1 = bt_object_string_create_init("hello"); | |
923 | struct bt_object *string2 = bt_object_string_create_init("bt_object"); | |
924 | struct bt_object *string3 = bt_object_string_create_init("hello"); | |
925 | ||
926 | assert(string1 && string2 && string3); | |
927 | ||
928 | ok(!bt_object_compare(bt_object_null, string1), | |
929 | "cannot compare null object and string object"); | |
930 | ok(!bt_object_compare(string1, string2), | |
931 | "string objects are not equivalent (\"hello\" and \"bt_object\")"); | |
932 | ok(bt_object_compare(string1, string3), | |
933 | "string objects are equivalent (\"hello\" and \"hello\")"); | |
934 | ||
935 | BT_OBJECT_PUT(string1); | |
936 | BT_OBJECT_PUT(string2); | |
937 | BT_OBJECT_PUT(string3); | |
938 | } | |
939 | ||
940 | static | |
941 | void test_compare_array(void) | |
942 | { | |
943 | struct bt_object *array1 = bt_object_array_create(); | |
944 | struct bt_object *array2 = bt_object_array_create(); | |
945 | struct bt_object *array3 = bt_object_array_create(); | |
946 | ||
947 | assert(array1 && array2 && array3); | |
948 | ||
949 | ok(bt_object_compare(array1, array2), | |
950 | "empty array objects are equivalent"); | |
951 | ||
952 | assert(!bt_object_array_append_integer(array1, 23)); | |
953 | assert(!bt_object_array_append_float(array1, 14.2)); | |
954 | assert(!bt_object_array_append_bool(array1, false)); | |
955 | assert(!bt_object_array_append_float(array2, 14.2)); | |
956 | assert(!bt_object_array_append_integer(array2, 23)); | |
957 | assert(!bt_object_array_append_bool(array2, false)); | |
958 | assert(!bt_object_array_append_integer(array3, 23)); | |
959 | assert(!bt_object_array_append_float(array3, 14.2)); | |
960 | assert(!bt_object_array_append_bool(array3, false)); | |
961 | assert(bt_object_array_size(array1) == 3); | |
962 | assert(bt_object_array_size(array2) == 3); | |
963 | assert(bt_object_array_size(array3) == 3); | |
964 | ||
965 | ok(!bt_object_compare(bt_object_null, array1), | |
966 | "cannot compare null object and array object"); | |
967 | ok(!bt_object_compare(array1, array2), | |
968 | "array objects are not equivalent ([23, 14.2, false] and [14.2, 23, false])"); | |
969 | ok(bt_object_compare(array1, array3), | |
970 | "array objects are equivalent ([23, 14.2, false] and [23, 14.2, false])"); | |
971 | ||
972 | BT_OBJECT_PUT(array1); | |
973 | BT_OBJECT_PUT(array2); | |
974 | BT_OBJECT_PUT(array3); | |
975 | } | |
976 | ||
977 | static | |
978 | void test_compare_map(void) | |
979 | { | |
980 | struct bt_object *map1 = bt_object_map_create(); | |
981 | struct bt_object *map2 = bt_object_map_create(); | |
982 | struct bt_object *map3 = bt_object_map_create(); | |
983 | ||
984 | assert(map1 && map2 && map3); | |
985 | ||
986 | ok(bt_object_compare(map1, map2), | |
987 | "empty map objects are equivalent"); | |
988 | ||
989 | assert(!bt_object_map_insert_integer(map1, "one", 23)); | |
990 | assert(!bt_object_map_insert_float(map1, "two", 14.2)); | |
991 | assert(!bt_object_map_insert_bool(map1, "three", false)); | |
992 | assert(!bt_object_map_insert_float(map2, "one", 14.2)); | |
993 | assert(!bt_object_map_insert_integer(map2, "two", 23)); | |
994 | assert(!bt_object_map_insert_bool(map2, "three", false)); | |
995 | assert(!bt_object_map_insert_bool(map3, "three", false)); | |
996 | assert(!bt_object_map_insert_integer(map3, "one", 23)); | |
997 | assert(!bt_object_map_insert_float(map3, "two", 14.2)); | |
998 | assert(bt_object_map_size(map1) == 3); | |
999 | assert(bt_object_map_size(map2) == 3); | |
1000 | assert(bt_object_map_size(map3) == 3); | |
1001 | ||
1002 | ok(!bt_object_compare(bt_object_null, map1), | |
1003 | "cannot compare null object and map object"); | |
1004 | ok(!bt_object_compare(map1, map2), | |
1005 | "map objects are not equivalent"); | |
1006 | ok(bt_object_compare(map1, map3), | |
1007 | "map objects are equivalent"); | |
1008 | ||
1009 | BT_OBJECT_PUT(map1); | |
1010 | BT_OBJECT_PUT(map2); | |
1011 | BT_OBJECT_PUT(map3); | |
1012 | } | |
1013 | ||
1014 | static | |
1015 | void test_compare(void) | |
1016 | { | |
1017 | ok(!bt_object_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 | ||
1027 | static | |
1028 | void test_copy(void) | |
1029 | { | |
1030 | /* | |
1031 | * Here's the deal here. If we make sure that each object | |
1032 | * of our deep copy has a different address than its source, | |
1033 | * and that bt_object_compare() returns true for the top-level | |
1034 | * object, taking into account that we test the correctness of | |
1035 | * bt_object_compare() elsewhere, then the deep copy is a | |
1036 | * success. | |
1037 | */ | |
1038 | struct bt_object *null_copy_obj; | |
1039 | struct bt_object *bool_obj, *bool_copy_obj; | |
1040 | struct bt_object *integer_obj, *integer_copy_obj; | |
1041 | struct bt_object *float_obj, *float_copy_obj; | |
1042 | struct bt_object *string_obj, *string_copy_obj; | |
1043 | struct bt_object *array_obj, *array_copy_obj; | |
1044 | struct bt_object *map_obj, *map_copy_obj; | |
1045 | ||
1046 | bool_obj = bt_object_bool_create_init(true); | |
1047 | integer_obj = bt_object_integer_create_init(23); | |
1048 | float_obj = bt_object_float_create_init(-3.1416); | |
1049 | string_obj = bt_object_string_create_init("test"); | |
1050 | array_obj = bt_object_array_create(); | |
1051 | map_obj = bt_object_map_create(); | |
1052 | ||
1053 | assert(bool_obj && integer_obj && float_obj && string_obj && | |
1054 | array_obj && map_obj); | |
1055 | ||
1056 | assert(!bt_object_array_append(array_obj, bool_obj)); | |
1057 | assert(!bt_object_array_append(array_obj, integer_obj)); | |
1058 | assert(!bt_object_array_append(array_obj, float_obj)); | |
1059 | assert(!bt_object_array_append(array_obj, bt_object_null)); | |
1060 | assert(!bt_object_map_insert(map_obj, "array", array_obj)); | |
1061 | assert(!bt_object_map_insert(map_obj, "string", string_obj)); | |
1062 | ||
1063 | map_copy_obj = bt_object_copy(NULL); | |
1064 | ok(!map_copy_obj, | |
1065 | "bt_object_copy() fails with a source object set to NULL"); | |
1066 | ||
1067 | map_copy_obj = bt_object_copy(map_obj); | |
1068 | ok(map_copy_obj, | |
1069 | "bt_object_copy() succeeds"); | |
1070 | ||
1071 | ok(map_obj != map_copy_obj, | |
1072 | "bt_object_copy() returns a different pointer (map)"); | |
1073 | string_copy_obj = bt_object_map_get(map_copy_obj, "string"); | |
1074 | ok(string_copy_obj != string_obj, | |
1075 | "bt_object_copy() returns a different pointer (string)"); | |
1076 | array_copy_obj = bt_object_map_get(map_copy_obj, "array"); | |
1077 | ok(array_copy_obj != array_obj, | |
1078 | "bt_object_copy() returns a different pointer (array)"); | |
1079 | bool_copy_obj = bt_object_array_get(array_copy_obj, 0); | |
1080 | ok(bool_copy_obj != bool_obj, | |
1081 | "bt_object_copy() returns a different pointer (bool)"); | |
1082 | integer_copy_obj = bt_object_array_get(array_copy_obj, 1); | |
1083 | ok(integer_copy_obj != integer_obj, | |
1084 | "bt_object_copy() returns a different pointer (integer)"); | |
1085 | float_copy_obj = bt_object_array_get(array_copy_obj, 2); | |
1086 | ok(float_copy_obj != float_obj, | |
1087 | "bt_object_copy() returns a different pointer (float)"); | |
1088 | null_copy_obj = bt_object_array_get(array_copy_obj, 3); | |
1089 | ok(null_copy_obj == bt_object_null, | |
1090 | "bt_object_copy() returns the same pointer (null)"); | |
1091 | ||
1092 | ok(bt_object_compare(map_obj, map_copy_obj), | |
1093 | "source and destination objects have the same content"); | |
1094 | ||
1095 | BT_OBJECT_PUT(bool_copy_obj); | |
1096 | BT_OBJECT_PUT(integer_copy_obj); | |
1097 | BT_OBJECT_PUT(float_copy_obj); | |
1098 | BT_OBJECT_PUT(string_copy_obj); | |
1099 | BT_OBJECT_PUT(array_copy_obj); | |
1100 | BT_OBJECT_PUT(map_copy_obj); | |
1101 | BT_OBJECT_PUT(bool_obj); | |
1102 | BT_OBJECT_PUT(integer_obj); | |
1103 | BT_OBJECT_PUT(float_obj); | |
1104 | BT_OBJECT_PUT(string_obj); | |
1105 | BT_OBJECT_PUT(array_obj); | |
1106 | BT_OBJECT_PUT(map_obj); | |
1107 | } | |
1108 | ||
1109 | static | |
1110 | void test_macros(void) | |
1111 | { | |
1112 | struct bt_object *obj = bt_object_bool_create(); | |
1113 | struct bt_object *src; | |
1114 | struct bt_object *dst; | |
1115 | ||
1116 | assert(obj); | |
1117 | BT_OBJECT_PUT(obj); | |
1118 | ok(!obj, "BT_OBJECT_PUT() resets the variable to NULL"); | |
1119 | ||
1120 | obj = bt_object_bool_create(); | |
1121 | assert(obj); | |
1122 | src = obj; | |
1123 | BT_OBJECT_MOVE(dst, src); | |
1124 | ok(!src, "BT_OBJECT_MOVE() resets the source variable to NULL"); | |
1125 | ok(dst == obj, "BT_OBJECT_MOVE() moves the ownership"); | |
1126 | ||
1127 | BT_OBJECT_PUT(dst); | |
1128 | } | |
1129 | ||
a40a567e PP |
1130 | static |
1131 | void test_freeze(void) | |
1132 | { | |
1133 | struct bt_object *obj; | |
1134 | ||
1135 | ok(bt_object_freeze(NULL) == BT_OBJECT_STATUS_INVAL, | |
1136 | "bt_object_freeze() fails with an object set to NULL"); | |
1137 | ok(!bt_object_freeze(bt_object_null), | |
1138 | "bt_object_freeze() succeeds with a null object"); | |
1139 | ||
1140 | ok(!bt_object_is_frozen(NULL), "NULL is not frozen"); | |
1141 | ok(bt_object_is_frozen(bt_object_null), | |
1142 | "the null singleton is frozen"); | |
06a0c632 JG |
1143 | obj = bt_object_integer_create(); |
1144 | assert(obj); | |
a40a567e PP |
1145 | ok(!bt_object_is_frozen(obj), |
1146 | "bt_object_is_frozen() returns false with a fresh object"); | |
1147 | assert(!bt_object_freeze(obj)); | |
1148 | ok(!bt_object_freeze(obj), | |
1149 | "bt_object_freeze() passes with a frozen object"); | |
1150 | ok(bt_object_is_frozen(obj), | |
1151 | "bt_object_is_frozen() returns true with a frozen object"); | |
1152 | ||
1153 | BT_OBJECT_PUT(obj); | |
1154 | } | |
1155 | ||
4f29b187 PP |
1156 | int main(void) |
1157 | { | |
1158 | plan_no_plan(); | |
1159 | ||
1160 | test_macros(); | |
a40a567e | 1161 | test_freeze(); |
4f29b187 PP |
1162 | test_types(); |
1163 | test_compare(); | |
1164 | test_copy(); | |
1165 | ||
1166 | return 0; | |
1167 | } |