Fix test: remove hardcoded /tmp path
[babeltrace.git] / tests / lib / test_ctf_ir_ref.c
index 3726b44bd604f37a0fc45f7c984cfde2a326c145..e2ee556c80adc53e515c83026c3aea206d7b85ce 100644 (file)
@@ -30,7 +30,7 @@
 #include <babeltrace/ctf-ir/event.h>
 #include <babeltrace/ctf-ir/event-class.h>
 #include <babeltrace/object-internal.h>
-#include <babeltrace/compat/stdlib.h>
+#include <babeltrace/compat/stdlib-internal.h>
 #include <assert.h>
 #include "common.h"
 
@@ -210,18 +210,62 @@ error:
        goto end;;
 }
 
+static void set_stream_class_field_types(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *packet_context_type;
+       struct bt_ctf_field_type *event_header_type;
+       struct bt_ctf_field_type *ft;
+       int ret;
+
+       packet_context_type = bt_ctf_field_type_structure_create();
+       assert(packet_context_type);
+       ft = bt_ctf_field_type_integer_create(32);
+       assert(ft);
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               ft, "packet_size");
+       assert(ret == 0);
+       bt_put(ft);
+       ft = bt_ctf_field_type_integer_create(32);
+       assert(ft);
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               ft, "content_size");
+       assert(ret == 0);
+       bt_put(ft);
+
+       event_header_type = bt_ctf_field_type_structure_create();
+       assert(event_header_type);
+       ft = bt_ctf_field_type_integer_create(32);
+       assert(ft);
+       ret = bt_ctf_field_type_structure_add_field(event_header_type,
+               ft, "id");
+       assert(ret == 0);
+       bt_put(ft);
+
+       ret = bt_ctf_stream_class_set_packet_context_type(stream_class,
+               packet_context_type);
+       assert(ret == 0);
+       ret = bt_ctf_stream_class_set_event_header_type(stream_class,
+               event_header_type);
+       assert(ret == 0);
+
+       bt_put(packet_context_type);
+       bt_put(event_header_type);
+}
+
 static struct bt_ctf_stream_class *create_sc1(void)
 {
        int ret;
        struct bt_ctf_event_class *ec1 = NULL, *ec2 = NULL;
        struct bt_ctf_stream_class *sc1 = NULL, *ret_stream = NULL;
 
-       sc1 = bt_ctf_stream_class_create("sc1");
+       sc1 = bt_ctf_stream_class_create_empty("sc1");
        if (!sc1) {
                diag("Failed to create Stream Class");
                goto error;
        }
 
+       set_stream_class_field_types(sc1);
        ec1 = create_complex_event("ec1");
        if (!ec1) {
                diag("Failed to create complex event EC1");
@@ -266,12 +310,13 @@ static struct bt_ctf_stream_class *create_sc2(void)
        struct bt_ctf_event_class *ec3 = NULL;
        struct bt_ctf_stream_class *sc2 = NULL, *ret_stream = NULL;
 
-       sc2 = bt_ctf_stream_class_create("sc2");
+       sc2 = bt_ctf_stream_class_create_empty("sc2");
        if (!sc2) {
                diag("Failed to create Stream Class");
                goto error;
        }
 
+       set_stream_class_field_types(sc2);
        ec3 = create_simple_event("ec3");
        if (!ec3) {
                diag("Failed to create simple event EC3");
@@ -294,6 +339,28 @@ error:
        goto end;
 }
 
+static void set_trace_packet_header(struct bt_ctf_trace *trace)
+{
+       struct bt_ctf_field_type *packet_header_type;
+       struct bt_ctf_field_type *ft;
+       int ret;
+
+       packet_header_type = bt_ctf_field_type_structure_create();
+       assert(packet_header_type);
+       ft = bt_ctf_field_type_integer_create(32);
+       assert(ft);
+       ret = bt_ctf_field_type_structure_add_field(packet_header_type,
+               ft, "stream_id");
+       assert(ret == 0);
+       bt_put(ft);
+
+       ret = bt_ctf_trace_set_packet_header_type(trace,
+               packet_header_type);
+       assert(ret == 0);
+
+       bt_put(packet_header_type);
+}
+
 static struct bt_ctf_trace *create_tc1(void)
 {
        int ret;
@@ -306,6 +373,7 @@ static struct bt_ctf_trace *create_tc1(void)
                goto error;
        }
 
+       set_trace_packet_header(tc1);
        sc1 = create_sc1();
        ok(sc1, "Create SC1");
        if (!sc1) {
@@ -345,11 +413,11 @@ static void init_weak_refs(struct bt_ctf_trace *tc,
                struct bt_ctf_event_class **ec3)
 {
        *tc1 = tc;
-       *sc1 = bt_ctf_trace_get_stream_class(tc, 0);
-       *sc2 = bt_ctf_trace_get_stream_class(tc, 1);
-       *ec1 = bt_ctf_stream_class_get_event_class(*sc1, 0);
-       *ec2 = bt_ctf_stream_class_get_event_class(*sc1, 1);
-       *ec3 = bt_ctf_stream_class_get_event_class(*sc2, 0);
+       *sc1 = bt_ctf_trace_get_stream_class_by_index(tc, 0);
+       *sc2 = bt_ctf_trace_get_stream_class_by_index(tc, 1);
+       *ec1 = bt_ctf_stream_class_get_event_class_by_index(*sc1, 0);
+       *ec2 = bt_ctf_stream_class_get_event_class_by_index(*sc1, 1);
+       *ec3 = bt_ctf_stream_class_get_event_class_by_index(*sc2, 0);
        bt_put(*sc1);
        bt_put(*sc2);
        bt_put(*ec1);
@@ -399,7 +467,7 @@ static void test_example_scenario(void)
                        "TC1 reference count is 1");
 
        /* User A acquires a reference to SC2 from TC1. */
-       user_a.sc = bt_ctf_trace_get_stream_class(user_a.tc, 1);
+       user_a.sc = bt_ctf_trace_get_stream_class_by_index(user_a.tc, 1);
        ok(user_a.sc, "User A acquires SC2 from TC1");
        ok(bt_object_get_ref_count(weak_tc1) == 2,
                        "TC1 reference count is 2");
@@ -407,7 +475,7 @@ static void test_example_scenario(void)
                        "SC2 reference count is 1");
 
        /* User A acquires a reference to EC3 from SC2. */
-       user_a.ec = bt_ctf_stream_class_get_event_class(user_a.sc, 0);
+       user_a.ec = bt_ctf_stream_class_get_event_class_by_index(user_a.sc, 0);
        ok(user_a.ec, "User A acquires EC3 from SC2");
        ok(bt_object_get_ref_count(weak_tc1) == 2,
                        "TC1 reference count is 2");
@@ -454,7 +522,7 @@ static void test_example_scenario(void)
 
        /* User C acquires a reference to EC1. */
        diag("User C acquires a reference to EC1");
-       user_c.ec = bt_ctf_stream_class_get_event_class(user_b.sc, 0);
+       user_c.ec = bt_ctf_stream_class_get_event_class_by_index(user_b.sc, 0);
        ok(bt_object_get_ref_count(weak_ec1) == 1,
                        "EC1 reference count is 1");
        ok(bt_object_get_ref_count(weak_sc1) == 2,
@@ -499,12 +567,13 @@ static void test_example_scenario(void)
 
 static void create_user_full(struct user *user)
 {
-       char trace_path[] = "/tmp/ctfwriter_XXXXXX";
+       gchar *trace_path;
        struct bt_ctf_field_type *ft;
        struct bt_ctf_field *field;
        struct bt_ctf_clock *clock;
        int ret;
 
+       trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL);
        if (!bt_mkdtemp(trace_path)) {
                perror("# perror");
        }
@@ -556,6 +625,7 @@ static void create_user_full(struct user *user)
        ret = bt_ctf_stream_append_event(user->stream, user->event);
        assert(!ret);
        recursive_rmdir(trace_path);
+       g_free(trace_path);
 }
 
 static void test_put_order_swap(size_t *array, size_t a, size_t b)
This page took 0.0279 seconds and 4 git commands to generate.