[Insight-developers] Test framework
Steve M. Robbins
steve at sumost.ca
Mon Dec 29 18:27:53 EST 2008
On Mon, Dec 29, 2008 at 12:14:32PM -0600, Daniel Blezek wrote:
> I would add the Google Test framework to the mix.
I've not used Google Test before, so I just had a quick look.
I have to say: I'm really impressed! It's got all the usual
stuff plus some neat features I've never seen before, like
SCOPED_TRACE and the death tests.
I'm sold: this is probably better than Boost.Test.
Thanks for the tip, Dan!
> I found a CMake macro that parses the source code and automatically adds
> tests making it trivial to add new tests:
>
> macro(ADD_GOOGLE_TESTS executable)
> foreach ( source ${ARGN} )
> file(READ "${source}" contents)
> string(REGEX MATCHALL "TEST_?F?\\(([A-Za-z_0-9 ,]+)\\)" found_tests
> ${contents})
> foreach(hit ${found_tests})
> string(REGEX REPLACE ".*\\(([A-Za-z_0-9]+)[, ]*([A-Za-z_0-9]+)\\).*"
> "\\1.\\2" test_name ${hit})
> add_test(${test_name} ${executable} --gtest_filter=${test_name}
> ${MI3CTestingDir})
> endforeach(hit)
> endforeach()
> endmacro()
Cute. But does this mean that all the test code source files are
scanned each time CMake enters the directory? Isn't that a bit slow?
FWIW, I've converted my previous Boost.Test-based code to Google Test;
see below. While doing so, I recoded it to not use fixtures since I
realized that all I really wanted was a convenient way to initialize
the regions.
Regards,
-Steve
#include <gtest/gtest.h>
#include "itkImageRegion.h"
typedef itk::ImageRegion<1> Region1D;
typedef itk::ImageRegion<2> Region2D;
typedef itk::ImageRegion<3> Region3D;
typedef itk::Index<1> Index1D;
typedef itk::ContinuousIndex<float,1> CIndex1D;
Region1D CreateRegion( int start0,
int size0 )
{
Region1D::IndexType start = {{ start0 }};
Region1D::SizeType size = {{ size0 }};
return Region1D( start, size );
}
Region2D CreateRegion( int start0, int start1,
int size0, int size1 )
{
Region2D::IndexType start = {{ start0, start1 }};
Region2D::SizeType size = {{ size0, size1 }};
return Region2D( start, size );
}
Region3D CreateRegion( int start0, int start1, int start2,
int size0, int size1, int size2 )
{
Region3D::IndexType start = {{ start0, start1, start2 }};
Region3D::SizeType size = {{ size0, size1, size2 }};
return Region3D( start, size );
}
TEST( itkImageRegionTest, testSlice )
{
Region3D volume = CreateRegion( 12, 12, 12, 10, 20, 30 );
Region2D slice0 = CreateRegion( 12, 12, 20, 30 );
Region2D slice1 = CreateRegion( 12, 12, 10, 30 );
Region2D slice2 = CreateRegion( 12, 12, 10, 20 );
EXPECT_EQ( slice0, volume.Slice(0) );
EXPECT_EQ( slice1, volume.Slice(1) );
EXPECT_EQ( slice2, volume.Slice(2) );
}
TEST( itkImageRegionTest, testSliceOutOfBounds )
{
Region3D volume = CreateRegion( 12, 12, 12, 10, 20, 30 );
EXPECT_THROW( volume.Slice(-1), std::exception );
EXPECT_THROW( volume.Slice(3), std::exception );
}
TEST( itkImageRegionTest, testVolumeIsInside )
{
Region3D volumeA = CreateRegion( 12, 12, 12, 10, 20, 30 );
Region3D volumeB = CreateRegion( 14, 14, 14, 5, 10, 15 );
EXPECT_TRUE( volumeA.IsInside( volumeB ) );
EXPECT_FALSE( volumeB.IsInside( volumeA ) );
}
// New tests start here
TEST( itkImageRegionTest, testIndexPointIsInside )
{
Region1D line = CreateRegion( 1, 2 );
Index1D i0 = {{ 0 }};
Index1D i1 = {{ 1 }};
Index1D i2 = {{ 2 }};
Index1D i3 = {{ 3 }};
EXPECT_FALSE( line.IsInside( i0 ) );
EXPECT_TRUE ( line.IsInside( i1 ) );
EXPECT_TRUE ( line.IsInside( i2 ) );
EXPECT_FALSE( line.IsInside( i3 ) );
}
TEST( itkImageRegionTest, testContinuousIndexPointIsInsideEdgeConvention )
{
// In pixel edge convention, line is half-open interval [1,3)
Region1D line = CreateRegion( 1, 2 );
CIndex1D p0, p1, p2, p3;
p0[0] = 0.99;
p1[0] = 1.01;
p2[0] = 2.99;
p3[0] = 3.01;
EXPECT_FALSE( line.IsInside( p0 ) );
EXPECT_TRUE ( line.IsInside( p1 ) );
EXPECT_TRUE ( line.IsInside( p2 ) );
EXPECT_FALSE( line.IsInside( p3 ) );
}
TEST( itkImageRegionTest, testContinuousIndexPointIsInsideCentreConvention )
{
// In pixel centre convention, line is half-open interval [0.5,2.5)
Region1D line = CreateRegion( 1, 2 );
CIndex1D p0, p1, p2, p3;
p0[0] = 0.49;
p1[0] = 0.51;
p2[0] = 2.49;
p3[0] = 2.51;
EXPECT_FALSE( line.IsInside( p0 ) );
EXPECT_TRUE ( line.IsInside( p1 ) );
EXPECT_TRUE ( line.IsInside( p2 ) );
EXPECT_FALSE( line.IsInside( p3 ) );
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20081229/5688ec02/attachment.pgp>
More information about the Insight-developers
mailing list