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