| 1 | /* |
| 2 | * test_ctf_ir_ref.c |
| 3 | * |
| 4 | * CTF IR Reference Count test |
| 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" |
| 23 | #include <babeltrace/ctf-writer/writer.h> |
| 24 | #include <babeltrace/ctf-writer/stream.h> |
| 25 | #include <babeltrace/ctf-writer/clock.h> |
| 26 | #include <babeltrace/ctf-ir/trace.h> |
| 27 | #include <babeltrace/ctf-writer/stream-class.h> |
| 28 | #include <babeltrace/ctf-ir/stream.h> |
| 29 | #include <babeltrace/ctf-ir/fields.h> |
| 30 | #include <babeltrace/ctf-ir/event.h> |
| 31 | #include <babeltrace/ctf-ir/event-class.h> |
| 32 | #include <babeltrace/object-internal.h> |
| 33 | #include <babeltrace/compat/stdlib.h> |
| 34 | #include <assert.h> |
| 35 | |
| 36 | #define NR_TESTS 41 |
| 37 | |
| 38 | struct user { |
| 39 | struct bt_ctf_writer *writer; |
| 40 | struct bt_ctf_trace *tc; |
| 41 | struct bt_ctf_stream_class *sc; |
| 42 | struct bt_ctf_event_class *ec; |
| 43 | struct bt_ctf_stream *stream; |
| 44 | struct bt_ctf_event *event; |
| 45 | }; |
| 46 | |
| 47 | const char *user_names[] = { |
| 48 | "writer", |
| 49 | "trace", |
| 50 | "stream class", |
| 51 | "event class", |
| 52 | "stream", |
| 53 | "event", |
| 54 | }; |
| 55 | |
| 56 | static const size_t USER_NR_ELEMENTS = sizeof(struct user) / sizeof(void *); |
| 57 | |
| 58 | /** |
| 59 | * Returns a structure containing the following fields: |
| 60 | * - uint8_t payload_8; |
| 61 | * - uint16_t payload_16; |
| 62 | * - uint32_t payload_32; |
| 63 | */ |
| 64 | static struct bt_ctf_field_type *create_integer_struct(void) |
| 65 | { |
| 66 | int ret; |
| 67 | struct bt_ctf_field_type *structure = NULL; |
| 68 | struct bt_ctf_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; |
| 69 | |
| 70 | structure = bt_ctf_field_type_structure_create(); |
| 71 | if (!structure) { |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | ui8 = bt_ctf_field_type_integer_create(8); |
| 76 | if (!ui8) { |
| 77 | diag("Failed to create uint8_t type"); |
| 78 | goto error; |
| 79 | } |
| 80 | ret = bt_ctf_field_type_structure_add_field(structure, ui8, |
| 81 | "payload_8"); |
| 82 | if (ret) { |
| 83 | diag("Failed to add uint8_t to structure"); |
| 84 | goto error; |
| 85 | } |
| 86 | ui16 = bt_ctf_field_type_integer_create(16); |
| 87 | if (!ui16) { |
| 88 | diag("Failed to create uint16_t type"); |
| 89 | goto error; |
| 90 | } |
| 91 | ret = bt_ctf_field_type_structure_add_field(structure, ui16, |
| 92 | "payload_16"); |
| 93 | if (ret) { |
| 94 | diag("Failed to add uint16_t to structure"); |
| 95 | goto error; |
| 96 | } |
| 97 | ui32 = bt_ctf_field_type_integer_create(32); |
| 98 | if (!ui32) { |
| 99 | diag("Failed to create uint32_t type"); |
| 100 | goto error; |
| 101 | } |
| 102 | ret = bt_ctf_field_type_structure_add_field(structure, ui32, |
| 103 | "payload_32"); |
| 104 | if (ret) { |
| 105 | diag("Failed to add uint32_t to structure"); |
| 106 | goto error; |
| 107 | } |
| 108 | end: |
| 109 | BT_PUT(ui8); |
| 110 | BT_PUT(ui16); |
| 111 | BT_PUT(ui32); |
| 112 | return structure; |
| 113 | error: |
| 114 | BT_PUT(structure); |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * A simple event has the following payload: |
| 120 | * - uint8_t payload_8; |
| 121 | * - uint16_t payload_16; |
| 122 | * - uint32_t payload_32; |
| 123 | */ |
| 124 | static struct bt_ctf_event_class *create_simple_event(const char *name) |
| 125 | { |
| 126 | int ret; |
| 127 | struct bt_ctf_event_class *event = NULL; |
| 128 | struct bt_ctf_field_type *payload = NULL; |
| 129 | |
| 130 | assert(name); |
| 131 | event = bt_ctf_event_class_create(name); |
| 132 | if (!event) { |
| 133 | diag("Failed to create simple event"); |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | payload = create_integer_struct(); |
| 138 | if (!payload) { |
| 139 | diag("Failed to initialize integer structure"); |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | ret = bt_ctf_event_class_set_payload_type(event, payload); |
| 144 | if (ret) { |
| 145 | diag("Failed to set simple event payload"); |
| 146 | goto error; |
| 147 | } |
| 148 | end: |
| 149 | BT_PUT(payload); |
| 150 | return event; |
| 151 | error: |
| 152 | BT_PUT(event); |
| 153 | goto end;; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * A complex event has the following payload: |
| 158 | * - uint8_t payload_8; |
| 159 | * - uint16_t payload_16; |
| 160 | * - uint32_t payload_32; |
| 161 | * - struct payload_struct: |
| 162 | * - uint8_t payload_8; |
| 163 | * - uint16_t payload_16; |
| 164 | * - uint32_t payload_32; |
| 165 | */ |
| 166 | static struct bt_ctf_event_class *create_complex_event(const char *name) |
| 167 | { |
| 168 | int ret; |
| 169 | struct bt_ctf_event_class *event = NULL; |
| 170 | struct bt_ctf_field_type *inner = NULL, *outer = NULL; |
| 171 | |
| 172 | assert(name); |
| 173 | event = bt_ctf_event_class_create(name); |
| 174 | if (!event) { |
| 175 | diag("Failed to create complex event"); |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | outer = create_integer_struct(); |
| 180 | if (!outer) { |
| 181 | diag("Failed to initialize integer structure"); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | inner = create_integer_struct(); |
| 186 | if (!inner) { |
| 187 | diag("Failed to initialize integer structure"); |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | ret = bt_ctf_field_type_structure_add_field(outer, inner, |
| 192 | "payload_struct"); |
| 193 | if (ret) { |
| 194 | diag("Failed to add inner structure to outer structure"); |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | ret = bt_ctf_event_class_set_payload_type(event, outer); |
| 199 | if (ret) { |
| 200 | diag("Failed to set complex event payload"); |
| 201 | goto error; |
| 202 | } |
| 203 | end: |
| 204 | BT_PUT(inner); |
| 205 | BT_PUT(outer); |
| 206 | return event; |
| 207 | error: |
| 208 | BT_PUT(event); |
| 209 | goto end;; |
| 210 | } |
| 211 | |
| 212 | static struct bt_ctf_stream_class *create_sc1(void) |
| 213 | { |
| 214 | int ret; |
| 215 | struct bt_ctf_event_class *ec1 = NULL, *ec2 = NULL; |
| 216 | struct bt_ctf_stream_class *sc1 = NULL, *ret_stream = NULL; |
| 217 | |
| 218 | sc1 = bt_ctf_stream_class_create("sc1"); |
| 219 | if (!sc1) { |
| 220 | diag("Failed to create Stream Class"); |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | ec1 = create_complex_event("ec1"); |
| 225 | if (!ec1) { |
| 226 | diag("Failed to create complex event EC1"); |
| 227 | goto error; |
| 228 | } |
| 229 | ret = bt_ctf_stream_class_add_event_class(sc1, ec1); |
| 230 | if (ret) { |
| 231 | diag("Failed to add EC1 to SC1"); |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | ec2 = create_simple_event("ec2"); |
| 236 | if (!ec2) { |
| 237 | diag("Failed to create simple event EC2"); |
| 238 | goto error; |
| 239 | } |
| 240 | ret = bt_ctf_stream_class_add_event_class(sc1, ec2); |
| 241 | if (ret) { |
| 242 | diag("Failed to add EC1 to SC1"); |
| 243 | goto error; |
| 244 | } |
| 245 | |
| 246 | ret_stream = bt_ctf_event_class_get_stream_class(ec1); |
| 247 | ok(ret_stream == sc1, "Get parent stream SC1 from EC1"); |
| 248 | BT_PUT(ret_stream); |
| 249 | |
| 250 | ret_stream = bt_ctf_event_class_get_stream_class(ec2); |
| 251 | ok(ret_stream == sc1, "Get parent stream SC1 from EC2"); |
| 252 | end: |
| 253 | BT_PUT(ret_stream); |
| 254 | BT_PUT(ec1); |
| 255 | BT_PUT(ec2); |
| 256 | return sc1; |
| 257 | error: |
| 258 | BT_PUT(sc1); |
| 259 | goto end; |
| 260 | } |
| 261 | |
| 262 | static struct bt_ctf_stream_class *create_sc2(void) |
| 263 | { |
| 264 | int ret; |
| 265 | struct bt_ctf_event_class *ec3 = NULL; |
| 266 | struct bt_ctf_stream_class *sc2 = NULL, *ret_stream = NULL; |
| 267 | |
| 268 | sc2 = bt_ctf_stream_class_create("sc2"); |
| 269 | if (!sc2) { |
| 270 | diag("Failed to create Stream Class"); |
| 271 | goto error; |
| 272 | } |
| 273 | |
| 274 | ec3 = create_simple_event("ec3"); |
| 275 | if (!ec3) { |
| 276 | diag("Failed to create simple event EC3"); |
| 277 | goto error; |
| 278 | } |
| 279 | ret = bt_ctf_stream_class_add_event_class(sc2, ec3); |
| 280 | if (ret) { |
| 281 | diag("Failed to add EC3 to SC2"); |
| 282 | goto error; |
| 283 | } |
| 284 | |
| 285 | ret_stream = bt_ctf_event_class_get_stream_class(ec3); |
| 286 | ok(ret_stream == sc2, "Get parent stream SC2 from EC3"); |
| 287 | end: |
| 288 | BT_PUT(ret_stream); |
| 289 | BT_PUT(ec3); |
| 290 | return sc2; |
| 291 | error: |
| 292 | BT_PUT(sc2); |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | static struct bt_ctf_trace *create_tc1(void) |
| 297 | { |
| 298 | int ret; |
| 299 | struct bt_ctf_trace *tc1 = NULL; |
| 300 | struct bt_ctf_stream_class *sc1 = NULL, *sc2 = NULL; |
| 301 | |
| 302 | tc1 = bt_ctf_trace_create(); |
| 303 | if (!tc1) { |
| 304 | diag("bt_ctf_trace_create returned NULL"); |
| 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | sc1 = create_sc1(); |
| 309 | ok(sc1, "Create SC1"); |
| 310 | if (!sc1) { |
| 311 | goto error; |
| 312 | } |
| 313 | ret = bt_ctf_trace_add_stream_class(tc1, sc1); |
| 314 | ok(!ret, "Add SC1 to TC1"); |
| 315 | if (ret) { |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | sc2 = create_sc2(); |
| 320 | ok(sc2, "Create SC2"); |
| 321 | if (!sc2) { |
| 322 | goto error; |
| 323 | } |
| 324 | ret = bt_ctf_trace_add_stream_class(tc1, sc2); |
| 325 | ok(!ret, "Add SC2 to TC1"); |
| 326 | if (ret) { |
| 327 | goto error; |
| 328 | } |
| 329 | end: |
| 330 | BT_PUT(sc1); |
| 331 | BT_PUT(sc2); |
| 332 | return tc1; |
| 333 | error: |
| 334 | BT_PUT(tc1); |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | static void init_weak_refs(struct bt_ctf_trace *tc, |
| 339 | struct bt_ctf_trace **tc1, |
| 340 | struct bt_ctf_stream_class **sc1, |
| 341 | struct bt_ctf_stream_class **sc2, |
| 342 | struct bt_ctf_event_class **ec1, |
| 343 | struct bt_ctf_event_class **ec2, |
| 344 | struct bt_ctf_event_class **ec3) |
| 345 | { |
| 346 | *tc1 = tc; |
| 347 | *sc1 = bt_ctf_trace_get_stream_class(tc, 0); |
| 348 | *sc2 = bt_ctf_trace_get_stream_class(tc, 1); |
| 349 | *ec1 = bt_ctf_stream_class_get_event_class(*sc1, 0); |
| 350 | *ec2 = bt_ctf_stream_class_get_event_class(*sc1, 1); |
| 351 | *ec3 = bt_ctf_stream_class_get_event_class(*sc2, 0); |
| 352 | bt_put(*sc1); |
| 353 | bt_put(*sc2); |
| 354 | bt_put(*ec1); |
| 355 | bt_put(*ec2); |
| 356 | bt_put(*ec3); |
| 357 | } |
| 358 | |
| 359 | static void test_example_scenario(void) |
| 360 | { |
| 361 | /** |
| 362 | * Weak pointers to CTF-IR objects are to be used very carefully. |
| 363 | * This is NOT a good practice and is strongly discouraged; this |
| 364 | * is only done to facilitate the validation of expected reference |
| 365 | * counts without affecting them by taking "real" references to the |
| 366 | * objects. |
| 367 | */ |
| 368 | struct bt_ctf_trace *tc1 = NULL, *weak_tc1 = NULL; |
| 369 | struct bt_ctf_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL; |
| 370 | struct bt_ctf_event_class *weak_ec1 = NULL, *weak_ec2 = NULL, |
| 371 | *weak_ec3 = NULL; |
| 372 | struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 }; |
| 373 | |
| 374 | /* The only reference which exists at this point is on TC1. */ |
| 375 | tc1 = create_tc1(); |
| 376 | ok(tc1, "Initialize trace"); |
| 377 | if (!tc1) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1, |
| 382 | &weak_ec2, &weak_ec3); |
| 383 | |
| 384 | ok(bt_object_get_ref_count(weak_sc1) == 0, |
| 385 | "Initial SC1 reference count is 0"); |
| 386 | ok(bt_object_get_ref_count(weak_sc2) == 0, |
| 387 | "Initial SC2 reference count is 0"); |
| 388 | ok(bt_object_get_ref_count(weak_ec1) == 0, |
| 389 | "Initial EC1 reference count is 0"); |
| 390 | ok(bt_object_get_ref_count(weak_ec2) == 0, |
| 391 | "Initial EC2 reference count is 0"); |
| 392 | ok(bt_object_get_ref_count(weak_ec3) == 0, |
| 393 | "Initial EC3 reference count is 0"); |
| 394 | |
| 395 | /* User A has ownership of the trace. */ |
| 396 | BT_MOVE(user_a.tc, tc1); |
| 397 | ok(bt_object_get_ref_count(user_a.tc) == 1, |
| 398 | "TC1 reference count is 1"); |
| 399 | |
| 400 | /* User A acquires a reference to SC2 from TC1. */ |
| 401 | user_a.sc = bt_ctf_trace_get_stream_class(user_a.tc, 1); |
| 402 | ok(user_a.sc, "User A acquires SC2 from TC1"); |
| 403 | ok(bt_object_get_ref_count(weak_tc1) == 2, |
| 404 | "TC1 reference count is 2"); |
| 405 | ok(bt_object_get_ref_count(weak_sc2) == 1, |
| 406 | "SC2 reference count is 1"); |
| 407 | |
| 408 | /* User A acquires a reference to EC3 from SC2. */ |
| 409 | user_a.ec = bt_ctf_stream_class_get_event_class(user_a.sc, 0); |
| 410 | ok(user_a.ec, "User A acquires EC3 from SC2"); |
| 411 | ok(bt_object_get_ref_count(weak_tc1) == 2, |
| 412 | "TC1 reference count is 2"); |
| 413 | ok(bt_object_get_ref_count(weak_sc2) == 2, |
| 414 | "SC2 reference count is 2"); |
| 415 | ok(bt_object_get_ref_count(weak_ec3) == 1, |
| 416 | "EC3 reference count is 1"); |
| 417 | |
| 418 | /* User A releases its reference to SC2. */ |
| 419 | diag("User A releases SC2"); |
| 420 | BT_PUT(user_a.sc); |
| 421 | /* |
| 422 | * We keep the pointer to SC2 around to validate its reference |
| 423 | * count. |
| 424 | */ |
| 425 | ok(bt_object_get_ref_count(weak_tc1) == 2, |
| 426 | "TC1 reference count is 2"); |
| 427 | ok(bt_object_get_ref_count(weak_sc2) == 1, |
| 428 | "SC2 reference count is 1"); |
| 429 | ok(bt_object_get_ref_count(weak_ec3) == 1, |
| 430 | "EC3 reference count is 1"); |
| 431 | |
| 432 | /* User A releases its reference to TC1. */ |
| 433 | diag("User A releases TC1"); |
| 434 | BT_PUT(user_a.tc); |
| 435 | /* |
| 436 | * We keep the pointer to TC1 around to validate its reference |
| 437 | * count. |
| 438 | */ |
| 439 | ok(bt_object_get_ref_count(weak_tc1) == 1, |
| 440 | "TC1 reference count is 1"); |
| 441 | ok(bt_object_get_ref_count(weak_sc2) == 1, |
| 442 | "SC2 reference count is 1"); |
| 443 | ok(bt_object_get_ref_count(weak_ec3) == 1, |
| 444 | "EC3 reference count is 1"); |
| 445 | |
| 446 | /* User B acquires a reference to SC1. */ |
| 447 | diag("User B acquires a reference to SC1"); |
| 448 | user_b.sc = bt_get(weak_sc1); |
| 449 | ok(bt_object_get_ref_count(weak_tc1) == 2, |
| 450 | "TC1 reference count is 2"); |
| 451 | ok(bt_object_get_ref_count(weak_sc1) == 1, |
| 452 | "SC1 reference count is 1"); |
| 453 | |
| 454 | /* User C acquires a reference to EC1. */ |
| 455 | diag("User C acquires a reference to EC1"); |
| 456 | user_c.ec = bt_ctf_stream_class_get_event_class(user_b.sc, 0); |
| 457 | ok(bt_object_get_ref_count(weak_ec1) == 1, |
| 458 | "EC1 reference count is 1"); |
| 459 | ok(bt_object_get_ref_count(weak_sc1) == 2, |
| 460 | "SC1 reference count is 2"); |
| 461 | |
| 462 | /* User A releases its reference on EC3. */ |
| 463 | diag("User A releases its reference on EC3"); |
| 464 | BT_PUT(user_a.ec); |
| 465 | ok(bt_object_get_ref_count(weak_ec3) == 0, |
| 466 | "EC3 reference count is 1"); |
| 467 | ok(bt_object_get_ref_count(weak_sc2) == 0, |
| 468 | "SC2 reference count is 0"); |
| 469 | ok(bt_object_get_ref_count(weak_tc1) == 1, |
| 470 | "TC1 reference count is 1"); |
| 471 | |
| 472 | /* User B releases its reference on SC1. */ |
| 473 | diag("User B releases its reference on SC1"); |
| 474 | BT_PUT(user_b.sc); |
| 475 | ok(bt_object_get_ref_count(weak_sc1) == 1, |
| 476 | "SC1 reference count is 1"); |
| 477 | |
| 478 | /* |
| 479 | * User C is the sole owner of an object and is keeping the whole |
| 480 | * trace hierarchy "alive" by holding a reference to EC1. |
| 481 | */ |
| 482 | ok(bt_object_get_ref_count(weak_tc1) == 1, |
| 483 | "TC1 reference count is 1"); |
| 484 | ok(bt_object_get_ref_count(weak_sc1) == 1, |
| 485 | "SC1 reference count is 1"); |
| 486 | ok(bt_object_get_ref_count(weak_sc2) == 0, |
| 487 | "SC2 reference count is 0"); |
| 488 | ok(bt_object_get_ref_count(weak_ec1) == 1, |
| 489 | "EC1 reference count is 1"); |
| 490 | ok(bt_object_get_ref_count(weak_ec2) == 0, |
| 491 | "EC2 reference count is 0"); |
| 492 | ok(bt_object_get_ref_count(weak_ec3) == 0, |
| 493 | "EC3 reference count is 0"); |
| 494 | |
| 495 | /* Reclaim last reference held by User C. */ |
| 496 | BT_PUT(user_c.ec); |
| 497 | } |
| 498 | |
| 499 | static void create_user_full(struct user *user) |
| 500 | { |
| 501 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; |
| 502 | struct bt_ctf_field_type *ft; |
| 503 | struct bt_ctf_field *field; |
| 504 | struct bt_ctf_clock *clock; |
| 505 | int ret; |
| 506 | |
| 507 | if (!bt_mkdtemp(trace_path)) { |
| 508 | perror("# perror"); |
| 509 | } |
| 510 | |
| 511 | user->writer = bt_ctf_writer_create(trace_path); |
| 512 | assert(user->writer); |
| 513 | user->tc = bt_ctf_writer_get_trace(user->writer); |
| 514 | assert(user->tc); |
| 515 | user->sc = bt_ctf_stream_class_create("sc"); |
| 516 | assert(user->sc); |
| 517 | clock = bt_ctf_clock_create("the_clock"); |
| 518 | assert(clock); |
| 519 | ret = bt_ctf_stream_class_set_clock(user->sc, clock); |
| 520 | assert(!ret); |
| 521 | ret = bt_ctf_clock_set_value(clock, 23); |
| 522 | assert(!ret); |
| 523 | BT_PUT(clock); |
| 524 | user->stream = bt_ctf_writer_create_stream(user->writer, user->sc); |
| 525 | assert(user->stream); |
| 526 | user->ec = bt_ctf_event_class_create("ec"); |
| 527 | assert(user->ec); |
| 528 | ft = create_integer_struct(); |
| 529 | assert(ft); |
| 530 | ret = bt_ctf_event_class_set_payload_type(user->ec, ft); |
| 531 | BT_PUT(ft); |
| 532 | assert(!ret); |
| 533 | ret = bt_ctf_stream_class_add_event_class(user->sc, user->ec); |
| 534 | assert(!ret); |
| 535 | user->event = bt_ctf_event_create(user->ec); |
| 536 | assert(user->event); |
| 537 | field = bt_ctf_event_get_payload(user->event, "payload_8"); |
| 538 | assert(field); |
| 539 | ret = bt_ctf_field_unsigned_integer_set_value(field, 10); |
| 540 | assert(!ret); |
| 541 | BT_PUT(field); |
| 542 | field = bt_ctf_event_get_payload(user->event, "payload_16"); |
| 543 | assert(field); |
| 544 | ret = bt_ctf_field_unsigned_integer_set_value(field, 20); |
| 545 | assert(!ret); |
| 546 | BT_PUT(field); |
| 547 | field = bt_ctf_event_get_payload(user->event, "payload_32"); |
| 548 | assert(field); |
| 549 | ret = bt_ctf_field_unsigned_integer_set_value(field, 30); |
| 550 | assert(!ret); |
| 551 | BT_PUT(field); |
| 552 | ret = bt_ctf_stream_append_event(user->stream, user->event); |
| 553 | assert(!ret); |
| 554 | } |
| 555 | |
| 556 | static void test_put_order_swap(size_t *array, size_t a, size_t b) |
| 557 | { |
| 558 | size_t temp = array[a]; |
| 559 | |
| 560 | array[a] = array[b]; |
| 561 | array[b] = temp; |
| 562 | } |
| 563 | |
| 564 | static void test_put_order_put_objects(size_t *array, size_t size) |
| 565 | { |
| 566 | size_t i; |
| 567 | struct user user = { 0 }; |
| 568 | void** objects = (void *) &user; |
| 569 | |
| 570 | create_user_full(&user); |
| 571 | printf("# "); |
| 572 | |
| 573 | for (i = 0; i < size; ++i) { |
| 574 | void* obj = objects[array[i]]; |
| 575 | |
| 576 | printf("%s", user_names[array[i]]); |
| 577 | BT_PUT(obj); |
| 578 | |
| 579 | if (i < size - 1) { |
| 580 | printf(" -> "); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | puts(""); |
| 585 | } |
| 586 | |
| 587 | static void test_put_order_permute(size_t *array, int k, size_t size) |
| 588 | { |
| 589 | if (k == 0) { |
| 590 | test_put_order_put_objects(array, size); |
| 591 | } else { |
| 592 | int i; |
| 593 | |
| 594 | for (i = k - 1; i >= 0; i--) { |
| 595 | size_t next_k = k - 1; |
| 596 | |
| 597 | test_put_order_swap(array, i, next_k); |
| 598 | test_put_order_permute(array, next_k, size); |
| 599 | test_put_order_swap(array, i, next_k); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void test_put_order(void) |
| 605 | { |
| 606 | size_t i; |
| 607 | size_t array[USER_NR_ELEMENTS]; |
| 608 | |
| 609 | /* Initialize array of indexes */ |
| 610 | for (i = 0; i < USER_NR_ELEMENTS; ++i) { |
| 611 | array[i] = i; |
| 612 | } |
| 613 | |
| 614 | test_put_order_permute(array, USER_NR_ELEMENTS, USER_NR_ELEMENTS); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * The objective of this test is to implement and expand upon the scenario |
| 619 | * described in the reference counting documentation and ensure that any node of |
| 620 | * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all |
| 621 | * other "alive" and reachable. |
| 622 | * |
| 623 | * External tools (e.g. valgrind) should be used to confirm that this |
| 624 | * known-good test does not leak memory. |
| 625 | */ |
| 626 | int main(int argc, char **argv) |
| 627 | { |
| 628 | /* Initialize tap harness before any tests */ |
| 629 | plan_tests(NR_TESTS); |
| 630 | |
| 631 | test_example_scenario(); |
| 632 | test_put_order(); |
| 633 | |
| 634 | return exit_status(); |
| 635 | } |