Skip to content

Commit 126d8ae

Browse files
committed
rewrote as parameterized tests
1 parent 566c27a commit 126d8ae

File tree

1 file changed

+9
-43
lines changed

1 file changed

+9
-43
lines changed

src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,21 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
class RabinKarpAlgorithmTest {
89

910
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
1011
int q = 101;
1112

12-
@Test
13-
// valid test case
14-
public void RabinKarpAlgorithmTestExample() {
15-
String txt = "This is an example for rabin karp algorithmn";
16-
String pat = "algorithmn";
17-
int value = RKA.search(pat, txt, q);
18-
assertEquals(value, 34);
19-
}
20-
21-
@Test
22-
// valid test case
23-
public void RabinKarpAlgorithmTestFront() {
24-
String txt = "AAABBDDG";
25-
String pat = "AAA";
26-
int value = RKA.search(pat, txt, q);
27-
assertEquals(value, 0);
28-
}
29-
30-
@Test
31-
// valid test case
32-
public void RabinKarpAlgorithmTestMiddle() {
33-
String txt = "AAABBCCBB";
34-
String pat = "BBCC";
35-
int value = RKA.search(pat, txt, q);
36-
assertEquals(value, 3);
37-
}
38-
39-
@Test
40-
// valid test case
41-
public void RabinKarpAlgorithmTestLast() {
42-
String txt = "AAAABBBBCCC";
43-
String pat = "CCC";
44-
int value = RKA.search(pat, txt, q);
45-
assertEquals(value, 8);
46-
}
13+
@ParameterizedTest
14+
@CsvSource({ "This is an example for rabin karp algorithmn, algorithmn", "AAABBDDG, AAA", "AAABBCCBB, BBCC",
15+
"AAAABBBBCCC, CCC", "ABCBCBCAAB, AADB" })
16+
void RabinKarpAlgorithmTestExample(String txt, String pat) {
4717

48-
@Test
49-
// valid test case
50-
public void RabinKarpAlgorithmTestNotFound() {
51-
String txt = "ABCBCBCAAB";
52-
String pat = "AADB";
53-
int value = RKA.search(pat, txt, q);
54-
assertEquals(value, -1);
18+
int indexFromOurAlgorithm = RKA.search(pat, txt, q);
19+
int indexFromLinearSearch = txt.indexOf(pat);
20+
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
5521
}
5622
}

0 commit comments

Comments
 (0)