|
|
@@ -0,0 +1,77 @@
|
|
|
+package com.cyl.algorithrms.search;
|
|
|
+
|
|
|
+import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+public class InsertionTest {
|
|
|
+
|
|
|
+ private Comparable[] ascArr;
|
|
|
+ private Comparable[] descArr;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void prepare() {
|
|
|
+ ascArr = new Comparable[]{0,1,2,3,4,5,6,7,8,9,10,15,20,31};
|
|
|
+ descArr = new Comparable[]{31,20,15,10,9,8,7,6,5,4,3,2,1,0};
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInAscLeft() {
|
|
|
+ int ret = Insertion.search(ascArr, 2);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 2, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInAscRight() {
|
|
|
+ int ret = Insertion.search(ascArr, 20);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 12, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInDescLeft() {
|
|
|
+ int ret = Insertion.search(descArr, 10);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 3, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInDescRight() {
|
|
|
+ int ret = Insertion.search(descArr, 1);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 12, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInAscStartLocation() {
|
|
|
+ int ret = Insertion.search(ascArr, 0);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 0, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInAscEndLocation() {
|
|
|
+ int ret = Insertion.search(ascArr, 31);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 13, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInDescStartLocation() {
|
|
|
+ int ret = Insertion.search(descArr, 31);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 0, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchInDescEndLocation() {
|
|
|
+ int ret = Insertion.search(descArr, 0);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), 13, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchNotInAsc() {
|
|
|
+ int ret = Insertion.search(ascArr, 100);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), -1, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSearchNotInDesc() {
|
|
|
+ int ret = Binary.search(descArr, 100);
|
|
|
+ Assert.assertEquals(String.format("找到的下标:%s", ret), -1, ret);
|
|
|
+ }
|
|
|
+}
|