iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Clamp.C
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
35 #include <nrt/Core/Debugging/Log.H>
36 #include <boost/numeric/conversion/bounds.hpp>
37 #include <boost/numeric/conversion/is_subranged.hpp>
38 #include <boost/numeric/conversion/conversion_traits.hpp>
39 #include <type_traits>
40 #include <iostream>
41 #include <boost/type_traits.hpp>
42 
43 #define BOOST_TEST_DYN_LINK
44 #define BOOST_TEST_MODULE ClampTest
45 #include <boost/test/unit_test.hpp>
46 
47 BOOST_AUTO_TEST_CASE(ClampTest)
48 {
49 
50  const short int sintmin = boost::numeric::bounds<short int>::lowest();
51  const short int sintmax = boost::numeric::bounds<short int>::highest();
52 
53  const unsigned int uintmin = boost::numeric::bounds<unsigned int>::lowest();
54  const unsigned int uintmax = boost::numeric::bounds<unsigned int>::highest();
55 
56  const int intmin = boost::numeric::bounds<int>::lowest();
57  const int intmax = boost::numeric::bounds<int>::highest();
58 
59  const float floatmin = boost::numeric::bounds<float>::lowest();
60  const float floatmax = boost::numeric::bounds<float>::highest();
61 
62  const double doublemin = boost::numeric::bounds<double>::lowest();
63  const double doublemax = boost::numeric::bounds<double>::highest();
64 
65  BOOST_CHECK_EQUAL(nrt::clamped_convert<short int>(intmin), sintmin);
66  BOOST_CHECK_EQUAL(nrt::clamped_convert<short int>(intmax), sintmax);
67 
68  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(uintmin), uintmin);
69  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(uintmax), intmax);
70 
71  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(uintmin), uintmin);
72  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(uintmax), intmax);
73 
74  BOOST_CHECK_EQUAL(nrt::clamped_convert<unsigned int>(intmin), uintmin);
75  BOOST_CHECK_EQUAL(nrt::clamped_convert<unsigned int>(intmax), intmax);
76 
77  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(uintmin), uintmin);
78  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(uintmax), uintmax);
79 
80  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(intmin), intmin);
81  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(intmax), intmax);
82 
83  BOOST_CHECK_EQUAL(nrt::clamped_convert<unsigned int>(floatmin), uintmin);
84  BOOST_CHECK_EQUAL(nrt::clamped_convert<unsigned int>(floatmax), uintmax);
85 
86  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(floatmin), intmin);
87  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(floatmax), intmax);
88 
89  BOOST_CHECK_EQUAL(nrt::clamped_convert<double>(floatmin), floatmin);
90  BOOST_CHECK_EQUAL(nrt::clamped_convert<double>(floatmax), floatmax);
91 
92  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(doublemin), floatmin);
93  BOOST_CHECK_EQUAL(nrt::clamped_convert<float>(doublemax), floatmax);
94 
95  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(0.9F), 1);
96  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(0.501F), 1);
97  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(0.5F), 1);
98  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(0.499F), 0);
99  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(0.1F), 0);
100 
101  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-0.9F), -1);
102  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-0.5F), -1);
103  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-0.1F), 0);
104 
105  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-1.0), -1);
106  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-1.499), -1);
107  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-1.5), -2);
108  BOOST_CHECK_EQUAL(nrt::clamped_convert<int>(-1.501), -2);
109 
110  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(254.6F), 255);
111  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(0.1F), 0);
112  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(255.1F), 255);
113  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(256.0F), 255);
114  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(500.0F), 255);
115  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(-0.1F), 0);
116  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(-0.5F), 0);
117  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(-0.9F), 0);
118  BOOST_CHECK_EQUAL(nrt::clamped_convert<nrt::byte>(-1.0F), 0);
119 }
120