Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
util
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
22
using namespace
std
;
23
using namespace
toolbox
;
24
25
namespace
utf
= boost::unit_test;
26
27
BOOST_AUTO_TEST_SUITE
(
UtilitySuite
)
28
29
BOOST_AUTO_TEST_CASE
(
StobCase
)
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
71
BOOST_AUTO_TEST_CASE
(
StodCase
, *utf::tolerance(0.0000001))
72
{
73
BOOST_CHECK_EQUAL
(
stod
(
""
sv
), 0);
74
BOOST_CHECK_EQUAL
(
stod
(
"1"
sv
), 1);
75
BOOST_CHECK_EQUAL
(
stod
(
"-1"
sv
), -1);
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
89
BOOST_AUTO_TEST_CASE
(
StonDoubleCase
, *utf::tolerance(0.0000001))
90
{
91
BOOST_CHECK_EQUAL
(
ston<double>
(
""
sv
), 0);
92
BOOST_CHECK_EQUAL
(
ston<double>
(
"1"
sv
), 1);
93
BOOST_CHECK_EQUAL
(
ston<double>
(
"-1"
sv
), -1);
94
BOOST_CHECK_EQUAL
(
ston<double>
(
"1.23E2"
sv
), 123);
95
BOOST_CHECK_EQUAL
(
ston<double>
(
"-1.23E2"
sv
), -123);
96
BOOST_CHECK_EQUAL
(
ston<double>
(
"1.23E1"
sv
), 12.3);
97
BOOST_CHECK_EQUAL
(
ston<double>
(
"-1.23E1"
sv
), -12.3);
98
BOOST_CHECK_EQUAL
(
ston<double>
(
"1.23"
sv
), 1.23);
99
BOOST_CHECK_EQUAL
(
ston<double>
(
"-1.23"
sv
), -1.23);
100
}
101
102
BOOST_AUTO_TEST_CASE
(
StonSubstrCase
)
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
109
BOOST_AUTO_TEST_CASE
(
Stoi16Case
)
110
{
111
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
""
sv
), 0);
112
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"1"
sv
), 1);
113
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"123"
sv
), 123);
114
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
" "
sv
), 0);
115
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"x"
sv
), 0);
116
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"x1 "
sv
), 0);
117
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"x123 "
sv
), 0);
118
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"32767"
sv
), 0x7fff);
119
BOOST_CHECK_EQUAL
(
ston<int16_t>
(
"-32768"
sv
), -0x8000);
120
}
121
122
BOOST_AUTO_TEST_CASE
(
Stoi32Case
)
123
{
124
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
""
sv
), 0);
125
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"1"
sv
), 1);
126
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"123"
sv
), 123);
127
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
" "
sv
), 0);
128
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"x"
sv
), 0);
129
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"x1 "
sv
), 0);
130
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"x123 "
sv
), 0);
131
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"2147483647"
sv
), 0x7fffffff);
132
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"-2147483648"
sv
), -0x80000000);
133
}
134
135
BOOST_AUTO_TEST_CASE
(
Stoi32NegSignCase
)
136
{
137
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"-"
sv
), 0);
138
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"-1"
sv
), -1);
139
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"-123"
sv
), -123);
140
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"-x"
sv
), 0);
141
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"x-1 "
sv
), 0);
142
BOOST_CHECK_EQUAL
(
ston<int32_t>
(
"x-123 "
sv
), 0);
143
}
144
145
BOOST_AUTO_TEST_CASE
(
Stoi64Case
)
146
{
147
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
""
sv
), 0
L
);
148
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"1"
sv
), 1L);
149
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"123"
sv
), 123L);
150
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
" "
sv
), 0
L
);
151
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"x"
sv
), 0
L
);
152
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"x1 "
sv
), 0
L
);
153
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"x123 "
sv
), 0
L
);
154
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"9223372036854775807"
sv
), 0x7fffffffffffffffLL);
155
BOOST_CHECK_EQUAL
(
ston<int64_t>
(
"-9223372036854775808"
sv
), -0x8000000000000000LL);
156
}
157
158
BOOST_AUTO_TEST_CASE
(
Stou16Case
)
159
{
160
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
""
sv
), 0);
161
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"1"
sv
), 1);
162
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"123"
sv
), 123);
163
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
" "
sv
), 0);
164
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"x"
sv
), 0);
165
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"x1 "
sv
), 0);
166
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"x123 "
sv
), 0);
167
BOOST_CHECK_EQUAL
(
ston<uint16_t>
(
"65535"
sv
), 0xffff);
168
}
169
170
BOOST_AUTO_TEST_CASE
(
Stou32Case
)
171
{
172
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
""
sv
), 0
U
);
173
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"1"
sv
), 1U);
174
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"123"
sv
), 123U);
175
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
" "
sv
), 0
U
);
176
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"x"
sv
), 0
U
);
177
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"x1 "
sv
), 0
U
);
178
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"x123 "
sv
), 0
U
);
179
BOOST_CHECK_EQUAL
(
ston<uint32_t>
(
"4294967295"
sv
), 0xffffffffU);
180
}
181
182
BOOST_AUTO_TEST_CASE
(
Stou64Case
)
183
{
184
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
""
sv
), 0
UL
);
185
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"1"
sv
), 1UL);
186
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"123"
sv
), 123UL);
187
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
" "
sv
), 0
UL
);
188
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"x"
sv
), 0
UL
);
189
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"x1 "
sv
), 0
UL
);
190
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"x123 "
sv
), 0
UL
);
191
BOOST_CHECK_EQUAL
(
ston<uint64_t>
(
"18446744073709551615"
sv
), 0xffffffffffffffffULL);
192
}
193
194
BOOST_AUTO_TEST_CASE
(
HexDigitsCase
)
195
{
196
BOOST_CHECK_EQUAL
(
hex_digits
(0x0U), 1);
197
BOOST_CHECK_EQUAL
(
hex_digits
(0x1U), 1);
198
BOOST_CHECK_EQUAL
(
hex_digits
(0xfU), 1);
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
211
BOOST_AUTO_TEST_CASE
(
DecDigitsCase
)
212
{
213
BOOST_CHECK_EQUAL
(
dec_digits
(0), 1);
214
BOOST_CHECK_EQUAL
(
dec_digits
(1), 1);
215
BOOST_CHECK_EQUAL
(
dec_digits
(10), 2);
216
BOOST_CHECK_EQUAL
(
dec_digits
(100), 3);
217
BOOST_CHECK_EQUAL
(
dec_digits
(1000), 4);
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
235
BOOST_AUTO_TEST_SUITE_END
()
std
STL namespace.
toolbox::util::stob
bool stob(string_view sv, bool dfl) noexcept
Definition
Utility.cpp:26
toolbox::util::hex_digits
constexpr int hex_digits(UIntegerT i) noexcept
Definition
Utility.hpp:76
toolbox::util::dec_digits
constexpr int dec_digits(std::int64_t i) noexcept
Definition
Utility.hpp:49
toolbox::util::sv
std::string_view sv
Definition
Tokeniser.hpp:26
toolbox::util::stod
double stod(std::string_view sv, double dfl) noexcept
Definition
Utility.cpp:89
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
Utility.hpp
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(StobCase)
Definition
Utility.ut.cpp:29
BOOST_CHECK
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(stod("1"sv), 1)
Generated by
1.9.8