Debian Codegraph @

xcode/ObjectiveChipmunkTests/BodyTest.m

raw
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
24 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
25
26 @interface UpdateTestBody : ChipmunkBody
27
28 @property(nonatomic, assign) BOOL velocityUpdated;
29 @property(nonatomic, assign) BOOL positionUpdated;
30
31 @end
32
33
34 @implementation UpdateTestBody
35
36 -(void)updateVelocity:(cpFloat)dt gravity:(cpVect)gravity damping:(cpFloat)damping
37 {
38 self.velocityUpdated = TRUE;
39 }
40
41 -(void)updatePosition:(cpFloat)dt
42 {
43 self.positionUpdated = TRUE;
44 }
45
46 @end
47
48
49 @interface BodyTest : XCTestCase {}
50 @end
51
52 @implementation BodyTest
53
54 #define TestAccessors(o, p, v) o.p = v; XCTAssertEqual(o.p, v, @"");
55 #define TestAccessorsV(o, p, v) o.p = v; XCTAssertTrue(cpveql(o.p, v), @"");
56
57 -(void)testProperties {
58 ChipmunkBody *body = [ChipmunkBody bodyWithMass:123 andMoment:123];
59 XCTAssertEqual(body.mass, (cpFloat)123, @"");
60 XCTAssertEqual(body.moment, (cpFloat)123, @"");
61
62 XCTAssertNotEqual(body.body, NULL, @"");
63 XCTAssertNil(body.userData, @"");
64
65 TestAccessors(body, userData, @"object");
66 TestAccessors(body, mass, (cpFloat)5);
67 TestAccessors(body, moment, (cpFloat)5);
68 TestAccessorsV(body, position, cpv(5,6));
69 TestAccessorsV(body, velocity, cpv(5,6));
70 TestAccessorsV(body, force, cpv(5,6));
71 TestAccessors(body, angle, (cpFloat)5);
72 TestAccessors(body, angularVelocity, (cpFloat)5);
73 TestAccessors(body, torque, (cpFloat)5);
74
75 body.angle = 0;
76 body.angle = CP_PI;
77 body.angle = CP_PI/2.0f;
78 // TODO transform tests
79
80 XCTAssertFalse(body.isSleeping, @"");
81 XCTAssertEqual(body.type, CP_BODY_TYPE_DYNAMIC, @"");
82 }
83
84 -(void)testBasic {
85 ChipmunkBody *body = [ChipmunkBody bodyWithMass:1 andMoment:1];
86
87 [body applyForce:cpv(0,1) atWorldPoint:cpv(1,0)];
88 XCTAssertTrue(body.force.y > 0, @"");
89 XCTAssertTrue(body.force.x == 0, @"");
90 XCTAssertTrue(body.torque > 0, @"");
91
92 body.force = cpvzero;
93 body.torque = 0.0f;
94 XCTAssertTrue(cpveql(body.force, cpvzero), @"");
95 XCTAssertEqual(body.torque, (cpFloat)0, @"");
96
97 [body applyImpulse:cpv(0,1) atWorldPoint:cpv(1,0)];
98 XCTAssertTrue(body.velocity.y > 0, @"");
99 XCTAssertTrue(body.velocity.x == 0, @"");
100 XCTAssertTrue(body.angularVelocity > 0, @"");
101 }
102
103 -(void)testMisc {
104 ChipmunkBody *staticBody = [ChipmunkBody staticBody];
105 XCTAssertFalse(staticBody.isSleeping, @"");
106 XCTAssertEqual(staticBody.type, CP_BODY_TYPE_STATIC, @"");
107
108 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
109 space.sleepTimeThreshold = 1.0;
110
111 ChipmunkBody *body = [ChipmunkBody bodyWithMass:1 andMoment:1];
112 [space add:body];
113 XCTAssertFalse(body.isSleeping, @"");
114 XCTAssertEqual(body.type, CP_BODY_TYPE_DYNAMIC, @"");
115
116 [body sleep];
117 XCTAssertTrue(body.isSleeping, @"");
118 [space release];
119 }
120
121 -(void)testUpdate {
122 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
123 UpdateTestBody *body = [space add:[UpdateTestBody bodyWithMass:1.0 andMoment:1.0]];
124
125 XCTAssertTrue(body.body->velocity_func != cpBodyUpdateVelocity, @"Did not set velocity integration callback.");
126 XCTAssertTrue(body.body->position_func != cpBodyUpdatePosition, @"Did not set position integration callback.");
127
128 [space step:1.0];
129 XCTAssertTrue(body.velocityUpdated, @"Did not call velocity integration callback.");
130 XCTAssertTrue(body.positionUpdated, @"Did not call position integration callback.");
131
132 [space release];
133 }
134
135 -(void)testSpace {
136 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
137 ChipmunkBody *body = [ChipmunkBody bodyWithMass:1.0 andMoment:1.0];
138
139 XCTAssertNil(body.space, @"body.space should be nil.");
140
141 [space add:body];
142 XCTAssertEqual(body.space, space, @"body.space should be space.");
143
144 [space release];
145 }
146
147 -(void)testTimeSteping {
148 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
149 space.gravity = cpv(0, -1);
150
151 ChipmunkBody *dynamicBody = [space add:[ChipmunkBody bodyWithMass:1.0 andMoment:1.0]];
152 ChipmunkBody *staticBody = [space add:[ChipmunkBody staticBody]];
153 ChipmunkBody *kinematicBody = [space add:[ChipmunkBody kinematicBody]];
154 kinematicBody.velocity = cpv(1, 0);
155
156 [space step:1.0];
157
158 // Should have accelerated but not moved.
159 XCTAssert(cpveql(dynamicBody.position, cpvzero), @"");
160 XCTAssert(cpveql(dynamicBody.velocity, cpv(0, -1)), @"");
161
162 // Should not have moved nor accelerated.
163 XCTAssert(cpveql(staticBody.position, cpvzero), @"");
164 XCTAssert(cpveql(staticBody.velocity, cpvzero), @"");
165
166 // Should have moved right but not accelerated downward.
167 XCTAssert(cpveql(kinematicBody.position, cpv(1, 0)), @"");
168 XCTAssert(cpveql(kinematicBody.velocity, cpv(1, 0)), @"");
169
170 [space step:1.0];
171
172 // Should have moved and accelerated more.
173 XCTAssert(cpveql(dynamicBody.position, cpv(0, -1)), @"");
174 XCTAssert(cpveql(dynamicBody.velocity, cpv(0, -2)), @"");
175
176 // Still should not have moved nor accelerated.
177 XCTAssert(cpveql(staticBody.position, cpvzero), @"");
178 XCTAssert(cpveql(staticBody.velocity, cpvzero), @"");
179
180 // Should have moved right more but still not accelerated downward.
181 XCTAssert(cpveql(kinematicBody.position, cpv(2, 0)), @"");
182 XCTAssert(cpveql(kinematicBody.velocity, cpv(1, 0)), @"");
183
184 [space release];
185 }
186
187 @end