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