Question

I have a method that is performing some arithmetic on a set of UIViews and their transformations. The unit test is expecting a specific answer; the program is kicking out a answer that is off by .0001 which just sounds like a minor rounding issue that I can deal with. What is the best way to deal with this? Is there a way to be less strict? Should I get pickier about the comparison and expect them to match perfectly?

The relevant code:

//the unit test
CGRect rect0 = CGRectMake(100, 100, 100, 100);
CGRect rect1 = CGRectMake(111, 111, 111, 111);
CGRect rect2 = CGRectMake(-200, -200, 2000, 2000);
CGRect rect3 = CGRectMake(333, -333, 333, 3333);
CGRect rect4 = CGRectMake(4, 4, 4, -4);
CGRect rect5 = CGRectMake(-54, 55, 55, -5555);

CGAffineTransform trans0 = CGAffineTransformIdentity;
CGAffineTransform trans1 = CGAffineTransformIdentity;
CGAffineTransform trans2 = CGAffineTransformMakeScale(2, 2);
CGAffineTransform trans3 = CGAffineTransformMakeScale(-5, 20);
CGAffineTransform trans4 = CGAffineTransformMakeRotation(M_PI / 2);
CGAffineTransform trans5 = CGAffineTransformMakeRotation(34);

CGRect rect0_1 = CGRectUnion(rect0, rect1);
CGRect rect0_2 = CGRectUnion(rect0, CGRectApplyAffineTransform(rect2, trans2));
CGRect rect0_3 = CGRectUnion(rect0, CGRectApplyAffineTransform(rect3, trans3));
CGRect rect0_4 = CGRectUnion(rect0, CGRectApplyAffineTransform(rect4, trans4));
CGRect rect0_5 = CGRectUnion(rect0, CGRectApplyAffineTransform(rect5, trans5));

.
.
.

rectTest = CGRectZero;
rectExpected = rect0_5;
STAssertNoThrow(rectTest = self.testCanvas.selectionBounds, @"Exception when retrieving selection bounds");
STAssertTrue(CGRectEqualToRect(rectTest, rectExpected), @"Selection bounds not correct: %@ vs %@", NSStringFromCGRect(rectTest), NSStringFromCGRect(rectExpected));

//the relevant method
- (CGRect)selectionBounds {
    CGRect selectionRect = CGRectZero;
    CGRect layerFrame;
    NSSet *layers = [self selectedLayers]; //TODO should this be optomized out?
    for(UIView *layer in layers) {
        CGPoint center = CGPointApplyAffineTransform(layer.center, layer.transform);
        layerFrame = layer.bounds;
        layerFrame = CGRectApplyAffineTransform(layerFrame, layer.transform);
        layerFrame = CGRectOffset(layerFrame, center.x - CGRectGetMidX(layerFrame), center.y - CGRectGetMidY(layerFrame));
        if(CGRectIsEmpty(selectionRect)) {
            selectionRect = layerFrame;
        } else {
            selectionRect = CGRectUnion(selectionRect, layerFrame);
        }
    }
    return selectionRect;
}

//the output
[snip]: error: -[CanvasTests testSelectionBounds] : "CGRectEqualToRect(rectTest, rectExpected)" should be true. Selection bounds not correct: {{-29.9481, -75.2419}, {2985.73, 4742.91}} vs {{-29.9481, -75.2418}, {2985.73, 4742.91}}
Was it helpful?

Solution

What is the best way to deal with this? Is there a way to be less strict?

write a comparison function which allows suitable tolerance.

Should I get pickier about the comparison and expect them to match perfectly?

in this case, i would say that the tolerance/difference you observe is acceptable (after all, it's fp, and an deviation you have said stated is acceptable).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top