2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2023 EfficiOS Inc.
7 #include "utils/run-in.hpp"
9 #include "common/assert.h"
12 static const int NR_TESTS
= 2;
14 static void test_string_clear()
16 runInMsgIterClsInit([](bt_self_message_iterator
* const self
) {
17 /* Boilerplate to get a string field */
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
);
25 const auto stringFieldCls
= bt_field_class_string_create(traceCls
);
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
);
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
);
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");
43 /* Set the field to a known non-empty value */
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);
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");
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
);