use of fr.neatmonster.nocheatplus.utilities.ds.map.LinkedCoordHashMap in project NoCheatPlus by NoCheatPlus.
the class TestCoordMap method testLinkedCoordHashMap.
@Test
public void testLinkedCoordHashMap() {
final Random random = new Random(System.nanoTime() - (System.currentTimeMillis() % 2 == 1 ? 37 : 137));
// Number of coordinates.
final int n = suggestedSamples;
// Coordinate maximum.
final int max = 800;
// Preparecoordinates.
int[][] coords = getUniqueRandomCoords(n, max, random);
LinkedCoordHashMap<Integer> map = new LinkedCoordHashMap<Integer>(1, 0.75f);
// Use a map with these coordinates.
fillMap(map, coords);
// Initial iteration order.
testIterationOrder(map, coords, 1);
// Re-put, moving to end.
for (int i = 0; i < coords.length; i++) {
map.put(coords[i][0], coords[i][1], coords[i][2], i, MoveOrder.END);
testLast(map, coords[i], i);
}
if (map.size() != coords.length) {
fail("Map different size than coords.");
}
testIterationOrder(map, coords, 1);
// Re-put, moving to front.
for (int i = coords.length - 1; i >= 0; i--) {
map.put(coords[i][0], coords[i][1], coords[i][2], i, MoveOrder.FRONT);
testFirst(map, coords[i], i);
}
if (map.size() != coords.length) {
fail("Map different size than coords.");
}
testIterationOrder(map, coords, 1);
// Map.clear
map.clear();
if (map.size() != 0) {
fail("Expect map size to be 0 after clear.");
}
if (map.iterator(false).hasNext()) {
fail("Expect no first element on iteration after clear.");
}
if (map.iterator(true).hasNext()) {
fail("Expect no last element on iteration after clear.");
}
// New map with all coordinates.
fillMap(map, coords);
// Half the coordinates.
int[][] halfCoords = new int[n / 2][3];
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < 3; j++) {
halfCoords[i][j] = coords[i * 2][j];
}
}
// Test remove every second entry.
for (int i = 0; i < n / 2; i++) {
map.remove(coords[i * 2 + 1][0], coords[i * 2 + 1][1], coords[i * 2 + 1][2]);
if (map.contains(coords[i * 2 + 1][0], coords[i * 2 + 1][1], coords[i * 2 + 1][2])) {
fail("Expect removed entries not to be contained in the map.");
}
}
if (map.size() != n / 2) {
fail("Map size should be halfed after removing every second element (" + map.size() + " instead of " + n / 2 + ").");
}
testIterationOrder(map, halfCoords, 2);
// Test iterator.remove every second entry.
map.clear();
fillMap(map, coords);
int i = 0;
Iterator<Entry<Integer>> it = map.iterator(false);
while (it.hasNext()) {
Entry<Integer> entry = it.next();
if (i % 2 == 1) {
it.remove();
if (map.contains(entry.getX(), entry.getY(), entry.getZ())) {
fail("Expect entries removed by iterator not to be in the map.");
}
}
i++;
}
if (map.size() != n / 2) {
fail("Map size should be halfed after removing every second element with an iterator (" + map.size() + " instead of " + n / 2 + ").");
}
testIterationOrder(map, halfCoords, 2);
// TODO: Some random mixtures.
}
Aggregations