Tests: Test CTF-IR event-field's getters
[babeltrace.git] / tests / lib / test_ctf_writer.c
index 0927a325cc0a06ab7b071b554d760ff5a4e8aa2b..3134b4d2b243045c60f0b193fd6e6fcd37b02be3 100644 (file)
 #include <fcntl.h>
 #include <dirent.h>
 #include "tap/tap.h"
+#include <math.h>
+#include <float.h>
 
 #define METADATA_LINE_SIZE 512
 #define SEQUENCE_TEST_LENGTH 10
 #define PACKET_RESIZE_TEST_LENGTH 100000
 
-static uint64_t current_time;
+#define DEFAULT_CLOCK_FREQ 1000000000
+#define DEFAULT_CLOCK_PRECISION 1
+#define DEFAULT_CLOCK_OFFSET 0
+#define DEFAULT_CLOCK_OFFSET_S 0
+#define DEFAULT_CLOCK_IS_ABSOLUTE 0
+#define DEFAULT_CLOCK_TIME 0
+
+static uint64_t current_time = 42;
 
 void validate_metadata(char *parser_path, char *metadata_path)
 {
@@ -261,6 +270,10 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        struct bt_ctf_field *float_field;
        struct bt_ctf_field *enum_field;
        struct bt_ctf_field *enum_container_field;
+       const char *mapping_name_test = "truie";
+       const char *mapping_name;
+       const double double_test_value = 3.1415;
+       double ret_double;
 
        bt_ctf_field_type_set_alignment(float_type, 32);
        bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11);
@@ -275,6 +288,8 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
                "event clock int float", 5, 22) == 0,
                "Accept enumeration mapping strings containing reserved keywords");
+       bt_ctf_field_type_enumeration_add_mapping(enum_type,
+               mapping_name_test, 42, 42);
        ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
                "enum_field") == 0, "Add enumeration field to event");
 
@@ -297,13 +312,30 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
                integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
 
        float_field = bt_ctf_event_get_payload(simple_event, "float_field");
-       bt_ctf_field_floating_point_set_value(float_field, 3.1415);
+       ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
+               "bt_ctf_field_floating_point_get_value fails on an unset float field");
+       bt_ctf_field_floating_point_set_value(float_field, double_test_value);
+       ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
+               "bt_ctf_field_floating_point_get_value properly handles a NULL field");
+       ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
+               "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
+       ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
+               "bt_ctf_field_floating_point_get_value returns a double value");
+       ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
+               "bt_ctf_field_floating_point_get_value returns a correct value");
+
        enum_field = bt_ctf_field_create(enum_type);
+       mapping_name = bt_ctf_field_enumeration_get_mapping_name(NULL);
+       ok(!mapping_name, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
+       mapping_name = bt_ctf_field_enumeration_get_mapping_name(enum_field);
+       ok(!mapping_name, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
        enum_container_field = bt_ctf_field_enumeration_get_container(
                enum_field);
        ok(bt_ctf_field_unsigned_integer_set_value(
-               enum_container_field, 1) == 0,
+               enum_container_field, 42) == 0,
                "Set enumeration container value");
+       mapping_name = bt_ctf_field_enumeration_get_mapping_name(enum_field);
+       ok(!strcmp(mapping_name, mapping_name_test), "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name");
        bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
 
        ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
@@ -329,6 +361,7 @@ void append_complex_event(struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
 {
        int i;
+       const char *test_string = "Test string";
        struct bt_ctf_field_type *uint_35_type =
                bt_ctf_field_type_integer_create(35);
        struct bt_ctf_field_type *int_16_type =
@@ -352,7 +385,10 @@ void append_complex_event(struct bt_ctf_stream_class *stream_class,
        struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
                *inner_structure_field, *complex_structure_field,
                *a_sequence_field, *enum_variant_field, *enum_container_field,
-               *variant_field;
+               *variant_field, *ret_field;
+       int64_t ret_signed_int;
+       uint64_t ret_unsigned_int;
+       const char *ret_string;
 
        bt_ctf_field_type_set_alignment(int_16_type, 32);
        bt_ctf_field_type_integer_set_signed(int_16_type, 1);
@@ -420,16 +456,55 @@ void append_complex_event(struct bt_ctf_stream_class *stream_class,
        ok(event, "Instanciate a complex event");
 
        uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
+       if (!uint_35_field)
+               printf("uint_35_field is NULL\n");
        ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
        bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
+       ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) == -1,
+               "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
+       ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) == -1,
+               "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
+       ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
+               &ret_unsigned_int) == 0,
+               "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
+       ok(ret_unsigned_int == 0x0DDF00D,
+               "bt_ctf_field_unsigned_integer_get_value returns the correct value");
+       ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
+               &ret_signed_int) == -1,
+               "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
        bt_ctf_field_put(uint_35_field);
 
        int_16_field = bt_ctf_event_get_payload(event, "int_16");
        bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
+       ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) == -1,
+               "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
+       ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) == -1,
+               "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
+       ok(bt_ctf_field_signed_integer_get_value(int_16_field,
+               &ret_signed_int) == 0,
+               "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
+       ok(ret_signed_int == -12345,
+               "bt_ctf_field_signed_integer_get_value returns the correct value");
+       ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
+               &ret_unsigned_int) == -1,
+               "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
        bt_ctf_field_put(int_16_field);
 
        complex_structure_field = bt_ctf_event_get_payload(event,
                "complex_structure");
+
+       ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
+               "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
+       ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
+               "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
+       inner_structure_field = bt_ctf_field_structure_get_field_by_index(
+               complex_structure_field, 3);
+       ret_field_type = bt_ctf_field_get_type(inner_structure_field);
+       bt_ctf_field_put(inner_structure_field);
+       ok(ret_field_type == inner_structure_type,
+               "bt_ctf_field_structure_get_field_by_index returns a correct field");
+       bt_ctf_field_type_put(ret_field_type);
+
        inner_structure_field = bt_ctf_field_structure_get_field(
                complex_structure_field, "inner_structure");
        a_string_field = bt_ctf_field_structure_get_field(
@@ -450,12 +525,28 @@ void append_complex_event(struct bt_ctf_stream_class *stream_class,
                enum_variant_field);
        bt_ctf_field_signed_integer_set_value(int_16_field, -200);
        bt_ctf_field_put(int_16_field);
+       ok(!bt_ctf_field_string_get_value(a_string_field),
+               "bt_ctf_field_string_get_value returns NULL on an unset field");
        bt_ctf_field_string_set_value(a_string_field,
-               "Test string");
+               test_string);
+       ok(!bt_ctf_field_string_get_value(NULL),
+               "bt_ctf_field_string_get_value correctly handles NULL");
+       ret_string = bt_ctf_field_string_get_value(a_string_field);
+       ok(ret_string, "bt_ctf_field_string_get_value returns a string");
+       ok(!strcmp(ret_string, test_string),
+               "bt_ctf_field_string_get_value returns a correct value");
        bt_ctf_field_unsigned_integer_set_value(uint_35_field,
                SEQUENCE_TEST_LENGTH);
+
+       ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
+               "bt_ctf_field_sequence_get_length returns NULL when length is unset");
        ok(bt_ctf_field_sequence_set_length(a_sequence_field,
                uint_35_field) == 0, "Set a sequence field's length");
+       ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
+       ok(ret_field == uint_35_field,
+               "bt_ctf_field_sequence_get_length returns the correct length field");
+       ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
+               "bt_ctf_field_sequence_get_length properly handles NULL");
 
        for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
                int_16_field = bt_ctf_field_sequence_get_field(
@@ -478,6 +569,7 @@ void append_complex_event(struct bt_ctf_stream_class *stream_class,
        bt_ctf_field_put(enum_variant_field);
        bt_ctf_field_put(enum_container_field);
        bt_ctf_field_put(variant_field);
+       bt_ctf_field_put(ret_field);
        bt_ctf_field_type_put(uint_35_type);
        bt_ctf_field_type_put(int_16_type);
        bt_ctf_field_type_put(string_type);
@@ -508,6 +600,10 @@ void type_field_tests()
        struct bt_ctf_field_type *enumeration_type;
        struct bt_ctf_field_type *enumeration_sequence_type;
        struct bt_ctf_field_type *enumeration_array_type;
+       struct bt_ctf_field_type *returned_type;
+
+       returned_type = bt_ctf_field_get_type(NULL);
+       ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
 
        ok(uint_12_type, "Create an unsigned integer type");
        ok(bt_ctf_field_type_integer_set_base(uint_12_type,
@@ -571,6 +667,10 @@ void type_field_tests()
        ok(int_16, "Instanciate a signed 16-bit integer");
        uint_12 = bt_ctf_field_create(uint_12_type);
        ok(uint_12, "Instanciate an unsigned 12-bit integer");
+       returned_type = bt_ctf_field_get_type(int_16);
+       ok(returned_type == int_16_type,
+               "bt_ctf_field_get_type returns the correct type");
+       bt_ctf_field_type_put(returned_type);
 
        /* Can't modify types after instanciating them */
        ok(bt_ctf_field_type_integer_set_base(uint_12_type,
@@ -708,10 +808,13 @@ int main(int argc, char **argv)
        char metadata_path[sizeof(trace_path) + 9];
        const char *clock_name = "test_clock";
        const char *clock_description = "This is a test clock";
-       const uint64_t frequency = 1000000000;
+       const char *returned_clock_name;
+       const char *returned_clock_description;
+       const uint64_t frequency = 1123456789;
        const uint64_t offset_s = 1351530929945824323;
        const uint64_t offset = 1234567;
        const uint64_t precision = 10;
+       const int is_absolute = 0xFF;
        char *metadata_string;
        struct bt_ctf_writer *writer;
        struct utsname name;
@@ -775,27 +878,105 @@ int main(int argc, char **argv)
                name.machine);
 
        /* Define a clock and add it to the trace */
-       ok(bt_ctf_clock_create("signed") == NULL, "Illegal clock name rejected");
+       ok(bt_ctf_clock_create("signed") == NULL,
+               "Illegal clock name rejected");
        ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
        clock = bt_ctf_clock_create(clock_name);
        ok(clock, "Clock created sucessfully");
-       bt_ctf_clock_set_description(clock, clock_description);
-
+       returned_clock_name = bt_ctf_clock_get_name(clock);
+       ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
+       ok(strcmp(returned_clock_name, clock_name) == 0,
+               "Returned clock name is valid");
+
+       returned_clock_description = bt_ctf_clock_get_description(clock);
+       ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
+       ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
+               "Clock description set successfully");
+
+       returned_clock_description = bt_ctf_clock_get_description(clock);
+       ok(returned_clock_description,
+               "bt_ctf_clock_get_description returns a description.");
+       ok(strcmp(returned_clock_description, clock_description) == 0,
+               "Returned clock description is valid");
+
+       ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
+               "bt_ctf_clock_get_frequency returns the correct default frequency");
        ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
                "Set clock frequency");
+       ok(bt_ctf_clock_get_frequency(clock) == frequency,
+               "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
+
+       ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
+               "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
        ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
                "Set clock offset (seconds)");
+       ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
+               "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
+
+       ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
+               "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
        ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
+       ok(bt_ctf_clock_get_offset(clock) == offset,
+               "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
+
+       ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
+               "bt_ctf_clock_get_precision returns the correct default precision");
        ok(bt_ctf_clock_set_precision(clock, precision) == 0,
                "Set clock precision");
-       ok(bt_ctf_clock_set_is_absolute(clock, 0xFF) == 0,
+       ok(bt_ctf_clock_get_precision(clock) == precision,
+               "bt_ctf_clock_get_precision returns the correct precision once it is set");
+
+       ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
+               "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
+       ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
                "Set clock absolute property");
+       ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
+               "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
+
+       ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
+               "bt_ctf_clock_get_time returns the correct default time");
+       ok(bt_ctf_clock_set_time(clock, current_time) == 0,
+               "Set clock time");
+       ok(bt_ctf_clock_get_time(clock) == current_time,
+               "bt_ctf_clock_get_time returns the correct time once it is set");
 
        ok(bt_ctf_writer_add_clock(writer, clock) == 0,
                "Add clock to writer instance");
        ok(bt_ctf_writer_add_clock(writer, clock),
                "Verify a clock can't be added twice to a writer instance");
 
+       ok(!bt_ctf_clock_get_name(NULL),
+               "bt_ctf_clock_get_name correctly handles NULL");
+       ok(!bt_ctf_clock_get_description(NULL),
+               "bt_ctf_clock_get_description correctly handles NULL");
+       ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
+               "bt_ctf_clock_get_frequency correctly handles NULL");
+       ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
+               "bt_ctf_clock_get_precision correctly handles NULL");
+       ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
+               "bt_ctf_clock_get_offset_s correctly handles NULL");
+       ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
+               "bt_ctf_clock_get_offset correctly handles NULL");
+       ok(bt_ctf_clock_get_is_absolute(NULL) == -1,
+               "bt_ctf_clock_get_is_absolute correctly handles NULL");
+       ok(bt_ctf_clock_get_time(NULL) == -1ULL,
+               "bt_ctf_clock_get_time correctly handles NULL");
+
+       ok(bt_ctf_clock_set_description(NULL, NULL) == -1,
+               "bt_ctf_clock_set_description correctly handles NULL clock");
+       ok(bt_ctf_clock_set_frequency(NULL, frequency) == -1,
+               "bt_ctf_clock_set_frequency correctly handles NULL clock");
+       ok(bt_ctf_clock_set_precision(NULL, precision) == -1,
+               "bt_ctf_clock_get_precision correctly handles NULL clock");
+       ok(bt_ctf_clock_set_offset_s(NULL, offset_s) == -1,
+               "bt_ctf_clock_set_offset_s correctly handles NULL clock");
+       ok(bt_ctf_clock_set_offset(NULL, offset) == -1,
+               "bt_ctf_clock_set_offset correctly handles NULL clock");
+       ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) == -1,
+               "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
+       ok(bt_ctf_clock_set_time(NULL, current_time) == -1,
+               "bt_ctf_clock_set_time correctly handles NULL clock");
+
        /* Define a stream class */
        stream_class = bt_ctf_stream_class_create("test_stream");
        ok(stream_class, "Create stream class");
This page took 0.028364 seconds and 4 git commands to generate.