| 1 |
/* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
* of this software and associated documentation files (the "Software"), to deal |
| 5 |
* in the Software without restriction, including without limitation the rights |
| 6 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
* copies of the Software, and to permit persons to whom the Software is |
| 8 |
* furnished to do so, subject to the following conditions: |
| 9 |
* |
| 10 |
* The above copyright notice and this permission notice shall be included in |
| 11 |
* all copies or substantial portions of the Software. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
* SOFTWARE. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <XCTest/XCTest.h> |
| 23 |
#import "ObjectiveChipmunk/ObjectiveChipmunk.h" |
| 24 |
|
| 25 |
@interface ShapeTest : XCTestCase {} |
| 26 |
@end |
| 27 |
|
| 28 |
@implementation ShapeTest |
| 29 |
|
| 30 |
#define AssertEqualStruct(a, b, m) {typeof(a) __a = a; typeof(b) __b = b; XCTAssertTrue(memcmp(&__a, &__b, sizeof(a)) == 0, m);} |
| 31 |
#define TestAccessors(o, p, v) o.p = v; XCTAssertEqual(o.p, v, @""); |
| 32 |
#define TestAccessorsV(o, p, v) o.p = v; XCTAssertTrue(cpveql(o.p, v), @""); |
| 33 |
#define TestAccessorsS(o, p, v) o.p = v; AssertEqualStruct(o.p, v, @""); |
| 34 |
|
| 35 |
static void |
| 36 |
testPropertiesHelper(id self, ChipmunkBody *body, ChipmunkShape *shape) |
| 37 |
{ |
| 38 |
XCTAssertNotEqual(shape.shape, NULL, @""); |
| 39 |
XCTAssertEqual(body, shape.body, @""); |
| 40 |
XCTAssertNil(shape.userData, @""); |
| 41 |
XCTAssertFalse(shape.sensor, @""); |
| 42 |
XCTAssertEqual(shape.elasticity, (cpFloat)0, @""); |
| 43 |
XCTAssertEqual(shape.friction, (cpFloat)0, @""); |
| 44 |
XCTAssertTrue(cpveql(shape.surfaceVelocity, cpvzero), @""); |
| 45 |
XCTAssertNil(shape.collisionType, @""); |
| 46 |
AssertEqualStruct(shape.filter, CP_SHAPE_FILTER_ALL, @""); |
| 47 |
|
| 48 |
cpBB bb = [shape cacheBB]; |
| 49 |
AssertEqualStruct(shape.bb, bb, @""); |
| 50 |
|
| 51 |
TestAccessors(shape, userData, @"object"); |
| 52 |
TestAccessors(shape, sensor, YES); |
| 53 |
TestAccessors(shape, elasticity, (cpFloat)0); |
| 54 |
TestAccessors(shape, friction, (cpFloat)0); |
| 55 |
TestAccessorsV(shape, surfaceVelocity, cpv(5,6)); |
| 56 |
TestAccessors(shape, collisionType, @"type"); |
| 57 |
cpShapeFilter f = {@"group", 456, 789}; |
| 58 |
TestAccessorsS(shape, filter, f); |
| 59 |
} |
| 60 |
|
| 61 |
-(void)testProperties { |
| 62 |
ChipmunkBody *body = [ChipmunkBody bodyWithMass:1 andMoment:1]; |
| 63 |
|
| 64 |
ChipmunkCircleShape *circle = [ChipmunkCircleShape circleWithBody:body radius:1 offset:cpv(1,2)]; |
| 65 |
testPropertiesHelper(self, body, circle); |
| 66 |
XCTAssertEqual(circle.radius, (cpFloat)1, @""); |
| 67 |
XCTAssertTrue(cpveql(circle.offset, cpv(1,2)), @""); |
| 68 |
|
| 69 |
XCTAssertTrue([circle pointQuery:cpv(1,2)].distance <= 0.0f, @""); |
| 70 |
XCTAssertTrue([circle pointQuery:cpv(1,2.9)].distance <= 0.0f, @""); |
| 71 |
XCTAssertFalse([circle pointQuery:cpv(1,3.1)].distance <= 0.0f, @""); |
| 72 |
|
| 73 |
|
| 74 |
ChipmunkSegmentShape *segment = [ChipmunkSegmentShape segmentWithBody:body from:cpvzero to:cpv(1,0) radius:1]; |
| 75 |
testPropertiesHelper(self, body, segment); |
| 76 |
XCTAssertTrue(cpveql(segment.a, cpvzero), @""); |
| 77 |
XCTAssertTrue(cpveql(segment.b, cpv(1,0)), @""); |
| 78 |
XCTAssertTrue(cpveql(segment.normal, cpv(0,-1)), @""); |
| 79 |
|
| 80 |
XCTAssertTrue([segment pointQuery:cpvzero].distance <= 0.0f, @""); |
| 81 |
XCTAssertTrue([segment pointQuery:cpv(1,0)].distance <= 0.0f, @""); |
| 82 |
XCTAssertTrue([segment pointQuery:cpv(0.5, 0.5)].distance <= 0.0f, @""); |
| 83 |
XCTAssertFalse([segment pointQuery:cpv(0,3)].distance <= 0.0f, @""); |
| 84 |
|
| 85 |
ChipmunkPolyShape *poly = [ChipmunkPolyShape boxWithBody:body width:10 height:10 radius:0.0f]; |
| 86 |
testPropertiesHelper(self, body, poly); |
| 87 |
XCTAssertTrue([poly pointQuery:cpv(0,0)].distance <= 0.0f, @""); |
| 88 |
XCTAssertTrue([poly pointQuery:cpv(3,3)].distance <= 0.0f, @""); |
| 89 |
XCTAssertFalse([poly pointQuery:cpv(-10,0)].distance <= 0.0f, @""); |
| 90 |
|
| 91 |
// TODO should add segment query tests |
| 92 |
} |
| 93 |
|
| 94 |
-(void)testSpace { |
| 95 |
// TODO |
| 96 |
} |
| 97 |
|
| 98 |
@end |