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