/*
* test_bt_values.c
*
- * Babeltrace value value objects tests
+ * Babeltrace value objects tests
*
* Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
* Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
#include <string.h>
#include "tap/tap.h"
-#define NR_TESTS 238
+#define NR_TESTS 249
static
void test_null(void)
ok(bt_value_array_append_string(NULL, "bt_obj") ==
BT_VALUE_STATUS_INVAL,
"bt_value_array_append_string() fails with an array value object set to NULL");
- ret = bt_value_array_append_array(array_obj);
- ok(!ret, "bt_value_array_append_array() succeeds");
- ok(bt_value_array_append_array(NULL) == BT_VALUE_STATUS_INVAL,
- "bt_value_array_append_array() fails with an array value object set to NULL");
- ret = bt_value_array_append_map(array_obj);
- ok(!ret, "bt_value_array_append_map() succeeds");
- ok(bt_value_array_append_map(NULL) == BT_VALUE_STATUS_INVAL,
- "bt_value_array_append_map() fails with an array value object set to NULL");
+ ret = bt_value_array_append_empty_array(array_obj);
+ ok(!ret, "bt_value_array_append_empty_array() succeeds");
+ ok(bt_value_array_append_empty_array(NULL) == BT_VALUE_STATUS_INVAL,
+ "bt_value_array_append_empty_array() fails with an array value object set to NULL");
+ ret = bt_value_array_append_empty_map(array_obj);
+ ok(!ret, "bt_value_array_append_empty_map() succeeds");
+ ok(bt_value_array_append_empty_map(NULL) == BT_VALUE_STATUS_INVAL,
+ "bt_value_array_append_empty_map() fails with an array value object set to NULL");
ok(bt_value_array_size(array_obj) == 10,
"the bt_value_array_append_*() functions increment the array value object's size");
BT_PUT(obj);
obj = bt_value_array_get(array_obj, 8);
ok(obj && bt_value_is_array(obj),
- "bt_value_array_append_array() appends an array value object");
+ "bt_value_array_append_empty_array() appends an array value object");
ok(bt_value_array_is_empty(obj),
- "bt_value_array_append_array() an empty array value object");
+ "bt_value_array_append_empty_array() an empty array value object");
BT_PUT(obj);
obj = bt_value_array_get(array_obj, 9);
ok(obj && bt_value_is_map(obj),
- "bt_value_array_append_map() appends a map value object");
+ "bt_value_array_append_empty_map() appends a map value object");
ok(bt_value_map_is_empty(obj),
- "bt_value_array_append_map() an empty map value object");
+ "bt_value_array_append_empty_map() an empty map value object");
BT_PUT(obj);
assert(!bt_value_freeze(array_obj));
ok(bt_value_array_append_string(array_obj, "yayayayaya") ==
BT_VALUE_STATUS_FROZEN,
"bt_value_array_append_string() fails with a frozen array value object");
- ok(bt_value_array_append_array(array_obj) ==
+ ok(bt_value_array_append_empty_array(array_obj) ==
BT_VALUE_STATUS_FROZEN,
- "bt_value_array_append_array() fails with a frozen array value object");
- ok(bt_value_array_append_map(array_obj) ==
+ "bt_value_array_append_empty_array() fails with a frozen array value object");
+ ok(bt_value_array_append_empty_map(array_obj) ==
BT_VALUE_STATUS_FROZEN,
- "bt_value_array_append_map() fails with a frozen array value object");
+ "bt_value_array_append_empty_map() fails with a frozen array value object");
ok(bt_value_array_set(array_obj, 2, bt_value_null) ==
BT_VALUE_STATUS_FROZEN,
"bt_value_array_set() fails with a frozen array value object");
ok(bt_value_map_insert_string(NULL, "string2", "bt_obj") ==
BT_VALUE_STATUS_INVAL,
"bt_value_map_insert_string() fails with a map value object set to NULL");
- ret = bt_value_map_insert_array(map_obj, "array2");
- ok(!ret, "bt_value_map_insert_array() succeeds");
- ok(bt_value_map_insert_array(NULL, "array2") == BT_VALUE_STATUS_INVAL,
- "bt_value_map_insert_array() fails with a map value object set to NULL");
- ret = bt_value_map_insert_map(map_obj, "map2");
- ok(!ret, "bt_value_map_insert_map() succeeds");
- ok(bt_value_map_insert_map(NULL, "map2") == BT_VALUE_STATUS_INVAL,
- "bt_value_map_insert_map() fails with a map value object set to NULL");
+ ret = bt_value_map_insert_empty_array(map_obj, "array2");
+ ok(!ret, "bt_value_map_insert_empty_array() succeeds");
+ ok(bt_value_map_insert_empty_array(NULL, "array2") == BT_VALUE_STATUS_INVAL,
+ "bt_value_map_insert_empty_array() fails with a map value object set to NULL");
+ ret = bt_value_map_insert_empty_map(map_obj, "map2");
+ ok(!ret, "bt_value_map_insert_empty_map() succeeds");
+ ok(bt_value_map_insert_empty_map(NULL, "map2") == BT_VALUE_STATUS_INVAL,
+ "bt_value_map_insert_empty_map() fails with a map value object set to NULL");
ok(bt_value_map_size(map_obj) == 10,
"the bt_value_map_insert*() functions increment the map value object's size");
ok(bt_value_map_insert_string(map_obj, "duh", "yayayayaya") ==
BT_VALUE_STATUS_FROZEN,
"bt_value_map_insert_string() fails with a frozen array value object");
- ok(bt_value_map_insert_array(map_obj, "duh") ==
+ ok(bt_value_map_insert_empty_array(map_obj, "duh") ==
BT_VALUE_STATUS_FROZEN,
- "bt_value_map_insert_array() fails with a frozen array value object");
- ok(bt_value_map_insert_map(map_obj, "duh") ==
+ "bt_value_map_insert_empty_array() fails with a frozen array value object");
+ ok(bt_value_map_insert_empty_map(map_obj, "duh") ==
BT_VALUE_STATUS_FROZEN,
- "bt_value_map_insert_map() fails with a frozen array value object");
+ "bt_value_map_insert_empty_map() fails with a frozen array value object");
ok(bt_value_map_size(map_obj) == 10,
"appending to a frozen map value object does not change its size");
BT_PUT(map_obj);
}
+static
+bool compare_map_elements(struct bt_value *map_a, struct bt_value *map_b,
+ const char *key)
+{
+ struct bt_value *elem_a = NULL;
+ struct bt_value *elem_b = NULL;
+ bool equal;
+
+ elem_a = bt_value_map_get(map_a, key);
+ elem_b = bt_value_map_get(map_b, key);
+ equal = bt_value_compare(elem_a, elem_b);
+ BT_PUT(elem_a);
+ BT_PUT(elem_b);
+
+ return equal;
+}
+
+static
+void test_extend(void)
+{
+ struct bt_value *base_map = bt_value_map_create();
+ struct bt_value *extension_map = bt_value_map_create();
+ struct bt_value *extended_map = NULL;
+ struct bt_value *array = bt_value_array_create();
+ enum bt_value_status status;
+
+ assert(base_map);
+ assert(extension_map);
+ assert(array);
+ status = bt_value_map_insert_bool(base_map, "file", true);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_bool(base_map, "edit", false);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_integer(base_map, "selection", 17);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_integer(base_map, "find", -34);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_bool(extension_map, "edit", true);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_integer(extension_map, "find", 101);
+ assert(status == BT_VALUE_STATUS_OK);
+ status = bt_value_map_insert_float(extension_map, "project", -404);
+ assert(status == BT_VALUE_STATUS_OK);
+ bt_value_freeze(base_map);
+ bt_value_freeze(extension_map);
+ bt_value_freeze(array);
+ ok(!bt_value_map_extend(NULL, extension_map),
+ "bt_value_map_extend() fails with a NULL base object");
+ ok(!bt_value_map_extend(base_map, NULL),
+ "bt_value_map_extend() fails with a NULL extension object");
+ ok(!bt_value_map_extend(array, extension_map),
+ "bt_value_map_extend() fails with a non-map base object");
+ ok(!bt_value_map_extend(base_map, array),
+ "bt_value_map_extend() fails with a non-map extension object");
+ extended_map = bt_value_map_extend(base_map, extension_map);
+ ok(extended_map, "bt_value_map_extend() succeeds");
+ ok(bt_value_map_size(extended_map) == 5,
+ "bt_value_map_extend() returns a map object with the correct size");
+ ok(compare_map_elements(base_map, extended_map, "file"),
+ "bt_value_map_extend() picks the appropriate element (file)");
+ ok(compare_map_elements(extension_map, extended_map, "edit"),
+ "bt_value_map_extend() picks the appropriate element (edit)");
+ ok(compare_map_elements(base_map, extended_map, "selection"),
+ "bt_value_map_extend() picks the appropriate element (selection)");
+ ok(compare_map_elements(extension_map, extended_map, "find"),
+ "bt_value_map_extend() picks the appropriate element (find)");
+ ok(compare_map_elements(extension_map, extended_map, "project"),
+ "bt_value_map_extend() picks the appropriate element (project)");
+
+ BT_PUT(array);
+ BT_PUT(base_map);
+ BT_PUT(extension_map);
+ BT_PUT(extended_map);
+}
+
static
void test_macros(void)
{
test_types();
test_compare();
test_copy();
+ test_extend();
return 0;
}