Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.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) 2021 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 "Utility.hpp"
18#include <cmath>
19
20#include <boost/test/unit_test.hpp>
21
22using namespace std;
23using namespace toolbox;
24
25namespace utf = boost::unit_test;
26
28
30{
31 BOOST_CHECK_EQUAL(stob(""sv, false), false);
32 BOOST_CHECK_EQUAL(stob(""sv, true), true);
33
34 BOOST_CHECK_EQUAL(stob("0"sv, true), false);
35 BOOST_CHECK_EQUAL(stob("F"sv, true), false);
36 BOOST_CHECK_EQUAL(stob("N"sv, true), false);
37 BOOST_CHECK_EQUAL(stob("f"sv, true), false);
38 BOOST_CHECK_EQUAL(stob("n"sv, true), false);
39
40 BOOST_CHECK_EQUAL(stob("1"sv, false), true);
41 BOOST_CHECK_EQUAL(stob("T"sv, false), true);
42 BOOST_CHECK_EQUAL(stob("Y"sv, false), true);
43 BOOST_CHECK_EQUAL(stob("t"sv, false), true);
44 BOOST_CHECK_EQUAL(stob("y"sv, false), true);
45
46 BOOST_CHECK_EQUAL(stob("NO"sv, true), false);
47 BOOST_CHECK_EQUAL(stob("No"sv, true), false);
48 BOOST_CHECK_EQUAL(stob("no"sv, true), false);
49
50 BOOST_CHECK_EQUAL(stob("ON"sv, false), true);
51 BOOST_CHECK_EQUAL(stob("On"sv, false), true);
52 BOOST_CHECK_EQUAL(stob("on"sv, false), true);
53
54 BOOST_CHECK_EQUAL(stob("OFF"sv, true), false);
55 BOOST_CHECK_EQUAL(stob("Off"sv, true), false);
56 BOOST_CHECK_EQUAL(stob("off"sv, true), false);
57
58 BOOST_CHECK_EQUAL(stob("YES"sv, false), true);
59 BOOST_CHECK_EQUAL(stob("Yes"sv, false), true);
60 BOOST_CHECK_EQUAL(stob("yes"sv, false), true);
61
62 BOOST_CHECK_EQUAL(stob("TRUE"sv, false), true);
63 BOOST_CHECK_EQUAL(stob("True"sv, false), true);
64 BOOST_CHECK_EQUAL(stob("true"sv, false), true);
65
66 BOOST_CHECK_EQUAL(stob("FALSE"sv, true), false);
67 BOOST_CHECK_EQUAL(stob("False"sv, true), false);
68 BOOST_CHECK_EQUAL(stob("false"sv, true), false);
69}
70
71BOOST_AUTO_TEST_CASE(StodCase, *utf::tolerance(0.0000001))
72{
76 BOOST_CHECK_EQUAL(stod("1.23E2"sv), 123);
77 BOOST_CHECK_EQUAL(stod("-1.23E2"sv), -123);
78 BOOST_CHECK_EQUAL(stod("1.23E1"sv), 12.3);
79 BOOST_CHECK_EQUAL(stod("-1.23E1"sv), -12.3);
80 BOOST_CHECK_EQUAL(stod("1.23"sv), 1.23);
81 BOOST_CHECK_EQUAL(stod("-1.23"sv), -1.23);
82
83 // with default value.
84 BOOST_CHECK(isnan(stod(""sv, numeric_limits<double>::quiet_NaN())));
85 BOOST_CHECK(isnan(stod("abc"sv, numeric_limits<double>::quiet_NaN())));
86 BOOST_CHECK_EQUAL(stod("1"sv, numeric_limits<double>::quiet_NaN()), 1);
87}
88
89BOOST_AUTO_TEST_CASE(StonDoubleCase, *utf::tolerance(0.0000001))
90{
100}
101
103{
104 constexpr auto s = "12345"sv;
105 BOOST_CHECK_EQUAL(ston<double>(s.substr(0, 3)), 123);
106 BOOST_CHECK_EQUAL(ston<int>(s.substr(0, 3)), 123);
107}
108
121
134
144
146{
154 BOOST_CHECK_EQUAL(ston<int64_t>("9223372036854775807"sv), 0x7fffffffffffffffLL);
155 BOOST_CHECK_EQUAL(ston<int64_t>("-9223372036854775808"sv), -0x8000000000000000LL);
156}
157
169
181
193
195{
199 BOOST_CHECK_EQUAL(hex_digits(0x10U), 2);
200 BOOST_CHECK_EQUAL(hex_digits(0xffU), 2);
201
202 BOOST_CHECK_EQUAL(hex_digits(0xcdefU), 4);
203 BOOST_CHECK_EQUAL(hex_digits(0x10000U), 5);
204 BOOST_CHECK_EQUAL(hex_digits(0x89abcdefU), 8);
205 BOOST_CHECK_EQUAL(hex_digits(0x100000000U), 9);
206 BOOST_CHECK_EQUAL(hex_digits(0x567890abcdefU), 12);
207 BOOST_CHECK_EQUAL(hex_digits(0x1000000000000U), 13);
208 BOOST_CHECK_EQUAL(hex_digits(0x1234567890abcdefU), 16);
209}
210
212{
218 BOOST_CHECK_EQUAL(dec_digits(10000), 5);
219 BOOST_CHECK_EQUAL(dec_digits(100000), 6);
220 BOOST_CHECK_EQUAL(dec_digits(1000000), 7);
221 BOOST_CHECK_EQUAL(dec_digits(10000000), 8);
222 BOOST_CHECK_EQUAL(dec_digits(100000000), 9);
223 BOOST_CHECK_EQUAL(dec_digits(1000000000), 10);
224 BOOST_CHECK_EQUAL(dec_digits(10000000000), 11);
225 BOOST_CHECK_EQUAL(dec_digits(100000000000), 12);
226 BOOST_CHECK_EQUAL(dec_digits(1000000000000), 13);
227 BOOST_CHECK_EQUAL(dec_digits(10000000000000), 14);
228 BOOST_CHECK_EQUAL(dec_digits(100000000000000), 15);
229 BOOST_CHECK_EQUAL(dec_digits(1000000000000000), 16);
230 BOOST_CHECK_EQUAL(dec_digits(10000000000000000), 17);
231 BOOST_CHECK_EQUAL(dec_digits(100000000000000000), 18);
232 BOOST_CHECK_EQUAL(dec_digits(1000000000000000000), 19);
233}
234
STL namespace.
bool stob(string_view sv, bool dfl) noexcept
Definition Utility.cpp:26
constexpr int hex_digits(UIntegerT i) noexcept
Definition Utility.hpp:76
constexpr int dec_digits(std::int64_t i) noexcept
Definition Utility.hpp:49
std::string_view sv
Definition Tokeniser.hpp:26
double stod(std::string_view sv, double dfl) noexcept
Definition Utility.cpp:89
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_AUTO_TEST_CASE(StobCase)
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))
BOOST_CHECK_EQUAL(stod("1"sv), 1)