-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipTest.java
111 lines (102 loc) · 2.81 KB
/
ShipTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* The ShipTest class is a JUnit test class for the Ship class in the
* Asteroid Adventure Game. It tests various functionalities of the Ship class.
*
* @version 1.0
* @since 2023-04-04
*/
public class ShipTest {
private Ship s;
private Pose pose;
/**
* Sets up the test environment for each test method.
*/
@BeforeEach
public void setup() {
s = new Ship();
}
/**
* Tests if the Ship class correctly implements the GameElement interface.
*/
@Test
public void testImplementsInterface() {
assertTrue(s instanceof GameElement);
}
/**
* Tests the constant values defined in the Ship class.
*/
@Test
public void testConstants() {
assertEquals(10, Ship.SHIP_WIDTH);
assertEquals(20, Ship.SHIP_HEIGHT);
assertEquals(0.1, Ship.SHIP_TURN);
assertEquals(0.1, Ship.SHIP_MOVE);
assertEquals(0.02, Ship.FRICTION);
}
/**
* Tests the constructor of the Ship class.
*/
@Test
public void testConstructor() {
TestHelpers.comparePoses(new Pose(240.0, 240.0, Math.PI/2),
s.getPose());
}
/**
* Tests the turnLeft method of the Ship class.
*/
@Test
public void testturnLeft() {
s.turnLeft();
double val = (Math.PI/2) + 0.1;
assertEquals(val, s.getPose().getHeading());
}
/**
* Tests the turnRight method of the Ship class.
*/
@Test
public void testturnRight() {
s.turnRight();
double val = (Math.PI/2) - 0.1;
assertEquals(val, s.getPose().getHeading());
}
/**
* Tests the thrust method of the Ship class.
*/
@Test
public void testthrust() {
s.thrust();
double speed = s.getVelocity().getMagnitude();
System.out.println(speed);
assertEquals(0.1, speed);
assertEquals(Math.PI/2, s.getPose().getHeading());
System.out.println(s.getPose().getHeading());
System.out.println(Math.PI/2);
}
/**
* Tests the thrust and update methods of the Ship class.
*/
@Test
public void testthrustwithupdate() {
s.thrust();
s.update();
double speed = s.getVelocity().getMagnitude();
assertEquals(0.08, speed);
assertEquals(240.0, s.getPose().getX());
assertEquals(240.1, s.getPose().getY());
assertEquals(Math.PI/2, s.getPose().getHeading());
for (int i = 0; i < 20; i ++ ) {
s.update();
}
double speed_zero = s.getVelocity().getMagnitude();
assertEquals(0.0, speed_zero);
}
/**
* Tests the draw method of the Ship class.
*/
@Test
public void testDraw() {
s.draw();
String actual = StdDraw.getLastDraw();
assertTrue("polygon()".equals(actual));
}
}