From: Simon Marchi Date: Tue, 13 Feb 2024 03:28:30 +0000 (-0500) Subject: tests/lib: C++ify `test-fields-bin.cpp` X-Git-Url: https://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=2dac3508348b0c8c2bc25844eae8b46cf055a8de tests/lib: C++ify `test-fields-bin.cpp` - use the C++ bindings - rename `test_string_clear` to `testStringClear` - use an anonymous namespace - declare testStringClear as `noexcept` - use constexpr I checked that the test still managed to catch the bug fixed in 0022a87819b0 ("Fix: clear_string_field(): set first character to 0"). Change-Id: I3631b22f9e70ea5db620ce0597d04e5035bfa91d Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11793 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/tests/lib/test-fields-bin.cpp b/tests/lib/test-fields-bin.cpp index 7f2c26d4..79f50f31 100644 --- a/tests/lib/test-fields-bin.cpp +++ b/tests/lib/test-fields-bin.cpp @@ -4,72 +4,51 @@ * Copyright (C) 2023 EfficiOS Inc. */ -#include - #include "common/assert.h" #include "utils/run-in.hpp" #include "tap/tap.h" -static const int NR_TESTS = 2; +namespace { + +constexpr int NR_TESTS = 2; -static void test_string_clear() +void testStringClear() noexcept { runInMsgIterClsInit([](const bt2::SelfMessageIterator self) { /* Boilerplate to get a string field */ - const auto traceCls = - bt_trace_class_create(bt_self_message_iterator_borrow_component(self.libObjPtr())); - const auto streamCls = bt_stream_class_create(traceCls); - const auto eventCls = bt_event_class_create(streamCls); - const auto payloadCls = bt_field_class_structure_create(traceCls); - - { - const auto stringFieldCls = bt_field_class_string_create(traceCls); - const auto status = - bt_field_class_structure_append_member(payloadCls, "str", stringFieldCls); - BT_ASSERT(status == BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK); - bt_field_class_put_ref(stringFieldCls); - } + const auto traceCls = self.component().createTraceClass(); + const auto streamCls = traceCls->createStreamClass(); + const auto eventCls = streamCls->createEventClass(); + const auto payloadCls = traceCls->createStructureFieldClass(); - { - const auto status = bt_event_class_set_payload_field_class(eventCls, payloadCls); - BT_ASSERT(status == BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK); - } + payloadCls->appendMember("str", *traceCls->createStringFieldClass()); + eventCls->payloadFieldClass(*payloadCls); - const auto trace = bt_trace_create(traceCls); - const auto stream = bt_stream_create(streamCls, trace); - const auto msg = bt_message_event_create(self.libObjPtr(), eventCls, stream); - const auto field = bt_field_structure_borrow_member_field_by_name( - bt_event_borrow_payload_field(bt_message_event_borrow_event(msg)), "str"); + const auto trace = traceCls->instantiate(); + const auto stream = streamCls->instantiate(*trace); + const auto msg = self.createEventMessage(*eventCls, *stream); + const auto field = (*msg->event().payloadField())["str"]->asString(); /* Set the field to a known non-empty value */ - { - const auto status = bt_field_string_set_value(field, "pomme"); - BT_ASSERT(status == BT_FIELD_STRING_SET_VALUE_STATUS_OK); - BT_ASSERT(std::strcmp(bt_field_string_get_value(field), "pomme") == 0); - } + *field = "pomme"; + BT_ASSERT(field.value() == "pomme"); /* Clear the field, verify its value and length */ - bt_field_string_clear(field); - ok(std::strcmp(bt_field_string_get_value(field), "") == 0, "string field is empty"); - ok(bt_field_string_get_length(field) == 0, "string field length is 0"); - - bt_message_put_ref(msg); - bt_stream_put_ref(stream); - bt_trace_put_ref(trace); - bt_field_class_put_ref(payloadCls); - bt_event_class_put_ref(eventCls); - bt_stream_class_put_ref(streamCls); - bt_trace_class_put_ref(traceCls); + field.clear(); + ok(field.value() == "", "string field is empty"); + ok(field.length() == 0, "string field length is 0"); }); } +} /* namespace */ + int main() { plan_tests(NR_TESTS); - test_string_clear(); + testStringClear(); return exit_status(); }