iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-ImageCopy.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 
37 
38 // We define NRT_ARRAY_DEEPCOPY_THROWS so that we can check that an exception is thrown when we expect a deep copy
39 #define NRT_ARRAY_DEEPCOPY_THROWS
40 
41 #include <nrt/config.h>
43 #include <nrt/Core/Image/Image.H>
44 
45 #define BOOST_TEST_DYN_LINK
46 #define BOOST_TEST_MODULE ImageDeepCopy
47 
48 #include <boost/test/unit_test.hpp>
49 
50 
51 BOOST_AUTO_TEST_CASE(ImageDeepCopyTest)
52 {
54 
55  bool threw = false;
56  try
57  {
58  im1 = nrt::Image< nrt::PixGray<int> >( { { 1, 2, 3 }, { 4, 5, 6} } );
59  }
60  catch (nrt::exception::Exception & e) { threw = true; }
61  BOOST_CHECK_EQUAL(threw, false);
62 
63  threw = false;
64  try
65  {
66  im2 = im1;
67  }
68  catch (nrt::exception::Exception & e) { threw = true; }
69  BOOST_CHECK_EQUAL(threw, false);
70 
71  threw = false;
72  try
73  {
74  // yep, this throws (since we defined NRT_ARRAY_DEEPCOPY_THROWS above), since im1 is not const and its data is
75  // shared with im2
77  for (nrt::PixGray<int> const & pix : im1) sum += pix;
78  }
79  catch (nrt::exception::Exception & e) { threw = true; }
80  BOOST_CHECK_EQUAL(threw, true);
81 
82  // let's try this again but using Image<T>::cref() to avoid the deep copy:
83  im1 = nrt::Image< nrt::PixGray<int> >( { { 1, 2, 3 }, { 4, 5, 6} } );
84  im2 = im1;
85 
86  threw = false;
87  try
88  {
89  // avoid the deep copy by using cref() on our non-const Image
90  nrt::PixGray<int> sum = 0;
91  for (nrt::PixGray<int> const & pix : im1.cref()) sum += pix;
92  }
93  catch (nrt::exception::Exception & e) { threw = true; }
94  BOOST_CHECK_EQUAL(threw, false);
95 
96  threw = false;
97  try
98  {
99  // no deep copy when accessing pixels of a const image:
100  nrt::Image< nrt::PixGray<int> > const im3 = im1;
101 
102  nrt::PixGray<int> sum = 0;
103  for (nrt::PixGray<int> const & pix : im3) sum += pix;
104  }
105  catch (nrt::exception::Exception & e) { threw = true; }
106  BOOST_CHECK_EQUAL(threw, false);
107 
108 }