Debian Codegraph @

xcode/ObjectiveChipmunkTests/CallbacksTest.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 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
24
25 @interface CallbacksTest : XCTestCase {}
26 @end
27
28 @implementation CallbacksTest
29
30 // TODO test callbacks trigger
31 // TODO test reject from begin
32 // TODO test reject from pre-solve
33 // TODO test sensors
34 // TODO test first collision
35 // TODO test post-step callbacks
36
37 static cpBool
38 Begin(cpArbiter *arb, cpSpace *space, NSMutableString *string){
39 [string appendString:@"Begin-"];
40
41 return cpTrue;
42 }
43
44 static cpBool
45 PreSolve(cpArbiter *arb, cpSpace *space, NSMutableString *string){
46 [string appendString:@"PreSolve-"];
47
48 return cpTrue;
49 }
50
51 static void
52 PostSolve(cpArbiter *arb, cpSpace *space, NSMutableString *string){
53 [string appendString:@"PostSolve-"];
54 }
55
56 static void
57 Separate(cpArbiter *arb, cpSpace *space, NSMutableString *string){
58 [string appendString:@"Separate-"];
59 }
60
61 static void
62 testHandlersHelper(id self, bool separateByRemove, bool enableContactGraph){
63 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
64 space.collisionBias = 1.0f;
65
66 cpFloat radius = 5;
67
68 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
69 body1.position = cpv(0*radius*1.5,0);
70
71 [space add:[ChipmunkCircleShape circleWithBody:body1 radius:radius offset:cpvzero]];
72
73 ChipmunkBody *body2 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
74 body2.position = cpv(1*radius*1.5,0);
75
76 ChipmunkShape *shape2 = [space add:[ChipmunkCircleShape circleWithBody:body2 radius:radius offset:cpvzero]];
77
78 NSMutableString *string = [NSMutableString string];
79
80 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space.space, nil, nil);
81 handler->beginFunc = (cpCollisionBeginFunc)Begin,
82 handler->preSolveFunc = (cpCollisionPreSolveFunc)PreSolve,
83 handler->postSolveFunc = (cpCollisionPostSolveFunc)PostSolve,
84 handler->separateFunc = (cpCollisionSeparateFunc)Separate,
85 handler->userData = string;
86
87 // Test for separate callback when moving:
88 [space step:0.1];
89 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-", @"");
90
91 [space step:0.1];
92 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-PostSolve-", @"");
93
94 if(separateByRemove){
95 [space remove:shape2];
96 } else {
97 body2.position = cpv(100, 100);
98 [space step:0.1];
99 }
100
101 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-PostSolve-Separate-", @"");
102
103 // Step once more to check for dangling pointers
104 [space step:0.1];
105
106 // Cleanup
107 [space release];
108 }
109
110 -(void)testHandlers {
111 testHandlersHelper(self, true, true);
112 testHandlersHelper(self, false, true);
113 testHandlersHelper(self, false, false);
114 testHandlersHelper(self, true, false);
115 }
116
117 static void
118 testHandlersSleepingHelper(id self, int wakeRemoveType){
119 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
120 space.collisionBias = 1.0f;
121 space.sleepTimeThreshold = 0.15;
122
123 NSString *type = @"type";
124 cpFloat radius = 5;
125
126 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
127 body1.position = cpv(0*radius*1.5,0);
128
129 ChipmunkShape *shape1 = [space add:[ChipmunkCircleShape circleWithBody:body1 radius:radius offset:cpvzero]];
130 shape1.collisionType = type;
131
132 ChipmunkBody *body2 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
133 body2.position = cpv(1*radius*1.5,0);
134
135 ChipmunkShape *shape2 = [space add:[ChipmunkCircleShape circleWithBody:body2 radius:radius offset:cpvzero]];
136 shape2.collisionType = type;
137
138 NSMutableString *string = [NSMutableString string];
139
140 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space.space, type, type);
141 handler->beginFunc = (cpCollisionBeginFunc)Begin,
142 handler->preSolveFunc = (cpCollisionPreSolveFunc)PreSolve,
143 handler->postSolveFunc = (cpCollisionPostSolveFunc)PostSolve,
144 handler->separateFunc = (cpCollisionSeparateFunc)Separate,
145 handler->userData = string;
146
147 // Test for separate callback when moving:
148 [space step:0.1];
149 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-", @"");
150
151 [space step:0.1];
152 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-", @"");
153
154 [space step:0.1];
155 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-", @"");
156
157 switch(wakeRemoveType){
158 case 0:
159 // Separate by removal
160 [space remove:shape2];
161 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-Separate-", @"");
162 break;
163 case 1:
164 // Separate by move
165 body2.position = cpv(100, 100);
166 [space step:0.1];
167 XCTAssertEqualObjects(string, @"Begin-PreSolve-PostSolve-PreSolve-Separate-", @"");
168 break;
169
170 default:break;
171 }
172
173 // Step once more to check for dangling pointers
174 [space step:0.1];
175
176 // Cleanup
177 [space release];
178 }
179
180 -(void)testHandlersSleeping {
181 testHandlersSleepingHelper(self, 0);
182 testHandlersSleepingHelper(self, 1);
183
184 // BUG if the time threshold is less than dt the bodies fall asleep the same frame after being awoken
185 // Separate is not called because of the short circuit in cpSpaceArbiterSetFilter().
186 // This is a weird edge case though as it's a really bad idea to use such a small threshold
187 }
188
189 static void
190 testSleepingSensorCallbacksHelper(id self, int wakeRemoveType){
191 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
192 space.collisionBias = 1.0f;
193 space.sleepTimeThreshold = 0.15;
194
195 cpFloat radius = 5;
196
197 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
198 body1.position = cpv(0*radius*1.5,0);
199
200 [space add:[ChipmunkCircleShape circleWithBody:body1 radius:radius offset:cpvzero]];
201
202 ChipmunkBody *body2 = [space add:[ChipmunkBody staticBody]];
203 body2.position = cpv(1*radius*1.5,0);
204
205 ChipmunkShape *shape2 = [space add:[ChipmunkCircleShape circleWithBody:body2 radius:radius offset:cpvzero]];
206 shape2.sensor = true;
207
208 NSMutableString *string = [NSMutableString string];
209
210 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space.space, nil, nil);
211 handler->beginFunc = (cpCollisionBeginFunc)Begin,
212 handler->preSolveFunc = (cpCollisionPreSolveFunc)PreSolve,
213 handler->postSolveFunc = (cpCollisionPostSolveFunc)PostSolve,
214 handler->separateFunc = (cpCollisionSeparateFunc)Separate,
215 handler->userData = string;
216
217 // Test for separate callback when moving:
218 [space step:0.1];
219 XCTAssertEqualObjects(string, @"Begin-PreSolve-", @"");
220
221 [space step:0.1];
222 XCTAssertEqualObjects(string, @"Begin-PreSolve-PreSolve-", @"");
223
224 [space step:0.1];
225 XCTAssertEqualObjects(string, @"Begin-PreSolve-PreSolve-", @"");
226
227 switch(wakeRemoveType){
228 case 0:
229 // Separate by removal
230 [space remove:shape2];
231 XCTAssertEqualObjects(string, @"Begin-PreSolve-PreSolve-Separate-", @"");
232 break;
233 case 1:
234 // Separate by move
235 body1.position = cpv(100, 100);
236 [space step:0.1];
237 XCTAssertEqualObjects(string, @"Begin-PreSolve-PreSolve-Separate-", @"");
238 break;
239
240 default:break;
241 }
242
243 // Step once more to check for dangling pointers
244 [space step:0.1];
245
246 // Cleanup
247 [space release];
248 }
249
250 -(void)testSleepingSensorCallbacks {
251 testSleepingSensorCallbacksHelper(self, 0);
252 testSleepingSensorCallbacksHelper(self, 1);
253 }
254
255 static cpBool CallBlock(cpArbiter *arb, cpSpace *space, cpBool (^block)(cpArbiter *arb)){return block(arb);}
256
257 -(void)testPostStepRemoval {
258 NSString *ballType = @"ballType";
259 NSString *barType = @"barType";
260
261 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
262 space.gravity = cpv(0, -100);
263
264 // TODO
265 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space.space, ballType, barType);
266 handler->beginFunc = (cpCollisionBeginFunc)CallBlock,
267 handler->userData = ^(cpArbiter *arb){
268 CHIPMUNK_ARBITER_GET_SHAPES(arb, ballShape, barShape);
269 [space addPostStepRemoval:barShape];
270
271 return TRUE;
272 };
273
274 ChipmunkShape *shape;
275
276 // The ball will stop on this bar
277 shape = [space add:[ChipmunkSegmentShape segmentWithBody:space.staticBody from:cpv(-10,0) to:cpv(10,0) radius:1]];
278
279 // but remove this one
280 shape = [space add:[ChipmunkSegmentShape segmentWithBody:space.staticBody from:cpv(-10,2) to:cpv(10,2) radius:1]];
281 shape.collisionType = barType;
282
283 ChipmunkBody *ball = [space add:[ChipmunkBody bodyWithMass:1 andMoment:cpMomentForCircle(1, 0, 1, cpvzero)]];
284 ball.position = cpv(0, 10);
285
286 shape = [space add:[ChipmunkCircleShape circleWithBody:ball radius:1 offset:cpvzero]];
287 shape.collisionType = ballType;
288
289 for(int i=0; i<100; i++) [space step:0.01];
290
291 XCTAssertEqualWithAccuracy(ball.position.y, (cpFloat)2, 1.1*space.collisionSlop, @"");
292
293 [space release];
294 }
295
296 // Make sure that adding a post step callback from inside a post step callback works correctly.
297 -(void)testPostStepFromPostStep
298 {
299 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
300
301 ChipmunkShape *staticShape = [space add:[ChipmunkCircleShape circleWithBody:space.staticBody radius:1.0 offset:cpvzero]];
302 staticShape.collisionType = staticShape;
303
304 ChipmunkBody *body1 = [space add:[ChipmunkBody kinematicBody]];
305 ChipmunkShape *shape1 = [space add:[ChipmunkCircleShape circleWithBody:body1 radius:1.0 offset:cpvzero]];
306 shape1.collisionType = shape1;
307 shape1.sensor = TRUE;
308
309 ChipmunkBody *body2 = [space add:[ChipmunkBody kinematicBody]];
310 ChipmunkShape *shape2 = [space add:[ChipmunkCircleShape circleWithBody:body2 radius:10.0 offset:cpvzero]];
311 shape2.collisionType = shape2;
312 shape2.sensor = TRUE;
313
314 __block bool trigger1 = FALSE;
315 __block bool trigger2 = FALSE;
316 __block bool trigger3 = FALSE;
317 __block bool trigger4 = FALSE;
318 __block bool trigger5 = FALSE;
319 __block bool trigger6 = FALSE;
320 __block bool trigger7 = FALSE;
321
322 cpCollisionHandler *handler1 = cpSpaceAddCollisionHandler(space.space, staticShape, shape1);
323 handler1->separateFunc = (cpCollisionSeparateFunc)CallBlock,
324 handler1->userData = ^(cpArbiter *arb){
325 XCTAssertTrue(cpSpaceIsLocked(space.space), @"");
326
327 // When body1 moves it will trigger the first separate callback.
328 [space addPostStepBlock:^{
329 XCTAssertFalse(cpSpaceIsLocked(space.space), @"");
330
331 // Calling remove will immediately trigger the next separate callback.
332 [space remove:shape1];
333
334 trigger2 = TRUE;
335 } key:shape1];
336
337 trigger1 = TRUE;
338 };
339
340 cpCollisionHandler *handler2 = cpSpaceAddCollisionHandler(space.space, shape1, shape2);
341 handler2->separateFunc = (cpCollisionSeparateFunc)CallBlock,
342 handler2->userData = ^(cpArbiter *arb){
343 XCTAssertTrue(cpSpaceIsLocked(space.space), @"");
344
345 // schedule a second post step callback within the old one with the same key.
346 // This one shouldn't be called.
347 [space addPostStepBlock:^{trigger4 = TRUE;} key:shape1];
348
349 trigger3 = TRUE;
350 };
351
352 cpCollisionHandler *handler3 = cpSpaceAddCollisionHandler(space.space, staticShape, shape2);
353 handler3->separateFunc = (cpCollisionSeparateFunc)CallBlock,
354 handler3->userData = ^(cpArbiter *arb){
355 XCTAssertTrue(cpSpaceIsLocked(space.space), @"");
356
357 [space addPostStepBlock:^{
358 [space addPostStepBlock:^{trigger7 = TRUE;} key:shape2];
359
360 trigger6 = TRUE;
361 } key:shape2];
362
363 trigger5 = TRUE;
364 };
365
366 [space step:1.0];
367 body1.position = cpv(10, 0);
368 [space step:1.0];
369
370 XCTAssertTrue(trigger1, @"");
371 XCTAssertTrue(trigger2, @"");
372 XCTAssertTrue(trigger3, @"");
373 XCTAssertFalse(trigger4, @"");
374
375 [space remove:shape2];
376
377 XCTAssertTrue(trigger5, @"");
378 XCTAssertTrue(trigger6, @"");
379 XCTAssertFalse(trigger7, @"");
380
381 [space release];
382 }
383
384 -(void)testBlockIterators {
385 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
386
387 ChipmunkShape *_staticShape = [ChipmunkCircleShape circleWithBody:space.staticBody radius:1.0 offset:cpvzero];
388
389 ChipmunkBody *_body = [ChipmunkBody bodyWithMass:1.0 andMoment:1.0];
390 ChipmunkShape *_shape = [ChipmunkCircleShape circleWithBody:_body radius:1.0 offset:cpvzero];
391 ChipmunkConstraint *_constraint = [ChipmunkPivotJoint pivotJointWithBodyA:space.staticBody bodyB:_body pivot:cpvzero];
392
393 {
394 __block int counter = 0;
395
396 cpSpaceEachBody_b(space.space, ^(cpBody *body){
397 XCTAssertEqual(cpBodyGetUserData(body), _body, @"");
398 counter++;
399 });
400
401 cpSpaceEachShape_b(space.space, ^(cpShape *shape){
402 if(cpBodyGetType(cpShapeGetBody(shape)) == CP_BODY_TYPE_STATIC){
403 XCTAssertEqual(shape, _staticShape.shape, @"");
404 } else {
405 XCTAssertEqual(shape, _shape.shape, @"");
406 }
407
408 counter++;
409 });
410
411 cpSpaceEachConstraint_b(space.space, ^(cpConstraint *constraint){
412 XCTAssertEqual(constraint, _constraint.constraint, @"");
413 counter++;
414 });
415
416
417 cpBodyEachShape_b(space.staticBody.body, ^(cpShape *shape){
418 XCTAssertEqual(shape, _staticShape.shape, @"");
419 counter++;
420 });
421
422 cpBodyEachConstraint_b(space.staticBody.body, ^(cpConstraint *constraint){
423 XCTAssertEqual(constraint, _constraint.constraint, @"");
424 counter++;
425 });
426
427
428 cpBodyEachShape_b(_body.body, ^(cpShape *shape){
429 XCTAssertEqual(shape, _shape.shape, @"");
430 counter++;
431 });
432
433 cpBodyEachConstraint_b(_body.body, ^(cpConstraint *constraint){
434 XCTAssertEqual(constraint, _constraint.constraint, @"");
435 counter++;
436 });
437
438 XCTAssertEqual(counter, 0, @"");
439 }
440
441 [space add:_staticShape];
442 [space add:_body];
443 [space add:_shape];
444 [space add:_constraint];
445
446 {
447 __block int counter = 0;
448
449 cpSpaceEachBody_b(space.space, ^(cpBody *body){
450 XCTAssertEqual(body, _body.body, @"");
451 counter++;
452 });
453
454 cpSpaceEachShape_b(space.space, ^(cpShape *shape){
455 if(cpBodyGetType(cpShapeGetBody(shape)) == CP_BODY_TYPE_STATIC){
456 XCTAssertEqual(shape, _staticShape.shape, @"");
457 } else {
458 XCTAssertEqual(shape, _shape.shape, @"");
459 }
460
461 counter++;
462 });
463
464 cpSpaceEachConstraint_b(space.space, ^(cpConstraint *constraint){
465 XCTAssertEqual(constraint, _constraint.constraint, @"");
466 counter++;
467 });
468
469
470 cpBodyEachShape_b(space.staticBody.body, ^(cpShape *shape){
471 XCTAssertEqual(shape, _staticShape.shape, @"");
472 counter++;
473 });
474
475 cpBodyEachConstraint_b(space.staticBody.body, ^(cpConstraint *constraint){
476 XCTAssertEqual(constraint, _constraint.constraint, @"");
477 counter++;
478 });
479
480
481 cpBodyEachShape_b(_body.body, ^(cpShape *shape){
482 XCTAssertEqual(shape, _shape.shape, @"");
483 counter++;
484 });
485
486 cpBodyEachConstraint_b(_body.body, ^(cpConstraint *constraint){
487 XCTAssertEqual(constraint, _constraint.constraint, @"");
488 counter++;
489 });
490
491 XCTAssertEqual(counter, 8, @"");
492 }
493
494 [space release];
495 }
496
497 @end