tests/lib: C++ify `test-fields-bin.cpp`
[babeltrace.git] / tests / lib / test-fields-bin.cpp
CommitLineData
08b4db41
SM
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2023 EfficiOS Inc.
5 */
6
c802cacb
SM
7#include "common/assert.h"
8
08b4db41 9#include "utils/run-in.hpp"
c802cacb 10
08b4db41 11#include "tap/tap.h"
08b4db41 12
2dac3508
SM
13namespace {
14
15constexpr int NR_TESTS = 2;
08b4db41 16
2dac3508 17void testStringClear() noexcept
08b4db41 18{
5d15e4ca 19 runInMsgIterClsInit([](const bt2::SelfMessageIterator self) {
08b4db41 20 /* Boilerplate to get a string field */
2dac3508
SM
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();
08b4db41 25
2dac3508
SM
26 payloadCls->appendMember("str", *traceCls->createStringFieldClass());
27 eventCls->payloadFieldClass(*payloadCls);
08b4db41 28
2dac3508
SM
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();
08b4db41
SM
33
34 /* Set the field to a known non-empty value */
2dac3508
SM
35 *field = "pomme";
36 BT_ASSERT(field.value() == "pomme");
08b4db41
SM
37
38 /* Clear the field, verify its value and length */
2dac3508
SM
39 field.clear();
40 ok(field.value() == "", "string field is empty");
41 ok(field.length() == 0, "string field length is 0");
08b4db41
SM
42 });
43}
44
2dac3508
SM
45} /* namespace */
46
08b4db41
SM
47int main()
48{
49 plan_tests(NR_TESTS);
50
2dac3508 51 testStringClear();
08b4db41
SM
52
53 return exit_status();
54}
This page took 0.033856 seconds and 4 git commands to generate.