-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSimplifyTest.java
More file actions
executable file
·178 lines (145 loc) · 5.26 KB
/
SimplifyTest.java
File metadata and controls
executable file
·178 lines (145 loc) · 5.26 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
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
package com.goebl.simplify;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Test class for {@link Simplify}.
*
* @author goebl
* @since 06.07.13
*/
public class SimplifyTest {
private static Point[] allPoints;
private static final float[] TOLERANCES = new float[]{
1.0f, 1.5f, 2.0f, 4.0f, 5.0f
};
private static final float[][] POINTS_2D = new float[][]{
{3.14f, 5.2f}, {5.7f, 8.1f}, {4.6f, -1.3f}
};
@BeforeClass
public static void readPoints() throws Exception {
allPoints = readPoints("points-all.txt");
}
@Test
public void testSimpleQuality() throws Exception {
for (float tolerance : TOLERANCES) {
assertPointsEqual(tolerance, false);
}
}
@Test
public void testHighQuality() throws Exception {
for (float tolerance : TOLERANCES) {
assertPointsEqual(tolerance, true);
}
}
@Test
public void testCustomPointExtractor() {
PointExtractor<float[]> pointExtractor = new PointExtractor<float[]>() {
@Override
public double getX(float[] point) {
return point[0];
}
@Override
public double getY(float[] point) {
return point[1];
}
};
Simplify<float[]> simplify = new Simplify<float[]>(new float[0][0], pointExtractor);
float[][] simplified = simplify.simplify(POINTS_2D, 5.0f, false);
Assert.assertEquals("array should be simplified", 2, simplified.length);
simplified = simplify.simplify(POINTS_2D, 5.0f, true);
Assert.assertEquals("array should be simplified", 2, simplified.length);
}
@Test
public void testInvalidPointsParam() {
Simplify<Point> aut = new Simplify<Point>(new MyPoint[0]);
Assert.assertNull("return null when point-array is null", aut.simplify(null, 1f, false));
Point[] only2 = new Point[2];
only2[0] = new MyPoint(1, 2);
only2[1] = new MyPoint(2, 3);
Assert.assertTrue("return identical array when less than 3 points",
only2 == aut.simplify(only2, 1f, false));
}
private void assertPointsEqual(float tolerance, boolean highQuality) throws Exception {
Point[] pointsExpected = readPoints(tolerance, highQuality);
long start = System.nanoTime();
Simplify<Point> aut = new Simplify<Point>(new MyPoint[0]);
Point[] pointsActual = aut.simplify(allPoints, tolerance, highQuality);
long end = System.nanoTime();
System.out.println("tolerance=" + tolerance + " hq=" + highQuality
+ " nanos=" + (end - start));
Assert.assertNotNull("wrong test setup", pointsExpected);
Assert.assertNotNull("simplify must return Point[]", pointsActual);
Assert.assertArrayEquals("tolerance=" + tolerance + " hq=" + highQuality,
pointsExpected, pointsActual);
}
private static Point[] readPoints(float tolerance, boolean highQuality) throws Exception {
return readPoints(String.format(Locale.US, "points-%01.1f-%s.txt",
tolerance, highQuality ? "hq" : "sq"));
}
static Point[] readPoints(String fileName) throws Exception {
List<MyPoint> pointList = new ArrayList<MyPoint>();
File file = new File("src/test/resources", fileName);
InputStream is = null;
BufferedReader reader = null;
try {
is = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
String[] xy = line.split(",");
double x = Double.parseDouble(xy[0]);
double y = Double.parseDouble(xy[1]);
pointList.add(new MyPoint(x, y));
}
} finally {
if (reader != null) {
reader.close();
}
if (is != null) {
is.close();
}
}
return pointList.toArray(new MyPoint[pointList.size()]);
}
private static class MyPoint implements Point {
double x;
double y;
private MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public double getX() {
return x;
}
@Override
public double getY() {
return y;
}
@Override
public String toString() {
return "{" + "x=" + x + ", y=" + y + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyPoint myPoint = (MyPoint) o;
if (Double.compare(myPoint.x, x) != 0) return false;
if (Double.compare(myPoint.y, y) != 0) return false;
return true;
}
}
}