iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-ImageCopy.C
/*! @file
@author Laurent Itti
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
// We define NRT_ARRAY_DEEPCOPY_THROWS so that we can check that an exception is thrown when we expect a deep copy
#define NRT_ARRAY_DEEPCOPY_THROWS
#include <nrt/config.h>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ImageDeepCopy
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(ImageDeepCopyTest)
{
bool threw = false;
try
{
im1 = nrt::Image< nrt::PixGray<int> >( { { 1, 2, 3 }, { 4, 5, 6} } );
}
catch (nrt::exception::Exception & e) { threw = true; }
BOOST_CHECK_EQUAL(threw, false);
threw = false;
try
{
im2 = im1;
}
catch (nrt::exception::Exception & e) { threw = true; }
BOOST_CHECK_EQUAL(threw, false);
threw = false;
try
{
// yep, this throws (since we defined NRT_ARRAY_DEEPCOPY_THROWS above), since im1 is not const and its data is
// shared with im2
for (nrt::PixGray<int> const & pix : im1) sum += pix;
}
catch (nrt::exception::Exception & e) { threw = true; }
BOOST_CHECK_EQUAL(threw, true);
// let's try this again but using Image<T>::cref() to avoid the deep copy:
im1 = nrt::Image< nrt::PixGray<int> >( { { 1, 2, 3 }, { 4, 5, 6} } );
im2 = im1;
threw = false;
try
{
// avoid the deep copy by using cref() on our non-const Image
for (nrt::PixGray<int> const & pix : im1.cref()) sum += pix;
}
catch (nrt::exception::Exception & e) { threw = true; }
BOOST_CHECK_EQUAL(threw, false);
threw = false;
try
{
// no deep copy when accessing pixels of a const image:
nrt::Image< nrt::PixGray<int> > const im3 = im1;
for (nrt::PixGray<int> const & pix : im3) sum += pix;
}
catch (nrt::exception::Exception & e) { threw = true; }
BOOST_CHECK_EQUAL(threw, false);
}