Skip to content

Commit

Permalink
Updates to unit test of SpanSets for Review
Browse files Browse the repository at this point in the history
  • Loading branch information
natelust committed Nov 10, 2016
1 parent 859e961 commit c8f8e1e
Showing 1 changed file with 203 additions and 7 deletions.
210 changes: 203 additions & 7 deletions tests/testSpanSets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ BOOST_AUTO_TEST_CASE(SpanSet_testContiguous) {
afwGeom::Span(1, 0, 6)};
afwGeom::SpanSet SSWithHole(SpanSetWithHoleVec);
BOOST_CHECK(SSWithHole.isContiguous() == true);

// Test a null SpanSet
afwGeom::SpanSet nullSpanSet;
BOOST_CHECK(nullSpanSet.isContiguous() == true);
}

BOOST_AUTO_TEST_CASE(SpanSet_testSplit) {
Expand Down Expand Up @@ -142,6 +146,10 @@ BOOST_AUTO_TEST_CASE(SpanSet_testSplit) {
BOOST_CHECK(value == *vectorIterator);
++vectorIterator;
}
// Check for a nullSpanSet
afwGeom::SpanSet nullSpanSet;
auto result3 = nullSpanSet.split();
BOOST_CHECK(*(result3[0]) == nullSpanSet);
}

BOOST_AUTO_TEST_CASE(SpanSet_testShiftedBy) {
Expand All @@ -152,16 +160,26 @@ BOOST_AUTO_TEST_CASE(SpanSet_testShiftedBy) {

BOOST_CHECK(shiftedSpanSet->getBBox().getMinX() == 0);
BOOST_CHECK(shiftedSpanSet->getBBox().getMinY() == 0);

// Try to shift a nullSpanSet
afwGeom::SpanSet nullSpanSet;
auto shiftedNullSpanSet = nullSpanSet.shiftedBy(SSextent);
BOOST_CHECK(shiftedNullSpanSet->size() == 0);
}

BOOST_AUTO_TEST_CASE(SpanSet_testClippedTo) {
afwGeom::Box2I clipBox(afwGeom::Box2I(afwGeom::Point2I(-2, -2), afwGeom::Point2I(2, 2)));
// BBox lower corner shouuld be at -4,-4
auto SpanSetNoClip = afwGeom::SpanSet::spanSetFromShape(4, afwGeom::Stencil::CIRCLE);
auto SpanSetClip = (*SpanSetNoClip).clippedTo(clipBox);
auto SpanSetClip = SpanSetNoClip->clippedTo(clipBox);

BOOST_CHECK(SpanSetClip->getBBox().getMinX() == -2);
BOOST_CHECK(SpanSetClip->getBBox().getMinY() == -2);

// Try clipping a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto clippedNullSpanSet = nullSpanSet.clippedTo(clipBox);
BOOST_CHECK(clippedNullSpanSet->size() == 0);
}

BOOST_AUTO_TEST_CASE(SpanSet_testTransformed) {
Expand All @@ -177,6 +195,11 @@ BOOST_AUTO_TEST_CASE(SpanSet_testTransformed) {

BOOST_CHECK(SpanSetPostScale->getBBox().getMinX() == -4);
BOOST_CHECK(SpanSetPostScale->getBBox().getMinY() == -4);

// Try transforming a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto transformedNullSpanSet = nullSpanSet.transformedBy(transform);
BOOST_CHECK(transformedNullSpanSet->size() == 0);
}

BOOST_AUTO_TEST_CASE(SpanSet_testOverlaps) {
Expand All @@ -186,6 +209,11 @@ BOOST_AUTO_TEST_CASE(SpanSet_testOverlaps) {

BOOST_CHECK(SpanSetNoShift->overlaps(*SpanSetShift) == true);
BOOST_CHECK(SpanSetNoShift->overlaps(*SpanSetShiftFar) == false);

// Try with a nullSpanSet, null spansets should not overlap anything
afwGeom::SpanSet nullSpanSet;
BOOST_CHECK(SpanSetNoShift->overlaps(nullSpanSet) == false);
BOOST_CHECK(nullSpanSet.overlaps(*SpanSetNoShift) == false);
}

BOOST_AUTO_TEST_CASE(SpanSet_testContains) {
Expand All @@ -199,6 +227,11 @@ BOOST_AUTO_TEST_CASE(SpanSet_testContains) {
BOOST_CHECK(SpanSetLarge->contains(*SpanSetSmallFar) == false);
BOOST_CHECK(SpanSetLarge->contains(pointIn) == true);
BOOST_CHECK(SpanSetLarge->contains(pointOut) == false);

// Test with a nullSpanSet
afwGeom::SpanSet nullSpanSet;
BOOST_CHECK(SpanSetLarge->contains(nullSpanSet) == false);
BOOST_CHECK(nullSpanSet.contains(*SpanSetSmall) == false);
}

BOOST_AUTO_TEST_CASE(SpanSet_testComputeCentroid) {
Expand All @@ -207,6 +240,13 @@ BOOST_AUTO_TEST_CASE(SpanSet_testComputeCentroid) {

BOOST_CHECK(center.getX() == 2);
BOOST_CHECK(center.getY() == 2);

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullCenter = nullSpanSet.computeCentroid();
BOOST_CHECK(std::isnan(nullCenter.getX()));
BOOST_CHECK(std::isnan(nullCenter.getY()));

}

BOOST_AUTO_TEST_CASE(SpanSet_testComputeShape) {
Expand All @@ -216,6 +256,13 @@ BOOST_AUTO_TEST_CASE(SpanSet_testComputeShape) {
BOOST_CHECK(quad.getIxx() == 0.4);
BOOST_CHECK(quad.getIyy() == 0.4);
BOOST_CHECK(quad.getIxy() == 0);

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullShape = nullSpanSet.computeShape();
BOOST_CHECK(std::isnan(nullShape.getIxx()));
BOOST_CHECK(std::isnan(nullShape.getIyy()));
BOOST_CHECK(std::isnan(nullShape.getIxy()));
}

BOOST_AUTO_TEST_CASE(SpanSet_testDilate) {
Expand All @@ -226,6 +273,18 @@ BOOST_AUTO_TEST_CASE(SpanSet_testDilate) {

BOOST_CHECK(SpanSetPostDilate->getBBox().getMinX() == -2);
BOOST_CHECK(SpanSetPostDilate->getBBox().getMinY() == -2);

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullSpanSetDialte = nullSpanSet.dilate(1);

BOOST_CHECK(nullSpanSetDialte->getBBox().getMinX() == 0);
BOOST_CHECK(nullSpanSetDialte->getBBox().getMinY() == 0);

auto SpanSetNullDilate = SpanSetPreDilate->dilate(nullSpanSet);
BOOST_CHECK(SpanSetNullDilate->getBBox().getMinX() == -1);
BOOST_CHECK(SpanSetNullDilate->getBBox().getMinY() == -1);

}

BOOST_AUTO_TEST_CASE(SpanSet_testErode) {
Expand All @@ -236,20 +295,32 @@ BOOST_AUTO_TEST_CASE(SpanSet_testErode) {

BOOST_CHECK(SpanSetPostErode->getBBox().getMinX() == -1);
BOOST_CHECK(SpanSetPostErode->getBBox().getMinY() == -1);

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullSpanSetErode = nullSpanSet.erode(1);

BOOST_CHECK(nullSpanSetErode->getBBox().getMinX() == 0);
BOOST_CHECK(nullSpanSetErode->getBBox().getMinY() == 0);

auto SpanSetNullErode = SpanSetPreErode->erode(nullSpanSet);
BOOST_CHECK(SpanSetNullErode->getBBox().getMinX() == -2);
BOOST_CHECK(SpanSetNullErode->getBBox().getMinY() == -2);
}

BOOST_AUTO_TEST_CASE(SpanSet_testFlatten) {
// Test version without output array, as this simply delegates
// Create an array and initialize it to 9
ndarray::Array<int, 2, 2> input = ndarray::allocate(ndarray::makeVector(6, 6));
input.deep() = 9;
int dummyArrayValue = 9;
input.deep() = dummyArrayValue;

// overload a few pixels with the value 1
// overload a few pixels with the different values
input[1][1] = 1;
input[1][2] = 2;
input[2][1] = 3;
input[2][2] = 4;
// Create a spanSet for those points
// Create a SpanSet for those points
std::vector<afwGeom::Span> spanVector = {afwGeom::Span(0, 0, 1), afwGeom::Span(1, 0, 1)};
afwGeom::SpanSet SpnSt(spanVector);
auto flatArr = SpnSt.flatten(input, afwGeom::Point2I(-1, -1));
Expand All @@ -261,6 +332,23 @@ BOOST_AUTO_TEST_CASE(SpanSet_testFlatten) {
BOOST_CHECK(val == inputValue);
++inputValue;
}

// Test with an explicit ouput array
ndarray::Array<int, 1, 1> flatOutput = ndarray::allocate(ndarray::makeVector(4));
flatOutput.deep() = 0;

SpnSt.flatten(flatOutput, input, afwGeom::Point2I(-1, -1));

auto nextInputValue = 1;
for (auto const & val : flatOutput) {
BOOST_CHECK(val == nextInputValue);
++nextInputValue;
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullFlatArray = nullSpanSet.flatten(input, afwGeom::Point2I(-1, -1));
BOOST_CHECK(nullFlatArray.size() == 0);
}

BOOST_AUTO_TEST_CASE(SpanSet_testUnflatten) {
Expand All @@ -279,7 +367,7 @@ BOOST_AUTO_TEST_CASE(SpanSet_testUnflatten) {
BOOST_CHECK(static_cast<int>(arrayShape[0]) == spnSt.getBBox().getHeight());
BOOST_CHECK(static_cast<int>(arrayShape[1]) == spnSt.getBBox().getWidth());

// Loop over each point in the array correspointing to the spanset, minux xy0 (the min of the SpanSet)
// Loop over each point in the array corresponding to the spanset, minus xy0 (the min of the SpanSet)
// verify the array contains the value dummpArrayValue
for (auto const & spn : spnSt) {
for (auto const & pt : spn) {
Expand All @@ -290,6 +378,28 @@ BOOST_AUTO_TEST_CASE(SpanSet_testUnflatten) {
BOOST_CHECK(output[0][2] == 0);
BOOST_CHECK(output[1][0] == 0);
BOOST_CHECK(output[2][2] == 0);

// Test with an array for an output
int outputSize = 6;
ndarray::Array<int, 2, 2> unflatOutput = ndarray::allocate(ndarray::makeVector(outputSize, outputSize));
unflatOutput.deep() = 0;
afwGeom::Point2I outputXY0(spnSt.getBBox().getMinX(), spnSt.getBBox().getMinY());
spnSt.unflatten(unflatOutput, input, outputXY0);

for (int i = 0; i < outputSize; ++i) {
for (int j = 0; j < outputSize; ++j) {
if (spnSt.contains(afwGeom::Point2I(j + outputXY0.getX(), i + outputXY0.getY()))) {
BOOST_CHECK(unflatOutput[i][j] == dummyArrayValue);
} else {
BOOST_CHECK(unflatOutput[i][j] == 0);
}
}
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullUnflatArray = nullSpanSet.unflatten(input);
BOOST_CHECK(nullUnflatArray.size() == 0);
}

std::pair<lsst::afw::image::Mask<lsst::afw::image::MaskPixel>, std::shared_ptr<afwGeom::SpanSet>> populateMask() {
Expand All @@ -312,6 +422,14 @@ BOOST_AUTO_TEST_CASE(SpanSet_testSetMask) {
BOOST_CHECK(mskArray[pt.getY()][pt.getX()] == static_cast<lsst::afw::image::MaskPixel>(2));
}
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
lsst::afw::image::Mask<lsst::afw::image::MaskPixel> nullMask(10, 10);
nullSpanSet.setMask(nullMask, static_cast<lsst::afw::image::MaskPixel>(2));
for (auto iter = nullMask.begin(); iter != nullMask.end(); ++iter){
BOOST_CHECK(*iter == 0);
}
}

BOOST_AUTO_TEST_CASE(SpanSet_testClearMask) {
Expand All @@ -326,6 +444,19 @@ BOOST_AUTO_TEST_CASE(SpanSet_testClearMask) {
BOOST_CHECK(mskArray[pt.getY()][pt.getX()] == static_cast<lsst::afw::image::MaskPixel>(0));
}
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
lsst::afw::image::Mask<lsst::afw::image::MaskPixel> nullMask(10, 10);
int maskValue = 9;
for (auto iter = nullMask.begin(); iter != nullMask.end(); ++iter){
*iter = static_cast<lsst::afw::image::MaskPixel>(maskValue);
}
nullSpanSet.clearMask(nullMask, static_cast<lsst::afw::image::MaskPixel>(maskValue));

for (auto iter = nullMask.begin(); iter != nullMask.end(); ++iter){
BOOST_CHECK(*iter == static_cast<lsst::afw::image::MaskPixel>(maskValue));
}
}

std::pair<std::shared_ptr<afwGeom::SpanSet>, std::shared_ptr<afwGeom::SpanSet>> makeOverlapSpanSets() {
Expand All @@ -351,6 +482,14 @@ BOOST_AUTO_TEST_CASE(SpanSet_testIntersect) {
BOOST_CHECK(spn.getMaxX() == 4);
++yStart;
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullAsOther = firstSS->intersect(nullSpanSet);
BOOST_CHECK(nullAsOther->size() == 0);

auto spanSetAsOther = nullSpanSet.intersect(*firstSS);
BOOST_CHECK(spanSetAsOther->size() == 0);
}

BOOST_AUTO_TEST_CASE(SpanSet_testIntersectNot) {
Expand All @@ -368,6 +507,16 @@ BOOST_AUTO_TEST_CASE(SpanSet_testIntersectNot) {
BOOST_CHECK(spn.getMaxX() == 4);
++yStart;
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullAsOther = firstSS->intersectNot(nullSpanSet);
// A SpanSet intersectedNot with a null SpanSet should just return itself
BOOST_CHECK(*firstSS == *nullAsOther);

// A null SpanSet intersectedNot with anything should return a null SpanSet
auto spanSetAsOther = nullSpanSet.intersectNot(*firstSS);
BOOST_CHECK(nullSpanSet == *spanSetAsOther);
}

BOOST_AUTO_TEST_CASE(SpanSet_testUnion) {
Expand All @@ -385,6 +534,16 @@ BOOST_AUTO_TEST_CASE(SpanSet_testUnion) {
BOOST_CHECK(spn.getMaxX() == 4);
++yStart;
}

// Test with a null SpanSet
afwGeom::SpanSet nullSpanSet;
auto nullAsOther = firstSS->union_(nullSpanSet);
// A null SpanSet unioned with another SpanSet should return the other SpanSet
BOOST_CHECK(*nullAsOther == *firstSS);

auto spanSetAsOther = nullSpanSet.union_(*firstSS);
BOOST_CHECK(*spanSetAsOther == *firstSS);

}

BOOST_AUTO_TEST_CASE(SpanSet_testEquality) {
Expand All @@ -402,23 +561,37 @@ BOOST_AUTO_TEST_CASE(SpanSet_testEquality) {
BOOST_AUTO_TEST_CASE(SpanSet_testFunctor) {
// Test the remaining types of functors. Above code has tested ndarray functors
// need to test, constants, iterators, Images
namespace afwImage = lsst::afw::image;
int imageDim = 6;
int initialValue = 0;
lsst::afw::image::Image<int> imageObject(imageDim, imageDim, initialValue);
afwImage::Image<int> imageObject(imageDim, imageDim, initialValue);
std::vector<int> vecObject(imageDim*imageDim, initialValue);

int spanRadius = 2;
auto SSShape = afwGeom::SpanSet::spanSetFromShape(spanRadius,
afwGeom::Stencil::BOX)->shiftedBy(spanRadius,
spanRadius);
// use a constant as a test of constantGetter
namespace afwImage = lsst::afw::image;
int dataValue = 6;
SSShape->applyFunctor([](afwGeom::Point2I pt, afwImage::Image<int>::Pixel & out, int in){ out = in; },
imageObject, dataValue);
SSShape->applyFunctor([](afwGeom::Point2I, int & out, int in){out = in;},
vecObject.begin(), dataValue);

// Check on the point input of a functor
std::vector<afwGeom::Point2I> capturedPoints;
capturedPoints.reserve(SSShape->getArea());
SSShape->applyFunctor([](afwGeom::Point2I point, afwGeom::Point2I & out){out = point;},
capturedPoints.begin());

auto capturedPointsIter = capturedPoints.begin();
for (auto const & spn : *SSShape) {
for (auto const & pnt : spn) {
BOOST_CHECK(pnt == *capturedPointsIter);
++capturedPointsIter;
}
}

// Check the Image values
auto bounds = SSShape->getBBox();
for (int i = bounds.getMinY(); i <= bounds.getMaxY(); ++i) {
Expand All @@ -439,4 +612,27 @@ BOOST_AUTO_TEST_CASE(SpanSet_testFunctor) {
BOOST_CHECK(vecObject[i] == initialValue);
}
}

// Test null SpanSets
lsst::afw::image::Image<int> nullImageObject(imageDim, imageDim, initialValue);
std::vector<int> nullVecObject(imageDim*imageDim, initialValue);
afwGeom::SpanSet nullSpanSet;

nullSpanSet.applyFunctor([]
(afwGeom::Point2I pt, afwImage::Image<int>::Pixel & out, int in)
{out = in;},
imageObject, dataValue);
nullSpanSet.applyFunctor([]
(afwGeom::Point2I, int & out, int in)
{out = in;},
vecObject.begin(), dataValue);

// nullSpanSet should not have changed any values
for (auto const & nullImageValue: nullImageObject) {
BOOST_CHECK(nullImageValue == initialValue);
}

for (auto const & nullVecValue : nullVecObject) {
BOOST_CHECK(nullVecValue == initialValue);
}
}

0 comments on commit c8f8e1e

Please sign in to comment.