mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
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 <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -1,56 +1,18 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
class RabinKarpAlgorithmTest {
|
||||
|
||||
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
|
||||
int q = 101;
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestExample() {
|
||||
String txt = "This is an example for rabin karp algorithmn";
|
||||
String pat = "algorithmn";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, 34);
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestFront() {
|
||||
String txt = "AAABBDDG";
|
||||
String pat = "AAA";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestMiddle() {
|
||||
String txt = "AAABBCCBB";
|
||||
String pat = "BBCC";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestLast() {
|
||||
String txt = "AAAABBBBCCC";
|
||||
String pat = "CCC";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, 8);
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestNotFound() {
|
||||
String txt = "ABCBCBCAAB";
|
||||
String pat = "AADB";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, -1);
|
||||
@ParameterizedTest
|
||||
@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"})
|
||||
void RabinKarpAlgorithmTestExample(String txt, String pat, int q) {
|
||||
int indexFromOurAlgorithm = RKA.search(pat, txt, q);
|
||||
int indexFromLinearSearch = txt.indexOf(pat);
|
||||
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user