/*
* 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>
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#define _GNU_SOURCE
#include <babeltrace/values.h>
#include <assert.h>
#include <string.h>
#include "tap/tap.h"
+#define NR_TESTS 249
+
static
void test_null(void)
{
ok(bt_value_null, "bt_value_null is not NULL");
ok(bt_value_is_null(bt_value_null),
"bt_value_null is a null value object");
- bt_value_get(bt_value_null);
+ bt_get(bt_value_null);
pass("getting bt_value_null does not cause a crash");
- bt_value_put(bt_value_null);
+ bt_put(bt_value_null);
pass("putting bt_value_null does not cause a crash");
- bt_value_get(NULL);
+ bt_get(NULL);
pass("getting NULL does not cause a crash");
- bt_value_put(NULL);
+ bt_put(NULL);
pass("putting NULL does not cause a crash");
ok(bt_value_get_type(NULL) == BT_VALUE_TYPE_UNKNOWN,
ret = bt_value_bool_get(obj, &value);
ok(!ret && value, "bt_value_bool_set() works");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
pass("putting an existing boolean value object does not cause a crash")
value = false;
ok(!ret && value,
"bt_value_bool_set() does not alter a frozen floating point number value object");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
}
static
ret = bt_value_integer_get(obj, &value);
ok(!ret && value == -98765, "bt_value_integer_set() works");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
pass("putting an existing integer value object does not cause a crash")
obj = bt_value_integer_create_init(321456987);
ok(!ret && value == 321456987,
"bt_value_integer_set() does not alter a frozen integer value object");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
}
static
ret = bt_value_float_get(obj, &value);
ok(!ret && value == -3.1416, "bt_value_float_set() works");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
pass("putting an existing floating point number value object does not cause a crash")
obj = bt_value_float_create_init(33.1649758);
ok(!ret && value == 33.1649758,
"bt_value_float_set() does not alter a frozen floating point number value object");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
}
static
ok(!ret && value && !strcmp(value, "hello worldz"),
"bt_value_string_get() works");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
pass("putting an existing string value object does not cause a crash")
obj = bt_value_string_create_init(NULL);
ok(!ret && value && !strcmp(value, "initial value"),
"bt_value_string_set() does not alter a frozen string value object");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
}
static
obj = bt_value_integer_create_init(345);
ret = bt_value_array_append(array_obj, obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_float_create_init(-17.45);
ret |= bt_value_array_append(array_obj, obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_bool_create_init(true);
ret |= bt_value_array_append(array_obj, obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
ret |= bt_value_array_append(array_obj, bt_value_null);
ok(!ret, "bt_value_array_append() succeeds");
ok(bt_value_array_size(array_obj) == 4,
ret = bt_value_integer_get(obj, &int_value);
ok(!ret && int_value == 345,
"bt_value_array_get() returns an value object with the appropriate value (integer)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 1);
ok(obj && bt_value_is_float(obj),
"bt_value_array_get() returns an value object with the appropriate type (floating point number)");
ret = bt_value_float_get(obj, &float_value);
ok(!ret && float_value == -17.45,
"bt_value_array_get() returns an value object with the appropriate value (floating point number)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 2);
ok(obj && bt_value_is_bool(obj),
"bt_value_array_get() returns an value object with the appropriate type (boolean)");
ret = bt_value_bool_get(obj, &bool_value);
ok(!ret && bool_value,
"bt_value_array_get() returns an value object with the appropriate value (boolean)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 3);
ok(obj == bt_value_null,
"bt_value_array_get() returns an value object with the appropriate type (null)");
assert(obj);
ok(!bt_value_array_set(array_obj, 2, obj),
"bt_value_array_set() succeeds");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 2);
ok(obj && bt_value_is_integer(obj),
"bt_value_array_set() inserts an value object with the appropriate type");
assert(!ret);
ok(int_value == 1001,
"bt_value_array_set() inserts an value object with the appropriate value");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
ret = bt_value_array_append_bool(array_obj, false);
ok(!ret, "bt_value_array_append_bool() succeeds");
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");
ret = bt_value_bool_get(obj, &bool_value);
ok(!ret && !bool_value,
"bt_value_array_append_bool() appends the appropriate value");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 5);
ok(obj && bt_value_is_integer(obj),
"bt_value_array_append_integer() appends an integer value object");
ret = bt_value_integer_get(obj, &int_value);
ok(!ret && int_value == 98765,
"bt_value_array_append_integer() appends the appropriate value");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 6);
ok(obj && bt_value_is_float(obj),
"bt_value_array_append_float() appends a floating point number value object");
ret = bt_value_float_get(obj, &float_value);
ok(!ret && float_value == 2.49578,
"bt_value_array_append_float() appends the appropriate value");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_array_get(array_obj, 7);
ok(obj && bt_value_is_string(obj),
"bt_value_array_append_string() appends a string value object");
ret = bt_value_string_get(obj, &string_value);
ok(!ret && string_value && !strcmp(string_value, "bt_value"),
"bt_value_array_append_string() appends the appropriate value");
- BT_VALUE_PUT(obj);
+ 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_PUT(obj);
+ "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_PUT(obj);
+ "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(array_obj, bt_value_null) ==
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");
assert(obj);
ok(bt_value_float_set(obj, 14.52) == BT_VALUE_STATUS_FROZEN,
"freezing an array value object also freezes its elements");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
- BT_VALUE_PUT(array_obj);
+ BT_PUT(array_obj);
pass("putting an existing array value object does not cause a crash")
}
obj = bt_value_integer_create_init(19457);
ret = bt_value_map_insert(map_obj, "int", obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_float_create_init(5.444);
ret |= bt_value_map_insert(map_obj, "float", obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_bool_create();
ret |= bt_value_map_insert(map_obj, "bool", obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
ret |= bt_value_map_insert(map_obj, "null", bt_value_null);
ok(!ret, "bt_value_map_insert() succeeds");
ok(bt_value_map_size(map_obj) == 4,
obj = bt_value_bool_create_init(true);
ret = bt_value_map_insert(map_obj, "bool", obj);
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
ok(!ret, "bt_value_map_insert() accepts an existing key");
obj = bt_value_map_get(map_obj, NULL);
ret = bt_value_float_get(obj, &float_value);
ok(!ret && float_value == 5.444,
"bt_value_map_get() returns an value object with the appropriate value (float)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_map_get(map_obj, "int");
ok(obj && bt_value_is_integer(obj),
"bt_value_map_get() returns an value object with the appropriate type (integer)");
ret = bt_value_integer_get(obj, &int_value);
ok(!ret && int_value == 19457,
"bt_value_map_get() returns an value object with the appropriate value (integer)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
obj = bt_value_map_get(map_obj, "null");
ok(obj && bt_value_is_null(obj),
"bt_value_map_get() returns an value object with the appropriate type (null)");
ret = bt_value_bool_get(obj, &bool_value);
ok(!ret && bool_value,
"bt_value_map_get() returns an value object with the appropriate value (boolean)");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
ret = bt_value_map_insert_bool(map_obj, "bool2", true);
ok(!ret, "bt_value_map_insert_bool() succeeds");
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_VALUE_PUT(map_obj);
+ BT_PUT(map_obj);
pass("putting an existing map value object does not cause a crash")
}
ok(bt_value_compare(bool1, bool3),
"integer value objects are equivalent (false and false)");
- BT_VALUE_PUT(bool1);
- BT_VALUE_PUT(bool2);
- BT_VALUE_PUT(bool3);
+ BT_PUT(bool1);
+ BT_PUT(bool2);
+ BT_PUT(bool3);
}
static
ok(bt_value_compare(int1, int3),
"integer value objects are equivalent (10 and 10)");
- BT_VALUE_PUT(int1);
- BT_VALUE_PUT(int2);
- BT_VALUE_PUT(int3);
+ BT_PUT(int1);
+ BT_PUT(int2);
+ BT_PUT(int3);
}
static
ok(bt_value_compare(float1, float3),
"floating point number value objects are equivalent (17.38 and 17.38)");
- BT_VALUE_PUT(float1);
- BT_VALUE_PUT(float2);
- BT_VALUE_PUT(float3);
+ BT_PUT(float1);
+ BT_PUT(float2);
+ BT_PUT(float3);
}
static
ok(bt_value_compare(string1, string3),
"string value objects are equivalent (\"hello\" and \"hello\")");
- BT_VALUE_PUT(string1);
- BT_VALUE_PUT(string2);
- BT_VALUE_PUT(string3);
+ BT_PUT(string1);
+ BT_PUT(string2);
+ BT_PUT(string3);
}
static
ok(bt_value_compare(array1, array3),
"array value objects are equivalent ([23, 14.2, false] and [23, 14.2, false])");
- BT_VALUE_PUT(array1);
- BT_VALUE_PUT(array2);
- BT_VALUE_PUT(array3);
+ BT_PUT(array1);
+ BT_PUT(array2);
+ BT_PUT(array3);
}
static
ok(bt_value_compare(map1, map3),
"map value objects are equivalent");
- BT_VALUE_PUT(map1);
- BT_VALUE_PUT(map2);
- BT_VALUE_PUT(map3);
+ BT_PUT(map1);
+ BT_PUT(map2);
+ BT_PUT(map3);
}
static
ok(bt_value_compare(map_obj, map_copy_obj),
"source and destination value objects have the same content");
- BT_VALUE_PUT(bool_copy_obj);
- BT_VALUE_PUT(integer_copy_obj);
- BT_VALUE_PUT(float_copy_obj);
- BT_VALUE_PUT(string_copy_obj);
- BT_VALUE_PUT(array_copy_obj);
- BT_VALUE_PUT(map_copy_obj);
- BT_VALUE_PUT(bool_obj);
- BT_VALUE_PUT(integer_obj);
- BT_VALUE_PUT(float_obj);
- BT_VALUE_PUT(string_obj);
- BT_VALUE_PUT(array_obj);
- BT_VALUE_PUT(map_obj);
+ BT_PUT(bool_copy_obj);
+ BT_PUT(integer_copy_obj);
+ BT_PUT(float_copy_obj);
+ BT_PUT(string_copy_obj);
+ BT_PUT(array_copy_obj);
+ BT_PUT(map_copy_obj);
+ BT_PUT(bool_obj);
+ BT_PUT(integer_obj);
+ BT_PUT(float_obj);
+ BT_PUT(string_obj);
+ BT_PUT(array_obj);
+ 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
{
struct bt_value *obj = bt_value_bool_create();
struct bt_value *src;
- struct bt_value *dst;
+ struct bt_value *dst = NULL;
assert(obj);
- BT_VALUE_PUT(obj);
- ok(!obj, "BT_VALUE_PUT() resets the variable to NULL");
+ BT_PUT(obj);
+ ok(!obj, "BT_PUT() resets the variable to NULL");
obj = bt_value_bool_create();
assert(obj);
src = obj;
- BT_VALUE_MOVE(dst, src);
- ok(!src, "BT_VALUE_MOVE() resets the source variable to NULL");
- ok(dst == obj, "BT_VALUE_MOVE() moves the ownership");
+ BT_MOVE(dst, src);
+ ok(!src, "BT_MOVE() resets the source variable to NULL");
+ ok(dst == obj, "BT_MOVE() moves the ownership");
- BT_VALUE_PUT(dst);
+ BT_PUT(dst);
}
static
ok(bt_value_is_frozen(obj),
"bt_value_is_frozen() returns true with a frozen value object");
- BT_VALUE_PUT(obj);
+ BT_PUT(obj);
}
int main(void)
{
- plan_no_plan();
+ plan_tests(NR_TESTS);
test_macros();
test_freeze();
test_types();
test_compare();
test_copy();
+ test_extend();
return 0;
}