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