Skip to content

rewrote as parameterized tests #4458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 1, 2023
Original file line number Diff line number Diff line change
@@ -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);
}
}