Freeze event classes as they are added to a stream class
[babeltrace.git] / tests / lib / test_ctf_writer.c
index c2b590f9b1ba5df5e41c5b7eb8f538a1536e5b01..3a56a558d44102ec94e6f90198ecfe3b20e5c73b 100644 (file)
@@ -454,8 +454,28 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
                "integer_field");
        bt_ctf_event_class_add_field(simple_event_class, float_type,
                "float_field");
-       bt_ctf_stream_class_add_event_class(stream_class,
-               simple_event_class);
+
+       /* Set an event context type which will contain a single integer*/
+       ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
+               "event_specific_context"),
+               "Add event specific context field");
+       ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
+               "bt_ctf_event_class_get_context_type handles NULL correctly");
+       ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
+               "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
+
+       ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
+               "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
+       ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
+               "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
+       ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
+               "Set an event class' context type successfully");
+       returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
+       ok(returned_type == event_context_type,
+               "bt_ctf_event_class_get_context_type returns the appropriate type");
+       bt_ctf_field_type_put(returned_type);
+
+       bt_ctf_stream_class_add_event_class(stream_class, simple_event_class);
 
        ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
                "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
@@ -481,25 +501,6 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
                "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
        bt_ctf_event_class_put(ret_event_class);
 
-       /* Set an event context type which will contain a single integer*/
-       ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
-               "event_specific_context"),
-               "Add event specific context field");
-       ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
-               "bt_ctf_event_class_get_context_type handles NULL correctly");
-       ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
-               "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
-       ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
-               "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
-       ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
-               "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
-       ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
-               "Set an event class' context type successfully");
-       returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
-       ok(returned_type == event_context_type,
-               "bt_ctf_event_class_get_context_type returns the appropriate type");
-       bt_ctf_field_type_put(returned_type);
-
        simple_event = bt_ctf_event_create(simple_event_class);
        ok(simple_event,
                "Instantiate an event containing a single integer field");
@@ -1485,7 +1486,6 @@ void test_empty_stream(struct bt_ctf_writer *writer)
 {
        int ret = 0;
        struct bt_ctf_trace *trace = NULL;
-       struct bt_ctf_clock *clock = NULL;
        struct bt_ctf_stream_class *stream_class = NULL;
        struct bt_ctf_stream *stream = NULL;
 
@@ -1513,11 +1513,115 @@ end:
        ok(ret == 0,
                "Created a stream class with default attributes and an empty stream");
        bt_ctf_trace_put(trace);
-       bt_ctf_clock_put(clock);
        bt_ctf_stream_put(stream);
        bt_ctf_stream_class_put(stream_class);
 }
 
+void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
+{
+       int ret = 0;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_stream *stream = NULL;
+       struct bt_ctf_event_class *event_class = NULL;
+       struct bt_ctf_event *event = NULL;
+       struct bt_ctf_field_type *integer_type = NULL;
+       struct bt_ctf_field *integer = NULL;
+
+       trace = bt_ctf_writer_get_trace(writer);
+       if (!trace) {
+               diag("Failed to get trace from writer");
+               ret = -1;
+               goto end;
+       }
+
+       clock = bt_ctf_trace_get_clock(trace, 0);
+       if (!clock) {
+               diag("Failed to get clock from trace");
+               ret = -1;
+               goto end;
+       }
+
+       stream_class = bt_ctf_stream_class_create("event_before_stream_test");
+       if (!stream_class) {
+               diag("Failed to create stream class");
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_stream_class_set_clock(stream_class, clock);
+       if (ret) {
+               diag("Failed to set stream class clock");
+               goto end;
+       }
+
+       event_class = bt_ctf_event_class_create("some_event_class_name");
+       integer_type = bt_ctf_field_type_integer_create(32);
+       if (!integer_type) {
+               diag("Failed to create integer field type");
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_event_class_add_field(event_class, integer_type,
+               "integer_field");
+       if (ret) {
+               diag("Failed to add field to event class");
+               goto end;
+       }
+
+       ret = bt_ctf_stream_class_add_event_class(stream_class,
+               event_class);
+       if (ret) {
+               diag("Failed to add event class to stream class");
+       }
+
+       event = bt_ctf_event_create(event_class);
+       if (!event) {
+               diag("Failed to create event");
+               ret = -1;
+               goto end;
+       }
+
+       integer = bt_ctf_event_get_payload_by_index(event, 0);
+       if (!integer) {
+               diag("Failed to get integer field payload from event");
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
+       if (ret) {
+               diag("Failed to set integer field value");
+               goto end;
+       }
+
+       stream = bt_ctf_writer_create_stream(writer, stream_class);
+       if (!stream) {
+               diag("Failed to create writer stream");
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_stream_append_event(stream, event);
+       if (ret) {
+               diag("Failed to append event to stream");
+               goto end;
+       }
+end:
+       ok(ret == 0,
+               "Create an event before instanciating its associated stream");
+       bt_ctf_trace_put(trace);
+       bt_ctf_stream_put(stream);
+       bt_ctf_stream_class_put(stream_class);
+       bt_ctf_event_class_put(event_class);
+       bt_ctf_event_put(event);
+       bt_ctf_field_type_put(integer_type);
+       bt_ctf_field_put(integer);
+       bt_ctf_clock_put(clock);
+}
+
 int main(int argc, char **argv)
 {
        char trace_path[] = "/tmp/ctfwriter_XXXXXX";
@@ -2037,6 +2141,8 @@ int main(int argc, char **argv)
        ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
                "Successfully set a stream's packet header");
 
+       test_instanciate_event_before_stream(writer);
+
        append_simple_event(stream_class, stream1, clock);
 
        packet_resize_test(stream_class, stream1, clock);
This page took 0.025517 seconds and 4 git commands to generate.