tests/lib: C++ify `test-fields-bin.cpp`
[babeltrace.git] / tests / lib / test-fields-bin.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2023 EfficiOS Inc.
5 */
6
7 #include "common/assert.h"
8
9 #include "utils/run-in.hpp"
10
11 #include "tap/tap.h"
12
13 namespace {
14
15 constexpr int NR_TESTS = 2;
16
17 void testStringClear() noexcept
18 {
19 runInMsgIterClsInit([](const bt2::SelfMessageIterator self) {
20 /* Boilerplate to get a string field */
21 const auto traceCls = self.component().createTraceClass();
22 const auto streamCls = traceCls->createStreamClass();
23 const auto eventCls = streamCls->createEventClass();
24 const auto payloadCls = traceCls->createStructureFieldClass();
25
26 payloadCls->appendMember("str", *traceCls->createStringFieldClass());
27 eventCls->payloadFieldClass(*payloadCls);
28
29 const auto trace = traceCls->instantiate();
30 const auto stream = streamCls->instantiate(*trace);
31 const auto msg = self.createEventMessage(*eventCls, *stream);
32 const auto field = (*msg->event().payloadField())["str"]->asString();
33
34 /* Set the field to a known non-empty value */
35 *field = "pomme";
36 BT_ASSERT(field.value() == "pomme");
37
38 /* Clear the field, verify its value and length */
39 field.clear();
40 ok(field.value() == "", "string field is empty");
41 ok(field.length() == 0, "string field length is 0");
42 });
43 }
44
45 } /* namespace */
46
47 int main()
48 {
49 plan_tests(NR_TESTS);
50
51 testStringClear();
52
53 return exit_status();
54 }
This page took 0.029818 seconds and 4 git commands to generate.