package org.psjava.example;

import junit.framework.Assert;

import org.junit.Test;
import org.psjava.algo.search.BinarySearchFirst;
import org.psjava.algo.search.BinarySearchFirstFalse;
import org.psjava.algo.search.BinarySearchFirstInArray;
import org.psjava.algo.search.BinarySearchFirstTrue;
import org.psjava.algo.search.BinarySearchLast;
import org.psjava.algo.search.BinarySearchLastFalse;
import org.psjava.algo.search.BinarySearchLastInArray;
import org.psjava.algo.search.BinarySearchLastTrue;
import org.psjava.ds.array.MutableArray;
import org.psjava.ds.array.MutableArrayFromValues;
import org.psjava.javautil.DefaultComparator;
import org.psjava.javautil.ReversedComparator;
import org.psjava.math.Function;
import org.psjava.math.ns.IntegerNumberSystem;

public class BinarySearchExample {

	@Test
	public void example() {

		// Search a value 5 in increasing int array. result is the position = 2

		MutableArray<Integer> array1 = MutableArrayFromValues.create(1, 3, 5, 7, 9);
		int res1 = BinarySearchFirstInArray.search(array1, new DefaultComparator<Integer>(), 5, -1);


		// Following is an example for decresing array.
		// You can use the reversed comparator.

		MutableArray<Integer> array2 = MutableArrayFromValues.create(9, 7, 5, 3, 1);
		int res2 = BinarySearchFirstInArray.search(array2, ReversedComparator.wrap(new DefaultComparator<Integer>()), 3, -1);


		// You don't have to prepare an array. Any function is enough.
		
		int res3 = BinarySearchFirst.search(IntegerNumberSystem.getInstance(), new Function<Integer, Integer>() {
			@Override
			public Integer get(Integer input) {
				return input * 8;
			}
		}, new DefaultComparator<Integer>(), 100, 200, 888, -1);


		// And, there are many alternatives. check them

		BinarySearchFirst.class.getClass();
		BinarySearchFirstTrue.class.getClass();
		BinarySearchFirstFalse.class.getClass();
		BinarySearchFirstInArray.class.getClass();
		BinarySearchLast.class.getClass();
		BinarySearchLastInArray.class.getClass();
		BinarySearchLastTrue.class.getClass();
		BinarySearchLastFalse.class.getClass();
		
		
		

		// these are assertion
		Assert.assertEquals(2, res1);
		Assert.assertEquals(3, res2);
		Assert.assertEquals(111, res3);
	}

}

@@ -1108,24 +1108,59 @@
  5, -1);%0D%0A%0D%0A
+%09%09Assert.assertEquals(2, res1);%0D%0A%0D%0A
 %0D%0A%09%09// Follo
@@ -1444,24 +1444,59 @@
  3, -1);%0D%0A%0D%0A
+%09%09Assert.assertEquals(3, res2);%0D%0A%0D%0A
 %0D%0A%09%09// You d
@@ -1805,24 +1805,61 @@
 88, -1);%0D%0A%0D%0A
+%09%09Assert.assertEquals(111, res3);%0D%0A%0D%0A
 %0D%0A%09%09// And, 
@@ -1885,16 +1885,27 @@
 rnatives
+ interfaces
 . check 
@@ -2256,149 +2256,8 @@
 );%0D%0A
-%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%0D%0A%09%09// these are assertion%0D%0A%09%09Assert.assertEquals(2, res1);%0D%0A%09%09Assert.assertEquals(3, res2);%0D%0A%09%09Assert.assertEquals(111, res3);%0D%0A
 %09%7D%0D%0A

improve example