From 2905cd1586b10831e24c3b2a8d5cd893223b6dc0 Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Thu, 12 Jul 2012 00:19:10 +0700 Subject: [PATCH] Linked methods from CPGeometry to CGGeometry + added unit tests --- Foundation/CPGeometry.j | 92 ++++--------------------------- Tests/Foundation/CPGeometryTest.j | 52 ++++++++++++++++- 2 files changed, 61 insertions(+), 83 deletions(-) diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index 7e7fa2079..e98ba26d8 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -33,22 +33,21 @@ CPMakePoint = CGPointMake; CPMakeSize = CGSizeMake; CPMakeRect = CGRectMake; +CPPointCreateCopy = CGPointMakeCopy; + +CPPointEqualToPoint = CGPointEqualToPoint; +CPRectEqualToRect = CGRectEqualToRect; + +CPRectIsEmpty = CGRectIsEmpty; + +CPRectContainsRect = CGRectContainsRect; +CPRectIntersection = CGRectIntersection; + /*! @addtogroup appkit @{ */ -/*! - Creates a copy of a specified point and returns the copy - @group CGPoint - @param the point to be copied - @return CGPoint the copy of the provided CGPoint - */ -function CPPointCreateCopy(aPoint) -{ - return { x: aPoint.x, y: aPoint.y }; -} - /*! Makes a CGPoint object out of two numbers provided as arguments @group CGPoint @@ -86,26 +85,6 @@ function CPRectIntegral(aRect) alert("CPRectIntegral unimplemented"); } -/*! - Returns the intersection of the two provided rectangles as a new rectangle - @group CGRect - @param lhsRect the first rectangle used for calculation - @param rhsRect the second rectangle used for calculation - @return CGRect the intersection of the two rectangles -*/ -function CPRectIntersection(lhsRect, rhsRect) -{ - var intersection = CPRectMake( - MAX(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), - MAX(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), - 0, 0); - - intersection.size.width = MIN(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)) - CPRectGetMinX(intersection); - intersection.size.height = MIN(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)) - CPRectGetMinY(intersection); - - return CPRectIsEmpty(intersection) ? CPRectMakeZero() : intersection; -} - /*! Creates a copy of the provided rectangle @group CGRect @@ -226,45 +205,6 @@ function CPRectContainsPoint(aRect, aPoint) aPoint.y < CPRectGetMaxY(aRect); } -/*! - Returns a \c BOOL indicating whether CGRect \c possibleOuter - contains CGRect \c possibleInner. - @group CGRect - @param possibleOuter the CGRect to test if \c possibleInner is inside of - @param possibleInner the CGRect to test if it fits inside \c possibleOuter. - @return BOOL \c YES if \c possibleInner fits inside \c possibleOuter. -*/ -function CPRectContainsRect(possibleOuter, possibleInner) -{ - return CGRectContainsRect(possibleOuter, possibleInner); -} - -/*! - Tests whether the two CGPoints are equal to each other by comparing their - \c x and \c y members. - @group @CGPoint - @param lhsPoint the first CGPoint to check - @param rhsPoint the second CGPoint to check - @return BOOL \c YES if the two points have the same x's, and the same y's. -*/ -function CPPointEqualToPoint(lhsPoint, rhsPoint) -{ - return lhsPoint.x == rhsPoint.x && lhsPoint.y == rhsPoint.y; -} - -/*! - Test whether the two CGRects have the same origin and size - @group CGRect - @param lhsRect the first CGRect to compare - @param rhsRect the second CGRect to compare - @return BOOL \c YES if the two rectangles have the same origin and size. \c NO, otherwise. -*/ -function CPRectEqualToRect(lhsRect, rhsRect) -{ - return CPPointEqualToPoint(lhsRect.origin, rhsRect.origin) && - CPSizeEqualToSize(lhsRect.size, rhsRect.size); -} - /*! @group CGRect @param aRect a CGRect @@ -357,18 +297,6 @@ function CPRectIntersectsRect(lhsRect, rhsRect) return !CPRectIsEmpty(CPRectIntersection(lhsRect, rhsRect)); } -/*! - Returns \c YES if the CGRect has no area. - The test is performed by checking if the width and height are both zero. - @group CGRect - @param aRect the CGRect to test - @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. -*/ -function CPRectIsEmpty(aRect) -{ - return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; -} - /*! Returns \c YES if the CGRect has no area. The test is performed by checking if the width and height are both zero. diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index 50f91828e..7eb30c800 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -1,4 +1,4 @@ -@import +@import @implementation CPGeometryTest : OJTestCase { @@ -14,4 +14,54 @@ [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 1, 1))]; } +-(void)testCPPointEqualToPoint +{ + [self assertTrue:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(0, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(0, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 0), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 0))]; +} + +-(void)testCPRectEqualToRect +{ + [self assertTrue:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(50, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(50, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 50, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 50, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 50, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 50), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 50))]; +} + +-(void)textCPRectIsEmpty +{ + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsEmpty(CGRectMake(0, 0, 10, 10))]; +} + +-(void)testCPRectIntersection +{ + var lhsRect = CGRectMake(0, 0, 20, 20), + rhsRect = CGRectMake(10, 10, 20, 20), + intersectionResultRect = CGRectMake(10, 10, 10, 10); + + [self assertTrue:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; + intersectionResultRect.origin.x = 30; + intersectionResultRect.origin.y = 30; + [self assertFalse:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; +} + +-(void)testCPPointCreateCopy +{ + var point = CGPointMake(1, 1); + [self assertTrue:CPPointEqualToPoint(CPPointCreateCopy(point),point)]; + [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; +} + @end