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