Alg { static int linearSearch(IntBuffer buff, int key) { return linearSearch(buff, 0, buff.limit(), key) ; }  static final int encodeIndex(int i); static final int decodeIndex(int i); static int binarySearch(IntBuffer buff, int value); static int binarySearch(IntBuffer buff, int low, int high, int value); static int binarySearch( List<T> array, int low, int high, T value, Comparator<T> comparator ); static int binarySearch(T[] array, int low, int high, T value);  }
@Test public void linear1() { int[] data = {1, 2, 3} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.linearSearch(b, 1) ; assertEquals(0, idx) ; idx = Alg.linearSearch(b, 2) ; assertEquals(1, idx) ; idx = Alg.linearSearch(b, 3) ; assertEquals(2, idx) ; }
@Test public void linear2() { int[] data = {2, 4, 6} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.linearSearch(b, 1) ; assertEquals(-1, idx) ; idx = Alg.linearSearch(b, 3) ; assertEquals(-2, idx) ; idx = Alg.linearSearch(b, 5) ; assertEquals(-3, idx) ; idx = Alg.linearSearch(b, 7) ; assertEquals(-4, idx) ; }
@Test public void linear3() { int[] data = {} ; IntBuffer b = make(data) ; int idx = Alg.linearSearch(b, 1) ; assertEquals(-1, idx) ; }
@Test public void linear4() { int[] data = {9} ; IntBuffer b = make(data) ; int idx ; idx = Alg.linearSearch(b, 1) ; assertEquals(-1, idx) ; idx = Alg.linearSearch(b, 9) ; assertEquals(0, idx) ; idx = Alg.linearSearch(b, 100) ; assertEquals(-2, idx) ; }
@Test public void linear5() { int[] data = {2, 4, 6, 8, 10} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.linearSearch(b, 1, 4, 6) ; assertEquals(2, idx) ; idx = Alg.linearSearch(b, 1, 4, 5) ; assertEquals(-3, idx) ; idx = Alg.linearSearch(b, 1, 4, 2) ; assertEquals(-2, idx) ; idx = Alg.linearSearch(b, 1, 4, 10) ; assertEquals(-5, idx) ; }
@Test public void linear6() { int[] data = {2, 4, 6, 8, 10} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.linearSearch(b, 3, 3, 6) ; assertEquals(-4, idx) ; idx = Alg.linearSearch(b, 3, 3, 5) ; assertEquals(-4, idx) ; idx = Alg.linearSearch(b, 3, 3, 50) ; assertEquals(-4, idx) ; }
@Test public void linear7() { int[] data = {2, 4, 4, 8, 8} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.linearSearch(b, 4) ; assertEquals(1, idx) ; idx = Alg.linearSearch(b, 8) ; assertEquals(3, idx) ; }