iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Dims.C
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
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 */
34 
35 
36 #include <nrt/config.h>
37 #include <nrt/Core/Geometry/Dims.H>
38 
39 #define BOOST_TEST_DYN_LINK
40 #define BOOST_TEST_MODULE DimsTest
41 #include <boost/test/unit_test.hpp>
42 
43 
44 void func(nrt::Dims<int> const & d) { }
45 
46 BOOST_AUTO_TEST_CASE(DimsTest)
47 {
48  // *** Default constructor
49  nrt::Dims<int> d0;
50  BOOST_CHECK_EQUAL(d0.width(), 0);
51  BOOST_CHECK_EQUAL(d0.height(), 0);
52  BOOST_CHECK_EQUAL(d0.size(), 0);
53  BOOST_CHECK_EQUAL(d0.empty(), true);
54 
55  // *** Non-default constructor:
56  nrt::Dims<int> d1(300, 128);
57  BOOST_CHECK_EQUAL(d1.width(), 300);
58  BOOST_CHECK_EQUAL(d1.height(), 128);
59  BOOST_CHECK_EQUAL(d1.size(), 300*128);
60  BOOST_CHECK_EQUAL(d1.empty(), false);
61 
62  // *** Copy construction, copy:
63  nrt::Dims<int> d2(d0);
64  BOOST_CHECK_EQUAL(d2.size(), 0);
65  d2 = d1;
66  BOOST_CHECK_EQUAL(d2.size(), 300*128);
67 
68  // *** Conversion-construction: clamps values to destination range
69  nrt::Dims<nrt::byte> d3(d1);
70  BOOST_CHECK_EQUAL(d3.width(), nrt::byte(255));
71  BOOST_CHECK_EQUAL(d3.height(), nrt::byte(128));
72 
73  // ** Assignment when template parameters match:
74  d2 = d1; // ok
75 
76  // *** Assignment with conversion: forbidden implicitly, must be explicit about it by using default constructor for
77  // the destination type:
78 
79  // d3 = d0; // does not compile
80 
81  // *** No implicit conversion is allowed at all, to avoid confusing hidden bugs associated with implicit
82  // conversions. For example, cannot call func (which wants an nrt::Dims<int>) with nrt::Dims<nrt::byte>:
83 
84  // func(d3); // Does not compile
85  func(nrt::Dims<int>(d3)); // ok, we are explicit in knowing that func wants Dims<int>, which d3 is not
86  func(d1); // Ok, exact type match
87 
88  // *** Operators: we explicitly forbid mixing types, here again you must be very explicit
89  d1 += 10; // ok, Dims<int> + int
90  d1 += d2; // ok, Dims<int> + Dims<int>
91 
92  //d1 += 1.23F; // does not compile: Dims<int> += float is forbidden
93  d1 += int(1.23F); // here is one possible explicit way to do Dims<int> + float
94  d1 = nrt::Dims<int>(nrt::Dims<float>(d1) + 1.23F); // here is another possible explicit way for Dims<int> + float
95 
96  //nrt::Dims<float> d4 = d1 + 4.56F; // does not compile: Dims<int> + float is forbidden
97  nrt::Dims<float> d4 = nrt::Dims<float>(d1) + 4.56F; // ok, nice and explicit abeout what we want
98 
99  d4 += 2.0F; // ok, Dims<float> += float
100  //d4 += 2.0; // does not compile: Dims<float> += double
101  d4 += d4; // ok, Dims<float> += Dims<float>
102 }
103