Commit | Line | Data |
---|---|---|
dac5c838 PP |
1 | /* |
2 | * test_bt_values.c | |
3 | * | |
705b853d | 4 | * Babeltrace value objects tests |
dac5c838 PP |
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 | ||
9d408fca | 23 | #include <babeltrace/ref.h> |
dac5c838 | 24 | #include <babeltrace/values.h> |
25583cd0 | 25 | #include <babeltrace/assert-internal.h> |
dac5c838 PP |
26 | #include <string.h> |
27 | #include "tap/tap.h" | |
28 | ||
f6ccaed9 | 29 | #define NR_TESTS 158 |
8bbe269d | 30 | |
dac5c838 PP |
31 | static |
32 | void test_null(void) | |
33 | { | |
34 | ok(bt_value_null, "bt_value_null is not NULL"); | |
35 | ok(bt_value_is_null(bt_value_null), | |
36 | "bt_value_null is a null value object"); | |
83509119 | 37 | bt_get(bt_value_null); |
dac5c838 | 38 | pass("getting bt_value_null does not cause a crash"); |
83509119 | 39 | bt_put(bt_value_null); |
dac5c838 | 40 | pass("putting bt_value_null does not cause a crash"); |
dac5c838 PP |
41 | } |
42 | ||
43 | static | |
44 | void test_bool(void) | |
45 | { | |
46 | int ret; | |
c55a9f58 | 47 | bt_bool value; |
dac5c838 PP |
48 | struct bt_value *obj; |
49 | ||
50 | obj = bt_value_bool_create(); | |
51 | ok(obj && bt_value_is_bool(obj), | |
52 | "bt_value_bool_create() returns a boolean value object"); | |
53 | ||
c55a9f58 | 54 | value = BT_TRUE; |
dac5c838 | 55 | ret = bt_value_bool_get(obj, &value); |
c55a9f58 | 56 | ok(!ret && !value, "default boolean value object value is BT_FALSE"); |
dac5c838 | 57 | |
25583cd0 | 58 | BT_ASSERT(!bt_value_bool_set(obj, BT_FALSE)); |
c55a9f58 | 59 | ret = bt_value_bool_set(obj, BT_TRUE); |
dac5c838 PP |
60 | ok(!ret, "bt_value_bool_set() succeeds"); |
61 | ret = bt_value_bool_get(obj, &value); | |
62 | ok(!ret && value, "bt_value_bool_set() works"); | |
63 | ||
83509119 | 64 | BT_PUT(obj); |
dac5c838 PP |
65 | pass("putting an existing boolean value object does not cause a crash") |
66 | ||
c55a9f58 PP |
67 | value = BT_FALSE; |
68 | obj = bt_value_bool_create_init(BT_TRUE); | |
dac5c838 PP |
69 | ok(obj && bt_value_is_bool(obj), |
70 | "bt_value_bool_create_init() returns a boolean value object"); | |
71 | ret = bt_value_bool_get(obj, &value); | |
72 | ok(!ret && value, | |
73 | "bt_value_bool_create_init() sets the appropriate initial value"); | |
74 | ||
83509119 | 75 | BT_PUT(obj); |
dac5c838 PP |
76 | } |
77 | ||
78 | static | |
79 | void test_integer(void) | |
80 | { | |
81 | int ret; | |
82 | int64_t value; | |
83 | struct bt_value *obj; | |
84 | ||
85 | obj = bt_value_integer_create(); | |
86 | ok(obj && bt_value_is_integer(obj), | |
87 | "bt_value_integer_create() returns an integer value object"); | |
88 | ||
dac5c838 PP |
89 | value = 1961; |
90 | ret = bt_value_integer_get(obj, &value); | |
91 | ok(!ret && value == 0, "default integer value object value is 0"); | |
92 | ||
93 | ret = bt_value_integer_set(obj, -98765); | |
94 | ok(!ret, "bt_value_integer_set() succeeds"); | |
95 | ret = bt_value_integer_get(obj, &value); | |
96 | ok(!ret && value == -98765, "bt_value_integer_set() works"); | |
97 | ||
83509119 | 98 | BT_PUT(obj); |
dac5c838 PP |
99 | pass("putting an existing integer value object does not cause a crash") |
100 | ||
101 | obj = bt_value_integer_create_init(321456987); | |
102 | ok(obj && bt_value_is_integer(obj), | |
103 | "bt_value_integer_create_init() returns an integer value object"); | |
104 | ret = bt_value_integer_get(obj, &value); | |
105 | ok(!ret && value == 321456987, | |
106 | "bt_value_integer_create_init() sets the appropriate initial value"); | |
107 | ||
83509119 | 108 | BT_PUT(obj); |
dac5c838 PP |
109 | } |
110 | ||
111 | static | |
a373bf69 | 112 | void test_real(void) |
dac5c838 PP |
113 | { |
114 | int ret; | |
115 | double value; | |
116 | struct bt_value *obj; | |
117 | ||
a373bf69 PP |
118 | obj = bt_value_real_create(); |
119 | ok(obj && bt_value_is_real(obj), | |
120 | "bt_value_real_create() returns a real number value object"); | |
dac5c838 | 121 | |
dac5c838 | 122 | value = 17.34; |
a373bf69 | 123 | ret = bt_value_real_get(obj, &value); |
dac5c838 | 124 | ok(!ret && value == 0., |
a373bf69 | 125 | "default real number value object value is 0"); |
dac5c838 | 126 | |
a373bf69 PP |
127 | ret = bt_value_real_set(obj, -3.1416); |
128 | ok(!ret, "bt_value_real_set() succeeds"); | |
129 | ret = bt_value_real_get(obj, &value); | |
130 | ok(!ret && value == -3.1416, "bt_value_real_set() works"); | |
dac5c838 | 131 | |
83509119 | 132 | BT_PUT(obj); |
a373bf69 | 133 | pass("putting an existing real number value object does not cause a crash") |
dac5c838 | 134 | |
a373bf69 PP |
135 | obj = bt_value_real_create_init(33.1649758); |
136 | ok(obj && bt_value_is_real(obj), | |
137 | "bt_value_real_create_init() returns a real number value object"); | |
138 | ret = bt_value_real_get(obj, &value); | |
dac5c838 | 139 | ok(!ret && value == 33.1649758, |
a373bf69 | 140 | "bt_value_real_create_init() sets the appropriate initial value"); |
dac5c838 | 141 | |
83509119 | 142 | BT_PUT(obj); |
dac5c838 PP |
143 | } |
144 | ||
145 | static | |
146 | void test_string(void) | |
147 | { | |
148 | int ret; | |
149 | const char *value; | |
150 | struct bt_value *obj; | |
151 | ||
152 | obj = bt_value_string_create(); | |
153 | ok(obj && bt_value_is_string(obj), | |
154 | "bt_value_string_create() returns a string value object"); | |
155 | ||
dac5c838 PP |
156 | ret = bt_value_string_get(obj, &value); |
157 | ok(!ret && value && !strcmp(value, ""), | |
158 | "default string value object value is \"\""); | |
159 | ||
160 | ret = bt_value_string_set(obj, "hello worldz"); | |
161 | ok(!ret, "bt_value_string_set() succeeds"); | |
162 | ret = bt_value_string_get(obj, &value); | |
163 | ok(!ret && value && !strcmp(value, "hello worldz"), | |
164 | "bt_value_string_get() works"); | |
165 | ||
83509119 | 166 | BT_PUT(obj); |
dac5c838 PP |
167 | pass("putting an existing string value object does not cause a crash") |
168 | ||
dac5c838 PP |
169 | obj = bt_value_string_create_init("initial value"); |
170 | ok(obj && bt_value_is_string(obj), | |
171 | "bt_value_string_create_init() returns a string value object"); | |
172 | ret = bt_value_string_get(obj, &value); | |
173 | ok(!ret && value && !strcmp(value, "initial value"), | |
174 | "bt_value_string_create_init() sets the appropriate initial value"); | |
175 | ||
83509119 | 176 | BT_PUT(obj); |
dac5c838 PP |
177 | } |
178 | ||
179 | static | |
180 | void test_array(void) | |
181 | { | |
182 | int ret; | |
c55a9f58 | 183 | bt_bool bool_value; |
dac5c838 | 184 | int64_t int_value; |
a373bf69 | 185 | double real_value; |
dac5c838 PP |
186 | struct bt_value *obj; |
187 | const char *string_value; | |
188 | struct bt_value *array_obj; | |
189 | ||
190 | array_obj = bt_value_array_create(); | |
191 | ok(array_obj && bt_value_is_array(array_obj), | |
192 | "bt_value_array_create() returns an array value object"); | |
dac5c838 PP |
193 | ok(bt_value_array_is_empty(array_obj), |
194 | "initial array value object size is 0"); | |
dac5c838 PP |
195 | |
196 | obj = bt_value_integer_create_init(345); | |
197 | ret = bt_value_array_append(array_obj, obj); | |
83509119 | 198 | BT_PUT(obj); |
a373bf69 | 199 | obj = bt_value_real_create_init(-17.45); |
dac5c838 | 200 | ret |= bt_value_array_append(array_obj, obj); |
83509119 | 201 | BT_PUT(obj); |
c55a9f58 | 202 | obj = bt_value_bool_create_init(BT_TRUE); |
dac5c838 | 203 | ret |= bt_value_array_append(array_obj, obj); |
83509119 | 204 | BT_PUT(obj); |
dac5c838 PP |
205 | ret |= bt_value_array_append(array_obj, bt_value_null); |
206 | ok(!ret, "bt_value_array_append() succeeds"); | |
207 | ok(bt_value_array_size(array_obj) == 4, | |
208 | "appending an element to an array value object increment its size"); | |
209 | ||
dac5c838 PP |
210 | obj = bt_value_array_get(array_obj, 0); |
211 | ok(obj && bt_value_is_integer(obj), | |
212 | "bt_value_array_get() returns an value object with the appropriate type (integer)"); | |
213 | ret = bt_value_integer_get(obj, &int_value); | |
214 | ok(!ret && int_value == 345, | |
215 | "bt_value_array_get() returns an value object with the appropriate value (integer)"); | |
83509119 | 216 | BT_PUT(obj); |
dac5c838 | 217 | obj = bt_value_array_get(array_obj, 1); |
a373bf69 PP |
218 | ok(obj && bt_value_is_real(obj), |
219 | "bt_value_array_get() returns an value object with the appropriate type (real number)"); | |
220 | ret = bt_value_real_get(obj, &real_value); | |
221 | ok(!ret && real_value == -17.45, | |
222 | "bt_value_array_get() returns an value object with the appropriate value (real number)"); | |
83509119 | 223 | BT_PUT(obj); |
dac5c838 PP |
224 | obj = bt_value_array_get(array_obj, 2); |
225 | ok(obj && bt_value_is_bool(obj), | |
226 | "bt_value_array_get() returns an value object with the appropriate type (boolean)"); | |
227 | ret = bt_value_bool_get(obj, &bool_value); | |
228 | ok(!ret && bool_value, | |
229 | "bt_value_array_get() returns an value object with the appropriate value (boolean)"); | |
83509119 | 230 | BT_PUT(obj); |
dac5c838 PP |
231 | obj = bt_value_array_get(array_obj, 3); |
232 | ok(obj == bt_value_null, | |
233 | "bt_value_array_get() returns an value object with the appropriate type (null)"); | |
234 | ||
dac5c838 | 235 | obj = bt_value_integer_create_init(1001); |
25583cd0 | 236 | BT_ASSERT(obj); |
dac5c838 PP |
237 | ok(!bt_value_array_set(array_obj, 2, obj), |
238 | "bt_value_array_set() succeeds"); | |
83509119 | 239 | BT_PUT(obj); |
dac5c838 PP |
240 | obj = bt_value_array_get(array_obj, 2); |
241 | ok(obj && bt_value_is_integer(obj), | |
242 | "bt_value_array_set() inserts an value object with the appropriate type"); | |
243 | ret = bt_value_integer_get(obj, &int_value); | |
25583cd0 | 244 | BT_ASSERT(!ret); |
dac5c838 PP |
245 | ok(int_value == 1001, |
246 | "bt_value_array_set() inserts an value object with the appropriate value"); | |
83509119 | 247 | BT_PUT(obj); |
dac5c838 | 248 | |
c55a9f58 | 249 | ret = bt_value_array_append_bool(array_obj, BT_FALSE); |
dac5c838 | 250 | ok(!ret, "bt_value_array_append_bool() succeeds"); |
dac5c838 PP |
251 | ret = bt_value_array_append_integer(array_obj, 98765); |
252 | ok(!ret, "bt_value_array_append_integer() succeeds"); | |
a373bf69 PP |
253 | ret = bt_value_array_append_real(array_obj, 2.49578); |
254 | ok(!ret, "bt_value_array_append_real() succeeds"); | |
dac5c838 PP |
255 | ret = bt_value_array_append_string(array_obj, "bt_value"); |
256 | ok(!ret, "bt_value_array_append_string() succeeds"); | |
5b79e8bf PP |
257 | ret = bt_value_array_append_empty_array(array_obj); |
258 | ok(!ret, "bt_value_array_append_empty_array() succeeds"); | |
5b79e8bf PP |
259 | ret = bt_value_array_append_empty_map(array_obj); |
260 | ok(!ret, "bt_value_array_append_empty_map() succeeds"); | |
dac5c838 PP |
261 | |
262 | ok(bt_value_array_size(array_obj) == 10, | |
263 | "the bt_value_array_append_*() functions increment the array value object's size"); | |
264 | ok(!bt_value_array_is_empty(array_obj), | |
265 | "map value object is not empty"); | |
266 | ||
267 | obj = bt_value_array_get(array_obj, 4); | |
268 | ok(obj && bt_value_is_bool(obj), | |
269 | "bt_value_array_append_bool() appends a boolean value object"); | |
270 | ret = bt_value_bool_get(obj, &bool_value); | |
271 | ok(!ret && !bool_value, | |
272 | "bt_value_array_append_bool() appends the appropriate value"); | |
83509119 | 273 | BT_PUT(obj); |
dac5c838 PP |
274 | obj = bt_value_array_get(array_obj, 5); |
275 | ok(obj && bt_value_is_integer(obj), | |
276 | "bt_value_array_append_integer() appends an integer value object"); | |
277 | ret = bt_value_integer_get(obj, &int_value); | |
278 | ok(!ret && int_value == 98765, | |
279 | "bt_value_array_append_integer() appends the appropriate value"); | |
83509119 | 280 | BT_PUT(obj); |
dac5c838 | 281 | obj = bt_value_array_get(array_obj, 6); |
a373bf69 PP |
282 | ok(obj && bt_value_is_real(obj), |
283 | "bt_value_array_append_real() appends a real number value object"); | |
284 | ret = bt_value_real_get(obj, &real_value); | |
285 | ok(!ret && real_value == 2.49578, | |
286 | "bt_value_array_append_real() appends the appropriate value"); | |
83509119 | 287 | BT_PUT(obj); |
dac5c838 PP |
288 | obj = bt_value_array_get(array_obj, 7); |
289 | ok(obj && bt_value_is_string(obj), | |
290 | "bt_value_array_append_string() appends a string value object"); | |
291 | ret = bt_value_string_get(obj, &string_value); | |
292 | ok(!ret && string_value && !strcmp(string_value, "bt_value"), | |
293 | "bt_value_array_append_string() appends the appropriate value"); | |
83509119 | 294 | BT_PUT(obj); |
dac5c838 PP |
295 | obj = bt_value_array_get(array_obj, 8); |
296 | ok(obj && bt_value_is_array(obj), | |
5b79e8bf | 297 | "bt_value_array_append_empty_array() appends an array value object"); |
dac5c838 | 298 | ok(bt_value_array_is_empty(obj), |
5b79e8bf | 299 | "bt_value_array_append_empty_array() an empty array value object"); |
83509119 | 300 | BT_PUT(obj); |
dac5c838 PP |
301 | obj = bt_value_array_get(array_obj, 9); |
302 | ok(obj && bt_value_is_map(obj), | |
5b79e8bf | 303 | "bt_value_array_append_empty_map() appends a map value object"); |
dac5c838 | 304 | ok(bt_value_map_is_empty(obj), |
5b79e8bf | 305 | "bt_value_array_append_empty_map() an empty map value object"); |
83509119 | 306 | BT_PUT(obj); |
dac5c838 | 307 | |
83509119 | 308 | BT_PUT(array_obj); |
dac5c838 PP |
309 | pass("putting an existing array value object does not cause a crash") |
310 | } | |
311 | ||
312 | static | |
c55a9f58 | 313 | bt_bool test_map_foreach_cb_count(const char *key, struct bt_value *object, |
dac5c838 PP |
314 | void *data) |
315 | { | |
316 | int *count = data; | |
317 | ||
318 | if (*count == 3) { | |
c55a9f58 | 319 | return BT_FALSE; |
dac5c838 PP |
320 | } |
321 | ||
322 | (*count)++; | |
323 | ||
c55a9f58 | 324 | return BT_TRUE; |
dac5c838 PP |
325 | } |
326 | ||
327 | struct map_foreach_checklist { | |
c55a9f58 PP |
328 | bt_bool bool1; |
329 | bt_bool int1; | |
a373bf69 | 330 | bt_bool real1; |
c55a9f58 PP |
331 | bt_bool null1; |
332 | bt_bool bool2; | |
333 | bt_bool int2; | |
a373bf69 | 334 | bt_bool real2; |
c55a9f58 PP |
335 | bt_bool string2; |
336 | bt_bool array2; | |
337 | bt_bool map2; | |
dac5c838 PP |
338 | }; |
339 | ||
340 | static | |
c55a9f58 | 341 | bt_bool test_map_foreach_cb_check(const char *key, struct bt_value *object, |
dac5c838 PP |
342 | void *data) |
343 | { | |
344 | int ret; | |
345 | struct map_foreach_checklist *checklist = data; | |
346 | ||
c55a9f58 | 347 | if (!strcmp(key, "bt_bool")) { |
dac5c838 | 348 | if (checklist->bool1) { |
c55a9f58 | 349 | fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\""); |
dac5c838 | 350 | } else { |
c55a9f58 | 351 | bt_bool val = BT_FALSE; |
dac5c838 PP |
352 | |
353 | ret = bt_value_bool_get(object, &val); | |
c55a9f58 | 354 | ok(!ret, "test_map_foreach_cb_check(): success getting \"bt_bool\" value"); |
dac5c838 PP |
355 | |
356 | if (val) { | |
c55a9f58 PP |
357 | pass("test_map_foreach_cb_check(): \"bt_bool\" value object has the right value"); |
358 | checklist->bool1 = BT_TRUE; | |
dac5c838 | 359 | } else { |
c55a9f58 | 360 | fail("test_map_foreach_cb_check(): \"bt_bool\" value object has the wrong value"); |
dac5c838 PP |
361 | } |
362 | } | |
363 | } else if (!strcmp(key, "int")) { | |
364 | if (checklist->int1) { | |
365 | fail("test_map_foreach_cb_check(): duplicate key \"int\""); | |
366 | } else { | |
367 | int64_t val = 0; | |
368 | ||
369 | ret = bt_value_integer_get(object, &val); | |
370 | ok(!ret, "test_map_foreach_cb_check(): success getting \"int\" value"); | |
371 | ||
372 | if (val == 19457) { | |
373 | pass("test_map_foreach_cb_check(): \"int\" value object has the right value"); | |
c55a9f58 | 374 | checklist->int1 = BT_TRUE; |
dac5c838 PP |
375 | } else { |
376 | fail("test_map_foreach_cb_check(): \"int\" value object has the wrong value"); | |
377 | } | |
378 | } | |
a373bf69 PP |
379 | } else if (!strcmp(key, "real")) { |
380 | if (checklist->real1) { | |
381 | fail("test_map_foreach_cb_check(): duplicate key \"real\""); | |
dac5c838 PP |
382 | } else { |
383 | double val = 0; | |
384 | ||
a373bf69 PP |
385 | ret = bt_value_real_get(object, &val); |
386 | ok(!ret, "test_map_foreach_cb_check(): success getting \"real\" value"); | |
dac5c838 PP |
387 | |
388 | if (val == 5.444) { | |
a373bf69 PP |
389 | pass("test_map_foreach_cb_check(): \"real\" value object has the right value"); |
390 | checklist->real1 = BT_TRUE; | |
dac5c838 | 391 | } else { |
a373bf69 | 392 | fail("test_map_foreach_cb_check(): \"real\" value object has the wrong value"); |
dac5c838 PP |
393 | } |
394 | } | |
395 | } else if (!strcmp(key, "null")) { | |
396 | if (checklist->null1) { | |
c55a9f58 | 397 | fail("test_map_foreach_cb_check(): duplicate key \"bt_bool\""); |
dac5c838 PP |
398 | } else { |
399 | ok(bt_value_is_null(object), "test_map_foreach_cb_check(): success getting \"null\" value object"); | |
c55a9f58 | 400 | checklist->null1 = BT_TRUE; |
dac5c838 PP |
401 | } |
402 | } else if (!strcmp(key, "bool2")) { | |
403 | if (checklist->bool2) { | |
404 | fail("test_map_foreach_cb_check(): duplicate key \"bool2\""); | |
405 | } else { | |
c55a9f58 | 406 | bt_bool val = BT_FALSE; |
dac5c838 PP |
407 | |
408 | ret = bt_value_bool_get(object, &val); | |
409 | ok(!ret, "test_map_foreach_cb_check(): success getting \"bool2\" value"); | |
410 | ||
411 | if (val) { | |
412 | pass("test_map_foreach_cb_check(): \"bool2\" value object has the right value"); | |
c55a9f58 | 413 | checklist->bool2 = BT_TRUE; |
dac5c838 PP |
414 | } else { |
415 | fail("test_map_foreach_cb_check(): \"bool2\" value object has the wrong value"); | |
416 | } | |
417 | } | |
418 | } else if (!strcmp(key, "int2")) { | |
419 | if (checklist->int2) { | |
420 | fail("test_map_foreach_cb_check(): duplicate key \"int2\""); | |
421 | } else { | |
422 | int64_t val = 0; | |
423 | ||
424 | ret = bt_value_integer_get(object, &val); | |
425 | ok(!ret, "test_map_foreach_cb_check(): success getting \"int2\" value"); | |
426 | ||
427 | if (val == 98765) { | |
428 | pass("test_map_foreach_cb_check(): \"int2\" value object has the right value"); | |
c55a9f58 | 429 | checklist->int2 = BT_TRUE; |
dac5c838 PP |
430 | } else { |
431 | fail("test_map_foreach_cb_check(): \"int2\" value object has the wrong value"); | |
432 | } | |
433 | } | |
a373bf69 PP |
434 | } else if (!strcmp(key, "real2")) { |
435 | if (checklist->real2) { | |
436 | fail("test_map_foreach_cb_check(): duplicate key \"real2\""); | |
dac5c838 PP |
437 | } else { |
438 | double val = 0; | |
439 | ||
a373bf69 PP |
440 | ret = bt_value_real_get(object, &val); |
441 | ok(!ret, "test_map_foreach_cb_check(): success getting \"real2\" value"); | |
dac5c838 PP |
442 | |
443 | if (val == -49.0001) { | |
a373bf69 PP |
444 | pass("test_map_foreach_cb_check(): \"real2\" value object has the right value"); |
445 | checklist->real2 = BT_TRUE; | |
dac5c838 | 446 | } else { |
a373bf69 | 447 | fail("test_map_foreach_cb_check(): \"real2\" value object has the wrong value"); |
dac5c838 PP |
448 | } |
449 | } | |
450 | } else if (!strcmp(key, "string2")) { | |
451 | if (checklist->string2) { | |
452 | fail("test_map_foreach_cb_check(): duplicate key \"string2\""); | |
453 | } else { | |
454 | const char *val; | |
455 | ||
456 | ret = bt_value_string_get(object, &val); | |
457 | ok(!ret, "test_map_foreach_cb_check(): success getting \"string2\" value"); | |
458 | ||
459 | if (val && !strcmp(val, "bt_value")) { | |
460 | pass("test_map_foreach_cb_check(): \"string2\" value object has the right value"); | |
c55a9f58 | 461 | checklist->string2 = BT_TRUE; |
dac5c838 PP |
462 | } else { |
463 | fail("test_map_foreach_cb_check(): \"string2\" value object has the wrong value"); | |
464 | } | |
465 | } | |
466 | } else if (!strcmp(key, "array2")) { | |
467 | if (checklist->array2) { | |
468 | fail("test_map_foreach_cb_check(): duplicate key \"array2\""); | |
469 | } else { | |
470 | ok(bt_value_is_array(object), "test_map_foreach_cb_check(): success getting \"array2\" value object"); | |
471 | ok(bt_value_array_is_empty(object), | |
472 | "test_map_foreach_cb_check(): \"array2\" value object is empty"); | |
c55a9f58 | 473 | checklist->array2 = BT_TRUE; |
dac5c838 PP |
474 | } |
475 | } else if (!strcmp(key, "map2")) { | |
476 | if (checklist->map2) { | |
477 | fail("test_map_foreach_cb_check(): duplicate key \"map2\""); | |
478 | } else { | |
479 | ok(bt_value_is_map(object), "test_map_foreach_cb_check(): success getting \"map2\" value object"); | |
480 | ok(bt_value_map_is_empty(object), | |
481 | "test_map_foreach_cb_check(): \"map2\" value object is empty"); | |
c55a9f58 | 482 | checklist->map2 = BT_TRUE; |
dac5c838 PP |
483 | } |
484 | } else { | |
485 | fail("test_map_foreach_cb_check(): unknown map key \"%s\"", key); | |
486 | } | |
487 | ||
c55a9f58 | 488 | return BT_TRUE; |
dac5c838 PP |
489 | } |
490 | ||
491 | static | |
492 | void test_map(void) | |
493 | { | |
494 | int ret; | |
495 | int count = 0; | |
c55a9f58 | 496 | bt_bool bool_value; |
dac5c838 | 497 | int64_t int_value; |
a373bf69 | 498 | double real_value; |
dac5c838 PP |
499 | struct bt_value *obj; |
500 | struct bt_value *map_obj; | |
501 | struct map_foreach_checklist checklist; | |
502 | ||
503 | map_obj = bt_value_map_create(); | |
504 | ok(map_obj && bt_value_is_map(map_obj), | |
505 | "bt_value_map_create() returns a map value object"); | |
506 | ok(bt_value_map_size(map_obj) == 0, | |
507 | "initial map value object size is 0"); | |
dac5c838 PP |
508 | |
509 | obj = bt_value_integer_create_init(19457); | |
510 | ret = bt_value_map_insert(map_obj, "int", obj); | |
83509119 | 511 | BT_PUT(obj); |
a373bf69 PP |
512 | obj = bt_value_real_create_init(5.444); |
513 | ret |= bt_value_map_insert(map_obj, "real", obj); | |
83509119 | 514 | BT_PUT(obj); |
dac5c838 | 515 | obj = bt_value_bool_create(); |
c55a9f58 | 516 | ret |= bt_value_map_insert(map_obj, "bt_bool", obj); |
83509119 | 517 | BT_PUT(obj); |
dac5c838 PP |
518 | ret |= bt_value_map_insert(map_obj, "null", bt_value_null); |
519 | ok(!ret, "bt_value_map_insert() succeeds"); | |
520 | ok(bt_value_map_size(map_obj) == 4, | |
521 | "inserting an element into a map value object increment its size"); | |
522 | ||
c55a9f58 PP |
523 | obj = bt_value_bool_create_init(BT_TRUE); |
524 | ret = bt_value_map_insert(map_obj, "bt_bool", obj); | |
83509119 | 525 | BT_PUT(obj); |
dac5c838 PP |
526 | ok(!ret, "bt_value_map_insert() accepts an existing key"); |
527 | ||
dac5c838 | 528 | obj = bt_value_map_get(map_obj, "life"); |
f6ccaed9 | 529 | ok(!obj, "bt_value_map_get() returns NULL with an non existing key"); |
a373bf69 PP |
530 | obj = bt_value_map_get(map_obj, "real"); |
531 | ok(obj && bt_value_is_real(obj), | |
532 | "bt_value_map_get() returns an value object with the appropriate type (real)"); | |
533 | ret = bt_value_real_get(obj, &real_value); | |
534 | ok(!ret && real_value == 5.444, | |
535 | "bt_value_map_get() returns an value object with the appropriate value (real)"); | |
83509119 | 536 | BT_PUT(obj); |
dac5c838 PP |
537 | obj = bt_value_map_get(map_obj, "int"); |
538 | ok(obj && bt_value_is_integer(obj), | |
539 | "bt_value_map_get() returns an value object with the appropriate type (integer)"); | |
540 | ret = bt_value_integer_get(obj, &int_value); | |
541 | ok(!ret && int_value == 19457, | |
542 | "bt_value_map_get() returns an value object with the appropriate value (integer)"); | |
83509119 | 543 | BT_PUT(obj); |
dac5c838 PP |
544 | obj = bt_value_map_get(map_obj, "null"); |
545 | ok(obj && bt_value_is_null(obj), | |
546 | "bt_value_map_get() returns an value object with the appropriate type (null)"); | |
c55a9f58 | 547 | obj = bt_value_map_get(map_obj, "bt_bool"); |
dac5c838 PP |
548 | ok(obj && bt_value_is_bool(obj), |
549 | "bt_value_map_get() returns an value object with the appropriate type (boolean)"); | |
550 | ret = bt_value_bool_get(obj, &bool_value); | |
551 | ok(!ret && bool_value, | |
552 | "bt_value_map_get() returns an value object with the appropriate value (boolean)"); | |
83509119 | 553 | BT_PUT(obj); |
dac5c838 | 554 | |
c55a9f58 | 555 | ret = bt_value_map_insert_bool(map_obj, "bool2", BT_TRUE); |
dac5c838 | 556 | ok(!ret, "bt_value_map_insert_bool() succeeds"); |
dac5c838 PP |
557 | ret = bt_value_map_insert_integer(map_obj, "int2", 98765); |
558 | ok(!ret, "bt_value_map_insert_integer() succeeds"); | |
a373bf69 PP |
559 | ret = bt_value_map_insert_real(map_obj, "real2", -49.0001); |
560 | ok(!ret, "bt_value_map_insert_real() succeeds"); | |
dac5c838 PP |
561 | ret = bt_value_map_insert_string(map_obj, "string2", "bt_value"); |
562 | ok(!ret, "bt_value_map_insert_string() succeeds"); | |
5b79e8bf PP |
563 | ret = bt_value_map_insert_empty_array(map_obj, "array2"); |
564 | ok(!ret, "bt_value_map_insert_empty_array() succeeds"); | |
5b79e8bf PP |
565 | ret = bt_value_map_insert_empty_map(map_obj, "map2"); |
566 | ok(!ret, "bt_value_map_insert_empty_map() succeeds"); | |
dac5c838 PP |
567 | |
568 | ok(bt_value_map_size(map_obj) == 10, | |
569 | "the bt_value_map_insert*() functions increment the map value object's size"); | |
570 | ||
571 | ok(!bt_value_map_has_key(map_obj, "hello"), | |
572 | "map value object does not have key \"hello\""); | |
c55a9f58 PP |
573 | ok(bt_value_map_has_key(map_obj, "bt_bool"), |
574 | "map value object has key \"bt_bool\""); | |
dac5c838 PP |
575 | ok(bt_value_map_has_key(map_obj, "int"), |
576 | "map value object has key \"int\""); | |
a373bf69 PP |
577 | ok(bt_value_map_has_key(map_obj, "real"), |
578 | "map value object has key \"real\""); | |
dac5c838 PP |
579 | ok(bt_value_map_has_key(map_obj, "null"), |
580 | "map value object has key \"null\""); | |
581 | ok(bt_value_map_has_key(map_obj, "bool2"), | |
582 | "map value object has key \"bool2\""); | |
583 | ok(bt_value_map_has_key(map_obj, "int2"), | |
584 | "map value object has key \"int2\""); | |
a373bf69 PP |
585 | ok(bt_value_map_has_key(map_obj, "real2"), |
586 | "map value object has key \"real2\""); | |
dac5c838 PP |
587 | ok(bt_value_map_has_key(map_obj, "string2"), |
588 | "map value object has key \"string2\""); | |
589 | ok(bt_value_map_has_key(map_obj, "array2"), | |
590 | "map value object has key \"array2\""); | |
591 | ok(bt_value_map_has_key(map_obj, "map2"), | |
592 | "map value object has key \"map2\""); | |
593 | ||
dac5c838 | 594 | ret = bt_value_map_foreach(map_obj, test_map_foreach_cb_count, &count); |
f6ccaed9 | 595 | ok(ret == BT_VALUE_STATUS_CANCELED && count == 3, |
c55a9f58 | 596 | "bt_value_map_foreach() breaks the loop when the user function returns BT_FALSE"); |
dac5c838 PP |
597 | |
598 | memset(&checklist, 0, sizeof(checklist)); | |
599 | ret = bt_value_map_foreach(map_obj, test_map_foreach_cb_check, | |
600 | &checklist); | |
601 | ok(ret == BT_VALUE_STATUS_OK, | |
602 | "bt_value_map_foreach() succeeds with test_map_foreach_cb_check()"); | |
a373bf69 | 603 | ok(checklist.bool1 && checklist.int1 && checklist.real1 && |
dac5c838 | 604 | checklist.null1 && checklist.bool2 && checklist.int2 && |
a373bf69 | 605 | checklist.real2 && checklist.string2 && |
dac5c838 PP |
606 | checklist.array2 && checklist.map2, |
607 | "bt_value_map_foreach() iterates over all the map value object's elements"); | |
608 | ||
83509119 | 609 | BT_PUT(map_obj); |
dac5c838 PP |
610 | pass("putting an existing map value object does not cause a crash") |
611 | } | |
612 | ||
613 | static | |
614 | void test_types(void) | |
615 | { | |
616 | test_null(); | |
617 | test_bool(); | |
618 | test_integer(); | |
a373bf69 | 619 | test_real(); |
dac5c838 PP |
620 | test_string(); |
621 | test_array(); | |
622 | test_map(); | |
623 | } | |
624 | ||
625 | static | |
626 | void test_compare_null(void) | |
627 | { | |
dac5c838 PP |
628 | ok(bt_value_compare(bt_value_null, bt_value_null), |
629 | "null value objects are equivalent"); | |
630 | } | |
631 | ||
632 | static | |
633 | void test_compare_bool(void) | |
634 | { | |
c55a9f58 PP |
635 | struct bt_value *bool1 = bt_value_bool_create_init(BT_FALSE); |
636 | struct bt_value *bool2 = bt_value_bool_create_init(BT_TRUE); | |
637 | struct bt_value *bool3 = bt_value_bool_create_init(BT_FALSE); | |
dac5c838 | 638 | |
25583cd0 | 639 | BT_ASSERT(bool1 && bool2 && bool3); |
dac5c838 | 640 | ok(!bt_value_compare(bt_value_null, bool1), |
c55a9f58 | 641 | "cannot compare null value object and bt_bool value object"); |
dac5c838 | 642 | ok(!bt_value_compare(bool1, bool2), |
c55a9f58 | 643 | "boolean value objects are not equivalent (BT_FALSE and BT_TRUE)"); |
dac5c838 | 644 | ok(bt_value_compare(bool1, bool3), |
c55a9f58 | 645 | "boolean value objects are equivalent (BT_FALSE and BT_FALSE)"); |
dac5c838 | 646 | |
83509119 JG |
647 | BT_PUT(bool1); |
648 | BT_PUT(bool2); | |
649 | BT_PUT(bool3); | |
dac5c838 PP |
650 | } |
651 | ||
652 | static | |
653 | void test_compare_integer(void) | |
654 | { | |
655 | struct bt_value *int1 = bt_value_integer_create_init(10); | |
656 | struct bt_value *int2 = bt_value_integer_create_init(-23); | |
657 | struct bt_value *int3 = bt_value_integer_create_init(10); | |
658 | ||
25583cd0 | 659 | BT_ASSERT(int1 && int2 && int3); |
dac5c838 PP |
660 | ok(!bt_value_compare(bt_value_null, int1), |
661 | "cannot compare null value object and integer value object"); | |
662 | ok(!bt_value_compare(int1, int2), | |
663 | "integer value objects are not equivalent (10 and -23)"); | |
664 | ok(bt_value_compare(int1, int3), | |
665 | "integer value objects are equivalent (10 and 10)"); | |
666 | ||
83509119 JG |
667 | BT_PUT(int1); |
668 | BT_PUT(int2); | |
669 | BT_PUT(int3); | |
dac5c838 PP |
670 | } |
671 | ||
672 | static | |
a373bf69 | 673 | void test_compare_real(void) |
dac5c838 | 674 | { |
a373bf69 PP |
675 | struct bt_value *real1 = bt_value_real_create_init(17.38); |
676 | struct bt_value *real2 = bt_value_real_create_init(-14.23); | |
677 | struct bt_value *real3 = bt_value_real_create_init(17.38); | |
678 | ||
679 | BT_ASSERT(real1 && real2 && real3); | |
680 | ||
681 | ok(!bt_value_compare(bt_value_null, real1), | |
682 | "cannot compare null value object and real number value object"); | |
683 | ok(!bt_value_compare(real1, real2), | |
684 | "real number value objects are not equivalent (17.38 and -14.23)"); | |
685 | ok(bt_value_compare(real1, real3), | |
686 | "real number value objects are equivalent (17.38 and 17.38)"); | |
687 | ||
688 | BT_PUT(real1); | |
689 | BT_PUT(real2); | |
690 | BT_PUT(real3); | |
dac5c838 PP |
691 | } |
692 | ||
693 | static | |
694 | void test_compare_string(void) | |
695 | { | |
696 | struct bt_value *string1 = bt_value_string_create_init("hello"); | |
697 | struct bt_value *string2 = bt_value_string_create_init("bt_value"); | |
698 | struct bt_value *string3 = bt_value_string_create_init("hello"); | |
699 | ||
25583cd0 | 700 | BT_ASSERT(string1 && string2 && string3); |
dac5c838 PP |
701 | |
702 | ok(!bt_value_compare(bt_value_null, string1), | |
703 | "cannot compare null value object and string value object"); | |
704 | ok(!bt_value_compare(string1, string2), | |
705 | "string value objects are not equivalent (\"hello\" and \"bt_value\")"); | |
706 | ok(bt_value_compare(string1, string3), | |
707 | "string value objects are equivalent (\"hello\" and \"hello\")"); | |
708 | ||
83509119 JG |
709 | BT_PUT(string1); |
710 | BT_PUT(string2); | |
711 | BT_PUT(string3); | |
dac5c838 PP |
712 | } |
713 | ||
714 | static | |
715 | void test_compare_array(void) | |
716 | { | |
717 | struct bt_value *array1 = bt_value_array_create(); | |
718 | struct bt_value *array2 = bt_value_array_create(); | |
719 | struct bt_value *array3 = bt_value_array_create(); | |
720 | ||
25583cd0 | 721 | BT_ASSERT(array1 && array2 && array3); |
dac5c838 PP |
722 | |
723 | ok(bt_value_compare(array1, array2), | |
724 | "empty array value objects are equivalent"); | |
725 | ||
25583cd0 | 726 | BT_ASSERT(!bt_value_array_append_integer(array1, 23)); |
a373bf69 | 727 | BT_ASSERT(!bt_value_array_append_real(array1, 14.2)); |
25583cd0 | 728 | BT_ASSERT(!bt_value_array_append_bool(array1, BT_FALSE)); |
a373bf69 | 729 | BT_ASSERT(!bt_value_array_append_real(array2, 14.2)); |
25583cd0 PP |
730 | BT_ASSERT(!bt_value_array_append_integer(array2, 23)); |
731 | BT_ASSERT(!bt_value_array_append_bool(array2, BT_FALSE)); | |
732 | BT_ASSERT(!bt_value_array_append_integer(array3, 23)); | |
a373bf69 | 733 | BT_ASSERT(!bt_value_array_append_real(array3, 14.2)); |
25583cd0 PP |
734 | BT_ASSERT(!bt_value_array_append_bool(array3, BT_FALSE)); |
735 | BT_ASSERT(bt_value_array_size(array1) == 3); | |
736 | BT_ASSERT(bt_value_array_size(array2) == 3); | |
737 | BT_ASSERT(bt_value_array_size(array3) == 3); | |
dac5c838 PP |
738 | |
739 | ok(!bt_value_compare(bt_value_null, array1), | |
740 | "cannot compare null value object and array value object"); | |
741 | ok(!bt_value_compare(array1, array2), | |
c55a9f58 | 742 | "array value objects are not equivalent ([23, 14.2, BT_FALSE] and [14.2, 23, BT_FALSE])"); |
dac5c838 | 743 | ok(bt_value_compare(array1, array3), |
c55a9f58 | 744 | "array value objects are equivalent ([23, 14.2, BT_FALSE] and [23, 14.2, BT_FALSE])"); |
dac5c838 | 745 | |
83509119 JG |
746 | BT_PUT(array1); |
747 | BT_PUT(array2); | |
748 | BT_PUT(array3); | |
dac5c838 PP |
749 | } |
750 | ||
751 | static | |
752 | void test_compare_map(void) | |
753 | { | |
754 | struct bt_value *map1 = bt_value_map_create(); | |
755 | struct bt_value *map2 = bt_value_map_create(); | |
756 | struct bt_value *map3 = bt_value_map_create(); | |
757 | ||
25583cd0 | 758 | BT_ASSERT(map1 && map2 && map3); |
dac5c838 PP |
759 | |
760 | ok(bt_value_compare(map1, map2), | |
761 | "empty map value objects are equivalent"); | |
762 | ||
25583cd0 | 763 | BT_ASSERT(!bt_value_map_insert_integer(map1, "one", 23)); |
a373bf69 | 764 | BT_ASSERT(!bt_value_map_insert_real(map1, "two", 14.2)); |
25583cd0 | 765 | BT_ASSERT(!bt_value_map_insert_bool(map1, "three", BT_FALSE)); |
a373bf69 | 766 | BT_ASSERT(!bt_value_map_insert_real(map2, "one", 14.2)); |
25583cd0 PP |
767 | BT_ASSERT(!bt_value_map_insert_integer(map2, "two", 23)); |
768 | BT_ASSERT(!bt_value_map_insert_bool(map2, "three", BT_FALSE)); | |
769 | BT_ASSERT(!bt_value_map_insert_bool(map3, "three", BT_FALSE)); | |
770 | BT_ASSERT(!bt_value_map_insert_integer(map3, "one", 23)); | |
a373bf69 | 771 | BT_ASSERT(!bt_value_map_insert_real(map3, "two", 14.2)); |
25583cd0 PP |
772 | BT_ASSERT(bt_value_map_size(map1) == 3); |
773 | BT_ASSERT(bt_value_map_size(map2) == 3); | |
774 | BT_ASSERT(bt_value_map_size(map3) == 3); | |
dac5c838 PP |
775 | |
776 | ok(!bt_value_compare(bt_value_null, map1), | |
777 | "cannot compare null value object and map value object"); | |
778 | ok(!bt_value_compare(map1, map2), | |
779 | "map value objects are not equivalent"); | |
780 | ok(bt_value_compare(map1, map3), | |
781 | "map value objects are equivalent"); | |
782 | ||
83509119 JG |
783 | BT_PUT(map1); |
784 | BT_PUT(map2); | |
785 | BT_PUT(map3); | |
dac5c838 PP |
786 | } |
787 | ||
788 | static | |
789 | void test_compare(void) | |
790 | { | |
dac5c838 PP |
791 | test_compare_null(); |
792 | test_compare_bool(); | |
793 | test_compare_integer(); | |
a373bf69 | 794 | test_compare_real(); |
dac5c838 PP |
795 | test_compare_string(); |
796 | test_compare_array(); | |
797 | test_compare_map(); | |
798 | } | |
799 | ||
800 | static | |
801 | void test_copy(void) | |
802 | { | |
803 | /* | |
804 | * Here's the deal here. If we make sure that each value object | |
805 | * of our deep copy has a different address than its source, | |
c55a9f58 | 806 | * and that bt_value_compare() returns BT_TRUE for the top-level |
dac5c838 PP |
807 | * value object, taking into account that we test the correctness of |
808 | * bt_value_compare() elsewhere, then the deep copy is a | |
809 | * success. | |
810 | */ | |
811 | struct bt_value *null_copy_obj; | |
812 | struct bt_value *bool_obj, *bool_copy_obj; | |
813 | struct bt_value *integer_obj, *integer_copy_obj; | |
a373bf69 | 814 | struct bt_value *real_obj, *real_copy_obj; |
dac5c838 PP |
815 | struct bt_value *string_obj, *string_copy_obj; |
816 | struct bt_value *array_obj, *array_copy_obj; | |
817 | struct bt_value *map_obj, *map_copy_obj; | |
818 | ||
c55a9f58 | 819 | bool_obj = bt_value_bool_create_init(BT_TRUE); |
dac5c838 | 820 | integer_obj = bt_value_integer_create_init(23); |
a373bf69 | 821 | real_obj = bt_value_real_create_init(-3.1416); |
dac5c838 PP |
822 | string_obj = bt_value_string_create_init("test"); |
823 | array_obj = bt_value_array_create(); | |
824 | map_obj = bt_value_map_create(); | |
825 | ||
a373bf69 | 826 | BT_ASSERT(bool_obj && integer_obj && real_obj && string_obj && |
dac5c838 PP |
827 | array_obj && map_obj); |
828 | ||
25583cd0 PP |
829 | BT_ASSERT(!bt_value_array_append(array_obj, bool_obj)); |
830 | BT_ASSERT(!bt_value_array_append(array_obj, integer_obj)); | |
a373bf69 | 831 | BT_ASSERT(!bt_value_array_append(array_obj, real_obj)); |
25583cd0 PP |
832 | BT_ASSERT(!bt_value_array_append(array_obj, bt_value_null)); |
833 | BT_ASSERT(!bt_value_map_insert(map_obj, "array", array_obj)); | |
834 | BT_ASSERT(!bt_value_map_insert(map_obj, "string", string_obj)); | |
dac5c838 | 835 | |
dac5c838 PP |
836 | map_copy_obj = bt_value_copy(map_obj); |
837 | ok(map_copy_obj, | |
838 | "bt_value_copy() succeeds"); | |
839 | ||
840 | ok(map_obj != map_copy_obj, | |
841 | "bt_value_copy() returns a different pointer (map)"); | |
842 | string_copy_obj = bt_value_map_get(map_copy_obj, "string"); | |
843 | ok(string_copy_obj != string_obj, | |
844 | "bt_value_copy() returns a different pointer (string)"); | |
845 | array_copy_obj = bt_value_map_get(map_copy_obj, "array"); | |
846 | ok(array_copy_obj != array_obj, | |
847 | "bt_value_copy() returns a different pointer (array)"); | |
848 | bool_copy_obj = bt_value_array_get(array_copy_obj, 0); | |
849 | ok(bool_copy_obj != bool_obj, | |
c55a9f58 | 850 | "bt_value_copy() returns a different pointer (bt_bool)"); |
dac5c838 PP |
851 | integer_copy_obj = bt_value_array_get(array_copy_obj, 1); |
852 | ok(integer_copy_obj != integer_obj, | |
853 | "bt_value_copy() returns a different pointer (integer)"); | |
a373bf69 PP |
854 | real_copy_obj = bt_value_array_get(array_copy_obj, 2); |
855 | ok(real_copy_obj != real_obj, | |
856 | "bt_value_copy() returns a different pointer (real)"); | |
dac5c838 PP |
857 | null_copy_obj = bt_value_array_get(array_copy_obj, 3); |
858 | ok(null_copy_obj == bt_value_null, | |
859 | "bt_value_copy() returns the same pointer (null)"); | |
860 | ||
861 | ok(bt_value_compare(map_obj, map_copy_obj), | |
862 | "source and destination value objects have the same content"); | |
863 | ||
83509119 JG |
864 | BT_PUT(bool_copy_obj); |
865 | BT_PUT(integer_copy_obj); | |
a373bf69 | 866 | BT_PUT(real_copy_obj); |
83509119 JG |
867 | BT_PUT(string_copy_obj); |
868 | BT_PUT(array_copy_obj); | |
869 | BT_PUT(map_copy_obj); | |
870 | BT_PUT(bool_obj); | |
871 | BT_PUT(integer_obj); | |
a373bf69 | 872 | BT_PUT(real_obj); |
83509119 JG |
873 | BT_PUT(string_obj); |
874 | BT_PUT(array_obj); | |
875 | BT_PUT(map_obj); | |
dac5c838 PP |
876 | } |
877 | ||
b6ba7620 | 878 | static |
c55a9f58 | 879 | bt_bool compare_map_elements(struct bt_value *map_a, struct bt_value *map_b, |
b6ba7620 PP |
880 | const char *key) |
881 | { | |
882 | struct bt_value *elem_a = NULL; | |
883 | struct bt_value *elem_b = NULL; | |
c55a9f58 | 884 | bt_bool equal; |
b6ba7620 PP |
885 | |
886 | elem_a = bt_value_map_get(map_a, key); | |
887 | elem_b = bt_value_map_get(map_b, key); | |
888 | equal = bt_value_compare(elem_a, elem_b); | |
889 | BT_PUT(elem_a); | |
890 | BT_PUT(elem_b); | |
891 | ||
892 | return equal; | |
893 | } | |
894 | ||
895 | static | |
896 | void test_extend(void) | |
897 | { | |
898 | struct bt_value *base_map = bt_value_map_create(); | |
899 | struct bt_value *extension_map = bt_value_map_create(); | |
900 | struct bt_value *extended_map = NULL; | |
901 | struct bt_value *array = bt_value_array_create(); | |
902 | enum bt_value_status status; | |
903 | ||
25583cd0 PP |
904 | BT_ASSERT(base_map); |
905 | BT_ASSERT(extension_map); | |
906 | BT_ASSERT(array); | |
c55a9f58 | 907 | status = bt_value_map_insert_bool(base_map, "file", BT_TRUE); |
25583cd0 | 908 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
c55a9f58 | 909 | status = bt_value_map_insert_bool(base_map, "edit", BT_FALSE); |
25583cd0 | 910 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
b6ba7620 | 911 | status = bt_value_map_insert_integer(base_map, "selection", 17); |
25583cd0 | 912 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
b6ba7620 | 913 | status = bt_value_map_insert_integer(base_map, "find", -34); |
25583cd0 | 914 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
c55a9f58 | 915 | status = bt_value_map_insert_bool(extension_map, "edit", BT_TRUE); |
25583cd0 | 916 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
b6ba7620 | 917 | status = bt_value_map_insert_integer(extension_map, "find", 101); |
25583cd0 | 918 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
a373bf69 | 919 | status = bt_value_map_insert_real(extension_map, "project", -404); |
25583cd0 | 920 | BT_ASSERT(status == BT_VALUE_STATUS_OK); |
b6ba7620 PP |
921 | extended_map = bt_value_map_extend(base_map, extension_map); |
922 | ok(extended_map, "bt_value_map_extend() succeeds"); | |
923 | ok(bt_value_map_size(extended_map) == 5, | |
924 | "bt_value_map_extend() returns a map object with the correct size"); | |
925 | ok(compare_map_elements(base_map, extended_map, "file"), | |
926 | "bt_value_map_extend() picks the appropriate element (file)"); | |
927 | ok(compare_map_elements(extension_map, extended_map, "edit"), | |
928 | "bt_value_map_extend() picks the appropriate element (edit)"); | |
929 | ok(compare_map_elements(base_map, extended_map, "selection"), | |
930 | "bt_value_map_extend() picks the appropriate element (selection)"); | |
931 | ok(compare_map_elements(extension_map, extended_map, "find"), | |
932 | "bt_value_map_extend() picks the appropriate element (find)"); | |
933 | ok(compare_map_elements(extension_map, extended_map, "project"), | |
934 | "bt_value_map_extend() picks the appropriate element (project)"); | |
935 | ||
936 | BT_PUT(array); | |
937 | BT_PUT(base_map); | |
938 | BT_PUT(extension_map); | |
939 | BT_PUT(extended_map); | |
940 | } | |
941 | ||
dac5c838 PP |
942 | int main(void) |
943 | { | |
8bbe269d | 944 | plan_tests(NR_TESTS); |
dac5c838 PP |
945 | test_types(); |
946 | test_compare(); | |
947 | test_copy(); | |
b6ba7620 | 948 | test_extend(); |
dac5c838 PP |
949 | return 0; |
950 | } |