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