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