Add license to tests.
[desert.git] / test / org / sourceforge / desert / ParticleUpdaterTest.java
blobf7fa7c2ad7242e361f81f64ed1156a29ecf2a70b
1 /*
2 * Copyright (c) 2010 The Desert team
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 package org.sourceforge.desert;
28 import java.awt.Rectangle;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.awt.Color;
32 import org.junit.Test;
34 import static org.junit.Assert.*;
36 /**
38 * @author codistmonk (creation 2010-04-13)
40 public final class ParticleUpdaterTest {
42 @Test
43 public final void testUpdateWithoutBounds() {
44 final Double x = 0.0;
45 final Double y = 2.0;
46 final Double speedX = 3.0;
47 final Double speedY = 5.0;
48 final Particle particle = new Particle(x, y, speedX, speedY, Color.blue);
49 final List<Particle> particles = Arrays.asList(particle);
50 final Double deltaTime = 7.0;
51 final ParticleUpdater particleUpdater = new ParticleUpdater(particles);
53 particleUpdater.update(deltaTime);
55 assertEquals(x + speedX * deltaTime, particle.getX(), DELTA);
56 assertEquals(y + speedY * deltaTime + particleUpdater.getGravity() * deltaTime * deltaTime, particle.getY(), DELTA);
57 assertEquals(speedX, particle.getSpeedX(), DELTA);
58 assertEquals(speedY + particleUpdater.getGravity() * deltaTime, particle.getSpeedY(), DELTA);
61 @Test
62 public final void testUpdateWithBottomBound() {
63 final Double x = 5.0;
64 final Double y = 4.0;
65 final Double speedX = 1.0;
66 final Double speedY = 5.0; // Go downward in screen coordinates
67 final Particle particle = new Particle(x, y, speedX, speedY, Color.red);
68 final List<Particle> particles = Arrays.asList(particle);
69 final Double deltaTime = 2.0;
70 final ParticleUpdater particleUpdater = new ParticleUpdater(particles);
71 final Integer width = 10;
72 final Integer height = 10;
73 particleUpdater.setBounds(new Rectangle(width, height));
74 particleUpdater.setGravity(0.0);
76 particleUpdater.update(deltaTime);
78 assertEquals(x + speedX * deltaTime, particle.getX(), DELTA);
79 assertEquals(height - 1.0, particle.getY(), DELTA);
80 assertEquals(speedX, particle.getSpeedX(), DELTA);
81 assertEquals(0.0, particle.getSpeedY(), DELTA);
84 @Test
85 public final void testUpdateWithTopBound() {
86 final Double x = 5.0;
87 final Double y = 4.0;
88 final Double speedX = 1.0;
89 final Double speedY = -5.0; // Go upward in screen coordinates
90 final Particle particle = new Particle(x, y, speedX, speedY, Color.green);
91 final List<Particle> particles = Arrays.asList(particle);
92 final Double deltaTime = 2.0;
93 final ParticleUpdater particleUpdater = new ParticleUpdater(particles);
94 final Integer width = 10;
95 final Integer height = 10;
96 particleUpdater.setBounds(new Rectangle(width, height));
97 particleUpdater.setGravity(0.0);
99 particleUpdater.update(deltaTime);
101 assertEquals(x + speedX * deltaTime, particle.getX(), DELTA);
102 assertEquals(0.0, particle.getY(), DELTA);
103 assertEquals(speedX, particle.getSpeedX(), DELTA);
104 assertEquals(0.0, particle.getSpeedY(), DELTA);
107 @Test
108 public final void testUpdateWithLeftBound() {
109 final Double x = 5.0;
110 final Double y = 4.0;
111 final Double speedX = -5.0;
112 final Double speedY = 1.0;
113 final Particle particle = new Particle(x, y, speedX, speedY, Color.yellow);
114 final List<Particle> particles = Arrays.asList(particle);
115 final Double deltaTime = 2.0;
116 final ParticleUpdater particleUpdater = new ParticleUpdater(particles);
117 final Integer width = 10;
118 final Integer height = 10;
119 particleUpdater.setBounds(new Rectangle(width, height));
120 particleUpdater.setGravity(0.0);
122 particleUpdater.update(deltaTime);
124 assertEquals(0.0, particle.getX(), DELTA);
125 assertEquals(y + speedY * deltaTime, particle.getY(), DELTA);
126 assertEquals(0.0, particle.getSpeedX(), DELTA);
127 assertEquals(speedY, particle.getSpeedY(), DELTA);
130 @Test
131 public final void testUpdateWithRightBound() {
132 final Double x = 5.0;
133 final Double y = 4.0;
134 final Double speedX = 5.0;
135 final Double speedY = 1.0;
136 final Particle particle = new Particle(x, y, speedX, speedY, Color.CYAN);
137 final List<Particle> particles = Arrays.asList(particle);
138 final Double deltaTime = 2.0;
139 final ParticleUpdater particleUpdater = new ParticleUpdater(particles);
140 final Integer width = 10;
141 final Integer height = 10;
142 particleUpdater.setBounds(new Rectangle(width, height));
143 particleUpdater.setGravity(0.0);
145 particleUpdater.update(deltaTime);
147 assertEquals(width - 1.0, particle.getX(), DELTA);
148 assertEquals(y + speedY * deltaTime, particle.getY(), DELTA);
149 assertEquals(0.0, particle.getSpeedX(), DELTA);
150 assertEquals(speedY, particle.getSpeedY(), DELTA);
153 private static final Double DELTA = 1e-12;