-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3DTester.java
265 lines (240 loc) · 6.91 KB
/
Vector3DTester.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package Lab3;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class Vector3DTester {
private double rand() {
return (int) ((Math.random() * 100.0)*10)/10.0;
}
private double negRand() {
return (int) ((Math.random() * 100.0)*10)/-10.0;
}
@Test
void testCnstr1() {
double r = Math.random();
double x = 0;
double y = 0;
double z = 0;
if (r < 0.5) x = negRand();
else x = rand();
r = Math.random();
if (r < 0.5) y = negRand();
else y = rand();
r = Math.random();
if (r < 0.5) z = negRand();
else z = rand();
Vector3D vect = new Vector3D(x, y, z);
testGetX(x, vect);
testGetY(y, vect);
testGetZ(z, vect);
}
private void testGetX(double x, Vector3D vect) {
boolean equal = Math.abs(x - vect.getX()) < 0.00001;
String message = "getX() or the Constructor is wrong/missing for x = " + x;
assertTrue(equal, message);
}
private void testGetY(double y, Vector3D vect) {
boolean equal = Math.abs(y - vect.getY()) < 0.00001;
String message = "getY() or the Constructor is wrong for y = " + y;
assertTrue(equal, message);
}
private void testGetZ(double z, Vector3D vect) {
boolean equal = Math.abs(z - vect.getZ()) < 0.00001;
String message = "getX() or the Constructor is wrong for z = " + z;
assertTrue(equal, message);
}
@Test
void testSetX() {
Vector3D vect = new Vector3D(5, 6.0, 7.0);
vect.setX(99.0);
boolean equal = Math.abs(99.0 - vect.getX()) < 0.00001;
String message = "getX() or setX() is wrong/missing";
assertTrue(equal, message);
}
@Test
void testAttributeType1() {
Vector3D vect = new Vector3D(0, 0, 0);
boolean equal = (((Object) vect.getX()).getClass().getSimpleName().compareTo("Double") == 0);
String message = "Type of x is not double";
assertTrue(equal, message);
}
@Test
void testAdd2() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
r = Math.random();
double x2 = 0;
double y2 = 0;
double z2 = 0;
if (r < 0.5) x2 = negRand();
else x2 = rand();
r = Math.random();
if (r < 0.5) y2 = negRand();
else y2 = rand();
r = Math.random();
if (r < 0.5) z2 = negRand();
else z2 = rand();
Vector3D vect2 = new Vector3D(x2, y2, z2);
Vector3D vect3 = vect1.add(vect2);
testGetX(x1 + x2 , vect3);
testGetY(y1 + y2, vect3);
testGetZ(z1 + z2, vect3);
}
@Test
void testSubtract1() {
double x1 = 0;
double y1 = 0;
double z1 = 0;
Vector3D vect1 = new Vector3D(x1, y1, z1);
double x2 = 10;
double y2 = 10;
double z2 = 10;
Vector3D vect2 = new Vector3D(x2, y2, z2);
Vector3D vect3 = vect1.subtract(vect2);
testGetX(x1 - x2 , vect3);
testGetY(y1 - y2, vect3);
testGetZ(z1 - z2, vect3);
}
@Test
void testScalarMultiplication1() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
r = 0;
vect1 = vect1.scalarMultiplication(r);
testGetX(0, vect1);
testGetY(0, vect1);
testGetZ(0, vect1);
}
@Test
void testDotProduct1() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
r = Math.random();
double x2 = 0;
double y2 = 0;
double z2 = 0;
if (r < 0.5) x2 = negRand();
else x2 = rand();
r = Math.random();
if (r < 0.5) y2 = negRand();
else y2 = rand();
r = Math.random();
if (r < 0.5) z2 = negRand();
else z2 = rand();
Vector3D vect2 = new Vector3D(x2, y2, z2);
double prod = vect1.dotProduct(vect2);
boolean equal = Math.abs((x1*x2+y1*y2+z1*z2)-prod) < 0.00001;
String message = "dot product is not correct for ["+x1+ ", " + y1 + ", "+ z1 + "] and ["+x2+ ", " + y2 + ", "+ z2 + "]";
assertTrue(equal, message);
}
@Test
void testMagnitude() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
r = Math.sqrt(x1 * x1 + y1 * y1 + z1 * z1);
double m = vect1.magnitude();
boolean equal = Math.abs(m - r) < 0.00001;
String message = "magnitude() is not correct for ["+x1+ ", " + y1 + ", "+ z1 + "]";
assertTrue(equal, message);
}
@Test
void testToString() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
String s1 = vect1.toString();
String s2 = "[" + x1 + ", "+ y1 + ", "+ z1 + "]";
int eq = s1.compareTo(s2);
boolean equal = eq == 0 ? true : false;
String message = "toString() is not correct for ["+x1+ ", " + y1 + ", "+ z1 + "]";
assertTrue(equal, message);
}
@Test
void testEqual() {
double r = Math.random();
double x1 = 0;
double y1 = 0;
double z1 = 0;
if (r < 0.5) x1 = negRand();
else x1 = rand();
r = Math.random();
if (r < 0.5) y1 = negRand();
else y1 = rand();
r = Math.random();
if (r < 0.5) z1 = negRand();
else z1 = rand();
Vector3D vect1 = new Vector3D(x1, y1, z1);
r = Math.random();
double x2 = 0;
double y2 = 0;
double z2 = 0;
if (r < 0.5) x2 = negRand();
else x2 = rand();
r = Math.random();
if (r < 0.5) y2 = negRand();
else y2 = rand();
r = Math.random();
if (r < 0.5) z2 = negRand();
else z2 = rand();
Vector3D vect2 = new Vector3D(x2, y2, z2);
r = Math.random();
boolean magExpected = vect1.equalTo(vect2, r);
boolean magActual = Math.abs((Math.sqrt(x1 * x1 + y1 * y1 + z1 * z1)- Math.sqrt(x2 * x2 + y2 * y2 + z2 * z2)))< r;
boolean equal = (magExpected == magActual);
String message = "equalTo() is not correct for ["+x1+ ", " + y1 + ", "+ z1 + "], ["+x2+ ", " + y2 + ", "+ z2 + "] and threshold = " + r ;
assertTrue(equal, message);
}
}