Skip to content

Commit

Permalink
MathUtils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jigish Patel committed Oct 16, 2012
1 parent 8b02824 commit f4a50e3
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Slate.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
3B9B301E151BFC330069D95E /* SwitchAppView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B9B301D151BFC330069D95E /* SwitchAppView.m */; };
3B9B3021151C830F0069D95E /* SwitchAppQuittingOverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B9B3020151C830E0069D95E /* SwitchAppQuittingOverlayView.m */; };
3BA1BA58162DD3630026774E /* TestNSString+Levenshtein.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA1BA57162DD3630026774E /* TestNSString+Levenshtein.m */; };
3BA1BA5C162DD7190026774E /* TestMathUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA1BA5B162DD7190026774E /* TestMathUtils.m */; };
3BA7485913B1D0F500CFA792 /* MathUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA7485813B1D0F500CFA792 /* MathUtils.m */; };
3BA85C9A16276DAB00FAAF0B /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BA85C9916276DAB00FAAF0B /* Sparkle.framework */; };
3BA85C9B16276E9E00FAAF0B /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3BA85C9916276DAB00FAAF0B /* Sparkle.framework */; };
Expand Down Expand Up @@ -150,6 +151,8 @@
3B9B3020151C830E0069D95E /* SwitchAppQuittingOverlayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwitchAppQuittingOverlayView.m; sourceTree = "<group>"; };
3BA1BA56162DD3630026774E /* TestNSString+Levenshtein.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "TestNSString+Levenshtein.h"; sourceTree = "<group>"; };
3BA1BA57162DD3630026774E /* TestNSString+Levenshtein.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "TestNSString+Levenshtein.m"; sourceTree = "<group>"; };
3BA1BA5A162DD7190026774E /* TestMathUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestMathUtils.h; sourceTree = "<group>"; };
3BA1BA5B162DD7190026774E /* TestMathUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestMathUtils.m; sourceTree = "<group>"; };
3BA7485713B1D0F500CFA792 /* MathUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MathUtils.h; sourceTree = "<group>"; };
3BA7485813B1D0F500CFA792 /* MathUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MathUtils.m; sourceTree = "<group>"; };
3BA85C9916276DAB00FAAF0B /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -266,6 +269,15 @@
name = Categories;
sourceTree = "<group>";
};
3BA1BA59162DD6F20026774E /* Common */ = {
isa = PBXGroup;
children = (
3BA1BA5A162DD7190026774E /* TestMathUtils.h */,
3BA1BA5B162DD7190026774E /* TestMathUtils.m */,
);
name = Common;
sourceTree = "<group>";
};
3BC9E97015005F5F002FD1FA /* Categories */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -482,6 +494,7 @@
isa = PBXGroup;
children = (
3B8B5524161A1FC600E21A6C /* Categories */,
3BA1BA59162DD6F20026774E /* Common */,
47C4A2BB1384E8890066B6DE /* SlateTests.h */,
47C4A2BD1384E8890066B6DE /* SlateTests.m */,
47C4A2B51384E8890066B6DE /* Supporting Files */,
Expand Down Expand Up @@ -672,6 +685,7 @@
47C4A2BE1384E8890066B6DE /* SlateTests.m in Sources */,
3B8B5527161A1FDF00E21A6C /* TestNSString+Indicies.m in Sources */,
3BA1BA58162DD3630026774E /* TestNSString+Levenshtein.m in Sources */,
3BA1BA5C162DD7190026774E /* TestMathUtils.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 25 additions & 0 deletions SlateTests/TestMathUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// TestMathUtils.h
// Slate
//
// Created by Jigish Patel on 10/16/12.
// Copyright 2011 Jigish Patel. All rights reserved.
//
// This program 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.
//
// This program 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 this program. If not, see http://www.gnu.org/licenses

#import <SenTestingKit/SenTestingKit.h>

@interface TestMathUtils : SenTestCase

@end
92 changes: 92 additions & 0 deletions SlateTests/TestMathUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// TestMathUtils.m
// Slate
//
// Created by Jigish Patel on 10/16/12.
// Copyright 2011 Jigish Patel. All rights reserved.
//
// This program 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.
//
// This program 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 this program. If not, see http://www.gnu.org/licenses

#import "TestMathUtils.h"
#import "MathUtils.h"

@implementation TestMathUtils

- (void)testIsRectBiggerThan {
NSRect a = NSMakeRect(0, 0, 100, 100);
NSRect b = NSMakeRect(0, 0, 200, 90);
NSRect c = NSMakeRect(0, 0, 90, 200);
NSRect d = NSMakeRect(0, 0, 200, 200);
NSRect e = NSMakeRect(0, 0, 200, 1);
NSRect f = NSMakeRect(0, 0, 1, 200);
NSRect g = NSMakeRect(1000, 1000, 1, 1);
NSRect h = NSMakeRect(1000, 1000, 101, 101);
STAssertFalse([MathUtils isRect:a biggerThan:b], @"0,0,100,100 should be smaller than 0,0,200,90");
STAssertFalse([MathUtils isRect:a biggerThan:c], @"0,0,100,100 should be smaller than 0,0,90,200");
STAssertFalse([MathUtils isRect:a biggerThan:d], @"0,0,100,100 should be smaller than 0,0,200,200");
STAssertTrue([MathUtils isRect:a biggerThan:e], @"0,0,100,100 should be bigger than 0,0,200,1");
STAssertTrue([MathUtils isRect:a biggerThan:f], @"0,0,100,100 should be bigger than 0,0,1,200");
STAssertTrue([MathUtils isRect:a biggerThan:g], @"0,0,100,100 should be bigger than 1000,1000,1,1");
STAssertFalse([MathUtils isRect:a biggerThan:h], @"0,0,100,100 should be smaller than 1000,1000,101,101");
}

- (void)testFlipYCoordinateOfRectWithReference {
NSRect a = NSMakeRect(0, 0, 100, 100);
NSRect b = NSMakeRect(0, 0, 1000, 1000);
NSRect flipped = [MathUtils flipYCoordinateOfRect:a withReference:b];
STAssertTrue(NSEqualRects(flipped, NSMakeRect(0,900,100,100)), @"Flipping should work");
a = NSMakeRect(0,-100, 100, 1500);
b = NSMakeRect(0, 0, 1000, 1000);
flipped = [MathUtils flipYCoordinateOfRect:a withReference:b];
STAssertTrue(NSEqualRects(flipped, NSMakeRect(0,-400,100,1500)), @"Flipping should work");
}

- (void)testScaleRectFactor {
NSRect a = NSMakeRect(100, 200, 100, 200);
NSRect scaled = [MathUtils scaleRect:a factor:2.0];
STAssertTrue(NSEqualRects(scaled, NSMakeRect(100,200,200,400)), @"Scaling should work");
a = NSMakeRect(100, 200, 100, 200);
scaled = [MathUtils scaleRect:a factor:0.5];
STAssertTrue(NSEqualRects(scaled, NSMakeRect(100,200,50,100)), @"Scaling should work");
a = NSMakeRect(100, 200, 100, 200);
scaled = [MathUtils scaleRect:a factor:2.5];
STAssertTrue(NSEqualRects(scaled, NSMakeRect(100,200,250,500)), @"Scaling should work");
}

- (void)testWeightedIntersectionOfAndWeight {
NSRect a = NSMakeRect(0, 0, 100, 100);
NSRect b = NSMakeRect(1000, 1000, 100, 100);
NSRect c = NSMakeRect(0, 0, 50, 50);
NSRect d = NSMakeRect(50, 50, 100, 100);
NSRect intersection = [MathUtils weightedIntersectionOf:a and:b weight:1];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 0, 0)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:b weight:2];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 0, 0)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:b weight:0.5];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 0, 0)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:c weight:1];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 50, 50)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:c weight:2];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 100, 100)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:c weight:0.5];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(0, 0, 25, 25)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:d weight:1];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(50, 50, 50, 50)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:d weight:2];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(50, 50, 100, 100)), @"Weighted Intersection should work");
intersection = [MathUtils weightedIntersectionOf:a and:d weight:0.5];
STAssertTrue(NSEqualRects(intersection, NSMakeRect(50, 50, 25, 25)), @"Weighted Intersection should work");
}

@end

0 comments on commit f4a50e3

Please sign in to comment.