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