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 | ||
27c61ce8 | 28 | #define NR_TESTS 190 |
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 | |
27c61ce8 PP |
371 | bt_value_map_foreach_entry_func_status test_map_foreach_cb_count( |
372 | const char *key, bt_value *object, | |
21d04293 PP |
373 | void *data) |
374 | { | |
375 | int *count = data; | |
376 | ||
377 | if (*count == 3) { | |
27c61ce8 PP |
378 | return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_INTERRUPT; |
379 | } else if (*count == 4) { | |
380 | return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR; | |
381 | } else if (*count == 5) { | |
382 | return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_MEMORY_ERROR; | |
21d04293 PP |
383 | } |
384 | ||
385 | (*count)++; | |
386 | ||
27c61ce8 | 387 | return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK; |
21d04293 PP |
388 | } |
389 | ||
390 | struct map_foreach_checklist { | |
391 | bt_bool bool1; | |
392 | bt_bool uint; | |
393 | bt_bool int1; | |
394 | bt_bool real1; | |
395 | bt_bool null1; | |
396 | bt_bool bool2; | |
397 | bt_bool int2; | |
398 | bt_bool real2; | |
399 | bt_bool string2; | |
400 | bt_bool array2; | |
847ab606 | 401 | bt_bool array3; |
21d04293 | 402 | bt_bool map2; |
847ab606 | 403 | bt_bool map3; |
21d04293 PP |
404 | }; |
405 | ||
406 | static | |
27c61ce8 PP |
407 | bt_value_map_foreach_entry_func_status test_map_foreach_cb_check( |
408 | const char *key, bt_value *object, void *data) | |
21d04293 PP |
409 | { |
410 | struct map_foreach_checklist *checklist = data; | |
411 | ||
2242b43d | 412 | if (strcmp(key, "bt_bool") == 0) { |
21d04293 PP |
413 | if (checklist->bool1) { |
414 | fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\""); | |
415 | } else { | |
416 | bt_bool val = BT_FALSE; | |
417 | ||
418 | val = bt_value_bool_get(object); | |
419 | ||
420 | if (val) { | |
421 | pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value"); | |
422 | checklist->bool1 = BT_TRUE; | |
423 | } else { | |
424 | fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value"); | |
425 | } | |
426 | } | |
2242b43d | 427 | } else if (strcmp(key, "uint") == 0) { |
21d04293 PP |
428 | if (checklist->uint) { |
429 | fail("test_map_foreach_cb_check(): duplicate key \"uint\""); | |
430 | } else { | |
431 | uint64_t val = 0; | |
432 | ||
9c08c816 | 433 | val = bt_value_integer_unsigned_get(object); |
21d04293 PP |
434 | |
435 | if (val == 19457) { | |
436 | pass("test_map_foreach_cb_check(): \"uint\" value object has the right value"); | |
437 | checklist->uint = BT_TRUE; | |
438 | } else { | |
439 | fail("test_map_foreach_cb_check(): \"uint\" value object has the wrong value"); | |
440 | } | |
441 | } | |
2242b43d | 442 | } else if (strcmp(key, "int") == 0) { |
21d04293 PP |
443 | if (checklist->int1) { |
444 | fail("test_map_foreach_cb_check(): duplicate key \"int\""); | |
445 | } else { | |
446 | int64_t val = 0; | |
447 | ||
9c08c816 | 448 | val = bt_value_integer_signed_get(object); |
21d04293 PP |
449 | |
450 | if (val == -12345) { | |
451 | pass("test_map_foreach_cb_check(): \"int\" value object has the right value"); | |
452 | checklist->int1 = BT_TRUE; | |
453 | } else { | |
454 | fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value"); | |
455 | } | |
456 | } | |
2242b43d | 457 | } else if (strcmp(key, "real") == 0) { |
21d04293 PP |
458 | if (checklist->real1) { |
459 | fail("test_map_foreach_cb_check(): duplicate key \"real\""); | |
460 | } else { | |
461 | double val = 0; | |
462 | ||
463 | val = bt_value_real_get(object); | |
464 | ||
465 | if (val == 5.444) { | |
466 | pass("test_map_foreach_cb_check(): \"real\" value object has the right value"); | |
467 | checklist->real1 = BT_TRUE; | |
468 | } else { | |
469 | fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value"); | |
470 | } | |
471 | } | |
2242b43d | 472 | } else if (strcmp(key, "null") == 0) { |
21d04293 PP |
473 | if (checklist->null1) { |
474 | fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\""); | |
475 | } else { | |
476 | ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object"); | |
477 | checklist->null1 = BT_TRUE; | |
478 | } | |
2242b43d | 479 | } else if (strcmp(key, "bool2") == 0) { |
21d04293 PP |
480 | if (checklist->bool2) { |
481 | fail("test_map_foreach_cb_check(): duplicate key \"bool2\""); | |
482 | } else { | |
483 | bt_bool val = BT_FALSE; | |
484 | ||
485 | val = bt_value_bool_get(object); | |
486 | ||
487 | if (val) { | |
488 | pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value"); | |
489 | checklist->bool2 = BT_TRUE; | |
490 | } else { | |
491 | fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value"); | |
492 | } | |
493 | } | |
2242b43d | 494 | } else if (strcmp(key, "int2") == 0) { |
21d04293 PP |
495 | if (checklist->int2) { |
496 | fail("test_map_foreach_cb_check(): duplicate key \"int2\""); | |
497 | } else { | |
498 | int64_t val = 0; | |
499 | ||
9c08c816 | 500 | val = bt_value_integer_signed_get(object); |
21d04293 PP |
501 | |
502 | if (val == 98765) { | |
503 | pass("test_map_foreach_cb_check(): \"int2\" value object has the right value"); | |
504 | checklist->int2 = BT_TRUE; | |
505 | } else { | |
506 | fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value"); | |
507 | } | |
508 | } | |
2242b43d | 509 | } else if (strcmp(key, "real2") == 0) { |
21d04293 PP |
510 | if (checklist->real2) { |
511 | fail("test_map_foreach_cb_check(): duplicate key \"real2\""); | |
512 | } else { | |
513 | double val = 0; | |
514 | ||
515 | val = bt_value_real_get(object); | |
516 | ||
517 | if (val == -49.0001) { | |
518 | pass("test_map_foreach_cb_check(): \"real2\" value object has the right value"); | |
519 | checklist->real2 = BT_TRUE; | |
520 | } else { | |
521 | fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value"); | |
522 | } | |
523 | } | |
2242b43d | 524 | } else if (strcmp(key, "string2") == 0) { |
21d04293 PP |
525 | if (checklist->string2) { |
526 | fail("test_map_foreach_cb_check(): duplicate key \"string2\""); | |
527 | } else { | |
528 | const char *val; | |
529 | ||
530 | val = bt_value_string_get(object); | |
531 | ||
2242b43d | 532 | if (val && strcmp(val, "bt_value") == 0) { |
21d04293 PP |
533 | pass("test_map_foreach_cb_check(): \"string2\" value object has the right value"); |
534 | checklist->string2 = BT_TRUE; | |
535 | } else { | |
536 | fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value"); | |
537 | } | |
538 | } | |
2242b43d | 539 | } else if (strcmp(key, "array2") == 0) { |
21d04293 PP |
540 | if (checklist->array2) { |
541 | fail("test_map_foreach_cb_check(): duplicate key \"array2\""); | |
542 | } else { | |
543 | ok(bt_value_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" value object"); | |
544 | ok(bt_value_array_is_empty(object), | |
545 | "test_map_foreach_cb_check(): \"array2\" value object is empty"); | |
546 | checklist->array2 = BT_TRUE; | |
547 | } | |
847ab606 PP |
548 | } else if (strcmp(key, "array3") == 0) { |
549 | if (checklist->array3) { | |
550 | fail("test_map_foreach_cb_check(): duplicate key \"array3\""); | |
551 | } else { | |
552 | ok(bt_value_is_array(object), "test_map_foreach_cb_check(): success getting \"array3\" value object"); | |
553 | ok(bt_value_array_is_empty(object), | |
554 | "test_map_foreach_cb_check(): \"array3\" value object is empty"); | |
555 | checklist->array3 = BT_TRUE; | |
556 | } | |
557 | } else if (strcmp(key, "map3") == 0) { | |
558 | if (checklist->map3) { | |
559 | fail("test_map_foreach_cb_check(): duplicate key \"map3\""); | |
560 | } else { | |
561 | ok(bt_value_is_map(object), "test_map_foreach_cb_check(): success getting \"map3\" value object"); | |
562 | ok(bt_value_map_is_empty(object), | |
563 | "test_map_foreach_cb_check(): \"map3\" value object is empty"); | |
564 | checklist->map3 = BT_TRUE; | |
565 | } | |
2242b43d | 566 | } else if (strcmp(key, "map2") == 0) { |
21d04293 PP |
567 | if (checklist->map2) { |
568 | fail("test_map_foreach_cb_check(): duplicate key \"map2\""); | |
569 | } else { | |
570 | ok(bt_value_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" value object"); | |
571 | ok(bt_value_map_is_empty(object), | |
572 | "test_map_foreach_cb_check(): \"map2\" value object is empty"); | |
573 | checklist->map2 = BT_TRUE; | |
574 | } | |
575 | } else { | |
576 | fail("test_map_foreach_cb_check(): unknown map key \"%s\"", | |
577 | key); | |
578 | } | |
579 | ||
27c61ce8 | 580 | return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK; |
21d04293 PP |
581 | } |
582 | ||
583 | static | |
584 | void test_map(void) | |
585 | { | |
586 | int ret; | |
587 | int count = 0; | |
588 | bt_bool bool_value; | |
589 | int64_t int_value; | |
590 | double real_value; | |
591 | bt_value *obj; | |
592 | bt_value *map_obj; | |
847ab606 | 593 | bt_value *inserted_obj; |
21d04293 PP |
594 | struct map_foreach_checklist checklist; |
595 | ||
596 | map_obj = bt_value_map_create(); | |
597 | ok(map_obj && bt_value_is_map(map_obj), | |
598 | "bt_value_map_create() returns a map value object"); | |
599 | ok(bt_value_map_get_size(map_obj) == 0, | |
600 | "initial map value object size is 0"); | |
601 | ||
9c08c816 | 602 | obj = bt_value_integer_unsigned_create_init(19457); |
21d04293 PP |
603 | ret = bt_value_map_insert_entry(map_obj, "uint", obj); |
604 | BT_VALUE_PUT_REF_AND_RESET(obj); | |
9c08c816 | 605 | obj = bt_value_integer_signed_create_init(-12345); |
21d04293 PP |
606 | ret |= bt_value_map_insert_entry(map_obj, "int", obj); |
607 | BT_VALUE_PUT_REF_AND_RESET(obj); | |
608 | obj = bt_value_real_create_init(5.444); | |
609 | ret |= bt_value_map_insert_entry(map_obj, "real", obj); | |
610 | BT_VALUE_PUT_REF_AND_RESET(obj); | |
611 | obj = bt_value_bool_create(); | |
612 | ret |= bt_value_map_insert_entry(map_obj, "bt_bool", obj); | |
613 | BT_VALUE_PUT_REF_AND_RESET(obj); | |
614 | ret |= bt_value_map_insert_entry(map_obj, "null", | |
615 | bt_value_null); | |
616 | ok(!ret, "bt_value_map_insert_entry() succeeds"); | |
617 | ok(bt_value_map_get_size(map_obj) == 5, | |
618 | "inserting an element into a map value object increment its size"); | |
619 | ||
620 | obj = bt_value_bool_create_init(BT_TRUE); | |
621 | ret = bt_value_map_insert_entry(map_obj, "bt_bool", obj); | |
622 | BT_VALUE_PUT_REF_AND_RESET(obj); | |
623 | ok(!ret, "bt_value_map_insert_entry() accepts an existing key"); | |
624 | ||
625 | obj = bt_value_map_borrow_entry_value(map_obj, "life"); | |
626 | ok(!obj, "bt_value_map_borrow_entry_value() returns NULL with an non existing key"); | |
627 | obj = bt_value_map_borrow_entry_value(map_obj, "real"); | |
628 | ok(obj && bt_value_is_real(obj), | |
629 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (real)"); | |
630 | real_value = bt_value_real_get(obj); | |
631 | ok(real_value == 5.444, | |
632 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (real)"); | |
633 | obj = bt_value_map_borrow_entry_value(map_obj, "uint"); | |
634 | ok(obj && bt_value_is_unsigned_integer(obj), | |
635 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (unsigned integer)"); | |
9c08c816 | 636 | int_value = bt_value_integer_unsigned_get(obj); |
21d04293 PP |
637 | ok(int_value == 19457, |
638 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (unsigned integer)"); | |
639 | obj = bt_value_map_borrow_entry_value(map_obj, "int"); | |
640 | ok(obj && bt_value_is_signed_integer(obj), | |
641 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (signed integer)"); | |
9c08c816 | 642 | int_value = bt_value_integer_signed_get(obj); |
21d04293 PP |
643 | ok(int_value == -12345, |
644 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (signed integer)"); | |
645 | obj = bt_value_map_borrow_entry_value(map_obj, "null"); | |
646 | ok(obj && bt_value_is_null(obj), | |
647 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (null)"); | |
648 | obj = bt_value_map_borrow_entry_value(map_obj, "bt_bool"); | |
649 | ok(obj && bt_value_is_bool(obj), | |
650 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate type (boolean)"); | |
651 | bool_value = bt_value_bool_get(obj); | |
652 | ok(bool_value, | |
653 | "bt_value_map_borrow_entry_value() returns an value object with the appropriate value (boolean)"); | |
654 | ||
655 | ret = bt_value_map_insert_bool_entry(map_obj, "bool2", | |
656 | BT_TRUE); | |
657 | ok(!ret, "bt_value_map_insert_bool_entry() succeeds"); | |
658 | ret = bt_value_map_insert_signed_integer_entry(map_obj, "int2", | |
659 | 98765); | |
660 | ok(!ret, "bt_value_map_insert_signed_integer_entry() succeeds"); | |
661 | ret = bt_value_map_insert_real_entry(map_obj, "real2", | |
662 | -49.0001); | |
663 | ok(!ret, "bt_value_map_insert_real_entry() succeeds"); | |
664 | ret = bt_value_map_insert_string_entry(map_obj, "string2", | |
665 | "bt_value"); | |
666 | ok(!ret, "bt_value_map_insert_string_entry() succeeds"); | |
847ab606 | 667 | ret = bt_value_map_insert_empty_array_entry(map_obj, "array2", NULL); |
21d04293 | 668 | ok(!ret, "bt_value_map_insert_empty_array_entry() succeeds"); |
847ab606 PP |
669 | ret = bt_value_map_insert_empty_array_entry(map_obj, "array3", &inserted_obj); |
670 | ok(!ret, "bt_value_map_insert_empty_array_entry() with returned value object succeeds"); | |
671 | ok(inserted_obj, | |
672 | "object returned by bt_value_map_insert_empty_array_entry() is not NULL"); | |
673 | ok(bt_value_is_array(inserted_obj), | |
674 | "object returned by bt_value_map_insert_empty_array_entry() is an array value"); | |
675 | ret = bt_value_map_insert_empty_map_entry(map_obj, "map2", NULL); | |
21d04293 | 676 | ok(!ret, "bt_value_map_insert_empty_map_entry() succeeds"); |
847ab606 PP |
677 | ret = bt_value_map_insert_empty_map_entry(map_obj, "map3", &inserted_obj); |
678 | ok(!ret, "bt_value_map_insert_empty_map_entry() with returned value object succeeds"); | |
679 | ok(inserted_obj, | |
680 | "object returned by bt_value_map_insert_empty_map_entry() is not NULL"); | |
681 | ok(bt_value_is_map(inserted_obj), | |
682 | "object returned by bt_value_map_insert_empty_map_entry() is an array value"); | |
683 | ||
684 | ok(bt_value_map_get_size(map_obj) == 13, | |
21d04293 PP |
685 | "the bt_value_map_insert*() functions increment the map value object's size"); |
686 | ||
687 | ok(!bt_value_map_has_entry(map_obj, "hello"), | |
688 | "map value object does not have key \"hello\""); | |
689 | ok(bt_value_map_has_entry(map_obj, "bt_bool"), | |
690 | "map value object has key \"bt_bool\""); | |
691 | ok(bt_value_map_has_entry(map_obj, "uint"), | |
692 | "map value object has key \"uint\""); | |
693 | ok(bt_value_map_has_entry(map_obj, "int"), | |
694 | "map value object has key \"int\""); | |
695 | ok(bt_value_map_has_entry(map_obj, "real"), | |
696 | "map value object has key \"real\""); | |
697 | ok(bt_value_map_has_entry(map_obj, "null"), | |
698 | "map value object has key \"null\""); | |
699 | ok(bt_value_map_has_entry(map_obj, "bool2"), | |
700 | "map value object has key \"bool2\""); | |
701 | ok(bt_value_map_has_entry(map_obj, "int2"), | |
702 | "map value object has key \"int2\""); | |
703 | ok(bt_value_map_has_entry(map_obj, "real2"), | |
704 | "map value object has key \"real2\""); | |
705 | ok(bt_value_map_has_entry(map_obj, "string2"), | |
706 | "map value object has key \"string2\""); | |
707 | ok(bt_value_map_has_entry(map_obj, "array2"), | |
708 | "map value object has key \"array2\""); | |
847ab606 PP |
709 | ok(bt_value_map_has_entry(map_obj, "array3"), |
710 | "map value object has key \"array3\""); | |
21d04293 PP |
711 | ok(bt_value_map_has_entry(map_obj, "map2"), |
712 | "map value object has key \"map2\""); | |
847ab606 PP |
713 | ok(bt_value_map_has_entry(map_obj, "map3"), |
714 | "map value object has key \"map3\""); | |
21d04293 PP |
715 | |
716 | ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count, | |
717 | &count); | |
9b4f9b42 | 718 | ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_INTERRUPTED && count == 3, |
27c61ce8 PP |
719 | "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_INTERRUPT"); |
720 | ||
721 | count = 4; | |
722 | ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count, | |
723 | &count); | |
724 | ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR, | |
725 | "bt_value_map_foreach_entry() fails when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR"); | |
726 | bt_current_thread_clear_error(); | |
727 | ||
728 | count = 5; | |
729 | ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count, | |
730 | &count); | |
731 | ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_MEMORY_ERROR, | |
732 | "bt_value_map_foreach_entry() fails when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_MEMORY_ERROR"); | |
733 | bt_current_thread_clear_error(); | |
21d04293 PP |
734 | |
735 | memset(&checklist, 0, sizeof(checklist)); | |
736 | ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_check, | |
737 | &checklist); | |
738 | ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK, | |
739 | "bt_value_map_foreach_entry() succeeds with test_map_foreach_cb_check()"); | |
740 | ok(checklist.bool1 && checklist.uint && checklist.int1 && | |
741 | checklist.real1 && checklist.null1 && checklist.bool2 && | |
742 | checklist.int2 && checklist.real2 && checklist.string2 && | |
743 | checklist.array2 && checklist.map2, | |
744 | "bt_value_map_foreach_entry() iterates over all the map value object's elements"); | |
745 | ||
746 | BT_VALUE_PUT_REF_AND_RESET(map_obj); | |
747 | pass("putting an existing map value object does not cause a crash") | |
748 | } | |
749 | ||
750 | static | |
751 | void test_types(void) | |
752 | { | |
753 | test_null(); | |
754 | test_bool(); | |
755 | test_unsigned_integer(); | |
756 | test_signed_integer(); | |
757 | test_real(); | |
758 | test_string(); | |
759 | test_array(); | |
760 | test_map(); | |
761 | } | |
762 | ||
763 | static | |
cd933d89 | 764 | void test_is_equal_null(void) |
21d04293 | 765 | { |
cd933d89 | 766 | ok(bt_value_is_equal(bt_value_null, bt_value_null), |
21d04293 PP |
767 | "null value objects are equivalent"); |
768 | } | |
769 | ||
770 | static | |
cd933d89 | 771 | void test_is_equal_bool(void) |
21d04293 PP |
772 | { |
773 | bt_value *bool1 = | |
774 | bt_value_bool_create_init(BT_FALSE); | |
775 | bt_value *bool2 = | |
776 | bt_value_bool_create_init(BT_TRUE); | |
777 | bt_value *bool3 = | |
778 | bt_value_bool_create_init(BT_FALSE); | |
779 | ||
780 | BT_ASSERT(bool1 && bool2 && bool3); | |
cd933d89 | 781 | ok(!bt_value_is_equal(bt_value_null, bool1), |
21d04293 | 782 | "cannot compare null value object and bt_bool value object"); |
cd933d89 | 783 | ok(!bt_value_is_equal(bool1, bool2), |
21d04293 | 784 | "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)"); |
cd933d89 | 785 | ok(bt_value_is_equal(bool1, bool3), |
21d04293 PP |
786 | "boolean value objects are equivalent (BT_FALSE and BT_FALSE)"); |
787 | ||
788 | BT_VALUE_PUT_REF_AND_RESET(bool1); | |
789 | BT_VALUE_PUT_REF_AND_RESET(bool2); | |
790 | BT_VALUE_PUT_REF_AND_RESET(bool3); | |
791 | } | |
792 | ||
793 | static | |
cd933d89 | 794 | void test_is_equal_unsigned_integer(void) |
21d04293 PP |
795 | { |
796 | bt_value *int1 = | |
9c08c816 | 797 | bt_value_integer_unsigned_create_init(10); |
21d04293 | 798 | bt_value *int2 = |
9c08c816 | 799 | bt_value_integer_unsigned_create_init(23); |
21d04293 | 800 | bt_value *int3 = |
9c08c816 | 801 | bt_value_integer_unsigned_create_init(10); |
21d04293 PP |
802 | |
803 | BT_ASSERT(int1 && int2 && int3); | |
cd933d89 | 804 | ok(!bt_value_is_equal(bt_value_null, int1), |
21d04293 | 805 | "cannot compare null value object and unsigned integer value object"); |
cd933d89 | 806 | ok(!bt_value_is_equal(int1, int2), |
21d04293 | 807 | "unsigned integer value objects are not equivalent (10 and 23)"); |
cd933d89 | 808 | ok(bt_value_is_equal(int1, int3), |
21d04293 PP |
809 | "unsigned integer value objects are equivalent (10 and 10)"); |
810 | ||
811 | BT_VALUE_PUT_REF_AND_RESET(int1); | |
812 | BT_VALUE_PUT_REF_AND_RESET(int2); | |
813 | BT_VALUE_PUT_REF_AND_RESET(int3); | |
814 | } | |
815 | ||
7c7301d5 | 816 | static |
cd933d89 | 817 | void test_is_equal_signed_integer(void) |
21d04293 PP |
818 | { |
819 | bt_value *int1 = | |
9c08c816 | 820 | bt_value_integer_signed_create_init(10); |
21d04293 | 821 | bt_value *int2 = |
9c08c816 | 822 | bt_value_integer_signed_create_init(-23); |
21d04293 | 823 | bt_value *int3 = |
9c08c816 | 824 | bt_value_integer_signed_create_init(10); |
21d04293 PP |
825 | |
826 | BT_ASSERT(int1 && int2 && int3); | |
cd933d89 | 827 | ok(!bt_value_is_equal(bt_value_null, int1), |
21d04293 | 828 | "cannot compare null value object and signed integer value object"); |
cd933d89 | 829 | ok(!bt_value_is_equal(int1, int2), |
21d04293 | 830 | "signed integer value objects are not equivalent (10 and -23)"); |
cd933d89 | 831 | ok(bt_value_is_equal(int1, int3), |
21d04293 PP |
832 | "signed integer value objects are equivalent (10 and 10)"); |
833 | ||
834 | BT_VALUE_PUT_REF_AND_RESET(int1); | |
835 | BT_VALUE_PUT_REF_AND_RESET(int2); | |
836 | BT_VALUE_PUT_REF_AND_RESET(int3); | |
837 | } | |
838 | ||
839 | static | |
cd933d89 | 840 | void test_is_equal_real(void) |
21d04293 PP |
841 | { |
842 | bt_value *real1 = | |
843 | bt_value_real_create_init(17.38); | |
844 | bt_value *real2 = | |
845 | bt_value_real_create_init(-14.23); | |
846 | bt_value *real3 = | |
847 | bt_value_real_create_init(17.38); | |
848 | ||
849 | BT_ASSERT(real1 && real2 && real3); | |
850 | ||
cd933d89 | 851 | ok(!bt_value_is_equal(bt_value_null, real1), |
21d04293 | 852 | "cannot compare null value object and real number value object"); |
cd933d89 | 853 | ok(!bt_value_is_equal(real1, real2), |
21d04293 | 854 | "real number value objects are not equivalent (17.38 and -14.23)"); |
cd933d89 | 855 | ok(bt_value_is_equal(real1, real3), |
21d04293 PP |
856 | "real number value objects are equivalent (17.38 and 17.38)"); |
857 | ||
858 | BT_VALUE_PUT_REF_AND_RESET(real1); | |
859 | BT_VALUE_PUT_REF_AND_RESET(real2); | |
860 | BT_VALUE_PUT_REF_AND_RESET(real3); | |
861 | } | |
862 | ||
863 | static | |
cd933d89 | 864 | void test_is_equal_string(void) |
21d04293 PP |
865 | { |
866 | bt_value *string1 = | |
867 | bt_value_string_create_init("hello"); | |
868 | bt_value *string2 = | |
869 | bt_value_string_create_init("bt_value"); | |
870 | bt_value *string3 = | |
871 | bt_value_string_create_init("hello"); | |
872 | ||
873 | BT_ASSERT(string1 && string2 && string3); | |
874 | ||
cd933d89 | 875 | ok(!bt_value_is_equal(bt_value_null, string1), |
21d04293 | 876 | "cannot compare null value object and string value object"); |
cd933d89 | 877 | ok(!bt_value_is_equal(string1, string2), |
21d04293 | 878 | "string value objects are not equivalent (\"hello\" and \"bt_value\")"); |
cd933d89 | 879 | ok(bt_value_is_equal(string1, string3), |
21d04293 PP |
880 | "string value objects are equivalent (\"hello\" and \"hello\")"); |
881 | ||
882 | BT_VALUE_PUT_REF_AND_RESET(string1); | |
883 | BT_VALUE_PUT_REF_AND_RESET(string2); | |
884 | BT_VALUE_PUT_REF_AND_RESET(string3); | |
885 | } | |
886 | ||
887 | static | |
cd933d89 | 888 | void test_is_equal_array(void) |
21d04293 PP |
889 | { |
890 | bt_value *array1 = bt_value_array_create(); | |
891 | bt_value *array2 = bt_value_array_create(); | |
892 | bt_value *array3 = bt_value_array_create(); | |
893 | bt_value_array_append_element_status append_status; | |
894 | ||
895 | BT_ASSERT(array1 && array2 && array3); | |
896 | ||
cd933d89 | 897 | ok(bt_value_is_equal(array1, array2), |
21d04293 PP |
898 | "empty array value objects are equivalent"); |
899 | ||
900 | append_status = bt_value_array_append_signed_integer_element(array1, 23); | |
901 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
902 | append_status = bt_value_array_append_real_element(array1, 14.2); | |
903 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
904 | append_status = bt_value_array_append_bool_element(array1, BT_FALSE); | |
905 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
906 | append_status = bt_value_array_append_real_element(array2, 14.2); | |
907 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
908 | append_status = bt_value_array_append_signed_integer_element(array2, 23); | |
909 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
910 | append_status = bt_value_array_append_bool_element(array2, BT_FALSE); | |
911 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
912 | append_status = bt_value_array_append_signed_integer_element(array3, 23); | |
913 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
914 | append_status = bt_value_array_append_real_element(array3, 14.2); | |
915 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
916 | append_status = bt_value_array_append_bool_element(array3, BT_FALSE); | |
917 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
393729a6 PP |
918 | BT_ASSERT(bt_value_array_get_length(array1) == 3); |
919 | BT_ASSERT(bt_value_array_get_length(array2) == 3); | |
920 | BT_ASSERT(bt_value_array_get_length(array3) == 3); | |
21d04293 | 921 | |
cd933d89 | 922 | ok(!bt_value_is_equal(bt_value_null, array1), |
21d04293 | 923 | "cannot compare null value object and array value object"); |
cd933d89 | 924 | ok(!bt_value_is_equal(array1, array2), |
21d04293 | 925 | "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])"); |
cd933d89 | 926 | ok(bt_value_is_equal(array1, array3), |
21d04293 PP |
927 | "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])"); |
928 | ||
929 | BT_VALUE_PUT_REF_AND_RESET(array1); | |
930 | BT_VALUE_PUT_REF_AND_RESET(array2); | |
931 | BT_VALUE_PUT_REF_AND_RESET(array3); | |
932 | } | |
933 | ||
934 | static | |
cd933d89 | 935 | void test_is_equal_map(void) |
21d04293 PP |
936 | { |
937 | bt_value *map1 = bt_value_map_create(); | |
938 | bt_value *map2 = bt_value_map_create(); | |
939 | bt_value *map3 = bt_value_map_create(); | |
940 | bt_value_map_insert_entry_status insert_status; | |
941 | ||
942 | BT_ASSERT(map1 && map2 && map3); | |
943 | ||
cd933d89 | 944 | ok(bt_value_is_equal(map1, map2), |
21d04293 PP |
945 | "empty map value objects are equivalent"); |
946 | ||
947 | ||
948 | insert_status = bt_value_map_insert_signed_integer_entry(map1, "one", 23); | |
949 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
950 | insert_status = bt_value_map_insert_real_entry(map1, "two", 14.2); | |
951 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
952 | insert_status = bt_value_map_insert_bool_entry(map1, "three", | |
953 | BT_FALSE); | |
954 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
955 | insert_status = bt_value_map_insert_real_entry(map2, "one", 14.2); | |
956 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
957 | insert_status = bt_value_map_insert_signed_integer_entry(map2, "two", 23); | |
958 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
959 | insert_status = bt_value_map_insert_bool_entry(map2, "three", | |
960 | BT_FALSE); | |
961 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
962 | insert_status = bt_value_map_insert_bool_entry(map3, "three", | |
963 | BT_FALSE); | |
964 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
965 | insert_status = bt_value_map_insert_signed_integer_entry(map3, "one", 23); | |
966 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
967 | insert_status = bt_value_map_insert_real_entry(map3, "two", 14.2); | |
968 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
969 | BT_ASSERT(bt_value_map_get_size(map1) == 3); | |
970 | BT_ASSERT(bt_value_map_get_size(map2) == 3); | |
971 | BT_ASSERT(bt_value_map_get_size(map3) == 3); | |
972 | ||
cd933d89 | 973 | ok(!bt_value_is_equal(bt_value_null, map1), |
21d04293 | 974 | "cannot compare null value object and map value object"); |
cd933d89 | 975 | ok(!bt_value_is_equal(map1, map2), |
21d04293 | 976 | "map value objects are not equivalent"); |
cd933d89 | 977 | ok(bt_value_is_equal(map1, map3), |
21d04293 PP |
978 | "map value objects are equivalent"); |
979 | ||
980 | BT_VALUE_PUT_REF_AND_RESET(map1); | |
981 | BT_VALUE_PUT_REF_AND_RESET(map2); | |
982 | BT_VALUE_PUT_REF_AND_RESET(map3); | |
983 | } | |
984 | ||
985 | static | |
cd933d89 | 986 | void test_is_equal(void) |
21d04293 | 987 | { |
cd933d89 FD |
988 | test_is_equal_null(); |
989 | test_is_equal_bool(); | |
990 | test_is_equal_unsigned_integer(); | |
991 | test_is_equal_signed_integer(); | |
992 | test_is_equal_real(); | |
993 | test_is_equal_string(); | |
994 | test_is_equal_array(); | |
995 | test_is_equal_map(); | |
21d04293 PP |
996 | } |
997 | ||
998 | static | |
999 | void test_copy(void) | |
1000 | { | |
1001 | /* | |
1002 | * Here's the deal here. If we make sure that each value object | |
1003 | * of our deep copy has a different address than its source, and | |
cd933d89 | 1004 | * that bt_value_is_equal() returns BT_TRUE for the top-level |
21d04293 | 1005 | * value object, taking into account that we test the |
cd933d89 | 1006 | * correctness of bt_value_is_equal() elsewhere, then the deep |
21d04293 PP |
1007 | * copy is a success. |
1008 | */ | |
1009 | bt_value *null_copy_obj; | |
1010 | bt_value *bool_obj, *bool_copy_obj; | |
1011 | bt_value *unsigned_integer_obj, *unsigned_integer_copy_obj; | |
1012 | bt_value *signed_integer_obj, *signed_integer_copy_obj; | |
1013 | bt_value *real_obj, *real_copy_obj; | |
1014 | bt_value *string_obj, *string_copy_obj; | |
1015 | bt_value *array_obj, *array_copy_obj; | |
1016 | bt_value *map_obj, *map_copy_obj; | |
1017 | bt_value_array_append_element_status append_status; | |
1018 | bt_value_map_insert_entry_status insert_status; | |
1019 | bt_value_copy_status copy_status; | |
1020 | ||
1021 | bool_obj = bt_value_bool_create_init(BT_TRUE); | |
9c08c816 PP |
1022 | unsigned_integer_obj = bt_value_integer_unsigned_create_init(23); |
1023 | signed_integer_obj = bt_value_integer_signed_create_init(-47); | |
21d04293 PP |
1024 | real_obj = bt_value_real_create_init(-3.1416); |
1025 | string_obj = bt_value_string_create_init("test"); | |
1026 | array_obj = bt_value_array_create(); | |
1027 | map_obj = bt_value_map_create(); | |
1028 | ||
1029 | BT_ASSERT(bool_obj && unsigned_integer_obj && signed_integer_obj && | |
1030 | real_obj && string_obj && array_obj && map_obj); | |
1031 | ||
1032 | append_status = bt_value_array_append_element(array_obj, bool_obj); | |
1033 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
1034 | append_status = bt_value_array_append_element(array_obj, unsigned_integer_obj); | |
1035 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
1036 | append_status = bt_value_array_append_element(array_obj, signed_integer_obj); | |
1037 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
1038 | append_status = bt_value_array_append_element(array_obj, real_obj); | |
1039 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
1040 | append_status = bt_value_array_append_element(array_obj, bt_value_null); | |
1041 | BT_ASSERT(append_status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK); | |
1042 | insert_status = bt_value_map_insert_entry(map_obj, "array", array_obj); | |
1043 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1044 | insert_status = bt_value_map_insert_entry(map_obj, "string", string_obj); | |
1045 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1046 | ||
1047 | copy_status = bt_value_copy(map_obj, &map_copy_obj); | |
1048 | ok(copy_status == BT_VALUE_COPY_STATUS_OK && map_copy_obj, | |
1049 | "bt_value_copy() succeeds"); | |
1050 | ||
1051 | ok(map_obj != map_copy_obj, | |
1052 | "bt_value_copy() returns a different pointer (map)"); | |
1053 | string_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj, | |
1054 | "string"); | |
1055 | ok(string_copy_obj != string_obj, | |
1056 | "bt_value_copy() returns a different pointer (string)"); | |
1057 | array_copy_obj = bt_value_map_borrow_entry_value(map_copy_obj, | |
1058 | "array"); | |
1059 | ok(array_copy_obj != array_obj, | |
1060 | "bt_value_copy() returns a different pointer (array)"); | |
1061 | bool_copy_obj = bt_value_array_borrow_element_by_index( | |
1062 | array_copy_obj, 0); | |
1063 | ok(bool_copy_obj != bool_obj, | |
1064 | "bt_value_copy() returns a different pointer (bool)"); | |
1065 | unsigned_integer_copy_obj = bt_value_array_borrow_element_by_index( | |
1066 | array_copy_obj, 1); | |
1067 | ok(unsigned_integer_copy_obj != unsigned_integer_obj, | |
1068 | "bt_value_copy() returns a different pointer (unsigned integer)"); | |
1069 | signed_integer_copy_obj = bt_value_array_borrow_element_by_index( | |
1070 | array_copy_obj, 2); | |
1071 | ok(signed_integer_copy_obj != signed_integer_obj, | |
1072 | "bt_value_copy() returns a different pointer (signed integer)"); | |
1073 | real_copy_obj = bt_value_array_borrow_element_by_index( | |
1074 | array_copy_obj, 3); | |
1075 | ok(real_copy_obj != real_obj, | |
1076 | "bt_value_copy() returns a different pointer (real)"); | |
1077 | null_copy_obj = bt_value_array_borrow_element_by_index( | |
1078 | array_copy_obj, 4); | |
1079 | ok(null_copy_obj == bt_value_null, | |
1080 | "bt_value_copy() returns the same pointer (null)"); | |
1081 | ||
cd933d89 | 1082 | ok(bt_value_is_equal(map_obj, map_copy_obj), |
21d04293 PP |
1083 | "source and destination value objects have the same content"); |
1084 | ||
1085 | BT_VALUE_PUT_REF_AND_RESET(map_copy_obj); | |
1086 | BT_VALUE_PUT_REF_AND_RESET(bool_obj); | |
1087 | BT_VALUE_PUT_REF_AND_RESET(unsigned_integer_obj); | |
1088 | BT_VALUE_PUT_REF_AND_RESET(signed_integer_obj); | |
1089 | BT_VALUE_PUT_REF_AND_RESET(real_obj); | |
1090 | BT_VALUE_PUT_REF_AND_RESET(string_obj); | |
1091 | BT_VALUE_PUT_REF_AND_RESET(array_obj); | |
1092 | BT_VALUE_PUT_REF_AND_RESET(map_obj); | |
1093 | } | |
1094 | ||
1095 | static | |
1096 | bt_bool compare_map_elements(const bt_value *map_a, const bt_value *map_b, | |
1097 | const char *key) | |
1098 | { | |
1099 | const bt_value *elem_a = NULL; | |
1100 | const bt_value *elem_b = NULL; | |
1101 | bt_bool equal; | |
1102 | ||
1103 | elem_a = bt_value_map_borrow_entry_value_const(map_a, key); | |
1104 | elem_b = bt_value_map_borrow_entry_value_const(map_b, key); | |
cd933d89 | 1105 | equal = bt_value_is_equal(elem_a, elem_b); |
21d04293 PP |
1106 | return equal; |
1107 | } | |
1108 | ||
1109 | static | |
1110 | void test_extend(void) | |
1111 | { | |
1112 | bt_value *base_map = bt_value_map_create(); | |
1113 | bt_value *extension_map = bt_value_map_create(); | |
1114 | bt_value *extended_map = NULL; | |
1115 | bt_value *array = bt_value_array_create(); | |
1116 | bt_value_map_insert_entry_status insert_status; | |
ab26240f | 1117 | bt_value_copy_status copy_status; |
21d04293 PP |
1118 | bt_value_map_extend_status extend_status; |
1119 | ||
1120 | BT_ASSERT(base_map); | |
1121 | BT_ASSERT(extension_map); | |
1122 | BT_ASSERT(array); | |
1123 | insert_status = bt_value_map_insert_bool_entry(base_map, "file", | |
1124 | BT_TRUE); | |
1125 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1126 | insert_status = bt_value_map_insert_bool_entry(base_map, "edit", | |
1127 | BT_FALSE); | |
1128 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1129 | insert_status = bt_value_map_insert_signed_integer_entry(base_map, | |
1130 | "selection", 17); | |
1131 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1132 | insert_status = bt_value_map_insert_signed_integer_entry(base_map, "find", | |
1133 | -34); | |
1134 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1135 | insert_status = bt_value_map_insert_bool_entry(extension_map, "edit", | |
1136 | BT_TRUE); | |
1137 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1138 | insert_status = bt_value_map_insert_signed_integer_entry(extension_map, | |
1139 | "find", 101); | |
1140 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
1141 | insert_status = bt_value_map_insert_real_entry(extension_map, | |
1142 | "project", -404); | |
1143 | BT_ASSERT(insert_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK); | |
ab26240f FD |
1144 | copy_status = bt_value_copy(base_map, &extended_map); |
1145 | BT_ASSERT(copy_status == BT_VALUE_COPY_STATUS_OK); | |
1146 | extend_status = bt_value_map_extend(extended_map, extension_map); | |
21d04293 PP |
1147 | ok(extend_status == BT_VALUE_MAP_EXTEND_STATUS_OK && |
1148 | extended_map, "bt_value_map_extend() succeeds"); | |
1149 | ok(bt_value_map_get_size(extended_map) == 5, | |
1150 | "bt_value_map_extend() returns a map object with the correct size"); | |
1151 | ok(compare_map_elements(base_map, | |
1152 | extended_map, "file"), | |
1153 | "bt_value_map_extend() picks the appropriate element (file)"); | |
1154 | ok(compare_map_elements(extension_map, | |
1155 | extended_map, "edit"), | |
1156 | "bt_value_map_extend() picks the appropriate element (edit)"); | |
1157 | ok(compare_map_elements(base_map, | |
1158 | extended_map, "selection"), | |
1159 | "bt_value_map_extend() picks the appropriate element (selection)"); | |
1160 | ok(compare_map_elements(extension_map, | |
1161 | extended_map, "find"), | |
1162 | "bt_value_map_extend() picks the appropriate element (find)"); | |
1163 | ok(compare_map_elements(extension_map, | |
1164 | extended_map, "project"), | |
1165 | "bt_value_map_extend() picks the appropriate element (project)"); | |
1166 | ||
1167 | BT_VALUE_PUT_REF_AND_RESET(array); | |
1168 | BT_VALUE_PUT_REF_AND_RESET(base_map); | |
1169 | BT_VALUE_PUT_REF_AND_RESET(extension_map); | |
1170 | BT_VALUE_PUT_REF_AND_RESET(extended_map); | |
1171 | } | |
1172 | ||
1173 | int main(void) | |
1174 | { | |
1175 | plan_tests(NR_TESTS); | |
1176 | test_types(); | |
cd933d89 | 1177 | test_is_equal(); |
21d04293 PP |
1178 | test_copy(); |
1179 | test_extend(); | |
1180 | return 0; | |
1181 | } |