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