Skip to content

Commit f72b80b

Browse files
asapekiavil02
andauthored
rewrote as parameterized tests (#4458)
* rewrote as parameterized tests * formatting issue resolved * added test with different prime number q * style: run `clang-format` * tests: add missing test case --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent edb0489 commit f72b80b

File tree

1 file changed

+9
-47
lines changed

1 file changed

+9
-47
lines changed
Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,18 @@
11
package com.thealgorithms.searches;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
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 {
8-
99
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
10-
int q = 101;
11-
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-
}
4710

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);
11+
@ParameterizedTest
12+
@CsvSource({"This is an example for rabin karp algorithmn, algorithmn, 101", "AAABBDDG, AAA, 137", "AAABBCCBB, BBCC, 101", "AAABBCCBB, BBCC, 131", "AAAABBBBCCC, CCC, 41", "ABCBCBCAAB, AADB, 293", "Algorithm The Algorithm, Algorithm, 101"})
13+
void RabinKarpAlgorithmTestExample(String txt, String pat, int q) {
14+
int indexFromOurAlgorithm = RKA.search(pat, txt, q);
15+
int indexFromLinearSearch = txt.indexOf(pat);
16+
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
5517
}
5618
}

0 commit comments

Comments
 (0)