1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import org.junit.Rule;
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openstreetmap.josm.TestUtils;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
13 |
|
---|
14 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
15 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
16 | import nl.jqno.equalsverifier.Warning;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Unit tests for class {@link BBox}.
|
---|
20 | */
|
---|
21 | public class BBoxTest {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Setup test.
|
---|
25 | */
|
---|
26 | @Rule
|
---|
27 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
28 | public JOSMTestRules test = new JOSMTestRules();
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit test of methods {@link BBox#equals} and {@link BBox#hashCode}.
|
---|
32 | */
|
---|
33 | @Test
|
---|
34 | public void testEqualsContract() {
|
---|
35 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
36 | EqualsVerifier.forClass(BBox.class).usingGetClass()
|
---|
37 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
38 | .verify();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Unit test of method {@link BBox#bboxesAreFunctionallyEqual}
|
---|
43 | */
|
---|
44 | @Test
|
---|
45 | public void testBboxesAreFunctionallyEqual() {
|
---|
46 | BBox bbox1 = new BBox(0, 1, 1, 0);
|
---|
47 | BBox bbox2 = new BBox(0.1, 0.9, 0.9, 0.1);
|
---|
48 |
|
---|
49 | assertFalse(BBox.bboxesAreFunctionallyEqual(bbox1, null, null));
|
---|
50 | assertFalse(BBox.bboxesAreFunctionallyEqual(null, bbox2, null));
|
---|
51 | assertFalse(BBox.bboxesAreFunctionallyEqual(null, null, null));
|
---|
52 |
|
---|
53 | assertFalse(bbox1.bboxIsFunctionallyEqual(bbox2, null));
|
---|
54 | assertTrue(bbox1.bboxIsFunctionallyEqual(bbox2, 0.1));
|
---|
55 | bbox1.add(0, 1.1);
|
---|
56 | assertFalse(bbox1.bboxIsFunctionallyEqual(bbox2, 0.1));
|
---|
57 | bbox1.add(2, 0);
|
---|
58 | assertFalse(bbox1.bboxIsFunctionallyEqual(bbox2, 0.1));
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Test LatLon constructor which might result in invalid bbox
|
---|
63 | */
|
---|
64 | @Test
|
---|
65 | public void testLatLonConstructor() {
|
---|
66 | LatLon latLon1 = new LatLon(10, 20);
|
---|
67 | LatLon latLon2 = new LatLon(20, 10);
|
---|
68 | BBox b1 = new BBox(latLon1, latLon2);
|
---|
69 | BBox b2 = new BBox(latLon2, latLon1);
|
---|
70 | assertTrue(b1.bounds(latLon1));
|
---|
71 | assertTrue(b2.bounds(latLon1));
|
---|
72 | assertTrue(b1.bounds(latLon2));
|
---|
73 | assertTrue(b2.bounds(latLon2));
|
---|
74 | assertTrue(b2.bounds(b1));
|
---|
75 | assertTrue(b1.bounds(b2));
|
---|
76 |
|
---|
77 | // outside of world latlon values
|
---|
78 | LatLon outOfWorld = new LatLon(-190, 340);
|
---|
79 | BBox b3 = new BBox(outOfWorld, latLon1);
|
---|
80 | BBox b4 = new BBox(latLon1, outOfWorld);
|
---|
81 | BBox b5 = new BBox(outOfWorld, outOfWorld);
|
---|
82 |
|
---|
83 | assertTrue(b3.isValid());
|
---|
84 | assertTrue(b4.isValid());
|
---|
85 | assertTrue(b3.bounds(latLon1));
|
---|
86 | assertTrue(b4.bounds(latLon1));
|
---|
87 | assertTrue(b5.isValid());
|
---|
88 | assertFalse(b3.isInWorld());
|
---|
89 | assertFalse(b4.isInWorld());
|
---|
90 | assertFalse(b5.isInWorld());
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Test double constructor which might result in invalid bbox
|
---|
95 | */
|
---|
96 | @Test
|
---|
97 | public void testDoubleConstructor() {
|
---|
98 | assertTrue(new BBox(1, 2, 3, 4).isValid());
|
---|
99 | assertFalse(new BBox(Double.NaN, 2, 3, 4).isValid());
|
---|
100 | assertFalse(new BBox(1, Double.NaN, 3, 4).isValid());
|
---|
101 | assertFalse(new BBox(1, 2, Double.NaN, 4).isValid());
|
---|
102 | assertFalse(new BBox(1, 2, 3, Double.NaN).isValid());
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Test Node constructor which might result in invalid bbox
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | public void testNodeConstructor() {
|
---|
110 | assertTrue(new BBox(new Node(LatLon.NORTH_POLE)).isValid());
|
---|
111 | assertFalse(new BBox(new Node()).isValid());
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Unit test of {@link BBox#add(LatLon)} method.
|
---|
116 | */
|
---|
117 | @Test
|
---|
118 | public void testAddLatLon() {
|
---|
119 | BBox b = new BBox();
|
---|
120 | b.add((LatLon) null);
|
---|
121 | b.add(new LatLon(Double.NaN, Double.NaN));
|
---|
122 | assertFalse(b.isValid());
|
---|
123 | b.add(LatLon.NORTH_POLE);
|
---|
124 | assertTrue(b.isValid());
|
---|
125 | assertEquals(LatLon.NORTH_POLE, b.getCenter());
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Unit test of {@link BBox#addLatLon} method.
|
---|
130 | */
|
---|
131 | @Test
|
---|
132 | public void testAddLatLonBuffer() {
|
---|
133 | BBox b = new BBox();
|
---|
134 | b.addLatLon(LatLon.NORTH_POLE, 0.5);
|
---|
135 | assertEquals(LatLon.NORTH_POLE, b.getCenter());
|
---|
136 | assertEquals(new LatLon(90.5, -0.5), b.getTopLeft());
|
---|
137 | assertEquals(new LatLon(89.5, +0.5), b.getBottomRight());
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Unit test of {@link BBox#add(double, double)} method.
|
---|
142 | */
|
---|
143 | @Test
|
---|
144 | public void testAddDouble() {
|
---|
145 | BBox b = new BBox();
|
---|
146 | b.add(1, Double.NaN);
|
---|
147 | assertFalse(b.isValid());
|
---|
148 | b.add(Double.NaN, 2);
|
---|
149 | assertFalse(b.isValid());
|
---|
150 | b.add(1, 2);
|
---|
151 | assertTrue(b.isValid());
|
---|
152 | assertEquals(new LatLon(2, 1), b.getCenter());
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Unit test of {@link BBox#addPrimitive} method.
|
---|
157 | */
|
---|
158 | @Test
|
---|
159 | public void testAddPrimitive() {
|
---|
160 | BBox b = new BBox();
|
---|
161 | b.addPrimitive(new Node(LatLon.NORTH_POLE), 0.5);
|
---|
162 | assertEquals(LatLon.NORTH_POLE, b.getCenter());
|
---|
163 | assertEquals(new LatLon(90.5, -0.5), b.getTopLeft());
|
---|
164 | assertEquals(new LatLon(89.5, +0.5), b.getBottomRight());
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Unit test of {@link BBox#height} and {@link BBox#width} and {@link BBox#area} methods.
|
---|
169 | */
|
---|
170 | @Test
|
---|
171 | public void testHeightWidthArea() {
|
---|
172 | BBox b1 = new BBox(1, 2, 3, 5);
|
---|
173 | assertEquals(2, b1.width(), 1e-7);
|
---|
174 | assertEquals(3, b1.height(), 1e-7);
|
---|
175 | assertEquals(6, b1.area(), 1e-7);
|
---|
176 | BBox b2 = new BBox();
|
---|
177 | assertEquals(0, b2.width(), 1e-7);
|
---|
178 | assertEquals(0, b2.height(), 1e-7);
|
---|
179 | assertEquals(0, b2.area(), 1e-7);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Unit test of {@link BBox#toString} method.
|
---|
184 | */
|
---|
185 | @Test
|
---|
186 | public void testToString() {
|
---|
187 | assertEquals("[ x: Infinity -> -Infinity, y: Infinity -> -Infinity ]", new BBox().toString());
|
---|
188 | assertEquals("[ x: 1.0 -> 3.0, y: 2.0 -> 4.0 ]", new BBox(1, 2, 3, 4).toString());
|
---|
189 | }
|
---|
190 | }
|
---|