Improve code / xml formatting, closes #54

This commit is contained in:
Emux
2016-07-09 19:45:22 +03:00
parent 7919d0ab9c
commit e793e8851b
458 changed files with 58405 additions and 63062 deletions

View File

@@ -17,127 +17,127 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class MapEventLayerTest {
private MapEventLayer layer;
private Map mockMap;
private ViewController mockViewport;
private Animator mockAnimator;
private ArgumentCaptor<Float> argumentCaptor;
private MapEventLayer layer;
private Map mockMap;
private ViewController mockViewport;
private Animator mockAnimator;
private ArgumentCaptor<Float> argumentCaptor;
@Before
public void setUp() throws Exception {
mockMap = Mockito.mock(Map.class);
mockViewport = Mockito.mock(ViewController.class);
mockAnimator = Mockito.mock(Animator.class);
layer = new MapEventLayer(mockMap);
when(mockMap.viewport()).thenReturn(mockViewport);
when(mockMap.animator()).thenReturn(mockAnimator);
when(mockMap.getHeight()).thenReturn(6);
argumentCaptor = ArgumentCaptor.forClass(float.class);
}
@Before
public void setUp() throws Exception {
mockMap = Mockito.mock(Map.class);
mockViewport = Mockito.mock(ViewController.class);
mockAnimator = Mockito.mock(Animator.class);
layer = new MapEventLayer(mockMap);
when(mockMap.viewport()).thenReturn(mockViewport);
when(mockMap.animator()).thenReturn(mockAnimator);
when(mockMap.getHeight()).thenReturn(6);
argumentCaptor = ArgumentCaptor.forClass(float.class);
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(layer).isNotNull();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(layer).isNotNull();
}
@Test
public void doubleTap_shouldAnimateZoom() throws Exception {
simulateDoubleTap();
verify(mockAnimator).animateZoom(300, 2, 1.0f, -2.0f);
}
@Test
public void doubleTap_shouldAnimateZoom() throws Exception {
simulateDoubleTap();
verify(mockAnimator).animateZoom(300, 2, 1.0f, -2.0f);
}
@Test
public void doubleTap_shouldAnimateZoomAfterDoubleTouchDrag() throws Exception {
simulateDoubleTouchDragUp();
simulateDoubleTap();
verify(mockAnimator).animateZoom(300, 2, 1.0f, -2.0f);
}
@Test
public void doubleTap_shouldAnimateZoomAfterDoubleTouchDrag() throws Exception {
simulateDoubleTouchDragUp();
simulateDoubleTap();
verify(mockAnimator).animateZoom(300, 2, 1.0f, -2.0f);
}
@Test
public void doubleTouchDrag_shouldNotAnimateZoom() throws Exception {
simulateDoubleTouchDragUp();
verify(mockAnimator, never()).animateZoom(any(long.class), any(double.class),
any(float.class), any(float.class));
}
@Test
public void doubleTouchDrag_shouldNotAnimateZoom() throws Exception {
simulateDoubleTouchDragUp();
verify(mockAnimator, never()).animateZoom(any(long.class), any(double.class),
any(float.class), any(float.class));
}
@Test
public void doubleTouchDragUp_shouldDecreaseContentScale() throws Exception {
simulateDoubleTouchDragUp();
verify(mockViewport).scaleMap(argumentCaptor.capture(), any(float.class), any(float.class));
assertThat(argumentCaptor.getValue()).isLessThan(1);
}
@Test
public void doubleTouchDragUp_shouldDecreaseContentScale() throws Exception {
simulateDoubleTouchDragUp();
verify(mockViewport).scaleMap(argumentCaptor.capture(), any(float.class), any(float.class));
assertThat(argumentCaptor.getValue()).isLessThan(1);
}
@Test
public void doubleTouchDragDown_shouldIncreaseContentScale() throws Exception {
simulateDoubleTouchDragDown();
verify(mockViewport).scaleMap(argumentCaptor.capture(), any(float.class), any(float.class));
assertThat(argumentCaptor.getValue()).isGreaterThan(1);
}
@Test
public void doubleTouchDragDown_shouldIncreaseContentScale() throws Exception {
simulateDoubleTouchDragDown();
verify(mockViewport).scaleMap(argumentCaptor.capture(), any(float.class), any(float.class));
assertThat(argumentCaptor.getValue()).isGreaterThan(1);
}
private void simulateDoubleTap() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_UP, 1, 1));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 1));
}
private void simulateDoubleTap() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_UP, 1, 1));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 1));
}
private void simulateDoubleTouchDragUp() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_MOVE, 1, 0));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_MOVE, -100, 0));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 0));
}
private void simulateDoubleTouchDragUp() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_MOVE, 1, 0));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_MOVE, -100, 0));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 0));
}
private void simulateDoubleTouchDragDown() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_MOVE, 1, 2));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_MOVE, 100, 2));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 2));
}
private void simulateDoubleTouchDragDown() {
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_DOWN, 1, 1));
layer.onGesture(Gesture.DOUBLE_TAP, new TestMotionEvent(MotionEvent.ACTION_MOVE, 1, 2));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_MOVE, 100, 2));
layer.onTouchEvent(new TestMotionEvent(MotionEvent.ACTION_UP, 1, 2));
}
class TestMotionEvent extends MotionEvent {
final int action;
final float x;
final float y;
class TestMotionEvent extends MotionEvent {
final int action;
final float x;
final float y;
public TestMotionEvent(int action, float x, float y) {
this.action = action;
this.x = x;
this.y = y;
}
public TestMotionEvent(int action, float x, float y) {
this.action = action;
this.x = x;
this.y = y;
}
@Override
public long getTime() {
return 0;
}
@Override
public long getTime() {
return 0;
}
@Override
public int getAction() {
return action;
}
@Override
public int getAction() {
return action;
}
@Override
public float getX() {
return x;
}
@Override
public float getX() {
return x;
}
@Override
public float getY() {
return y;
}
@Override
public float getY() {
return y;
}
@Override
public float getX(int idx) {
return x;
}
@Override
public float getX(int idx) {
return x;
}
@Override
public float getY(int idx) {
return y;
}
@Override
public float getY(int idx) {
return y;
}
@Override
public int getPointerCount() {
return 0;
}
}
@Override
public int getPointerCount() {
return 0;
}
}
}

View File

@@ -1,168 +1,168 @@
package org.oscim.layers.tile;
import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Test;
import org.oscim.layers.tile.MapTile.TileNode;
import org.oscim.renderer.MapRenderer;
import static org.fest.assertions.api.Assertions.assertThat;
public class TileRendererTest {
static {
/* makes default minFadeTime == 1000 */
MapRenderer.frametime = 1050;
}
static {
/* makes default minFadeTime == 1000 */
MapRenderer.frametime = 1050;
}
static TileNode makeNode(int fadeTime, TileNode parent) {
TileNode n = new TileNode();
n.item = new MapTile(n, 0, 0, 0);
n.item.fadeTime = fadeTime;
static TileNode makeNode(int fadeTime, TileNode parent) {
TileNode n = new TileNode();
n.item = new MapTile(n, 0, 0, 0);
n.item.fadeTime = fadeTime;
if (parent == null) {
// root node is its own parent
n.parent = n;
} else {
parent.child00 = n;
n.parent = parent;
}
if (parent == null) {
// root node is its own parent
n.parent = n;
} else {
parent.child00 = n;
n.parent = parent;
}
return n;
}
return n;
}
@Test
public void shouldGetMinFadeTimeOf1000() {
@Test
public void shouldGetMinFadeTimeOf1000() {
TileNode r = makeNode(0, null);
TileNode n = makeNode(0, r);
TileNode r = makeNode(0, null);
TileNode n = makeNode(0, r);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
assertThat(fadeTime).isEqualTo(1000);
}
assertThat(fadeTime).isEqualTo(1000);
}
@Test
public void shouldGetMinFadeTimeFromParent() {
@Test
public void shouldGetMinFadeTimeFromParent() {
TileNode r = makeNode(100, null);
TileNode n = makeNode(0, r);
TileNode r = makeNode(100, null);
TileNode n = makeNode(0, r);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromGrandparent() {
@Test
public void shouldGetMinFadeTimeFromGrandparent() {
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromParentWhenParentsTimeIsLessThanGrandpasTime() {
@Test
public void shouldGetMinFadeTimeFromParentWhenParentsTimeIsLessThanGrandpasTime() {
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromParentForParentProxy() {
@Test
public void shouldGetMinFadeTimeFromParentForParentProxy() {
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(n.item, -1);
long fadeTime = TileRenderer.getMinFade(n.item, -1);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldNotGetMinFadeTimeFromGradparentForParentProxy() {
/* (as the grandparent will not be locked) */
@Test
public void shouldNotGetMinFadeTimeFromGradparentForParentProxy() {
/* (as the grandparent will not be locked) */
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(n.item, -1);
long fadeTime = TileRenderer.getMinFade(n.item, -1);
assertThat(fadeTime).isNotEqualTo(100);
assertThat(fadeTime).isEqualTo(200);
}
assertThat(fadeTime).isNotEqualTo(100);
assertThat(fadeTime).isEqualTo(200);
}
@Test
public void shouldGetMinFadeTimeFromParentForChildProxy() {
@Test
public void shouldGetMinFadeTimeFromParentForChildProxy() {
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(200, null);
TileNode p = makeNode(100, r);
TileNode n = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(n.item, 1);
long fadeTime = TileRenderer.getMinFade(n.item, 1);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromGrandparentForChildProxy() {
@Test
public void shouldGetMinFadeTimeFromGrandparentForChildProxy() {
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode c = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode c = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(c.item, 1);
long fadeTime = TileRenderer.getMinFade(c.item, 1);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromGrandgrandparentForChildProxy() {
@Test
public void shouldGetMinFadeTimeFromGrandgrandparentForChildProxy() {
TileNode r = makeNode(100, null);
TileNode gp = makeNode(200, r);
TileNode p = makeNode(200, gp);
TileNode c = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode gp = makeNode(200, r);
TileNode p = makeNode(200, gp);
TileNode c = makeNode(0, p);
long fadeTime = TileRenderer.getMinFade(c.item, 1);
long fadeTime = TileRenderer.getMinFade(c.item, 1);
assertThat(fadeTime).isEqualTo(100);
}
assertThat(fadeTime).isEqualTo(100);
}
@Test
public void shouldGetMinFadeTimeFromChild() {
@Test
public void shouldGetMinFadeTimeFromChild() {
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
n.child00 = makeNode(50, n);
n.child00 = makeNode(50, n);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
long fadeTime = TileRenderer.getMinFade(n.item, 0);
assertThat(fadeTime).isEqualTo(50);
}
assertThat(fadeTime).isEqualTo(50);
}
@Test
public void shouldGetMinFadeTimeForParentFromChild() {
@Test
public void shouldGetMinFadeTimeForParentFromChild() {
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
TileNode r = makeNode(100, null);
TileNode p = makeNode(200, r);
TileNode n = makeNode(0, p);
n.child00 = makeNode(50, n);
n.child00 = makeNode(50, n);
long fadeTime = TileRenderer.getMinFade(p.item, -1);
long fadeTime = TileRenderer.getMinFade(p.item, -1);
assertThat(fadeTime).isEqualTo(50);
}
assertThat(fadeTime).isEqualTo(50);
}
}

View File

@@ -1,11 +1,9 @@
package org.oscim.tiling.source;
import static org.fest.assertions.api.Assertions.assertThat;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.squareup.okhttp.HttpResponseCache;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import org.junit.After;
import org.junit.Before;
@@ -13,92 +11,94 @@ import org.junit.Test;
import org.oscim.core.Tile;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
import com.squareup.okhttp.HttpResponseCache;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.fest.assertions.api.Assertions.assertThat;
public class OkHttpEngineTest {
private OkHttpEngine engine;
private MockWebServer server;
private MockResponse mockResponse;
private HttpResponseCache cache;
private OkHttpEngine engine;
private MockWebServer server;
private MockResponse mockResponse;
private HttpResponseCache cache;
@Before
public void setUp() throws Exception {
mockResponse = new MockResponse();
mockResponse.setBody("TEST RESPONSE".getBytes());
server = new MockWebServer();
server.enqueue(mockResponse);
server.play();
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
}
@Before
public void setUp() throws Exception {
mockResponse = new MockResponse();
mockResponse.setBody("TEST RESPONSE".getBytes());
server = new MockWebServer();
server.enqueue(mockResponse);
server.play();
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
}
@After
public void tearDown() throws Exception {
server.shutdown();
}
@After
public void tearDown() throws Exception {
server.shutdown();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(engine).isNotNull();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(engine).isNotNull();
}
@Test(expected = IllegalArgumentException.class)
public void sendRequest_shouldRejectNullTile() throws Exception {
engine.sendRequest(null);
}
@Test(expected = IllegalArgumentException.class)
public void sendRequest_shouldRejectNullTile() throws Exception {
engine.sendRequest(null);
}
@Test
public void sendRequest_shouldAppendXYZToPath() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
@Test
public void sendRequest_shouldAppendXYZToPath() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
}
RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
}
@Test
public void read_shouldReturnResponseStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
@Test
public void read_shouldReturnResponseStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
InputStream responseStream = engine.read();
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
assertThat(response).isEqualTo("TEST RESPONSE");
}
InputStream responseStream = engine.read();
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
assertThat(response).isEqualTo("TEST RESPONSE");
}
// @Test(expected = IOException.class)
// public void close_shouldCloseInputStream() throws Exception {
// engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
// engine.close();
// // Calling read after the stream is closed should throw an exception.
// InputStream responseStream = engine.read();
// responseStream.read();
// }
//
// @Test(expected = IOException.class)
// public void requestCompleted_shouldCloseInputStream() throws Exception {
// engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
// engine.requestCompleted(true);
// // Calling read after the stream is closed should throw an exception.
// InputStream responseStream = engine.read();
// responseStream.read();
// }
// @Test(expected = IOException.class)
// public void close_shouldCloseInputStream() throws Exception {
// engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
// engine.close();
// // Calling read after the stream is closed should throw an exception.
// InputStream responseStream = engine.read();
// responseStream.read();
// }
//
// @Test(expected = IOException.class)
// public void requestCompleted_shouldCloseInputStream() throws Exception {
// engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
// engine.requestCompleted(true);
// // Calling read after the stream is closed should throw an exception.
// InputStream responseStream = engine.read();
// responseStream.read();
// }
@Test
public void requestCompleted_shouldReturnValueGiven() throws Exception {
assertThat(engine.requestCompleted(true)).isTrue();
assertThat(engine.requestCompleted(false)).isFalse();
}
@Test
public void requestCompleted_shouldReturnValueGiven() throws Exception {
assertThat(engine.requestCompleted(true)).isTrue();
assertThat(engine.requestCompleted(false)).isFalse();
}
@Test
public void create_shouldUseTileSourceCache() throws Exception {
cache = new HttpResponseCache(new File("tmp"), 1024);
OSciMap4TileSource tileSource =
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.requestCompleted(true);
assertThat(cache.getRequestCount()).isEqualTo(1);
}
@Test
public void create_shouldUseTileSourceCache() throws Exception {
cache = new HttpResponseCache(new File("tmp"), 1024);
OSciMap4TileSource tileSource =
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.requestCompleted(true);
assertThat(cache.getRequestCount()).isEqualTo(1);
}
}

View File

@@ -16,64 +16,64 @@
*/
package org.oscim.tiling.source;
import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSource;
import static org.fest.assertions.api.Assertions.assertThat;
public class UrlTileSourceTest {
private UrlTileSource tileSource;
private UrlTileSource tileSource;
@Before
public void setUp() throws Exception {
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
}
@Before
public void setUp() throws Exception {
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
}
@Test
public void setApiKey_shouldAppendApiKey() throws Exception {
tileSource.setApiKey("testkey");
assertThat(tileSource.getTileUrl(new Tile(0, 0, (byte) 0))).endsWith("?api_key=testkey");
}
@Test
public void setApiKey_shouldAppendApiKey() throws Exception {
tileSource.setApiKey("testkey");
assertThat(tileSource.getTileUrl(new Tile(0, 0, (byte) 0))).endsWith("?api_key=testkey");
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldUseDefaultHttpEngine() throws Exception {
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(LwHttp.class);
}
@Test
public void shouldUseDefaultHttpEngine() throws Exception {
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(LwHttp.class);
}
@Test
public void shouldUseCustomHttpEngine() throws Exception {
tileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory());
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class);
}
@Test
public void shouldUseCustomHttpEngine() throws Exception {
tileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory());
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class);
}
class TestTileSource extends UrlTileSource {
public TestTileSource(String urlString, String tilePath) {
super(urlString, tilePath);
}
class TestTileSource extends UrlTileSource {
public TestTileSource(String urlString, String tilePath) {
super(urlString, tilePath);
}
@Override
public ITileDataSource getDataSource() {
return new TestTileDataSource(this, null, getHttpEngine());
}
}
@Override
public ITileDataSource getDataSource() {
return new TestTileDataSource(this, null, getHttpEngine());
}
}
class TestTileDataSource extends UrlTileDataSource {
public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder,
HttpEngine conn) {
super(tileSource, tileDecoder, conn);
}
class TestTileDataSource extends UrlTileDataSource {
public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder,
HttpEngine conn) {
super(tileSource, tileDecoder, conn);
}
public HttpEngine getConnection() {
return mConn;
}
}
public HttpEngine getConnection() {
return mConn;
}
}
}

View File

@@ -1,7 +1,5 @@
package org.oscim.tiling.source.bitmap;
import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@@ -13,66 +11,68 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
import static org.fest.assertions.api.Assertions.assertThat;
public class BitmapTileSourceTest {
private BitmapTileSource tileSource;
private BitmapTileSource tileSource;
@Before
public void setUp() throws Exception {
tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18);
}
@Before
public void setUp() throws Exception {
tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18);
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldUseLwHttp() throws Exception {
LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(lwHttp).close();
}
@Test
public void shouldUseLwHttp() throws Exception {
LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(lwHttp).close();
}
@Test
public void shouldUseOkHttp() throws Exception {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(okHttp).close();
}
@Test
public void shouldUseOkHttp() throws Exception {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(okHttp).close();
}
@Test
public void shouldUseBuilderConfig() {
BitmapTileSource ts = BitmapTileSource.builder()
.url("http://example.com")
.zoomMax(42)
.zoomMin(23)
.fadeSteps(new FadeStep[] { new FadeStep(0, 10, 0.5f, 1.0f) })
.build();
@Test
public void shouldUseBuilderConfig() {
BitmapTileSource ts = BitmapTileSource.builder()
.url("http://example.com")
.zoomMax(42)
.zoomMin(23)
.fadeSteps(new FadeStep[]{new FadeStep(0, 10, 0.5f, 1.0f)})
.build();
assertThat(ts.getUrl().getHost()).isEqualTo("example.com");
assertThat(ts.getZoomLevelMin()).isEqualTo(23);
assertThat(ts.getZoomLevelMax()).isEqualTo(42);
assertThat(ts.getFadeSteps()).isNotNull();
}
assertThat(ts.getUrl().getHost()).isEqualTo("example.com");
assertThat(ts.getZoomLevelMin()).isEqualTo(23);
assertThat(ts.getZoomLevelMax()).isEqualTo(42);
assertThat(ts.getFadeSteps()).isNotNull();
}
/**
* Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/
class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine;
/**
* Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/
class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine;
public TestHttpFactory(HttpEngine engine) {
this.engine = engine;
}
public TestHttpFactory(HttpEngine engine) {
this.engine = engine;
}
@Override
public HttpEngine create(UrlTileSource tileSource) {
return engine;
}
}
@Override
public HttpEngine create(UrlTileSource tileSource) {
return engine;
}
}
}

View File

@@ -1,7 +1,5 @@
package org.oscim.tiling.source.oscimap4;
import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@@ -11,51 +9,53 @@ import org.oscim.tiling.source.LwHttp;
import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import static org.fest.assertions.api.Assertions.assertThat;
public class OSciMap4TileSourceTest {
private OSciMap4TileSource tileSource;
private OSciMap4TileSource tileSource;
@Before
public void setUp() throws Exception {
tileSource = new OSciMap4TileSource("http://www.example.org/tiles/vtm");
}
@Before
public void setUp() throws Exception {
tileSource = new OSciMap4TileSource("http://www.example.org/tiles/vtm");
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}
@Test
public void shouldUseLwHttp() throws Exception {
LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(lwHttp).close();
}
@Test
public void shouldUseLwHttp() throws Exception {
LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(lwHttp).close();
}
@Test
public void shouldUseOkHttp() throws Exception {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(okHttp).close();
}
@Test
public void shouldUseOkHttp() throws Exception {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
ITileDataSource dataSource = tileSource.getDataSource();
dataSource.dispose();
Mockito.verify(okHttp).close();
}
/**
* Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/
class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine;
/**
* Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/
class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine;
public TestHttpFactory(HttpEngine engine) {
this.engine = engine;
}
public TestHttpFactory(HttpEngine engine) {
this.engine = engine;
}
@Override
public HttpEngine create(UrlTileSource tileSource) {
return engine;
}
}
@Override
public HttpEngine create(UrlTileSource tileSource) {
return engine;
}
}
}

View File

@@ -1,70 +1,70 @@
package org.oscim.utils;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
import org.oscim.utils.KeyMap.HashItem;
import java.util.ArrayList;
public class KeyMapTest {
static class Item extends HashItem {
int key;
static class Item extends HashItem {
int key;
public Item(int i) {
key = i;
}
public Item(int i) {
key = i;
}
@Override
public boolean equals(Object obj) {
Item it = (Item) obj;
@Override
public boolean equals(Object obj) {
Item it = (Item) obj;
return it.key == key;
}
return it.key == key;
}
@Override
public int hashCode() {
return key;
}
}
@Override
public int hashCode() {
return key;
}
}
@Test
public void shouldWork1() {
KeyMap<Item> map = new KeyMap<Item>(512);
KeyMap<Item> map2 = new KeyMap<Item>(512);
@Test
public void shouldWork1() {
KeyMap<Item> map = new KeyMap<Item>(512);
KeyMap<Item> map2 = new KeyMap<Item>(512);
ArrayList<Item> items = new ArrayList<Item>(1024);
ArrayList<Item> items = new ArrayList<Item>(1024);
for (int i = 0; i < 10000; i++) {
Item it = new Item(i);
items.add(it);
Assert.assertNull(map.put(it));
}
for (int i = 0; i < 10000; i++) {
Item it = new Item(i);
items.add(it);
Assert.assertNull(map.put(it));
}
for (Item it : items) {
Item it2 = map.remove(it);
Assert.assertTrue(it == it2);
map2.put(it2);
}
for (Item it : items) {
Item it2 = map.remove(it);
Assert.assertTrue(it == it2);
map2.put(it2);
}
for (Item it : items) {
Item it2 = map2.get(it);
Assert.assertTrue(it == it2);
}
for (Item it : items) {
Item it2 = map2.get(it);
Assert.assertTrue(it == it2);
}
/* replace the items with itself */
for (Item it : items) {
Item it2 = map2.put(it);
Assert.assertTrue(it == it2);
}
}
for (Item it : items) {
Item it2 = map2.put(it);
Assert.assertTrue(it == it2);
}
}
@Test
public void shouldWork2() {
for (int i = 0; i < 100; i++) {
long start = System.currentTimeMillis();
shouldWork1();
System.out.println("time: " + ((System.currentTimeMillis() - start)));
@Test
public void shouldWork2() {
for (int i = 0; i < 100; i++) {
long start = System.currentTimeMillis();
shouldWork1();
System.out.println("time: " + ((System.currentTimeMillis() - start)));
}
}
}
}
}

View File

@@ -1,234 +1,235 @@
package org.oscim.utils;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
import org.oscim.core.Box;
import org.oscim.utils.SpatialIndex.SearchCb;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
public class QuadTreeTest {
final static Random rand = new Random((long) (Math.PI * 10000000));
final static Random rand = new Random((long) (Math.PI * 10000000));
public class Item {
final int val;
final Box bbox;;
public class Item {
final int val;
final Box bbox;
;
Item(Box bbox, int val) {
this.val = val;
this.bbox = new Box(bbox);
}
Item(Box bbox, int val) {
this.val = val;
this.bbox = new Box(bbox);
}
@Override
public String toString() {
return String.valueOf(val) + ' ' + bbox;
}
}
@Override
public String toString() {
return String.valueOf(val) + ' ' + bbox;
}
}
ArrayList<Item> fillRandomTree(SpatialIndex<Item> q, int numItems) {
Box box = new Box();
ArrayList<Item> items = new ArrayList<Item>(numItems + 16);
ArrayList<Item> fillRandomTree(SpatialIndex<Item> q, int numItems) {
Box box = new Box();
ArrayList<Item> items = new ArrayList<Item>(numItems + 16);
for (int i = 0; i < numItems; i++) {
box.xmin = (int) (rand.nextDouble() * 10000 - 5000);
box.ymin = (int) (rand.nextDouble() * 10000 - 5000);
box.xmax = (int) (box.xmin + rand.nextDouble() * 500);
box.ymax = (int) (box.ymin + rand.nextDouble() * 500);
for (int i = 0; i < numItems; i++) {
box.xmin = (int) (rand.nextDouble() * 10000 - 5000);
box.ymin = (int) (rand.nextDouble() * 10000 - 5000);
box.xmax = (int) (box.xmin + rand.nextDouble() * 500);
box.ymax = (int) (box.ymin + rand.nextDouble() * 500);
Item it = new Item(box, i);
q.insert(box, it);
Item it = new Item(box, i);
q.insert(box, it);
items.add(it);
items.add(it);
}
return items;
}
}
return items;
}
@Test
public void shouldWork0() {
@Test
public void shouldWork0() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
//SpatialIndex<Item> q = new RTree<Item>();
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
//SpatialIndex<Item> q = new RTree<Item>();
int numItems = 10000;
int numItems = 10000;
List<Item> items = fillRandomTree(q, numItems);
List<Item> items = fillRandomTree(q, numItems);
final int[] found = { 0 };
final int[] matched = { 0 };
final int[] found = {0};
final int[] matched = {0};
for (Item it : items) {
int f = matched[0];
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
found[0]++;
if (context == item) {
matched[0]++;
return false;
}
return true;
}
}, it);
for (Item it : items) {
int f = matched[0];
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
found[0]++;
if (context == item) {
matched[0]++;
return false;
}
return true;
}
}, it);
if (f == matched[0])
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
}
if (f == matched[0])
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
}
//out.println("m:" + matched[0] + " f:" + found[0]);
Assert.assertEquals(numItems, matched[0]);
Assert.assertEquals(numItems, q.size());
}
//out.println("m:" + matched[0] + " f:" + found[0]);
Assert.assertEquals(numItems, matched[0]);
Assert.assertEquals(numItems, q.size());
}
@Test
public void shouldWork1() {
long time = currentTimeMillis();
@Test
public void shouldWork1() {
long time = currentTimeMillis();
for (int i = 0; i < 10; i++) {
shouldWork0();
}
for (int i = 0; i < 10; i++) {
shouldWork0();
}
long now = currentTimeMillis();
out.println("==>" + (now - time) / 10.0f + "ms");
}
long now = currentTimeMillis();
out.println("==>" + (now - time) / 10.0f + "ms");
}
@Test
public void shouldWork6() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
@Test
public void shouldWork6() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
Box box = new Box(-4184.653317773969,
3183.6174297948446,
-4088.3197324911957,
3222.7770427421046);
Box box = new Box(-4184.653317773969,
3183.6174297948446,
-4088.3197324911957,
3222.7770427421046);
Item it = new Item(box, 1);
q.insert(box, it);
Item it = new Item(box, 1);
q.insert(box, it);
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
out.println("==> " + item + " " + (context == item));
return true;
}
}, it);
Assert.assertEquals(1, q.size());
}
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
out.println("==> " + item + " " + (context == item));
return true;
}
}, it);
Assert.assertEquals(1, q.size());
}
@Test
public void shouldWork7() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 14);
//SpatialIndex<Item> q = new RTree<Item>();
@Test
public void shouldWork7() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 14);
//SpatialIndex<Item> q = new RTree<Item>();
int numItems = 10000;
int numItems = 10000;
List<Item> items = fillRandomTree(q, numItems);
List<Item> items = fillRandomTree(q, numItems);
Assert.assertEquals(numItems, q.size());
Assert.assertEquals(numItems, q.size());
int cnt = numItems;
for (Item it : items) {
if (!q.remove(it.bbox, it)) {
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
int cnt = numItems;
for (Item it : items) {
if (!q.remove(it.bbox, it)) {
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
if (context == item) {
out.println("found...");
return false;
}
return true;
}
}, it);
}
Assert.assertEquals(--cnt, q.size());
}
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
if (context == item) {
out.println("found...");
return false;
}
return true;
}
}, it);
}
Assert.assertEquals(--cnt, q.size());
}
items = fillRandomTree(q, numItems);
items = fillRandomTree(q, numItems);
Assert.assertEquals(numItems, q.size());
Assert.assertEquals(numItems, q.size());
cnt = numItems;
for (Item it : items) {
if (!q.remove(it.bbox, it))
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " => " + it);
cnt = numItems;
for (Item it : items) {
if (!q.remove(it.bbox, it))
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " => " + it);
Assert.assertEquals(--cnt, q.size());
}
Assert.assertEquals(0, q.size());
out.println("");
}
Assert.assertEquals(--cnt, q.size());
}
Assert.assertEquals(0, q.size());
out.println("");
}
@Test
public void shouldWork8() {
@Test
public void shouldWork8() {
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
//SpatialIndex<Item> q = new RTree<Item>();
SpatialIndex<Item> q = new QuadTree<Item>(Short.MAX_VALUE + 1, 16);
//SpatialIndex<Item> q = new RTree<Item>();
int numItems = 10000;
int numItems = 10000;
List<Item> items = fillRandomTree(q, numItems);
List<Item> items = fillRandomTree(q, numItems);
final int[] found = { 0 };
final int[] matched = { 0 };
final int[] found = {0};
final int[] matched = {0};
for (Item it : items) {
int f = matched[0];
int cnt = 0;
for (Item it2 : items) {
if (it2.bbox.overlap(it.bbox))
cnt++;
}
found[0] = 0;
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
found[0]++;
if (context == item) {
matched[0]++;
//return false;
}
return true;
}
}, it);
for (Item it : items) {
int f = matched[0];
int cnt = 0;
for (Item it2 : items) {
if (it2.bbox.overlap(it.bbox))
cnt++;
}
found[0] = 0;
q.search(it.bbox, new SearchCb<Item>() {
@Override
public boolean call(Item item, Object context) {
found[0]++;
if (context == item) {
matched[0]++;
//return false;
}
return true;
}
}, it);
if (found[0] != cnt) {
out.println("not found " + (found[0] - cnt));
}
//Assert.assertEquals(cnt, found[0]);
if (f == matched[0])
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
}
if (found[0] != cnt) {
out.println("not found " + (found[0] - cnt));
}
//Assert.assertEquals(cnt, found[0]);
if (f == matched[0])
out.println((it.bbox.xmax - it.bbox.xmin)
+ " x " + (it.bbox.ymax - it.bbox.ymin)
+ " ==> " + it);
}
//out.println("m:" + matched[0] + " f:" + found[0]);
Assert.assertEquals(numItems, matched[0]);
Assert.assertEquals(numItems, q.size());
}
//out.println("m:" + matched[0] + " f:" + found[0]);
Assert.assertEquals(numItems, matched[0]);
Assert.assertEquals(numItems, q.size());
}
public static void main(String[] args) {
QuadTreeTest qt = new QuadTreeTest();
public static void main(String[] args) {
QuadTreeTest qt = new QuadTreeTest();
long time = currentTimeMillis();
long time = currentTimeMillis();
for (int i = 0; i < 100; i++) {
qt.shouldWork0();
long now = currentTimeMillis();
for (int i = 0; i < 100; i++) {
qt.shouldWork0();
long now = currentTimeMillis();
out.println("==>" + (now - time));
time = now;
}
}
out.println("==>" + (now - time));
time = now;
}
}
}

View File

@@ -16,212 +16,212 @@
*/
package org.oscim.utils;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
import org.oscim.core.Box;
import org.oscim.utils.SpatialIndex.SearchCb;
import java.util.ArrayList;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class RTreeTest {
public class Item {
final int val;
final double[] min;
final double[] max;
public class Item {
final int val;
final double[] min;
final double[] max;
Item(final double[] min, final double[] max, int val) {
this.val = val;
this.min = min.clone();
this.max = max.clone();
}
Item(final double[] min, final double[] max, int val) {
this.val = val;
this.min = min.clone();
this.max = max.clone();
}
@Override
public String toString() {
// return val + "/"
// + Arrays.toString(min) + "/"
// + Arrays.toString(max);
return String.valueOf(val);
}
}
@Override
public String toString() {
// return val + "/"
// + Arrays.toString(min) + "/"
// + Arrays.toString(max);
return String.valueOf(val);
}
}
@Test
public void shouldRemove() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 1, 1 };
Item it = new Item(min, max, 1);
t.insert(min, max, it);
Assert.assertEquals(1, t.size());
t.remove(min, max, it);
Assert.assertEquals(0, t.size());
}
@Test
public void shouldRemove() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {1, 1};
Item it = new Item(min, max, 1);
t.insert(min, max, it);
Assert.assertEquals(1, t.size());
t.remove(min, max, it);
Assert.assertEquals(0, t.size());
}
@Test
public void shouldWork() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 1, 1 };
for (int i = 0; i < 1000; i++) {
@Test
public void shouldWork() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {1, 1};
for (int i = 0; i < 1000; i++) {
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
min[0]++;
min[1]++;
min[0]++;
min[1]++;
max[0]++;
max[1]++;
max[0]++;
max[1]++;
}
Assert.assertEquals(1000, t.size());
}
Assert.assertEquals(1000, t.size());
min[0] = 0;
min[1] = 0;
// max[0] = 4;
// max[1] = 4;
min[0] = 0;
min[1] = 0;
// max[0] = 4;
// max[1] = 4;
final ArrayList<Item> results = new ArrayList<Item>();
final ArrayList<Item> results = new ArrayList<Item>();
t.search(min, max, new SearchCb<RTreeTest.Item>() {
@Override
public boolean call(Item item, Object context) {
//out.println("found: " + item);
results.add(item);
return true;
}
},
null);
t.search(min, max, new SearchCb<RTreeTest.Item>() {
@Override
public boolean call(Item item, Object context) {
//out.println("found: " + item);
results.add(item);
return true;
}
},
null);
for (int i = 999; i >= 0; i--) {
Item it = results.remove(i);
//boolean removed =
t.remove(it.min, it.max, it);
//out.println("REMOVED: " + it + " " + removed);
for (int i = 999; i >= 0; i--) {
Item it = results.remove(i);
//boolean removed =
t.remove(it.min, it.max, it);
//out.println("REMOVED: " + it + " " + removed);
Assert.assertEquals(i, t.size());
}
Assert.assertEquals(0, t.size());
}
Assert.assertEquals(i, t.size());
}
Assert.assertEquals(0, t.size());
}
@Test
public void shouldStack1() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 1, 1 };
@Test
public void shouldStack1() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {1, 1};
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
min[0]++;
min[1]++;
min[0]++;
min[1]++;
max[0]++;
max[1]++;
max[0]++;
max[1]++;
}
assertEquals(numItems, t.size());
}
assertEquals(numItems, t.size());
min[0] = 0;
min[1] = 0;
min[0] = 0;
min[1] = 0;
final ArrayList<Item> results = new ArrayList<Item>();
final ArrayList<Item> results = new ArrayList<Item>();
t.search(min, max, new SearchCb<RTreeTest.Item>() {
@Override
public boolean call(Item item, Object context) {
//out.println("found: " + item);
results.add(item);
return true;
}
}, null);
t.search(min, max, new SearchCb<RTreeTest.Item>() {
@Override
public boolean call(Item item, Object context) {
//out.println("found: " + item);
results.add(item);
return true;
}
}, null);
assertEquals(results.size(), numItems);
assertEquals(results.size(), numItems);
// for (int i = 999; i >= 0; i--) {
// Item it = results.remove(i);
// boolean removed = t.remove(it.min, it.max, it);
// //out.println("REMOVED: " + it + " " + removed);
//
// Assert.assertEquals(i, t.count());
// }
// Assert.assertEquals(0, t.count());
}
// for (int i = 999; i >= 0; i--) {
// Item it = results.remove(i);
// boolean removed = t.remove(it.min, it.max, it);
// //out.println("REMOVED: " + it + " " + removed);
//
// Assert.assertEquals(i, t.count());
// }
// Assert.assertEquals(0, t.count());
}
@Test
public void shouldStack2() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 1, 1 };
@Test
public void shouldStack2() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {1, 1};
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
Item it = new Item(min, max, i);
//out.println("insert: " + it.val);
t.insert(min, max, it);
min[0]++;
min[1]++;
min[0]++;
min[1]++;
max[0]++;
max[1]++;
max[0]++;
max[1]++;
}
assertEquals(numItems, t.size());
}
assertEquals(numItems, t.size());
min[0] = 0;
min[1] = 0;
min[0] = 0;
min[1] = 0;
final ArrayList<Item> results = new ArrayList<Item>();
final ArrayList<Item> results = new ArrayList<Item>();
Box bbox = new Box(min[0], min[1], max[0], max[1]);
Box bbox = new Box(min[0], min[1], max[0], max[1]);
t.search(bbox, results);
t.search(bbox, results);
assertEquals(numItems, results.size());
assertEquals(numItems, results.size());
// for (int i = 999; i >= 0; i--) {
// Item it = results.remove(i);
// boolean removed = t.remove(it.min, it.max, it);
// //out.println("REMOVED: " + it + " " + removed);
//
// Assert.assertEquals(i, t.count());
// }
// Assert.assertEquals(0, t.count());
}
// for (int i = 999; i >= 0; i--) {
// Item it = results.remove(i);
// boolean removed = t.remove(it.min, it.max, it);
// //out.println("REMOVED: " + it + " " + removed);
//
// Assert.assertEquals(i, t.count());
// }
// Assert.assertEquals(0, t.count());
}
@Test
public void shouldWork0() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 0, 0 };
Random r = new Random((long) (Math.PI * 10000000));
ArrayList<Item> items = new ArrayList<Item>();
@Test
public void shouldWork0() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {0, 0};
Random r = new Random((long) (Math.PI * 10000000));
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < 10000; i++) {
min[0] = r.nextDouble() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 100;
max[1] = min[1] + Math.random() * 100;
for (int i = 0; i < 10000; i++) {
min[0] = r.nextDouble() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 100;
max[1] = min[1] + Math.random() * 100;
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Assert.assertEquals(10000, t.size());
Assert.assertEquals(10000, t.size());
/*SearchCb<RTreeTest.Item> cb = new SearchCb<RTreeTest.Item>() {
@Override
@Override
public boolean call(Item item, Object context) {
//out.println("found: " + item);
//results.add(item);
@@ -236,98 +236,98 @@ public class RTreeTest {
}
System.out.println("found: " + counter);*/
}
}
@Test
public void shouldWork1() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 0, 0 };
@Test
public void shouldWork1() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {0, 0};
ArrayList<Item> items = new ArrayList<Item>();
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < 10000; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
for (int i = 0; i < 10000; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Assert.assertEquals(10000, t.size());
Assert.assertEquals(10000, t.size());
for (Item it : items)
t.remove(it.min, it.max, it);
for (Item it : items)
t.remove(it.min, it.max, it);
Assert.assertEquals(0, t.size());
}
Assert.assertEquals(0, t.size());
}
@Test
public void shouldWork2() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 0, 0 };
@Test
public void shouldWork2() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {0, 0};
ArrayList<Item> items = new ArrayList<Item>();
ArrayList<Item> items = new ArrayList<Item>();
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
int numItems = 10000;
for (int i = 0; i < numItems; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Assert.assertEquals(numItems, t.size());
Assert.assertEquals(numItems, t.size());
for (Item it : items)
t.remove(it.min, it.max, it);
for (Item it : items)
t.remove(it.min, it.max, it);
Assert.assertEquals(0, t.size());
}
Assert.assertEquals(0, t.size());
}
@Test
public void shouldWork3() {
RTree<Item> t = new RTree<Item>();
double[] min = { 0, 0 };
double[] max = { 0, 0 };
@Test
public void shouldWork3() {
RTree<Item> t = new RTree<Item>();
double[] min = {0, 0};
double[] max = {0, 0};
ArrayList<Item> items = new ArrayList<Item>();
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < 1000; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
for (int i = 0; i < 1000; i++) {
min[0] = Math.random() * 10000 - 5000;
min[1] = Math.random() * 10000 - 5000;
max[0] = min[0] + Math.random() * 1000;
max[1] = min[1] + Math.random() * 1000;
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
Item it = new Item(min, max, i);
t.insert(min, max, it);
items.add(it);
}
int cnt = 0;
int cnt = 0;
for (@SuppressWarnings("unused")
Item it : t) {
//System.out.println(it.val);
cnt++;
}
for (@SuppressWarnings("unused")
Item it : t) {
//System.out.println(it.val);
cnt++;
}
Assert.assertEquals(1000, cnt);
Assert.assertEquals(1000, cnt);
Assert.assertEquals(1000, t.size());
Assert.assertEquals(1000, t.size());
}
}
public static void main(String[] args) {
RTreeTest t = new RTreeTest();
t.shouldWork2();
}
public static void main(String[] args) {
RTreeTest t = new RTreeTest();
t.shouldWork2();
}
}

View File

@@ -1,154 +1,156 @@
package org.oscim.utils.pool;
import static java.lang.System.out;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Assert;
import org.junit.Test;
import org.oscim.utils.pool.Inlist.List;
import static java.lang.System.out;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class InlistTest {
@Test
public void shouldWork() {
List<Thing> list = new List<Thing>();
@Test
public void shouldWork() {
List<Thing> list = new List<Thing>();
list.reverse();
assertNull(list.pop());
list.reverse();
assertNull(list.pop());
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
/* iterate items */
int i = 5;
for (Thing it : list)
assertEquals(it.value, i--);
int i = 5;
for (Thing it : list)
assertEquals(it.value, i--);
assertEquals(i, 0);
assertEquals(i, 0);
/* iterate with insertion order */
list.reverse();
i = 1;
for (Thing it : list)
assertEquals(it.value, i++);
list.reverse();
i = 1;
for (Thing it : list)
assertEquals(it.value, i++);
assertEquals(i, 6);
assertEquals(i, 6);
list.reverse();
list.reverse();
List<Thing> list2 = new List<Thing>();
List<Thing> list2 = new List<Thing>();
/* pop list and append to list2 */
for (int j = 5; j > 0; j--) {
Thing t = list.pop();
assertEquals(t.value, j);
Assert.assertNull(t.next);
for (int j = 5; j > 0; j--) {
Thing t = list.pop();
assertEquals(t.value, j);
Assert.assertNull(t.next);
list2.append(t);
}
list2.append(t);
}
/* check nothing to iterate */
for (Thing t : list)
assert (t == null && t != null);
for (Thing t : list)
assert (t == null && t != null);
assertNull(list.pop());
assertNull(list.head());
assertNull(list.pop());
assertNull(list.head());
list.push(new Thing(6));
list.push(new Thing(6));
/* move items from list2 to list */
list.appendList(list2.clear());
list.appendList(list2.clear());
assertNull(list2.head());
assertNull(list2.pop());
assertNull(list2.head());
assertNull(list2.pop());
list.reverse();
list.reverse();
list.push(new Thing(0));
i = 0;
for (Thing t : list)
assertEquals(t.value, i++);
list.push(new Thing(0));
i = 0;
for (Thing t : list)
assertEquals(t.value, i++);
assertEquals(i, 7);
}
assertEquals(i, 7);
}
@Test
public void shouldRemoveFirstInIterator() {
List<Thing> list = new List<Thing>();
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
@Test
public void shouldRemoveFirstInIterator() {
List<Thing> list = new List<Thing>();
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
out.println("\n shouldRemoveFirstInIterator");
out.println("\n shouldRemoveFirstInIterator");
int cnt = 5;
for (Thing t : list) {
list.remove();
cnt--;
assertEquals(cnt, list.size());
}
}
int cnt = 5;
for (Thing t : list) {
list.remove();
cnt--;
assertEquals(cnt, list.size());
}
}
@Test
public void shouldRemoveSomeInIterator() {
List<Thing> list = new List<Thing>();
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
list.push(new Thing(6));
out.println("\n shouldRemoveSomeInIterator");
@Test
public void shouldRemoveSomeInIterator() {
List<Thing> list = new List<Thing>();
list.push(new Thing(1));
list.push(new Thing(2));
list.push(new Thing(3));
list.push(new Thing(4));
list.push(new Thing(5));
list.push(new Thing(6));
out.println("\n shouldRemoveSomeInIterator");
int pos = 0;
for (Thing t : list) {
if (pos++ % 2 == 0) {
out.println(pos + " val:" + t.value);
list.remove();
}
}
int pos = 0;
for (Thing t : list) {
if (pos++ % 2 == 0) {
out.println(pos + " val:" + t.value);
list.remove();
}
}
assertEquals(3, list.size());
assertEquals(3, list.size());
for (Thing t : list) {
out.println(t.value);
}
}
for (Thing t : list) {
out.println(t.value);
}
}
@Test
public void shouldRemoveLastInIterator() {
List<Thing> list = new List<Thing>();
list.append(new Thing(1));
list.append(new Thing(2));
out.println("\n shouldRemoveLastInIterator");
@Test
public void shouldRemoveLastInIterator() {
List<Thing> list = new List<Thing>();
list.append(new Thing(1));
list.append(new Thing(2));
out.println("\n shouldRemoveLastInIterator");
int pos = 0;
for (Thing t : list) {
if (pos++ == 1) {
out.println(pos + " val:" + t.value);
list.remove();
}
}
int pos = 0;
for (Thing t : list) {
if (pos++ == 1) {
out.println(pos + " val:" + t.value);
list.remove();
}
}
assertEquals(1, list.size());
assertEquals(1, list.size());
for (Thing t : list) {
out.println(t.value);
}
}
for (Thing t : list) {
out.println(t.value);
}
}
static class Thing extends Inlist<Thing> {
final int value;
static class Thing extends Inlist<Thing> {
final int value;
public Thing(int val) {
value = val;
}
};
public Thing(int val) {
value = val;
}
}
;
}