ChebyshevDistanceCalculator extends DistanceCalculator { public double getDistance(double[] features1, double[] features2) { double distance = positiveInfinityIfEitherOrBothAreNull(features1, features2); if (distance < 0) { if(features1.length != features2.length) { throw new IllegalArgumentException("Both features should have the same length. Received lengths of [" + + features1.length + "] and [" + features2.length + "]"); } distance = 0.0; for (int i = 0; i < features1.length; i++) { double currentDistance = Math.abs(features1[i] - features2[i]); distance = (currentDistance > distance) ? currentDistance : distance; } } return distance; }  double getDistance(double[] features1, double[] features2);  }
@Test public void nullValueOfSecondParameterReturnsPositiveInfinity( ) { double distance = distanceCalculator.getDistance(new double[0], null); assertThat(distance, is(equalTo(Double.POSITIVE_INFINITY))); }
@Test(expected = IllegalArgumentException.class) public void parametersOfDifferentLengthThrowsIllegalArgumentException() { distanceCalculator.getDistance(identicalA, new double[identicalA.length + 1]); }
@Test public void identicalParametersReturnsDistanceOfZero() { double distance = distanceCalculator.getDistance(identicalA, identicalB); assertThat(distance, is(equalTo(0.0d))); }
@Test public void reversedIncrementalVectorsOfTwentyReturnsDistanceOfNineTeen() { int last = identicalA.length - 1; for(int i = 0; i < identicalA.length; i++) { identicalB[i] = identicalA[last - i]; } double distance = distanceCalculator.getDistance(identicalA, identicalB); assertThat(distance, is(equalTo(19.0))); }
@Test public void nullValueOfBothParametersReturnsPositiveInfinity( ) { double distance = distanceCalculator.getDistance(null, null); assertThat(distance, is(equalTo(Double.POSITIVE_INFINITY))); }
@Test public void nullValueOfFirstParametersReturnPositiveInfinity( ) { double distance = distanceCalculator.getDistance(null, new double[0]); assertThat(distance, is(equalTo(Double.POSITIVE_INFINITY))); }