World: Refactored tests a little.
[scd.git] / test / net / habraun / sd / WorldTest.scala
blobc95a28ac4863f40ff61b6eb9676db7022b2eba56
1 /*
2 Copyright (c) 2009 Hanno Braun <hanno@habraun.net>
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 package net.habraun.sd
23 import collision._
24 import math._
26 import scala.collection.mutable._
28 import org.junit._
29 import org.junit.Assert._
33 class WorldTest {
35 var world: World = null
39 @Before
40 def setup {
41 world = new World
46 @Test
47 def verifyBodyIterableIsAccessible {
48 assertTrue(world.bodies.isInstanceOf[Iterable[Body]])
53 @Test
54 def verifyBodyIterableCantChangeWorld {
55 val body = new Body
56 world.bodies.asInstanceOf[HashSet[Body]].addEntry(body)
57 assertFalse(world.bodies.exists(_ == body))
62 @Test
63 def verifyInitialIntegrator {
64 assertTrue(world.integrator.isInstanceOf[EulerIntegrator])
69 @Test { val expected = classOf[NullPointerException] }
70 def setIntegratorNullExpectException() {
71 world.integrator = null
76 @Test
77 def verifyInitialBroadPhase {
78 assertTrue(world.broadPhase.isInstanceOf[SimpleBroadPhase])
83 @Test { val expected = classOf[NullPointerException] }
84 def setBroadPhaseNullExpectException {
85 world.broadPhase = null
90 @Test
91 def verifyInitialNarrowPhase {
92 assertTrue(world.narrowPhase.isInstanceOf[SimpleNarrowPhase])
97 @Test { val expected = classOf[NullPointerException] }
98 def setNarrowPhaseNullExpectException {
99 world.narrowPhase = null
104 @Test
105 def verifyInitialConstraintSolver {
106 assertTrue(world.constraintSolver.isInstanceOf[ImpulseSolver])
111 @Test { val expected = classOf[NullPointerException] }
112 def setConstraintSolverNullExpectException {
113 world.constraintSolver = null
118 @Test { val expected = classOf[IllegalArgumentException] }
119 def stepPassNegativeDelta {
120 world.step(-1.0)
125 @Test
126 def addBodyVerifyIsIntegrated {
127 val integrate = new Integrator {
128 var _t: Double = Double.NaN
129 var _body: Body = null
130 def apply(t: Double, body: Body) = {
131 _t = t
132 _body = body
133 body
136 world.integrator = integrate
138 val body = new Body
139 world.add(body)
141 val t = 2.0
142 world.step(t)
144 assertEquals(t, integrate._t, 0.0)
145 assertEquals(body, integrate._body)
150 @Test
151 def addAndRemoveBodyVerifyIsNotIntegrated {
152 val body = new Body
154 val integrate = new Integrator {
155 var integrated = false
156 def apply(t: Double, b: Body) = {
157 integrated = b == body
158 body
161 world.integrator = integrate
163 world.add(body)
164 world.remove(body)
165 world.step(2.0)
167 assertFalse(integrate.integrated)
172 @Test
173 def addBodyVerifyItIsPassedToBroadPhase {
174 val broadPhase = new BroadPhase {
175 var passedBodies: List[Body] = null
176 def detectPossibleCollisions(bodies: List[Body]) = { passedBodies = bodies; Nil }
178 world.broadPhase = broadPhase
180 val body = new Body
181 world.add(body)
182 world.step(2.0)
184 assertEquals(body::Nil, broadPhase.passedBodies)
189 @Test
190 def addBroadPhaseReturningBodyPairsVerifyTheyArePassedToNarrowPhase {
191 val b1 = new Body
192 val b2 = new Body
193 val b3 = new Body
194 val b4 = new Body
196 world.broadPhase = new BroadPhase {
197 def detectPossibleCollisions(bodies: List[Body]) = (b1, b2)::(b3, b4)::Nil
200 val narrowPhase = new NarrowPhase {
201 var passedPairs: List[(Body, Body)] = Nil
202 def inspectCollision(b1: Body, b2: Body) = {
203 passedPairs = passedPairs:::List((b1, b2))
204 None
207 world.narrowPhase = narrowPhase
209 world.step(2.0)
211 assertEquals((b1, b2)::(b3, b4)::Nil, narrowPhase.passedPairs)
216 @Test
217 def verifyForceIsAppliedBeforeCollisionDetection {
218 val broadPhase = new BroadPhase {
219 var v: Vec2D = null
220 def detectPossibleCollisions(bodies: List[Body]) = {
221 v = bodies(0).velocity
225 world.broadPhase = broadPhase
227 val body = new Body
228 body.mass = 5
229 body.applyForce(Vec2D(10, 0))
230 world.add(body)
232 world.step(2.0)
234 assertEquals(Vec2D(4, 0), broadPhase.v)
239 @Test
240 def verifyImpulseIsAppliedBeforeCollisionDetection {
241 val broadPhase = new BroadPhase {
242 var v: Vec2D = null
243 def detectPossibleCollisions(bodies: List[Body]) = {
244 v = bodies(0).velocity
248 world.broadPhase = broadPhase
250 val body = new Body
251 body.mass = 5
252 body.applyImpulse(Vec2D(10, 0))
253 world.add(body)
255 world.step(2.0)
257 assertEquals(Vec2D(2, 0), broadPhase.v)
262 @Test
263 def verifyCollisionReturnedByNarrowPhaseIsPassedToSolver {
264 val b1 = new Body
265 val b2 = new Body
266 world.add(b1)
267 world.add(b2)
269 val collision = Collision(0.0, Contact(b1, Vec2D(1, 1), Vec2D(1, 0), b2),
270 Contact(b2, Vec2D(1, 1), Vec2D(-1, 0), b1))
272 world.narrowPhase = new NarrowPhase {
273 def inspectCollision(b1: Body, b2: Body) = {
274 Some(collision)
278 val solver = new ConstraintSolver {
279 var collision: Collision = null
280 def apply(t: Double, constraint: Collision) {
281 collision = constraint
284 world.constraintSolver = solver
286 world.step(2.0)
288 assertEquals(collision, solver.collision)