Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
util
Math.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 "
Math.hpp
"
18
19
#include <boost/test/unit_test.hpp>
20
21
using namespace
std
;
22
using namespace
toolbox
;
23
24
namespace
utf
= boost::unit_test;
25
26
BOOST_AUTO_TEST_SUITE
(
MathSuite
)
27
28
BOOST_AUTO_TEST_CASE
(
NextPow2Case
)
29
{
30
BOOST_CHECK_EQUAL
(
next_pow2
(1U), 1U);
31
BOOST_CHECK_EQUAL
(
next_pow2
(2U), 2U);
32
BOOST_CHECK_EQUAL
(
next_pow2
(3U), 4U);
33
BOOST_CHECK_EQUAL
(
next_pow2
(4U), 4U);
34
BOOST_CHECK_EQUAL
(
next_pow2
(5U), 8U);
35
BOOST_CHECK_EQUAL
(
next_pow2
(6U), 8U);
36
BOOST_CHECK_EQUAL
(
next_pow2
(7U), 8U);
37
BOOST_CHECK_EQUAL
(
next_pow2
(8U), 8U);
38
39
BOOST_CHECK_EQUAL
(
next_pow2
(1UL), 1UL);
40
BOOST_CHECK_EQUAL
(
next_pow2
(2UL), 2UL);
41
BOOST_CHECK_EQUAL
(
next_pow2
(3UL), 4UL);
42
BOOST_CHECK_EQUAL
(
next_pow2
(4UL), 4UL);
43
BOOST_CHECK_EQUAL
(
next_pow2
(5UL), 8UL);
44
BOOST_CHECK_EQUAL
(
next_pow2
(6UL), 8UL);
45
BOOST_CHECK_EQUAL
(
next_pow2
(7UL), 8UL);
46
BOOST_CHECK_EQUAL
(
next_pow2
(8UL), 8UL);
47
}
48
49
BOOST_AUTO_TEST_CASE
(
VarAccumCase
, *utf::tolerance(0.0000001))
50
{
51
VarAccum
v
;
52
v
.
append
(1345, 1301, 1368, 1322, 1310, 1370, 1318, 1350, 1303, 1299);
53
BOOST_CHECK_EQUAL
(
v
.size(), 10U);
54
BOOST_CHECK_EQUAL
(
v
.mean(), 1328.6);
55
BOOST_CHECK_CLOSE_FRACTION
(
var
(
v
), 754.2666667, 1e-7);
56
BOOST_CHECK_CLOSE_FRACTION
(
varp
(
v
), 678.84, 1e-7);
57
BOOST_CHECK_CLOSE_FRACTION
(
stdev
(
v
), 27.4639157, 1e-7);
58
BOOST_CHECK_CLOSE_FRACTION
(
stdevp
(
v
), 26.0545581, 1e-7);
59
BOOST_CHECK_EQUAL
(
v
.min(), 1299);
60
BOOST_CHECK_EQUAL
(
v
.max(), 1370);
61
}
62
63
BOOST_AUTO_TEST_CASE
(
CeilCase
)
64
{
65
BOOST_CHECK_EQUAL
(
ceil
(1U, 3U), 1U);
66
BOOST_CHECK_EQUAL
(
ceil
(2U, 3U), 1U);
67
BOOST_CHECK_EQUAL
(
ceil
(3U, 3U), 1U);
68
BOOST_CHECK_EQUAL
(
ceil
(4U, 3U), 2U);
69
BOOST_CHECK_EQUAL
(
ceil
(5U, 3U), 2U);
70
BOOST_CHECK_EQUAL
(
ceil
(6U, 3U), 2U);
71
}
72
73
BOOST_AUTO_TEST_SUITE_END
()
Math.hpp
BOOST_CHECK_CLOSE_FRACTION
BOOST_CHECK_CLOSE_FRACTION(var(v), 754.2666667, 1e-7)
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(NextPow2Case)
Definition
Math.ut.cpp:28
toolbox::VarAccum
Definition
Math.hpp:53
toolbox::util::VarAccum::append
void append(double val) noexcept
Definition
Math.hpp:81
std
STL namespace.
toolbox::util::var
double var(const VarAccum &v) noexcept
Definition
Math.hpp:106
toolbox::util::stdevp
double stdevp(const VarAccum &v) noexcept
Definition
Math.hpp:121
toolbox::util::next_pow2
unsigned next_pow2(unsigned n) noexcept
Definition
Math.hpp:35
toolbox::util::varp
double varp(const VarAccum &v) noexcept
Definition
Math.hpp:111
toolbox::util::ceil
constexpr std::size_t ceil(std::size_t dividend, std::size_t divisor) noexcept
Definition
Math.hpp:150
toolbox::util::stdev
double stdev(const VarAccum &v) noexcept
Definition
Math.hpp:116
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
Generated by
1.9.8