DiscreteAutocorrelationAtLagJ { public double autocorrelate(double[] buffer, int lag) { if(lag > -1 && lag < buffer.length) { double result = 0.0; for (int i = lag; i < buffer.length; i++) { result += buffer[i] * buffer[i - lag]; } return result; } else { throw new IndexOutOfBoundsException("Lag parameter range is : -1 < lag < buffer size. Received [" + lag + "] for buffer size of [" + buffer.length + "]"); } }  double autocorrelate(double[] buffer, int lag);  }
@Test(expected = IndexOutOfBoundsException.class) public void lagLowerBoundIsTestedBeforeMethodExecutes() { autocorrelation.autocorrelate(arrayOfTwos, -1); }
@Test(expected = IndexOutOfBoundsException.class) public void lagUpperBoundIsTestedBeforeMethodExecutes() { autocorrelation.autocorrelate(arrayOfTwos, arrayOfTwos.length + 1); }
@Test public void arrayOfTwosAutocorrelationReturnsAValueEqualToLengthMinusLagTimesFour() { for(int i = 0; i < arrayOfTwos.length; i++) { double autocorrelate = autocorrelation.autocorrelate(arrayOfTwos, i); assertThat(autocorrelate, is(equalTo((arrayOfTwos.length - i) * 4.0d))); } }