CTF writer: use own `bt_ctf_object` and `bt_ctf_value` internal APIs
[babeltrace.git] / tests / lib / test_trace_ir_ref.c
CommitLineData
c9b3f44b 1/*
56e18c4c 2 * test_trace_ir_ref.c
c9b3f44b 3 *
56e18c4c 4 * Trace IR Reference Count test
c9b3f44b
JG
5 *
6 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "tap/tap.h"
e5be10ef 23#include <babeltrace/babeltrace.h>
c9b3f44b 24#include <babeltrace/object-internal.h>
3d9990ac 25#include <babeltrace/compat/stdlib-internal.h>
25583cd0 26#include <babeltrace/assert-internal.h>
851299b9 27#include "common.h"
c9b3f44b 28
44c440bc 29#define NR_TESTS 37
8bbe269d 30
c9b3f44b 31struct user {
e5be10ef
PP
32 struct bt_private_trace *tc;
33 struct bt_private_stream_class *sc;
34 struct bt_private_event_class *ec;
35 struct bt_private_stream *stream;
36 struct bt_private_event *event;
c9b3f44b
JG
37};
38
3dca2276
PP
39struct writer_user {
40 struct bt_ctf_writer *writer;
41 struct bt_ctf_trace *tc;
42 struct bt_ctf_stream_class *sc;
43 struct bt_ctf_event_class *ec;
44 struct bt_ctf_stream *stream;
45 struct bt_ctf_event *event;
46};
47
48const char *writer_user_names[] = {
6271743c
PP
49 "writer",
50 "trace",
51 "stream class",
52 "event class",
53 "stream",
54 "event",
55};
56
3dca2276
PP
57static const size_t WRITER_USER_NR_ELEMENTS =
58 sizeof(struct writer_user) / sizeof(void *);
6271743c 59
c9b3f44b
JG
60/**
61 * Returns a structure containing the following fields:
62 * - uint8_t payload_8;
63 * - uint16_t payload_16;
64 * - uint32_t payload_32;
65 */
e5be10ef 66static struct bt_private_field_class *create_integer_struct(void)
c9b3f44b
JG
67{
68 int ret;
e5be10ef
PP
69 struct bt_private_field_class *structure = NULL;
70 struct bt_private_field_class *ui8 = NULL, *ui16 = NULL, *ui32 = NULL;
c9b3f44b 71
e5be10ef 72 structure = bt_private_field_class_structure_create();
44c440bc 73 BT_ASSERT(structure);
e5be10ef 74 ui8 = bt_private_field_class_unsigned_integer_create();
44c440bc 75 BT_ASSERT(ui8);
140e6d94 76 bt_private_field_class_integer_set_field_value_range(ui8, 8);
28e6ca8b 77 ret = bt_private_field_class_structure_append_member(structure,
44c440bc
PP
78 "payload_8", ui8);
79 BT_ASSERT(ret == 0);
e5be10ef 80 ui16 = bt_private_field_class_unsigned_integer_create();
44c440bc 81 BT_ASSERT(ui16);
140e6d94 82 bt_private_field_class_integer_set_field_value_range(ui16, 16);
28e6ca8b 83 ret = bt_private_field_class_structure_append_member(structure,
44c440bc
PP
84 "payload_16", ui16);
85 BT_ASSERT(ret == 0);
e5be10ef 86 ui32 = bt_private_field_class_unsigned_integer_create();
44c440bc 87 BT_ASSERT(ui32);
140e6d94 88 bt_private_field_class_integer_set_field_value_range(ui32, 32);
28e6ca8b 89 ret = bt_private_field_class_structure_append_member(structure,
44c440bc
PP
90 "payload_32", ui32);
91 BT_ASSERT(ret == 0);
65300d60
PP
92 BT_OBJECT_PUT_REF_AND_RESET(ui8);
93 BT_OBJECT_PUT_REF_AND_RESET(ui16);
94 BT_OBJECT_PUT_REF_AND_RESET(ui32);
c9b3f44b 95 return structure;
c9b3f44b
JG
96}
97
3dca2276
PP
98static struct bt_ctf_field_type *create_writer_integer_struct(void)
99{
100 int ret;
101 struct bt_ctf_field_type *structure = NULL;
102 struct bt_ctf_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL;
103
104 structure = bt_ctf_field_type_structure_create();
44c440bc 105 BT_ASSERT(structure);
3dca2276 106 ui8 = bt_ctf_field_type_integer_create(8);
44c440bc 107 BT_ASSERT(ui8);
3dca2276
PP
108 ret = bt_ctf_field_type_structure_add_field(structure, ui8,
109 "payload_8");
44c440bc 110 BT_ASSERT(ret == 0);
3dca2276 111 ui16 = bt_ctf_field_type_integer_create(16);
44c440bc 112 BT_ASSERT(ui16);
3dca2276
PP
113 ret = bt_ctf_field_type_structure_add_field(structure, ui16,
114 "payload_16");
44c440bc 115 BT_ASSERT(ret == 0);
3dca2276 116 ui32 = bt_ctf_field_type_integer_create(32);
44c440bc 117 BT_ASSERT(ui32);
3dca2276
PP
118 ret = bt_ctf_field_type_structure_add_field(structure, ui32,
119 "payload_32");
44c440bc 120 BT_ASSERT(ret == 0);
65300d60
PP
121 BT_OBJECT_PUT_REF_AND_RESET(ui8);
122 BT_OBJECT_PUT_REF_AND_RESET(ui16);
123 BT_OBJECT_PUT_REF_AND_RESET(ui32);
3dca2276 124 return structure;
3dca2276
PP
125}
126
c9b3f44b
JG
127/**
128 * A simple event has the following payload:
129 * - uint8_t payload_8;
130 * - uint16_t payload_16;
131 * - uint32_t payload_32;
132 */
e5be10ef
PP
133static struct bt_private_event_class *create_simple_event(
134 struct bt_private_stream_class *sc, const char *name)
c9b3f44b
JG
135{
136 int ret;
e5be10ef
PP
137 struct bt_private_event_class *event = NULL;
138 struct bt_private_field_class *payload = NULL;
c9b3f44b 139
25583cd0 140 BT_ASSERT(name);
e5be10ef 141 event = bt_private_event_class_create(sc);
44c440bc 142 BT_ASSERT(event);
e5be10ef 143 ret = bt_private_event_class_set_name(event, name);
44c440bc 144 BT_ASSERT(ret == 0);
c9b3f44b 145 payload = create_integer_struct();
44c440bc 146 BT_ASSERT(payload);
28e6ca8b 147 ret = bt_private_event_class_set_payload_field_class(event, payload);
44c440bc 148 BT_ASSERT(ret == 0);
65300d60 149 BT_OBJECT_PUT_REF_AND_RESET(payload);
c9b3f44b 150 return event;
c9b3f44b
JG
151}
152
153/**
154 * A complex event has the following payload:
155 * - uint8_t payload_8;
156 * - uint16_t payload_16;
157 * - uint32_t payload_32;
158 * - struct payload_struct:
159 * - uint8_t payload_8;
160 * - uint16_t payload_16;
161 * - uint32_t payload_32;
162 */
e5be10ef
PP
163static struct bt_private_event_class *create_complex_event(
164 struct bt_private_stream_class *sc,
44c440bc 165 const char *name)
c9b3f44b
JG
166{
167 int ret;
e5be10ef
PP
168 struct bt_private_event_class *event = NULL;
169 struct bt_private_field_class *inner = NULL, *outer = NULL;
c9b3f44b 170
25583cd0 171 BT_ASSERT(name);
e5be10ef 172 event = bt_private_event_class_create(sc);
44c440bc 173 BT_ASSERT(event);
e5be10ef 174 ret = bt_private_event_class_set_name(event, name);
44c440bc 175 BT_ASSERT(ret == 0);
27f4d205 176 outer = create_integer_struct();
44c440bc 177 BT_ASSERT(outer);
27f4d205 178 inner = create_integer_struct();
44c440bc 179 BT_ASSERT(inner);
28e6ca8b 180 ret = bt_private_field_class_structure_append_member(outer,
44c440bc
PP
181 "payload_struct", inner);
182 BT_ASSERT(ret == 0);
28e6ca8b 183 ret = bt_private_event_class_set_payload_field_class(event, outer);
44c440bc 184 BT_ASSERT(ret == 0);
65300d60
PP
185 BT_OBJECT_PUT_REF_AND_RESET(inner);
186 BT_OBJECT_PUT_REF_AND_RESET(outer);
c9b3f44b 187 return event;
c9b3f44b
JG
188}
189
5cd6d0e5 190static void set_stream_class_field_classes(
e5be10ef 191 struct bt_private_stream_class *stream_class)
da1cc671 192{
e5be10ef
PP
193 struct bt_private_field_class *packet_context_type;
194 struct bt_private_field_class *event_header_type;
195 struct bt_private_field_class *fc;
da1cc671
PP
196 int ret;
197
e5be10ef 198 packet_context_type = bt_private_field_class_structure_create();
25583cd0 199 BT_ASSERT(packet_context_type);
e5be10ef 200 fc = bt_private_field_class_unsigned_integer_create();
5cd6d0e5 201 BT_ASSERT(fc);
140e6d94 202 bt_private_field_class_integer_set_field_value_range(fc, 32);
28e6ca8b 203 ret = bt_private_field_class_structure_append_member(packet_context_type,
5cd6d0e5 204 "packet_size", fc);
25583cd0 205 BT_ASSERT(ret == 0);
65300d60 206 bt_object_put_ref(fc);
e5be10ef 207 fc = bt_private_field_class_unsigned_integer_create();
5cd6d0e5 208 BT_ASSERT(fc);
140e6d94 209 bt_private_field_class_integer_set_field_value_range(fc, 32);
28e6ca8b 210 ret = bt_private_field_class_structure_append_member(packet_context_type,
5cd6d0e5 211 "content_size", fc);
25583cd0 212 BT_ASSERT(ret == 0);
65300d60 213 bt_object_put_ref(fc);
e5be10ef 214 event_header_type = bt_private_field_class_structure_create();
25583cd0 215 BT_ASSERT(event_header_type);
e5be10ef 216 fc = bt_private_field_class_unsigned_integer_create();
5cd6d0e5 217 BT_ASSERT(fc);
140e6d94 218 bt_private_field_class_integer_set_field_value_range(fc, 32);
28e6ca8b 219 ret = bt_private_field_class_structure_append_member(event_header_type,
5cd6d0e5 220 "id", fc);
25583cd0 221 BT_ASSERT(ret == 0);
65300d60 222 bt_object_put_ref(fc);
28e6ca8b 223 ret = bt_private_stream_class_set_packet_context_field_class(
e5be10ef 224 stream_class, packet_context_type);
25583cd0 225 BT_ASSERT(ret == 0);
28e6ca8b 226 ret = bt_private_stream_class_set_event_header_field_class(
e5be10ef 227 stream_class, event_header_type);
25583cd0 228 BT_ASSERT(ret == 0);
65300d60
PP
229 bt_object_put_ref(packet_context_type);
230 bt_object_put_ref(event_header_type);
da1cc671
PP
231}
232
e5be10ef 233static void create_sc1(struct bt_private_trace *trace)
c9b3f44b
JG
234{
235 int ret;
e5be10ef
PP
236 struct bt_private_event_class *ec1 = NULL, *ec2 = NULL;
237 struct bt_private_stream_class *sc1 = NULL, *ret_stream = NULL;
c9b3f44b 238
e5be10ef 239 sc1 = bt_private_stream_class_create(trace);
44c440bc 240 BT_ASSERT(sc1);
e5be10ef 241 ret = bt_private_stream_class_set_name(sc1, "sc1");
44c440bc 242 BT_ASSERT(ret == 0);
5cd6d0e5 243 set_stream_class_field_classes(sc1);
44c440bc
PP
244 ec1 = create_complex_event(sc1, "ec1");
245 BT_ASSERT(ec1);
246 ec2 = create_simple_event(sc1, "ec2");
247 BT_ASSERT(ec2);
28e6ca8b 248 ret_stream = bt_private_event_class_borrow_stream_class(ec1);
44c440bc 249 ok(ret_stream == sc1, "Borrow parent stream SC1 from EC1");
28e6ca8b 250 ret_stream = bt_private_event_class_borrow_stream_class(ec2);
44c440bc 251 ok(ret_stream == sc1, "Borrow parent stream SC1 from EC2");
65300d60
PP
252 BT_OBJECT_PUT_REF_AND_RESET(ec1);
253 BT_OBJECT_PUT_REF_AND_RESET(ec2);
254 BT_OBJECT_PUT_REF_AND_RESET(sc1);
c9b3f44b
JG
255}
256
e5be10ef 257static void create_sc2(struct bt_private_trace *trace)
c9b3f44b
JG
258{
259 int ret;
e5be10ef
PP
260 struct bt_private_event_class *ec3 = NULL;
261 struct bt_private_stream_class *sc2 = NULL, *ret_stream = NULL;
c9b3f44b 262
e5be10ef 263 sc2 = bt_private_stream_class_create(trace);
44c440bc 264 BT_ASSERT(sc2);
e5be10ef 265 ret = bt_private_stream_class_set_name(sc2, "sc2");
44c440bc 266 BT_ASSERT(ret == 0);
5cd6d0e5 267 set_stream_class_field_classes(sc2);
44c440bc 268 ec3 = create_simple_event(sc2, "ec3");
28e6ca8b 269 ret_stream = bt_private_event_class_borrow_stream_class(ec3);
44c440bc 270 ok(ret_stream == sc2, "Borrow parent stream SC2 from EC3");
65300d60
PP
271 BT_OBJECT_PUT_REF_AND_RESET(ec3);
272 BT_OBJECT_PUT_REF_AND_RESET(sc2);
c9b3f44b
JG
273}
274
e5be10ef 275static void set_trace_packet_header(struct bt_private_trace *trace)
da1cc671 276{
e5be10ef
PP
277 struct bt_private_field_class *packet_header_type;
278 struct bt_private_field_class *fc;
da1cc671
PP
279 int ret;
280
e5be10ef 281 packet_header_type = bt_private_field_class_structure_create();
25583cd0 282 BT_ASSERT(packet_header_type);
e5be10ef 283 fc = bt_private_field_class_unsigned_integer_create();
5cd6d0e5 284 BT_ASSERT(fc);
140e6d94 285 bt_private_field_class_integer_set_field_value_range(fc, 32);
28e6ca8b 286 ret = bt_private_field_class_structure_append_member(packet_header_type,
5cd6d0e5 287 "stream_id", fc);
25583cd0 288 BT_ASSERT(ret == 0);
65300d60 289 bt_object_put_ref(fc);
28e6ca8b 290 ret = bt_private_trace_set_packet_header_field_class(trace,
da1cc671 291 packet_header_type);
25583cd0 292 BT_ASSERT(ret == 0);
da1cc671 293
65300d60 294 bt_object_put_ref(packet_header_type);
da1cc671
PP
295}
296
e5be10ef 297static struct bt_private_trace *create_tc1(void)
c9b3f44b 298{
e5be10ef 299 struct bt_private_trace *tc1 = NULL;
c9b3f44b 300
e5be10ef 301 tc1 = bt_private_trace_create();
44c440bc 302 BT_ASSERT(tc1);
da1cc671 303 set_trace_packet_header(tc1);
44c440bc
PP
304 create_sc1(tc1);
305 create_sc2(tc1);
c9b3f44b 306 return tc1;
c9b3f44b
JG
307}
308
e5be10ef
PP
309static void init_weak_refs(struct bt_private_trace *tc,
310 struct bt_private_trace **tc1,
311 struct bt_private_stream_class **sc1,
312 struct bt_private_stream_class **sc2,
313 struct bt_private_event_class **ec1,
314 struct bt_private_event_class **ec2,
315 struct bt_private_event_class **ec3)
c9b3f44b
JG
316{
317 *tc1 = tc;
28e6ca8b
PP
318 *sc1 = bt_private_trace_borrow_stream_class_by_index(tc, 0);
319 *sc2 = bt_private_trace_borrow_stream_class_by_index(tc, 1);
320 *ec1 = bt_private_stream_class_borrow_event_class_by_index(*sc1, 0);
321 *ec2 = bt_private_stream_class_borrow_event_class_by_index(*sc1, 1);
322 *ec3 = bt_private_stream_class_borrow_event_class_by_index(*sc2, 0);
c9b3f44b
JG
323}
324
6271743c 325static void test_example_scenario(void)
c9b3f44b 326{
56e18c4c
PP
327 /*
328 * Weak pointers to trace IR objects are to be used very
329 * carefully. This is NOT a good practice and is strongly
330 * discouraged; this is only done to facilitate the validation
331 * of expected reference counts without affecting them by taking
332 * "real" references to the objects.
c9b3f44b 333 */
e5be10ef
PP
334 struct bt_private_trace *tc1 = NULL, *weak_tc1 = NULL;
335 struct bt_private_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL;
336 struct bt_private_event_class *weak_ec1 = NULL, *weak_ec2 = NULL,
c9b3f44b
JG
337 *weak_ec3 = NULL;
338 struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 };
339
340 /* The only reference which exists at this point is on TC1. */
341 tc1 = create_tc1();
27f4d205 342 ok(tc1, "Initialize trace");
44c440bc 343 BT_ASSERT(tc1);
c9b3f44b
JG
344 init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1,
345 &weak_ec2, &weak_ec3);
3fea54f6 346 ok(bt_object_get_ref_count((void *) weak_sc1) == 0,
c9b3f44b 347 "Initial SC1 reference count is 0");
3fea54f6 348 ok(bt_object_get_ref_count((void *) weak_sc2) == 0,
c9b3f44b 349 "Initial SC2 reference count is 0");
3fea54f6 350 ok(bt_object_get_ref_count((void *) weak_ec1) == 0,
c9b3f44b 351 "Initial EC1 reference count is 0");
3fea54f6 352 ok(bt_object_get_ref_count((void *) weak_ec2) == 0,
c9b3f44b 353 "Initial EC2 reference count is 0");
3fea54f6 354 ok(bt_object_get_ref_count((void *) weak_ec3) == 0,
c9b3f44b
JG
355 "Initial EC3 reference count is 0");
356
357 /* User A has ownership of the trace. */
65300d60 358 BT_OBJECT_MOVE_REF(user_a.tc, tc1);
3fea54f6 359 ok(bt_object_get_ref_count((void *) user_a.tc) == 1,
c9b3f44b
JG
360 "TC1 reference count is 1");
361
362 /* User A acquires a reference to SC2 from TC1. */
e5be10ef 363 user_a.sc = bt_object_get_ref(
28e6ca8b 364 bt_private_trace_borrow_stream_class_by_index(
e5be10ef 365 user_a.tc, 1));
c9b3f44b 366 ok(user_a.sc, "User A acquires SC2 from TC1");
3fea54f6 367 ok(bt_object_get_ref_count((void *) weak_tc1) == 2,
c9b3f44b 368 "TC1 reference count is 2");
3fea54f6 369 ok(bt_object_get_ref_count((void *) weak_sc2) == 1,
c9b3f44b
JG
370 "SC2 reference count is 1");
371
372 /* User A acquires a reference to EC3 from SC2. */
65300d60 373 user_a.ec = bt_object_get_ref(
28e6ca8b 374 bt_private_stream_class_borrow_event_class_by_index(
e5be10ef 375 user_a.sc, 0));
c9b3f44b 376 ok(user_a.ec, "User A acquires EC3 from SC2");
3fea54f6 377 ok(bt_object_get_ref_count((void *) weak_tc1) == 2,
c9b3f44b 378 "TC1 reference count is 2");
3fea54f6 379 ok(bt_object_get_ref_count((void *) weak_sc2) == 2,
c9b3f44b 380 "SC2 reference count is 2");
3fea54f6 381 ok(bt_object_get_ref_count((void *) weak_ec3) == 1,
c9b3f44b
JG
382 "EC3 reference count is 1");
383
384 /* User A releases its reference to SC2. */
385 diag("User A releases SC2");
65300d60 386 BT_OBJECT_PUT_REF_AND_RESET(user_a.sc);
c9b3f44b
JG
387 /*
388 * We keep the pointer to SC2 around to validate its reference
389 * count.
390 */
3fea54f6 391 ok(bt_object_get_ref_count((void *) weak_tc1) == 2,
c9b3f44b 392 "TC1 reference count is 2");
3fea54f6 393 ok(bt_object_get_ref_count((void *) weak_sc2) == 1,
c9b3f44b 394 "SC2 reference count is 1");
3fea54f6 395 ok(bt_object_get_ref_count((void *) weak_ec3) == 1,
c9b3f44b
JG
396 "EC3 reference count is 1");
397
398 /* User A releases its reference to TC1. */
399 diag("User A releases TC1");
65300d60 400 BT_OBJECT_PUT_REF_AND_RESET(user_a.tc);
c9b3f44b
JG
401 /*
402 * We keep the pointer to TC1 around to validate its reference
403 * count.
404 */
3fea54f6 405 ok(bt_object_get_ref_count((void *) weak_tc1) == 1,
c9b3f44b 406 "TC1 reference count is 1");
3fea54f6 407 ok(bt_object_get_ref_count((void *) weak_sc2) == 1,
c9b3f44b 408 "SC2 reference count is 1");
3fea54f6 409 ok(bt_object_get_ref_count((void *) weak_ec3) == 1,
c9b3f44b
JG
410 "EC3 reference count is 1");
411
412 /* User B acquires a reference to SC1. */
413 diag("User B acquires a reference to SC1");
65300d60 414 user_b.sc = bt_object_get_ref(weak_sc1);
3fea54f6 415 ok(bt_object_get_ref_count((void *) weak_tc1) == 2,
c9b3f44b 416 "TC1 reference count is 2");
3fea54f6 417 ok(bt_object_get_ref_count((void *) weak_sc1) == 1,
c9b3f44b
JG
418 "SC1 reference count is 1");
419
420 /* User C acquires a reference to EC1. */
421 diag("User C acquires a reference to EC1");
65300d60 422 user_c.ec = bt_object_get_ref(
28e6ca8b 423 bt_private_stream_class_borrow_event_class_by_index(
e5be10ef 424 user_b.sc, 0));
3fea54f6 425 ok(bt_object_get_ref_count((void *) weak_ec1) == 1,
c9b3f44b 426 "EC1 reference count is 1");
3fea54f6 427 ok(bt_object_get_ref_count((void *) weak_sc1) == 2,
c9b3f44b
JG
428 "SC1 reference count is 2");
429
430 /* User A releases its reference on EC3. */
431 diag("User A releases its reference on EC3");
65300d60 432 BT_OBJECT_PUT_REF_AND_RESET(user_a.ec);
3fea54f6 433 ok(bt_object_get_ref_count((void *) weak_ec3) == 0,
c9b3f44b 434 "EC3 reference count is 1");
3fea54f6 435 ok(bt_object_get_ref_count((void *) weak_sc2) == 0,
c9b3f44b 436 "SC2 reference count is 0");
3fea54f6 437 ok(bt_object_get_ref_count((void *) weak_tc1) == 1,
c9b3f44b
JG
438 "TC1 reference count is 1");
439
440 /* User B releases its reference on SC1. */
441 diag("User B releases its reference on SC1");
65300d60 442 BT_OBJECT_PUT_REF_AND_RESET(user_b.sc);
3fea54f6 443 ok(bt_object_get_ref_count((void *) weak_sc1) == 1,
c9b3f44b
JG
444 "SC1 reference count is 1");
445
446 /*
447 * User C is the sole owner of an object and is keeping the whole
448 * trace hierarchy "alive" by holding a reference to EC1.
449 */
3fea54f6 450 ok(bt_object_get_ref_count((void *) weak_tc1) == 1,
c9b3f44b 451 "TC1 reference count is 1");
3fea54f6 452 ok(bt_object_get_ref_count((void *) weak_sc1) == 1,
c9b3f44b 453 "SC1 reference count is 1");
3fea54f6 454 ok(bt_object_get_ref_count((void *) weak_sc2) == 0,
c9b3f44b 455 "SC2 reference count is 0");
3fea54f6 456 ok(bt_object_get_ref_count((void *) weak_ec1) == 1,
c9b3f44b 457 "EC1 reference count is 1");
3fea54f6 458 ok(bt_object_get_ref_count((void *) weak_ec2) == 0,
c9b3f44b 459 "EC2 reference count is 0");
3fea54f6 460 ok(bt_object_get_ref_count((void *) weak_ec3) == 0,
c9b3f44b
JG
461 "EC3 reference count is 0");
462
463 /* Reclaim last reference held by User C. */
65300d60 464 BT_OBJECT_PUT_REF_AND_RESET(user_c.ec);
6271743c
PP
465}
466
3dca2276 467static void create_writer_user_full(struct writer_user *user)
6271743c 468{
32bd47d1 469 gchar *trace_path;
3dca2276
PP
470 struct bt_ctf_field_type *ft;
471 struct bt_ctf_field *field;
6271743c
PP
472 struct bt_ctf_clock *clock;
473 int ret;
474
32bd47d1 475 trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL);
6271743c
PP
476 if (!bt_mkdtemp(trace_path)) {
477 perror("# perror");
478 }
479
480 user->writer = bt_ctf_writer_create(trace_path);
25583cd0 481 BT_ASSERT(user->writer);
dc3fffef 482 ret = bt_ctf_writer_set_byte_order(user->writer,
3dca2276 483 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
25583cd0 484 BT_ASSERT(ret == 0);
6271743c 485 user->tc = bt_ctf_writer_get_trace(user->writer);
25583cd0 486 BT_ASSERT(user->tc);
3dca2276 487 user->sc = bt_ctf_stream_class_create("sc");
25583cd0 488 BT_ASSERT(user->sc);
6271743c 489 clock = bt_ctf_clock_create("the_clock");
25583cd0 490 BT_ASSERT(clock);
81582b6b 491 ret = bt_ctf_writer_add_clock(user->writer, clock);
25583cd0 492 BT_ASSERT(!ret);
3dca2276 493 ret = bt_ctf_stream_class_set_clock(user->sc, clock);
25583cd0 494 BT_ASSERT(!ret);
65300d60 495 BT_OBJECT_PUT_REF_AND_RESET(clock);
6271743c 496 user->stream = bt_ctf_writer_create_stream(user->writer, user->sc);
25583cd0 497 BT_ASSERT(user->stream);
3dca2276 498 user->ec = bt_ctf_event_class_create("ec");
25583cd0 499 BT_ASSERT(user->ec);
3dca2276 500 ft = create_writer_integer_struct();
25583cd0 501 BT_ASSERT(ft);
3dca2276 502 ret = bt_ctf_event_class_set_payload_field_type(user->ec, ft);
65300d60 503 BT_OBJECT_PUT_REF_AND_RESET(ft);
25583cd0 504 BT_ASSERT(!ret);
3dca2276 505 ret = bt_ctf_stream_class_add_event_class(user->sc, user->ec);
25583cd0 506 BT_ASSERT(!ret);
3dca2276 507 user->event = bt_ctf_event_create(user->ec);
25583cd0 508 BT_ASSERT(user->event);
3dca2276 509 field = bt_ctf_event_get_payload(user->event, "payload_8");
25583cd0 510 BT_ASSERT(field);
3dca2276 511 ret = bt_ctf_field_integer_unsigned_set_value(field, 10);
25583cd0 512 BT_ASSERT(!ret);
65300d60 513 BT_OBJECT_PUT_REF_AND_RESET(field);
3dca2276 514 field = bt_ctf_event_get_payload(user->event, "payload_16");
25583cd0 515 BT_ASSERT(field);
3dca2276 516 ret = bt_ctf_field_integer_unsigned_set_value(field, 20);
25583cd0 517 BT_ASSERT(!ret);
65300d60 518 BT_OBJECT_PUT_REF_AND_RESET(field);
3dca2276 519 field = bt_ctf_event_get_payload(user->event, "payload_32");
25583cd0 520 BT_ASSERT(field);
3dca2276 521 ret = bt_ctf_field_integer_unsigned_set_value(field, 30);
25583cd0 522 BT_ASSERT(!ret);
65300d60 523 BT_OBJECT_PUT_REF_AND_RESET(field);
3dca2276 524 ret = bt_ctf_stream_append_event(user->stream, user->event);
25583cd0 525 BT_ASSERT(!ret);
851299b9 526 recursive_rmdir(trace_path);
32bd47d1 527 g_free(trace_path);
6271743c
PP
528}
529
530static void test_put_order_swap(size_t *array, size_t a, size_t b)
531{
532 size_t temp = array[a];
533
534 array[a] = array[b];
535 array[b] = temp;
536}
537
538static void test_put_order_put_objects(size_t *array, size_t size)
539{
540 size_t i;
3dca2276 541 struct writer_user user = { 0 };
6f2097e2 542 void **objects = (void *) &user;
6271743c 543
3dca2276 544 create_writer_user_full(&user);
6271743c
PP
545 printf("# ");
546
547 for (i = 0; i < size; ++i) {
6f2097e2 548 void *obj = objects[array[i]];
6271743c 549
3dca2276 550 printf("%s", writer_user_names[array[i]]);
65300d60 551 BT_OBJECT_PUT_REF_AND_RESET(obj);
6271743c
PP
552
553 if (i < size - 1) {
554 printf(" -> ");
555 }
556 }
557
558 puts("");
559}
560
561static void test_put_order_permute(size_t *array, int k, size_t size)
562{
563 if (k == 0) {
564 test_put_order_put_objects(array, size);
565 } else {
566 int i;
567
568 for (i = k - 1; i >= 0; i--) {
569 size_t next_k = k - 1;
570
571 test_put_order_swap(array, i, next_k);
572 test_put_order_permute(array, next_k, size);
573 test_put_order_swap(array, i, next_k);
574 }
575 }
576}
577
578static void test_put_order(void)
579{
580 size_t i;
3dca2276 581 size_t array[WRITER_USER_NR_ELEMENTS];
6271743c
PP
582
583 /* Initialize array of indexes */
3dca2276 584 for (i = 0; i < WRITER_USER_NR_ELEMENTS; ++i) {
6271743c
PP
585 array[i] = i;
586 }
587
3dca2276
PP
588 test_put_order_permute(array, WRITER_USER_NR_ELEMENTS,
589 WRITER_USER_NR_ELEMENTS);
6271743c
PP
590}
591
592/**
593 * The objective of this test is to implement and expand upon the scenario
594 * described in the reference counting documentation and ensure that any node of
595 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
596 * other "alive" and reachable.
597 *
598 * External tools (e.g. valgrind) should be used to confirm that this
599 * known-good test does not leak memory.
600 */
601int main(int argc, char **argv)
602{
27f4d205 603 /* Initialize tap harness before any tests */
6271743c
PP
604 plan_tests(NR_TESTS);
605
606 test_example_scenario();
607 test_put_order();
608
c9b3f44b
JG
609 return exit_status();
610}
This page took 0.07302 seconds and 4 git commands to generate.