@SuppressWarnings("unchecked") private Set<Double>[] omit(double[] locs) { Set<Double>[] result = (Set<Double>[]) new Set<?>[locs.length + 1]; result[locs.length] = new HashSet<Double>(); int n = locs.length / 2 + 1; for (int i = 0; i < locs.length; i++) { Set<Double> s = new HashSet<Double>(); for (int j = 0; j < n; j++) { s.add(locs[(i + j) % locs.length]); } result[i] = s; assertFalse(result[i].isEmpty()); } return result; }
public void testGetClosestPeerLocation() { for (double[] peers : PEER_LOCATIONS) { PeerLocation pl = new PeerLocation("0.0"); assertTrue(pl.updateLocation(0.0, peers)); for (double target : TARGET_LOCATIONS) { for (Set<Double> exclude : omit(peers)) { double closest = pl.getClosestPeerLocation(target, exclude); assertFalse(exclude.contains(closest)); double ref = trivialFindClosestDistance(peers, target, exclude); if (!Double.isInfinite(ref)) { assertFalse(Double.isNaN(closest)); boolean isPeer = false; for (double peer : peers) { if (closest == peer) { isPeer = true; break; } } assertTrue(isPeer); assertEquals(ref, Location.distance(closest, target), EPSILON); } else { assertTrue(Double.isNaN(closest)); } } } } }
public void testFindClosestLocation() { for (double[] peers : PEER_LOCATIONS) { for (double target : TARGET_LOCATIONS) { int closest = PeerLocation.findClosestLocation(peers, target); double ref = trivialFindClosestDistance(peers, target); assertEquals(ref, Location.distance(peers[closest], target), EPSILON); } } }