DCCL v4
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 "../../common.h"
29
30bool same(double a, double b)
31{
32 // good enough comparison for this test
33 return std::abs(a - b) < 1e-10;
34}
35
36template <typename Int> bool same(Int a, Int b) { return a == b; }
37
38template <typename T> void check(T in, int prec, T out)
39{
40 std::cout << "Checking that " << in << " rounded to precision: " << prec << " is equal to "
41 << out << std::endl;
42 assert(same(dccl::round(in, prec), out));
43}
44
45int main()
46{
47 check(1.234, 2, 1.23);
48 check(1.25, 1, 1.3);
49 check(1.35, 1, 1.4);
50
51 check<int>(1239, -1, 1240);
52 check<int>(1351, -2, 1400);
53 check<int>(1450, -2, 1500);
54 check<int>(1344, -3, 1000);
55
56 check(1239.0, -1, 1240.0);
57 check(1351.0, -2, 1400.0);
58 check(1450.0, -2, 1500.0);
59 check(1344.0, -3, 1000.0);
60
61 check<int>(-499000, -3, -499000);
62 check<int>(-500000, -3, -500000);
63
64 check(-500000.0, -3, -500000.0);
65
66 check<int>(0, -3, 0);
67 check<int>(0, 2, 0);
68
69 check(0.0, -3, 0.0);
70 check(0.0, 2, 0.0);
71
72 check<dccl::int64>(1409165969804999ull, -3, 1409165969805000ull);
73
74 std::cout << "all tests passed" << std::endl;
75
76 return 0;
77}