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

@@ -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;
}
}
;
}