-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test class for InterpolationSearchTest
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/test/java/com/ordestiny/tdd/algorithms/InterpolationSearchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.ordestiny.tdd.algorithms; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class InterpolationSearchTest { | ||
|
||
private Integer[] arrayToTest; | ||
|
||
@BeforeEach | ||
public void init() { | ||
arrayToTest = new Integer[] { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 }; | ||
|
||
} | ||
|
||
@Test | ||
public void findPos_SearchValidValue_FindCorrectPosOfValue() throws Exception { | ||
int expected = 4; | ||
int elementToFindInArray = 18; | ||
InterpolationSearch interpolationSearch = new InterpolationSearch(); | ||
int actual = interpolationSearch.findPos(arrayToTest, elementToFindInArray); | ||
Assertions.assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void findPos_SearchValidValue_NotFindPosOfValue() throws Exception { | ||
int elementToFindInArray = 1; | ||
int expected = -1; | ||
InterpolationSearch interpolationSearch = new InterpolationSearch(); | ||
int actual = interpolationSearch.findPos(arrayToTest, elementToFindInArray); | ||
Assertions.assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void findPos_ArrayNotOrdered_ThrowExceptionifArrayNotOrdered() throws Exception { | ||
arrayToTest = new Integer[] { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47, 41 }; | ||
int elementToFindInArray = 1; | ||
InterpolationSearch interpolationSearch = new InterpolationSearch(); | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
() -> interpolationSearch.findPos(arrayToTest, elementToFindInArray)); | ||
|
||
} | ||
|
||
} |