Add facilities to test CLI and plugin regressions with expectation files
[babeltrace.git] / tests / plugins / src.ctf.fs / succeed / gen-trace-simple.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <stdint.h>
24 #include <babeltrace2/ctf-writer/writer.h>
25 #include <babeltrace2/ctf-writer/clock.h>
26 #include <babeltrace2/ctf-writer/clock-class.h>
27 #include <babeltrace2/ctf-writer/stream.h>
28 #include <babeltrace2/ctf-writer/event.h>
29 #include <babeltrace2/ctf-writer/event-types.h>
30 #include <babeltrace2/ctf-writer/event-fields.h>
31 #include <babeltrace2/ctf-writer/stream-class.h>
32 #include <babeltrace2/ctf-writer/trace.h>
33
34 #include "common/assert.h"
35
36 struct config {
37 struct bt_ctf_writer *writer;
38 struct bt_ctf_trace *trace;
39 struct bt_ctf_clock *clock;
40 struct bt_ctf_stream_class *sc;
41 struct bt_ctf_stream *stream;
42 struct bt_ctf_event_class *ec;
43 };
44
45 static
46 void fini_config(struct config *cfg)
47 {
48 bt_ctf_object_put_ref(cfg->stream);
49 bt_ctf_object_put_ref(cfg->sc);
50 bt_ctf_object_put_ref(cfg->ec);
51 bt_ctf_object_put_ref(cfg->clock);
52 bt_ctf_object_put_ref(cfg->trace);
53 bt_ctf_object_put_ref(cfg->writer);
54 }
55
56 static
57 void configure_writer(struct config *cfg, const char *path)
58 {
59 struct bt_ctf_field_type *ft;
60 int ret;
61
62 cfg->writer = bt_ctf_writer_create(path);
63 BT_ASSERT(cfg->writer);
64 cfg->trace = bt_ctf_writer_get_trace(cfg->writer);
65 BT_ASSERT(cfg->trace);
66 cfg->clock = bt_ctf_clock_create("default");
67 BT_ASSERT(cfg->clock);
68 ret = bt_ctf_writer_add_clock(cfg->writer, cfg->clock);
69 BT_ASSERT(ret == 0);
70 ret = bt_ctf_writer_set_byte_order(cfg->writer,
71 BT_CTF_BYTE_ORDER_BIG_ENDIAN);
72 BT_ASSERT(ret == 0);
73 cfg->sc = bt_ctf_stream_class_create("hello");
74 BT_ASSERT(cfg->sc);
75 ret = bt_ctf_stream_class_set_clock(cfg->sc, cfg->clock);
76 BT_ASSERT(ret == 0);
77 cfg->ec = bt_ctf_event_class_create("ev");
78 BT_ASSERT(cfg->ec);
79 ft = bt_ctf_field_type_integer_create(8);
80 BT_ASSERT(ft);
81 ret = bt_ctf_field_type_integer_set_is_signed(ft, BT_TRUE);
82 BT_ASSERT(ret == 0);
83 ret = bt_ctf_event_class_add_field(cfg->ec, ft, "first");
84 BT_ASSERT(ret == 0);
85 bt_ctf_object_put_ref(ft);
86 ft = bt_ctf_field_type_string_create();
87 BT_ASSERT(ft);
88 ret = bt_ctf_event_class_add_field(cfg->ec, ft, "second");
89 BT_ASSERT(ret == 0);
90 bt_ctf_object_put_ref(ft);
91 ret = bt_ctf_stream_class_add_event_class(cfg->sc, cfg->ec);
92 BT_ASSERT(ret == 0);
93 cfg->stream = bt_ctf_writer_create_stream(cfg->writer, cfg->sc);
94 BT_ASSERT(cfg->stream);
95 }
96
97 static
98 void write_stream(struct config *cfg)
99 {
100 struct bt_ctf_event *ev;
101 struct bt_ctf_field *field;
102 uint64_t i;
103 int ret;
104
105 for (i = 0; i < 25; i++) {
106 ev = bt_ctf_event_create(cfg->ec);
107 BT_ASSERT(ev);
108 field = bt_ctf_event_get_payload(ev, "first");
109 BT_ASSERT(field);
110 ret = bt_ctf_field_integer_signed_set_value(field, -23 + i);
111 BT_ASSERT(ret == 0);
112 bt_ctf_object_put_ref(field);
113 field = bt_ctf_event_get_payload(ev, "second");
114 BT_ASSERT(field);
115 ret = bt_ctf_field_string_set_value(field, "saluuuut");
116 BT_ASSERT(ret == 0);
117 bt_ctf_object_put_ref(field);
118 ret = bt_ctf_clock_set_time(cfg->clock, 3600 + i * 5000);
119 BT_ASSERT(ret == 0);
120 ret = bt_ctf_stream_append_event(cfg->stream, ev);
121 BT_ASSERT(ret == 0);
122 bt_ctf_object_put_ref(ev);
123 }
124
125 ret = bt_ctf_stream_flush(cfg->stream);
126 BT_ASSERT(ret == 0);
127 }
128
129 int main(int argc, char **argv)
130 {
131 struct config cfg = {0};
132
133 BT_ASSERT(argc >= 2);
134 configure_writer(&cfg, argv[1]);
135 write_stream(&cfg);
136 fini_config(&cfg);
137 return 0;
138 }
This page took 0.032062 seconds and 4 git commands to generate.