| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-only |
| 3 | * |
| 4 | * Copyright (C) 2023 EfficiOS Inc. |
| 5 | */ |
| 6 | |
| 7 | #include "utils/run-in.hpp" |
| 8 | #include "tap/tap.h" |
| 9 | #include "common/assert.h" |
| 10 | #include <cstring> |
| 11 | |
| 12 | static const int NR_TESTS = 2; |
| 13 | |
| 14 | static void test_string_clear() |
| 15 | { |
| 16 | runInMsgIterClsInit([](bt_self_message_iterator * const self) { |
| 17 | /* Boilerplate to get a string field */ |
| 18 | const auto traceCls = |
| 19 | bt_trace_class_create(bt_self_message_iterator_borrow_component(self)); |
| 20 | const auto streamCls = bt_stream_class_create(traceCls); |
| 21 | const auto eventCls = bt_event_class_create(streamCls); |
| 22 | const auto payloadCls = bt_field_class_structure_create(traceCls); |
| 23 | |
| 24 | { |
| 25 | const auto stringFieldCls = bt_field_class_string_create(traceCls); |
| 26 | const auto status = |
| 27 | bt_field_class_structure_append_member(payloadCls, "str", stringFieldCls); |
| 28 | BT_ASSERT(status == BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK); |
| 29 | bt_field_class_put_ref(stringFieldCls); |
| 30 | } |
| 31 | |
| 32 | { |
| 33 | const auto status = bt_event_class_set_payload_field_class(eventCls, payloadCls); |
| 34 | BT_ASSERT(status == BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK); |
| 35 | } |
| 36 | |
| 37 | const auto trace = bt_trace_create(traceCls); |
| 38 | const auto stream = bt_stream_create(streamCls, trace); |
| 39 | const auto msg = bt_message_event_create(self, eventCls, stream); |
| 40 | const auto field = bt_field_structure_borrow_member_field_by_name( |
| 41 | bt_event_borrow_payload_field(bt_message_event_borrow_event(msg)), "str"); |
| 42 | |
| 43 | /* Set the field to a known non-empty value */ |
| 44 | { |
| 45 | const auto status = bt_field_string_set_value(field, "pomme"); |
| 46 | BT_ASSERT(status == BT_FIELD_STRING_SET_VALUE_STATUS_OK); |
| 47 | BT_ASSERT(std::strcmp(bt_field_string_get_value(field), "pomme") == 0); |
| 48 | } |
| 49 | |
| 50 | /* Clear the field, verify its value and length */ |
| 51 | bt_field_string_clear(field); |
| 52 | ok(std::strcmp(bt_field_string_get_value(field), "") == 0, "string field is empty"); |
| 53 | ok(bt_field_string_get_length(field) == 0, "string field length is 0"); |
| 54 | |
| 55 | bt_message_put_ref(msg); |
| 56 | bt_stream_put_ref(stream); |
| 57 | bt_trace_put_ref(trace); |
| 58 | bt_field_class_put_ref(payloadCls); |
| 59 | bt_event_class_put_ref(eventCls); |
| 60 | bt_stream_class_put_ref(streamCls); |
| 61 | bt_trace_class_put_ref(traceCls); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | plan_tests(NR_TESTS); |
| 68 | |
| 69 | test_string_clear(); |
| 70 | |
| 71 | return exit_status(); |
| 72 | } |