package xyz.cques.rangefun;

import java.util.Iterator;

/**
 * Created by Jacques on 2017/06/20.
 */
public class RangeTestHelper {
    private RangeTestHelper() {
        throw new AssertionError(
                "RangeTestHelper is not instantiable"
        );
    }

    /**
     * Tests if the given {@code Iterable} returns no more and no less than all of the integers
     * in {@code expectedInts}, in order.
     *
     * @param range     The Iterable to be tested.
     * @param expectedInts The integers that should be returned by {@code interval}.
     * @return {@code true} if {@code interval} is the same size as {@code expectedInts} and
     * returns the values of {@code expectedInts}, in order, {@code false} otherwise.
     */
    public static boolean rangeReturnsExpectedInts(Iterable<Integer> range, int[] expectedInts) {
        Iterator<Integer> rangeIterator = range.iterator();
        return rangeReturnsExpectedInts(rangeIterator, expectedInts);
    }

    public static boolean rangeReturnsExpectedInts(Iterator<Integer> rangeIterator, int[] expectedInts) {
        int currentIndex = 0;
        int rangeSize = 0;

        while (rangeIterator.hasNext()) {
            int current = rangeIterator.next();

            int expected;
            try {
                expected = expectedInts[currentIndex];
            } catch (ArrayIndexOutOfBoundsException exception) {
                return false;
            }

            if (current != expected) {
                return false;
            }

            rangeSize++;
            currentIndex++;
        }

        return rangeSize == expectedInts.length;
    }
}

@@ -740,31 +740,24 @@
      */%0A    
-public 
 static boole
@@ -972,23 +972,16 @@
  %7D%0A%0A    
-public 
 static b
@@ -1233,66 +1233,37 @@
    i
-nt expected;%0A            try %7B%0A                e
+f (!intAtIndexEqualsE
 xpected
- = 
+(
 expe
@@ -1270,17 +1270,18 @@
 ctedInts
-%5B
+, 
 currentI
@@ -1288,72 +1288,18 @@
 ndex
-%5D;%0A            %7D catch (ArrayIndexOutOfBoundsException exception
+, current)
 ) %7B%0A
@@ -1359,34 +1359,48 @@
     
-if (current != expected) %7B
+rangeSize++;%0A            currentIndex++;
 %0A   
@@ -1396,32 +1396,35 @@
 ndex++;%0A        
+%7D%0A%0A
         return f
@@ -1426,81 +1426,215 @@
 urn 
-false
+rangeSize == expectedInts.length
 ;%0A    
+%7D%0A%0A
     
-    %7D%0A%0A            rangeSize++;%0A            currentIndex++
+private static boolean intAtIndexEqualsExpected(int%5B%5D array, int index, int expected) %7B%0A        if (index %3C 0 %7C%7C index %3E= array.length) %7B%0A            return false
 ;%0A  
@@ -1653,33 +1653,36 @@
      return 
-rangeSize
+array%5Bindex%5D
  == expected
@@ -1677,33 +1677,22 @@
  == expected
-Ints.length
 ;%0A    %7D%0A%7D%0A

Refactor RangeTestHelper method