1+ //
2+ // Copyright 2025 - 2025 (C). Alex Robenko. All rights reserved.
3+ //
4+ // This Source Code Form is subject to the terms of the Mozilla Public
5+ // License, v. 2.0. If a copy of the MPL was not distributed with this
6+ // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+ #include "CommsTestCommon.h"
9+
10+ #include "comms/CompileControl.h"
11+ #include "comms/GenericMessage.h"
12+ #include "comms/frame/MsgIdLayer.h"
13+ #include "comms/frame/MsgDataLayer.h"
14+
15+ CC_DISABLE_WARNINGS()
16+ #include "cxxtest/TestSuite.h"
17+ CC_ENABLE_WARNINGS()
18+
19+ class GenericMessageTestSuite : public CxxTest::TestSuite
20+ {
21+ public:
22+ void test1();
23+
24+ private:
25+ using BeFieldBase = comms::Field<comms::option::BigEndian>;
26+ using BeIdField1 = comms::field::EnumValue<BeFieldBase, MessageType, comms::option::def::FixedLength<1U>>;
27+
28+ template <typename... TOpts>
29+ using BeMsgBase =
30+ comms::Message<
31+ comms::option::BigEndian,
32+ TOpts...
33+ >;
34+
35+ using BePolymorphicMsgBase =
36+ BeMsgBase<
37+ comms::option::MsgIdType<MessageType>,
38+ comms::option::IdInfoInterface,
39+ comms::option::ReadIterator<const char*>,
40+ comms::option::WriteIterator<char*>,
41+ comms::option::ValidCheckInterface,
42+ comms::option::LengthInfoInterface
43+ >;
44+
45+
46+ template <typename TIdField, typename TMessage, template<class> class TAllMessages = AllTestMessages>
47+ class SimpleFrame : public
48+ comms::frame::MsgIdLayer<
49+ TIdField,
50+ TMessage,
51+ TAllMessages<TMessage>,
52+ comms::frame::MsgDataLayer<>,
53+ comms::option::SupportGenericMessage<comms::GenericMessage<TMessage> >
54+ >
55+ {
56+ using Base =
57+ comms::frame::MsgIdLayer<
58+ TIdField,
59+ TMessage,
60+ TAllMessages<TMessage>,
61+ comms::frame::MsgDataLayer<>,
62+ comms::option::SupportGenericMessage<comms::GenericMessage<TMessage> >
63+ >;
64+ public:
65+ COMMS_FRAME_LAYERS_NAMES_OUTER(id, payload);
66+ };
67+ };
68+
69+ void GenericMessageTestSuite::test1()
70+ {
71+ using Msg = comms::GenericMessage<BePolymorphicMsgBase>;
72+ using TestFrame = SimpleFrame<BeIdField1, BePolymorphicMsgBase>;
73+
74+ TestFrame frame;
75+ Msg msg(MessageType5);
76+
77+ static const char Buf[] = {
78+ MessageType5, 0x01, 0x02, 0x03,
79+ };
80+
81+ static const std::size_t BufSize = std::extent<decltype(Buf)>::value;
82+
83+ auto iter = comms::readIteratorFor(msg, Buf);
84+ auto es = frame.read(msg, iter, BufSize);
85+ TS_ASSERT_EQUALS(es, comms::ErrorStatus::Success);
86+
87+ std::vector<char> outBuf;
88+ outBuf.reserve(BufSize);
89+ auto outIter = std::back_inserter(outBuf);
90+ es = frame.write(msg, outIter, outBuf.max_size());
91+ TS_ASSERT_EQUALS(es, comms::ErrorStatus::Success);
92+ TS_ASSERT_EQUALS(outBuf.size(), BufSize);
93+ TS_ASSERT(std::equal(outBuf.begin(), outBuf.end(), std::begin(Buf)));
94+ }
0 commit comments