|
1 | 1 | package com.thealgorithms.searches;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.*; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 |
|
5 |
| -import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.CsvSource; |
6 | 7 |
|
7 | 8 | class RabinKarpAlgorithmTest {
|
8 |
| - |
9 | 9 | 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 |
| - } |
47 | 10 |
|
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); |
55 | 17 | }
|
56 | 18 | }
|
0 commit comments