| 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 |
#import "chipmunk/chipmunk_private.h" |
| 26 |
|
| 27 |
#define AssertRetainCount(obj, count) XCTAssertEqual([obj retainCount], (NSUInteger)count, @"") |
| 28 |
|
| 29 |
@interface MemoryTest : XCTestCase {} |
| 30 |
@end |
| 31 |
|
| 32 |
@implementation MemoryTest |
| 33 |
|
| 34 |
-(void)testBasic { |
| 35 |
ChipmunkSpace *space = [[ChipmunkSpace alloc] init]; |
| 36 |
|
| 37 |
ChipmunkBody *body1 = [[ChipmunkBody alloc] initWithMass:1 andMoment:1]; |
| 38 |
ChipmunkBody *body2 = [[ChipmunkBody alloc] initWithMass:1 andMoment:1]; |
| 39 |
|
| 40 |
ChipmunkShape *shape1 = [[ChipmunkCircleShape alloc] initWithBody:body1 radius:1 offset:cpvzero]; |
| 41 |
ChipmunkShape *shape2 = [[ChipmunkCircleShape alloc] initWithBody:body2 radius:1 offset:cpvzero]; |
| 42 |
|
| 43 |
ChipmunkConstraint *joint1 = [[ChipmunkPivotJoint alloc] initWithBodyA:body1 bodyB:body2 pivot:cpvzero]; |
| 44 |
ChipmunkConstraint *joint2 = [[ChipmunkPivotJoint alloc] initWithBodyA:body1 bodyB:body2 pivot:cpvzero]; |
| 45 |
|
| 46 |
[space add:body1]; |
| 47 |
[space add:body2]; |
| 48 |
[space add:shape1]; |
| 49 |
[space add:shape2]; |
| 50 |
[space add:joint1]; |
| 51 |
[space add:joint2]; |
| 52 |
|
| 53 |
AssertRetainCount(body1, 5); |
| 54 |
AssertRetainCount(body2, 5); |
| 55 |
AssertRetainCount(shape1, 2); |
| 56 |
AssertRetainCount(shape2, 2); |
| 57 |
AssertRetainCount(joint1, 2); |
| 58 |
AssertRetainCount(joint2, 2); |
| 59 |
|
| 60 |
[space remove:shape1]; |
| 61 |
[space remove:joint1]; |
| 62 |
|
| 63 |
AssertRetainCount(body1, 5); |
| 64 |
AssertRetainCount(body2, 5); |
| 65 |
AssertRetainCount(shape1, 1); |
| 66 |
AssertRetainCount(shape2, 2); |
| 67 |
AssertRetainCount(joint1, 1); |
| 68 |
AssertRetainCount(joint2, 2); |
| 69 |
|
| 70 |
[space release]; |
| 71 |
|
| 72 |
AssertRetainCount(body1, 4); |
| 73 |
AssertRetainCount(body2, 4); |
| 74 |
AssertRetainCount(shape1, 1); |
| 75 |
AssertRetainCount(shape2, 1); |
| 76 |
AssertRetainCount(joint1, 1); |
| 77 |
AssertRetainCount(joint2, 1); |
| 78 |
|
| 79 |
[joint1 release]; |
| 80 |
[joint2 release]; |
| 81 |
|
| 82 |
AssertRetainCount(body1, 2); |
| 83 |
AssertRetainCount(body2, 2); |
| 84 |
|
| 85 |
[shape1 release]; |
| 86 |
[shape2 release]; |
| 87 |
|
| 88 |
AssertRetainCount(body1, 1); |
| 89 |
AssertRetainCount(body2, 1); |
| 90 |
|
| 91 |
[body1 release]; |
| 92 |
[body2 release]; |
| 93 |
} |
| 94 |
|
| 95 |
-(void)testStaticBody { |
| 96 |
ChipmunkSpace *space = [[ChipmunkSpace alloc] init]; |
| 97 |
ChipmunkBody *staticBody = space.staticBody; |
| 98 |
|
| 99 |
AssertRetainCount(staticBody, 1); |
| 100 |
|
| 101 |
ChipmunkShape *shape = [[ChipmunkCircleShape alloc] initWithBody:space.staticBody radius:1 offset:cpvzero]; |
| 102 |
AssertRetainCount(staticBody, 2); |
| 103 |
|
| 104 |
[space add:shape]; |
| 105 |
AssertRetainCount(shape, 2); |
| 106 |
AssertRetainCount(staticBody, 2); |
| 107 |
|
| 108 |
[space release]; |
| 109 |
AssertRetainCount(shape, 1); |
| 110 |
AssertRetainCount(staticBody, 1); |
| 111 |
|
| 112 |
[shape release]; |
| 113 |
} |
| 114 |
|
| 115 |
-(void)testSetters { |
| 116 |
ChipmunkBody *body = [[ChipmunkBody alloc] initWithMass:1.0 andMoment:1.0]; |
| 117 |
|
| 118 |
|
| 119 |
ChipmunkShape *shape = [ChipmunkCircleShape circleWithBody:nil radius:1 offset:cpvzero]; |
| 120 |
shape.body = body; |
| 121 |
AssertRetainCount(body, 2); |
| 122 |
|
| 123 |
shape.body = body; |
| 124 |
AssertRetainCount(body, 2); |
| 125 |
|
| 126 |
shape.body = nil; |
| 127 |
AssertRetainCount(body, 1); |
| 128 |
|
| 129 |
|
| 130 |
// ChipmunkConstraint *joint = [ChipmunkPivotJoint pivotJointWithBodyA:nil bodyB:nil pivot:cpvzero]; |
| 131 |
// joint.bodyA = body; joint.bodyB = body; |
| 132 |
// AssertRetainCount(body, 3); |
| 133 |
// |
| 134 |
// joint.bodyA = body; joint.bodyB = body; |
| 135 |
// AssertRetainCount(body, 3); |
| 136 |
// |
| 137 |
// joint.bodyA = nil; joint.bodyB = nil; |
| 138 |
// AssertRetainCount(body, 1); |
| 139 |
} |
| 140 |
|
| 141 |
-(void)testPostStepCallbacks { |
| 142 |
ChipmunkSpace *space = [[ChipmunkSpace alloc] init]; |
| 143 |
NSObject *obj1 = [[NSObject alloc] init]; |
| 144 |
NSObject *obj2 = [[NSObject alloc] init]; |
| 145 |
|
| 146 |
// Lock the space to avoid triggering warnings. |
| 147 |
cpSpaceLock(space.space); |
| 148 |
|
| 149 |
// Registering the callback should retain the object twice |
| 150 |
XCTAssertTrue([space addPostStepCallback:obj1 selector:@selector(isEqual:) key:obj1], @""); |
| 151 |
AssertRetainCount(obj1, 3); |
| 152 |
|
| 153 |
// Registering the same callback a second time should not add more retains |
| 154 |
XCTAssertFalse([space addPostStepCallback:obj1 selector:@selector(isEqual:) key:obj1], @""); |
| 155 |
AssertRetainCount(obj1, 3); |
| 156 |
|
| 157 |
// A key can only be registered once to prevent double removals. |
| 158 |
// Registering a second target with the same key is a no-op. |
| 159 |
XCTAssertFalse([space addPostStepCallback:obj2 selector:@selector(isEqual:) key:obj1], @""); |
| 160 |
AssertRetainCount(obj1, 3); |
| 161 |
AssertRetainCount(obj2, 1); |
| 162 |
|
| 163 |
XCTAssertTrue([space addPostStepCallback:obj1 selector:@selector(isEqual:) key:obj2], @""); |
| 164 |
AssertRetainCount(obj1, 4); |
| 165 |
AssertRetainCount(obj2, 2); |
| 166 |
|
| 167 |
cpSpaceUnlock(space.space, FALSE); |
| 168 |
|
| 169 |
// Stepping the space should release the callback handler and both objects |
| 170 |
[space step:1]; |
| 171 |
AssertRetainCount(obj1, 1); |
| 172 |
AssertRetainCount(obj2, 1); |
| 173 |
|
| 174 |
[space release]; |
| 175 |
[obj1 release]; |
| 176 |
[obj2 release]; |
| 177 |
} |
| 178 |
|
| 179 |
@end |