source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/QuadBucketsTest.java@ 11324

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

findbugs

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 9.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.security.SecureRandom;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Random;
13
14import org.fest.reflect.core.Reflection;
15import org.fest.reflect.reference.TypeRef;
16import org.junit.Assert;
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.projection.Projections;
22import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
23import org.openstreetmap.josm.io.OsmReader;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests of {@link QuadBuckets}.
30 */
31public class QuadBucketsTest {
32
33 /**
34 * Setup test.
35 */
36 @Rule
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules();
39
40 private void removeAllTest(DataSet ds) {
41 List<Node> allNodes = new ArrayList<>(ds.getNodes());
42 List<Way> allWays = new ArrayList<>(ds.getWays());
43 List<Relation> allRelations = new ArrayList<>(ds.getRelations());
44
45 QuadBuckets<Node> nodes = Reflection.field("nodes").ofType(new TypeRef<QuadBuckets<Node>>() {}).in(ds).get();
46 QuadBuckets<Way> ways = Reflection.field("ways").ofType(new TypeRef<QuadBuckets<Way>>() {}).in(ds).get();
47 Collection<Relation> relations = Reflection.field("relations").ofType(new TypeRef<Collection<Relation>>() {}).in(ds).get();
48
49 int expectedCount = allNodes.size();
50 for (OsmPrimitive o: allNodes) {
51 ds.removePrimitive(o);
52 checkIterator(nodes, --expectedCount);
53 }
54 expectedCount = allWays.size();
55 for (OsmPrimitive o: allWays) {
56 ds.removePrimitive(o);
57 checkIterator(ways, --expectedCount);
58 }
59 for (OsmPrimitive o: allRelations) {
60 ds.removePrimitive(o);
61 }
62 Assert.assertTrue(nodes.isEmpty());
63 Assert.assertTrue(ways.isEmpty());
64 Assert.assertTrue(relations.isEmpty());
65 }
66
67 private void checkIterator(Collection<? extends OsmPrimitive> col, int expectedCount) {
68 int count = 0;
69 Iterator<? extends OsmPrimitive> it = col.iterator();
70 while (it.hasNext()) {
71 count++;
72 it.next();
73 }
74 Assert.assertEquals(expectedCount, count);
75 }
76
77 @Test
78 public void testRemove() throws Exception {
79 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
80 try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
81 DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
82 removeAllTest(ds);
83 }
84 }
85
86 @Test
87 public void testMove() throws Exception {
88 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
89 try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
90 DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
91
92 for (Node n: ds.getNodes()) {
93 n.setCoor(new LatLon(10, 10));
94 }
95
96 removeAllTest(ds);
97 }
98 }
99
100 /**
101 * Test handling of objects with invalid bbox
102 */
103 @Test
104 public void testSpecialBBox() {
105 QuadBuckets<Node> qbNodes = new QuadBuckets<>();
106 QuadBuckets<Way> qbWays = new QuadBuckets<>();
107 Way w1 = new Way(1);
108 Way w2 = new Way(2);
109 Way w3 = new Way(3);
110 Node n1 = new Node(1);
111 Node n2 = new Node(2); n2.setCoor(new LatLon(10, 20));
112 Node n3 = new Node(3); n2.setCoor(new LatLon(20, 30));
113 w2.setNodes(Arrays.asList(n1));
114 w3.setNodes(Arrays.asList(n1, n2, n3));
115
116 qbNodes.add(n1);
117 qbNodes.add(n2);
118 Assert.assertEquals(2, qbNodes.size());
119 Assert.assertTrue(qbNodes.contains(n1));
120 Assert.assertTrue(qbNodes.contains(n2));
121 Assert.assertFalse(qbNodes.contains(n3));
122 qbNodes.remove(n1);
123 Assert.assertEquals(1, qbNodes.size());
124 Assert.assertFalse(qbNodes.contains(n1));
125 Assert.assertTrue(qbNodes.contains(n2));
126 qbNodes.remove(n2);
127 Assert.assertEquals(0, qbNodes.size());
128 Assert.assertFalse(qbNodes.contains(n1));
129 Assert.assertFalse(qbNodes.contains(n2));
130
131 qbNodes.addAll(Arrays.asList(n1, n2, n3));
132 qbNodes.removeAll(Arrays.asList(n1, n3));
133 Assert.assertEquals(1, qbNodes.size());
134 Assert.assertTrue(qbNodes.contains(n2));
135
136 qbWays.add(w1);
137 qbWays.add(w2);
138 qbWays.add(w3);
139 Assert.assertEquals(3, qbWays.size());
140 Assert.assertTrue(qbWays.contains(w1));
141 Assert.assertTrue(qbWays.contains(w2));
142 Assert.assertTrue(qbWays.contains(w3));
143 qbWays.remove(w1);
144 Assert.assertEquals(2, qbWays.size());
145 Assert.assertFalse(qbWays.contains(w1));
146 Assert.assertTrue(qbWays.contains(w2));
147 Assert.assertTrue(qbWays.contains(w3));
148 qbWays.remove(w2);
149 Assert.assertEquals(1, qbWays.size());
150 Assert.assertFalse(qbWays.contains(w1));
151 Assert.assertFalse(qbWays.contains(w2));
152 Assert.assertTrue(qbWays.contains(w3));
153 qbWays.remove(w3);
154 Assert.assertEquals(0, qbWays.size());
155 Assert.assertFalse(qbWays.contains(w1));
156 Assert.assertFalse(qbWays.contains(w2));
157 Assert.assertFalse(qbWays.contains(w3));
158
159 qbWays.clear();
160 Assert.assertEquals(0, qbWays.size());
161 List<Way> allWays = new ArrayList<>(Arrays.asList(w1, w2, w3));
162 qbWays.addAll(allWays);
163 Assert.assertEquals(3, qbWays.size());
164 int count = 0;
165 for (Way w : qbWays) {
166 Assert.assertTrue(allWays.contains(w));
167 count++;
168 }
169 Assert.assertEquals(3, count);
170 // test remove with iterator
171 Iterator<Way> iter = qbWays.iterator();
172 while (iter.hasNext()) {
173 iter.next();
174 iter.remove();
175 count--;
176 Assert.assertEquals(count, qbWays.size());
177 }
178 Assert.assertEquals(0, qbWays.size());
179
180 }
181
182 /**
183 * Add more data so that quad buckets tree has a few leaves
184 */
185 @Test
186 public void testSplitsWithIncompleteData() {
187 DataSet ds = new DataSet();
188 long nodeId = 1;
189 long wayId = 1;
190 final int NUM_COMPLETE_WAYS = 300;
191 final int NUM_INCOMPLETE_WAYS = 10;
192 final int NUM_NODES_PER_WAY = 20;
193 final int NUM_INCOMPLETE_NODES = 10;
194
195 // force splits in quad buckets
196 Random random = new SecureRandom();
197 for (int i = 0; i < NUM_COMPLETE_WAYS; i++) {
198 Way w = new Way(wayId++);
199 List<Node> nodes = new ArrayList<>();
200 double center = random.nextDouble() * 10;
201 for (int j = 0; j < NUM_NODES_PER_WAY; j++) {
202 Node n = new Node(nodeId++);
203 double lat = random.nextDouble() * 0.001;
204 double lon = random.nextDouble() * 0.001;
205 n.setCoor(new LatLon(center + lat, center + lon));
206 nodes.add(n);
207 ds.addPrimitive(n);
208 }
209 w.setNodes(nodes);
210 ds.addPrimitive(w);
211 }
212 Assert.assertEquals(NUM_COMPLETE_WAYS, ds.getWays().size());
213 Assert.assertEquals(NUM_COMPLETE_WAYS * NUM_NODES_PER_WAY, ds.getNodes().size());
214
215 // add some incomplete nodes
216 for (int i = 0; i < NUM_INCOMPLETE_NODES; i++) {
217 Node n = new Node(nodeId++);
218 n.setIncomplete(true);
219 ds.addPrimitive(n);
220 }
221 Assert.assertEquals(NUM_COMPLETE_WAYS * NUM_NODES_PER_WAY + NUM_INCOMPLETE_NODES, ds.getNodes().size());
222 // add some incomplete ways
223 List<Way> incompleteWays = new ArrayList<>();
224 for (int i = 0; i < NUM_INCOMPLETE_WAYS; i++) {
225 Way w = new Way(wayId++);
226 incompleteWays.add(w);
227 w.setIncomplete(true);
228 ds.addPrimitive(w);
229 }
230 Assert.assertEquals(NUM_COMPLETE_WAYS + NUM_INCOMPLETE_WAYS, ds.getWays().size());
231
232 BBox planet = new BBox(-180, -90, 180, 90);
233 // incomplete ways should not be found with search
234 Assert.assertEquals(NUM_COMPLETE_WAYS, ds.searchWays(planet).size());
235 // incomplete ways are only retrieved via iterator or object reference
236 for (Way w : incompleteWays) {
237 Assert.assertTrue(ds.getWays().contains(w));
238 }
239
240 QuadBuckets<Way> qb = new QuadBuckets<>();
241 qb.addAll(ds.getWays());
242 int count = qb.size();
243 Assert.assertEquals(count, ds.getWays().size());
244 Iterator<Way> iter = qb.iterator();
245 while (iter.hasNext()) {
246 iter.next();
247 iter.remove();
248 count--;
249 Assert.assertEquals(count, qb.size());
250 }
251 Assert.assertEquals(0, qb.size());
252 }
253}
Note: See TracBrowser for help on using the repository browser.