iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Optional.C
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
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 #include <nrt/config.h>
35 #include <nrt/Core/Debugging/Log.H>
37 
38 #define BOOST_TEST_DYN_LINK
39 #define BOOST_TEST_MODULE OptionalTest
40 #include <boost/test/unit_test.hpp>
41 
42 using namespace nrt;
43 
44 // ######################################################################
45 BOOST_AUTO_TEST_CASE(OptionalConstructionTest)
46 {
47  Optional<int> myIntOptional1;
48  BOOST_CHECK_EQUAL(myIntOptional1.isSet(), false);
49  BOOST_CHECK_EQUAL(bool(myIntOptional1), false);
50 
51  Optional<int> myIntOptional2(99);
52  BOOST_CHECK_EQUAL(myIntOptional2.isSet(), true);
53  BOOST_CHECK_EQUAL(bool(myIntOptional2), true);
54  BOOST_CHECK_EQUAL(*myIntOptional2, 99);
55 
56  Optional<int> myIntOptional3(myIntOptional2);
57  BOOST_CHECK_EQUAL(myIntOptional3.isSet(), true);
58  BOOST_CHECK_EQUAL(bool(myIntOptional3), true);
59  BOOST_CHECK_EQUAL(*myIntOptional3, 99);
60 
61  Optional<int> myIntOptional4(std::move(myIntOptional3));
62  BOOST_CHECK_EQUAL(myIntOptional4.isSet(), true);
63  BOOST_CHECK_EQUAL(bool(myIntOptional4), true);
64  BOOST_CHECK_EQUAL(*myIntOptional4, 99);
65  BOOST_CHECK_EQUAL(myIntOptional3.isSet(), false);
66  BOOST_CHECK_EQUAL(bool(myIntOptional3), false);
67 }
68 
69 // ######################################################################
70 BOOST_AUTO_TEST_CASE(OptionalAssignmentTest)
71 {
72  Optional<int> myIntOptional1(1);
73  BOOST_CHECK_EQUAL(*myIntOptional1, 1);
74  myIntOptional1 = 2;
75  BOOST_CHECK_EQUAL(*myIntOptional1, 2);
76  myIntOptional1 = 3;
77  BOOST_CHECK_EQUAL(*myIntOptional1, 3);
78 }
79 
80 // ######################################################################
81 BOOST_AUTO_TEST_CASE(ReferenceOptionalAssignmentTest)
82 {
83  int myInt = 0;
84  Optional<int&> myIntOptional1(myInt);
85  BOOST_CHECK_EQUAL(*myIntOptional1, 0);
86  *myIntOptional1 = 1;
87  BOOST_CHECK_EQUAL(*myIntOptional1, 1);
88  BOOST_CHECK_EQUAL(myInt, 1);
89  myInt = 2;
90  BOOST_CHECK_EQUAL(*myIntOptional1, 2);
91  BOOST_CHECK_EQUAL(myInt, 2);
92  myIntOptional1.clear();
93  BOOST_CHECK_EQUAL(myInt, 2);
94 }
95 
96 // ######################################################################
97 BOOST_AUTO_TEST_CASE(ReferenceOptionalMixTest)
98 {
99  int myInt1 = 0;
100  int myInt2 = 1;
101  Optional<int&> myIntOptional1(myInt1);
102  Optional<int&> myIntOptional2(myInt2);
103  BOOST_CHECK_EQUAL(*myIntOptional1, 0);
104  BOOST_CHECK_EQUAL(*myIntOptional2, 1);
105 
106  Optional<int> myIntOptional3(myIntOptional1);
107  BOOST_CHECK_EQUAL(*myIntOptional3, 0);
108  *myIntOptional3 = 2;
109  BOOST_CHECK_EQUAL(*myIntOptional1, 0);
110  BOOST_CHECK_EQUAL(*myIntOptional3, 2);
111 }
112