DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #include <iostream>
23 #include <cassert>
24 #include <utility>
25 
26 #include "dccl/common.h"
27 
28 bool same(double a, double b)
29 {
30  // good enough comparison for this test
31  return std::abs(a-b) < 1e-10;
32 }
33 
34 template<typename Int>
35 bool same(Int a, Int b)
36 {
37  return a == b;
38 }
39 
40 
41 template<typename T>
42 void check(T in, int prec, T out)
43 {
44  std::cout << "Checking that " << in << " rounded to precision: " << prec << " is equal to " << out << std::endl;
45  assert(same(dccl::round(in, prec), out));
46 }
47 
48 int main()
49 {
50  check(1.234, 2, 1.23);
51  check(1.25, 1, 1.3);
52  check(1.35, 1, 1.4);
53 
54  check<int>(1239, -1, 1240);
55  check<int>(1351, -2, 1400);
56  check<int>(1450, -2, 1500);
57  check<int>(1344, -3, 1000);
58 
59  check(1239.0, -1, 1240.0);
60  check(1351.0, -2, 1400.0);
61  check(1450.0, -2, 1500.0);
62  check(1344.0, -3, 1000.0);
63 
64  check<int>(-499000, -3, -499000);
65  check<int>(-500000, -3, -500000);
66 
67  check(-500000.0, -3, -500000.0);
68 
69  check<int>(0, -3, 0);
70  check<int>(0, 2, 0);
71 
72  check(0.0, -3, 0.0);
73  check(0.0, 2, 0.0);
74 
75  check<dccl::int64>(1409165969804999ull, -3, 1409165969805000ull);
76 
77  std::cout << "all tests passed" << std::endl;
78 
79  return 0;
80 }