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