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