forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRectangle.java
291 lines (213 loc) · 7.78 KB
/
TestRectangle.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package technology.tabula;
import static org.junit.Assert.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class TestRectangle {
@Test
public void testCompareEqualsRectangles() {
Rectangle first = new Rectangle();
Rectangle second = new Rectangle();
assertTrue(first.equals(second));
assertTrue(second.equals(first));
}
@Test
public void testCompareAlignedHorizontalRectangle() {
Rectangle lower = new Rectangle(0f, 10f, 10f, 10f);
Rectangle upper = new Rectangle(0f,20f, 10f, 10f);
assertTrue(lower.compareTo(upper) < 0);
}
@Test
public void testCompareAlignedVerticalRectangle() {
Rectangle lower = new Rectangle(10f, 0f, 10f, 10f);
Rectangle upper = new Rectangle(20f,0f, 10f, 10f);
assertTrue(lower.compareTo(upper) < 0);
}
@Test
public void testCompareVerticalOverlapRectangle() {
Rectangle lower = new Rectangle(5f, 0f, 10f, 10f);
Rectangle upper = new Rectangle(0f, 10f, 10f, 10f);
assertTrue(lower.compareTo(upper) < 0);
}
@Test
public void testCompareVerticalOverlapLessThresholdRectangle() {
Rectangle lower = new Rectangle(0f, 10f, 10f, 10f);
Rectangle upper = new Rectangle(9.8f, 0f, 10f, 10f);
assertTrue(lower.compareTo(upper) < 0);
}
@Test
public void testQuickSortOneUpperThanOther() {
Rectangle lower = new Rectangle(175.72f, 72.72f, 1.67f, 1.52f); //, (Comma after AARON)
Rectangle upper = new Rectangle(169.21f, 161.16f, 4.33f, 4.31f); // R (REGIONAL PULMONARY)
assertTrue(lower.compareTo(upper) > 0);
}
@Test
public void testQuickSortRectangleList() {
//Testing wrong sorting
// Expected: AARON, JOSHUA, N
// but was: AARON JOSHUA N , ,
Rectangle first = new Rectangle(172.92999267578125f, 51.47999954223633f, 4.0f, 4.309999942779541f); //A
Rectangle second = new Rectangle(175.72000122070312f, 72.72000122070312f, 1.6699999570846558f, 1.5199999809265137f); //,
Rectangle third = new Rectangle(172.92999267578125f, 96.36000061035156f, 4.0f, 4.309999942779541f); //A
Rectangle fourth = new Rectangle(175.72000122070312f, 100.31999969482422f, 1.6699999570846558f, 1.5199999809265137f); //,
Rectangle fifth = new Rectangle(172.92999267578125f, 103.68000030517578f, 4.329999923706055f, 4.309999942779541f); //N
Rectangle sixth = new Rectangle(169.2100067138672f, 161.16000366210938f, 4.329999923706055f, 4.309999942779541f); //R
List<Rectangle> expectedList = new ArrayList<>();
expectedList.add(first);
expectedList.add(sixth);
expectedList.add(second);
expectedList.add(third);
expectedList.add(fourth);
expectedList.add(fifth);
List<Rectangle> toSortList = new ArrayList<>();
toSortList.add(sixth);
toSortList.add(second);
toSortList.add(third);
toSortList.add(fifth);
toSortList.add(first);
toSortList.add(fourth);
Collections.sort(toSortList, Rectangle.ILL_DEFINED_ORDER);
assertEquals(expectedList, toSortList);
}
@Test
public void testGetVerticalOverlapShouldReturnZero() {
Rectangle lower = new Rectangle(10f, 0f, 10f, 10f);
Rectangle upper = new Rectangle(20f,0f, 10f, 10f);
float overlap = lower.verticalOverlap(upper);
assertEquals(0f, overlap, 0);
assertTrue(!lower.verticallyOverlaps(upper));
assertEquals(0f, lower.verticalOverlapRatio(upper), 0);
assertEquals(0f, lower.overlapRatio(upper), 0);
}
@Test
public void testGetVerticalOverlapShouldReturnMoreThanZero() {
Rectangle lower = new Rectangle(15f, 10f, 10f, 10f);
Rectangle upper = new Rectangle(20f, 0f, 10f, 10f);
float overlap = lower.verticalOverlap(upper);
assertEquals(5f, overlap, 0);
assertTrue(lower.verticallyOverlaps(upper));
assertEquals(0.5f, lower.verticalOverlapRatio(upper), 0);
assertEquals(0f, lower.overlapRatio(upper), 0);
}
@Test
public void testGetHorizontalOverlapShouldReturnZero() {
Rectangle one = new Rectangle(0f, 0f, 10f, 10f);
Rectangle two = new Rectangle(10f, 10f, 10f, 10f);
assertTrue(!one.horizontallyOverlaps(two));
assertEquals(0f, one.overlapRatio(two), 0);
}
@Test
public void testGetHorizontalOverlapShouldReturnMoreThanZero() {
Rectangle one = new Rectangle(0f, 0f, 10f, 10f);
Rectangle two = new Rectangle(10f, 5f, 10f, 10f);
assertTrue(one.horizontallyOverlaps(two));
assertEquals(5f, one.horizontalOverlap(two), 0);
assertEquals(0f, one.overlapRatio(two), 0);
}
@Test
public void testGetOverlapShouldReturnMoreThanZero() {
Rectangle one = new Rectangle(0f, 0f, 10f, 10f);
Rectangle two = new Rectangle(5f, 5f, 10f, 10f);
assertTrue(one.horizontallyOverlaps(two));
assertTrue(one.verticallyOverlaps(two));
assertEquals(5f, one.horizontalOverlap(two), 0);
assertEquals(5f, one.verticalOverlap(two), 0);
assertEquals((25f/175), one.overlapRatio(two), 0);
}
@Test
public void testMergeNoOverlappingRectangles() {
Rectangle one = new Rectangle(0f, 0f, 10f, 10f);
Rectangle two = new Rectangle(0f, 10f, 10f, 10f);
one.merge(two);
assertEquals(20f, one.getWidth(), 0);
assertEquals(10f, one.getHeight(), 0);
assertEquals(0f, one.getLeft(), 0);
assertEquals(0f, one.getTop(), 0);
assertEquals(10f, one.getBottom(), 0);
assertEquals(20f * 10f, one.getArea(), 0);
}
@Test
public void testMergeOverlappingRectangles() {
Rectangle one = new Rectangle(0f, 0f, 10f, 10f);
Rectangle two = new Rectangle(5f, 5f, 10f, 10f);
one.merge(two);
assertEquals(15f, one.getWidth(), 0);
assertEquals(15f, one.getHeight(), 0);
assertEquals(0f, one.getLeft(), 0);
assertEquals(0f, one.getTop(), 0);
}
@Test
public void testRectangleGetPoints() {
Rectangle one = new Rectangle(10f, 20f, 30f, 40f);
Point2D[] points = one.getPoints();
Point2D[] expectedPoints = new Point2D[]{
new Point2D.Float(20f, 10f),
new Point2D.Float(50f, 10f),
new Point2D.Float(50f, 50f),
new Point2D.Float(20f, 50f)
};
Assert.assertArrayEquals(expectedPoints, points);
}
@Test
public void testGetBoundingBox() {
List<Rectangle> rectangles = new ArrayList<>();
rectangles.add(new Rectangle(0f, 0f, 10f, 10f));
rectangles.add(new Rectangle(20f, 30f, 10f, 10f));
Rectangle boundingBoxOf = Rectangle.boundingBoxOf(rectangles);
assertEquals(new Rectangle(0f, 0f, 40f, 30f), boundingBoxOf);
}
@Test
public void testTransitiveComparison1() {
// +-------+
// | |
// | A | +-------+
// | | | |
// +-------+ | B | +-------+
// | | | |
// +-------+ | C |
// | |
// +-------+
Rectangle a = new Rectangle(0,0,2,2);
Rectangle b = new Rectangle(1,1,2,2);
Rectangle c = new Rectangle(2,2,2,2);
assertTrue(a.compareTo(b) < 0);
assertTrue(b.compareTo(c) < 0);
assertTrue(a.compareTo(c) < 0);
}
@Test @Ignore
public void testTransitiveComparison2() {
// +-------+
// | |
// +-------+ | C |
// | | | |
// +-------+ | B | +-------+
// | | | |
// | A | +-------+
// | |
// +-------+
Rectangle a = new Rectangle(2,0,2,2);
Rectangle b = new Rectangle(1,1,2,2);
Rectangle c = new Rectangle(0,2,2,2);
assertTrue(a.compareTo(b) < 0);
assertTrue(b.compareTo(c) < 0);
assertTrue(a.compareTo(c) < 0);
}
@Test @Ignore
public void testWellDefinedComparison1() {
Rectangle a = new Rectangle(2,0,2,2);
Rectangle b = new Rectangle(1,1,2,2);
Rectangle c = new Rectangle(0,2,2,2);
List<Rectangle> l1 = new ArrayList<>(Arrays.asList(b, a, c));
List<Rectangle> l2 = new ArrayList<>(Arrays.asList(c, b, a));
QuickSort.sort(l1, Rectangle.ILL_DEFINED_ORDER);
QuickSort.sort(l2, Rectangle.ILL_DEFINED_ORDER);
assertEquals(l1.get(0), l2.get(0));
assertEquals(l1.get(1), l2.get(1));
assertEquals(l1.get(2), l2.get(2));
}
}