Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Frame.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2023 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Frame.hpp"
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std;
22using namespace toolbox;
23
25
27{
28 {
29 constexpr uint16_t Length{0x0102};
30 char buf[2]{};
31 detail::put_length<endian::little>(buf, Length, endian::little);
32 BOOST_CHECK_EQUAL(detail::get_length<endian::little>(buf, endian::little), Length);
33 }
34 {
35 constexpr uint16_t Length{0x0102};
36 char buf[2]{};
37 detail::put_length<endian::little>(buf, Length, endian::big);
38 BOOST_CHECK_EQUAL(detail::get_length<endian::little>(buf, endian::big), Length);
39 }
40 {
41 constexpr uint16_t Length{0x0201};
42 char buf[2]{};
43 detail::put_length<endian::big>(buf, Length, endian::big);
44 BOOST_CHECK_EQUAL(detail::get_length<endian::big>(buf, endian::big), Length);
45 }
46 {
47 constexpr uint16_t Length{0x0201};
48 char buf[2]{};
49 detail::put_length<endian::big>(buf, Length, endian::little);
50 BOOST_CHECK_EQUAL(detail::get_length<endian::big>(buf, endian::little), Length);
51 }
52}
53
55{
56 int msg_count{0};
57 string msg_data;
58 size_t consumed{0};
59 auto fn = [&](auto msg) {
60 ++msg_count;
61 msg_data.append(buffer_cast<const char*>(msg), buffer_size(msg));
62 };
63
64 consumed += parse_frame("\003"sv, fn, endian::little);
65 BOOST_CHECK_EQUAL(consumed, 0);
67 BOOST_CHECK(msg_data.empty());
68
69 consumed += parse_frame("\000\003"
70 "F"sv,
71 fn, endian::big);
72 BOOST_CHECK_EQUAL(consumed, 3);
75
76 consumed += parse_frame("\004\000"
77 "GH"sv,
78 fn, endian::little);
79 BOOST_CHECK_EQUAL(consumed, 3 + 4);
82 "F"
83 "GH");
84
85 // octal(\010) = decimal(8)
86 consumed += parse_frame("\000\010"
87 "FooBar"
88 "\000\005"
89 "Baz"sv,
90 fn, endian::big);
91 BOOST_CHECK_EQUAL(consumed, 3 + 4 + 8 + 5);
92 BOOST_CHECK_EQUAL(msg_count, 1 + 1 + 2);
94 "F"
95 "GH"
96 "FooBar"
97 "Baz");
98}
99
BOOST_AUTO_TEST_CASE(PutLengthCase)
Definition Frame.ut.cpp:26
BOOST_CHECK_EQUAL(v.size(), 10U)
STL namespace.
std::size_t parse_frame(ConstBuffer buf, FnT fn, std::endian net_byte_order)
Definition Frame.hpp:113
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))