source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/BBoxTest.java@ 12029

Last change on this file since 12029 was 12029, checked in by Don-vip, 7 years ago

add more unit tests

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