Fix test failure due to invalid input.

A merge can happen if the maximum distance between any two points
exceeds the merge distance, because LocationTrace attempts to balance
distances entries. Thus this test must not bne able to walk further than
the merge distance.
This commit is contained in:
asofold 2014-06-14 20:22:02 +02:00
parent 78b615cfbc
commit f53cf96e78
2 changed files with 30 additions and 12 deletions

View File

@ -131,7 +131,6 @@ public class LocationTrace {
}
public final void addEntry(final long time, final double x, final double y, final double z) {
// TODO: Consider setting the squared distance to last entry
double lastDistSq = 0.0;
if (size > 0) {
final TraceEntry latestEntry = entries[index];
@ -144,13 +143,7 @@ public class LocationTrace {
// TODO: Think about minMergeSize (1 = never merge the first two, size = first fill the ring).
if (size > 1 && lastDistSq <= mergeDistSq) {
// TODO: Could use Manhattan, after all.
/**
* TODO: <br>
* The last entry has to be up to date, but it can lead to a "stray entry" far off the second one on time.<br>
* Introducing a mergeTime could also help against keeping too many outdated entries.<br>
* On merging conditions, checking dist/time vs. the second latest element could be feasible, supposedly with double distance. <br>
*/
// Only merge if last distance was not greater than mergeDist.
// Only merge if last distance was not greater than mergeDist, to prevent too-far-off entries.
if (latestEntry.lastDistSq <= mergeDistSq) {
// Update lastDistSq, due to shifting the elements position.
final TraceEntry secondLatest = index - 1 < 0 ? entries[index - 1 + entries.length] : entries[index - 1];

View File

@ -3,6 +3,8 @@ package fr.neatmonster.nocheatplus.test;
import static org.junit.Assert.fail;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
@ -10,20 +12,42 @@ import org.junit.Test;
import fr.neatmonster.nocheatplus.checks.moving.LocationTrace;
import fr.neatmonster.nocheatplus.checks.moving.LocationTrace.TraceEntry;
import fr.neatmonster.nocheatplus.checks.moving.LocationTrace.TraceIterator;
import fr.neatmonster.nocheatplus.utilities.StringUtil;
public class TestLocationTrace {
protected static final Random random = new Random(System.nanoTime() + 133345691);
/**
* +- radius around 0.0.
* @param radius
* @return
*/
public static double rand(double radius) {
return rand(0.0, radius);
}
/**
* +- radius around center.
* @param center
* @param radius
* @return
*/
public static double rand(double center, double radius) {
return center + 2.0 * radius * (random.nextDouble() - 0.5);
}
/**
* center +- step
* @param center
* @param step
* @return
*/
public static double randStep(double center, double step) {
return center + (random.nextBoolean() ? step : -step);
}
@Test
public void testSize() {
int size = 80;
@ -69,16 +93,17 @@ public class TestLocationTrace {
public void testMergeUpdateAlwaysDist() {
// Extreme merge dist.
int size = 80;
double mergeDist = Math.sqrt(1000.0);
double mergeDist = 1000.0;
LocationTrace trace = new LocationTrace(size, mergeDist);
double x = 0;
double y = 0;
double z = 0;
trace.addEntry(0 , x, y, z);
// Note that entries might get split, if the distance to the second last gets too big, so the maximum number of steps must be limited.
for (int i = 0; i < 1000; i ++) {
x = rand(x, 0.5);
y = rand(y, 0.5);
z = rand(z, 0.5);
x = randStep(x, 0.5);
y = randStep(y, 0.5);
z = randStep(z, 0.5);
trace.addEntry(i + 1, x, y, z);
if (trace.size() != 2) {
fail("Wrong size, expect 2, got instead: " + trace.size());