MultiplyArrayElements { public int[] multiply(int[] input) { if (input == null) { throw new IllegalArgumentException("You can't pass a null array as input."); } if (input.length == 0) { return input; } int[] front = new int[input.length]; int[] rear = new int[input.length]; int[] output = new int[input.length]; front[0] = 1; rear[input.length - 1] = 1; for (int i = 1; i < input.length; i++) { front[i] = front[i - 1] * input[i - 1]; } for (int i = input.length - 2; i >= 0; i--) { rear[i] = rear[i + 1] * input[i + 1]; } for (int i = 0; i < input.length; i++) { output[i] = front[i] * rear[i]; } return output; }  int[] multiply(int[] input);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullArrays() { multiplyArrayElements.multiply(null); }
@Test public void shouldSupportEmptyArrays() { int[] array = new int[0]; int[] result = multiplyArrayElements.multiply(array); assertEquals(0, result.length); }
@Test public void shouldDoNothingIfTheArrayWithJustOneElement() { int[] array = { 1 }; int[] result = multiplyArrayElements.multiply(array); int[] expected = { 1 }; assertArrayEquals(expected, result); }
@Test public void shouldMultiplyEveryElementByTheRest() { int[] array = { 1, 2, 3 }; int[] result = multiplyArrayElements.multiply(array); int[] expected = { 6, 3, 2 }; assertArrayEquals(expected, result); }