DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2011-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24#include <cassert>
25#include <iostream>
26#include <utility>
27
28#include "dccl/logger.h"
29
30#include "../../common.h"
31
32bool same(double a, double b)
33{
34 // good enough comparison for this test
35 return std::abs(a - b) < 1e-10;
36}
37
38template <typename Int> bool same(Int a, Int b) { return a == b; }
39
40template <typename T> void check(T in, int prec, T out)
41{
42 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Checking that " << in << " rounded to precision: " << prec << " is equal to "
43 << out << std::endl;
44 assert(same(dccl::round(in, prec), out));
45}
46
47int main(int argc, char* argv[])
48{
49 bool verbose = false;
50 for (int i = 1; i < argc; ++i)
51 {
52 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
53 verbose = true;
54 }
55
56 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
57
58 check(1.234, 2, 1.23);
59 check(1.25, 1, 1.3);
60 check(1.35, 1, 1.4);
61
62 check<int>(1239, -1, 1240);
63 check<int>(1351, -2, 1400);
64 check<int>(1450, -2, 1500);
65 check<int>(1344, -3, 1000);
66
67 check(1239.0, -1, 1240.0);
68 check(1351.0, -2, 1400.0);
69 check(1450.0, -2, 1500.0);
70 check(1344.0, -3, 1000.0);
71
72 check<int>(-499000, -3, -499000);
73 check<int>(-500000, -3, -500000);
74
75 check(-500000.0, -3, -500000.0);
76
77 check<int>(0, -3, 0);
78 check<int>(0, 2, 0);
79
80 check(0.0, -3, 0.0);
81 check(0.0, 2, 0.0);
82
83 check<dccl::int64>(1409165969804999ull, -3, 1409165969805000ull);
84
85 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
86
87 return 0;
88}
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition logger.h:213
Dynamic Compact Control Language namespace.
Definition any.h:28