FindLongestConsecutiveSequence { public int findIterative(int[] numbers) { validateInput(numbers); if (numbers.length == 0) { return 0; } boolean consecutiveSequenceFound = false; int result = 0; int start = 0, end = 0; for (int i = 1; i < numbers.length; i++) { if (numbers[i - 1] < numbers[i]) { end = i; } else { start = i; } if (end - start > result) { consecutiveSequenceFound = true; result = end - start; } } if (consecutiveSequenceFound) { return result + 1; } else { return result; } }  int findIterative(int[] numbers); int findRecursive(int[] array);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullArraysIterative() { lcs.findIterative(null); }
@Test public void shouldReturnZeroIfTheArrayIsEmptyIterative() { int[] array = { }; int sequenceLength = lcs.findIterative(array); assertEquals(0, sequenceLength); }
@Test public void shouldReturnZeroIfThereIsNoAnyConsecutiveSequenceIterative() { int[] array = { 6, 5, 4, 3, 2, 1 }; int sequenceLength = lcs.findIterative(array); assertEquals(0, sequenceLength); }
@Test public void shouldReturnLengthArrayIfTheArrayIsOneConsecutiveSequenceIterative() { int[] array = { 1, 2, 3, 4, 5, 6 }; int sequenceLength = lcs.findIterative(array); assertEquals(6, sequenceLength); }
@Test public void shouldFindLongestConsecutiveSequenceIterative() { int[] array = { 1, 3, 4, 5, 64, 4, 5, 6, 7, 8, 9, 98, -1, -2 }; int sequenceLength = lcs.findIterative(array); assertEquals(7, sequenceLength); }
@Test public void shouldFindLongestConsecutiveSequenceIterativeSupportingEqualsConsecutiveNumbers() { int[] array = { 1, 1, 1, 1, 3, 4, 5, 64, 4, 5, 6, 7, 8, 9, 98, -1, -2 }; int sequenceLength = lcs.findIterative(array); assertEquals(7, sequenceLength); }