generated from mengjiaocyr/irregular-polygon-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTestSuite.java
More file actions
77 lines (66 loc) · 2.4 KB
/
TestSuite.java
File metadata and controls
77 lines (66 loc) · 2.4 KB
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
import java.awt.geom.*; // for Point2D.Double
import java.util.Arrays;
public class TestSuite {
// Run a bunch of basic tests on IrregularPolygon
public static void run()
{
System.out.println("Starting TestSuite");
boolean pass = true;
double[][] singlePoint = { { 0, 0 } };
pass &= basicTest("Single Point", singlePoint, 0, 0);
double[][] squarePoints = { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 0 } };
pass &= basicTest("Unit Square", squarePoints, 4, 1);
double[][] parallelogramPoints = { { 20, 10 }, { 70, 20 }, { 50, 50 }, { 0, 40 } };
pass &= basicTest("Parallelogram", parallelogramPoints, 174.0914, 1700);
double[][] bowtiePoints = { { 0, 0 }, { 100, 50 }, { 100, 0 }, { 0, 50 } };
pass &= basicTest("Bowtie", bowtiePoints, 323.6067, 0.0);
if (pass == true)
{
System.out.println("--- TEST PASSED! Congrats! ---");
}
else
{
System.out.println("--- TEST FAILED! :( ---");
}
}
public static boolean basicTest(String description, double[][] points, double expectedPerimeter, double expectedArea)
{
IrregularPolygon poly = new IrregularPolygon();
for (double[] point : points)
{
//System.out.println(" Adding point: " + Arrays.toString(point));
poly.add(new Point2D.Double(point[0], point[1]));
}
poly.draw();
double perimeter = poly.perimeter();
double area = poly.area();
boolean perimeterCorrect = compareDoubles(perimeter, expectedPerimeter);
boolean areaCorrect = compareDoubles(area, expectedArea);
if (perimeterCorrect && areaCorrect)
{
System.out.println("PASS: " + description);
return true;
}
else
{
System.out.println("FAIL: " + description);
if (!perimeterCorrect) {
System.out.println(" Perimeter should be: " + expectedPerimeter + " not: " + perimeter);
}
if (!areaCorrect) {
System.out.println(" Area should be: " + expectedArea + " not: " + area);
}
return false;
}
}
private static boolean compareDoubles(Double a, Double b)
{
if (Math.abs(a - b) < 0.001) {
return true;
}
else
{
return false;
}
}
}