From 3483539429bc2c1e22f7a670d3a712a1faa83846 Mon Sep 17 00:00:00 2001 From: renato Date: Sat, 29 Jul 2023 00:53:25 +0200 Subject: [PATCH] test: test Boyer-Moore algorithm matches KMP results --- .../strings/BoyerMooreStringSearchTest.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/williamfiset/algorithms/strings/BoyerMooreStringSearchTest.java b/src/test/java/com/williamfiset/algorithms/strings/BoyerMooreStringSearchTest.java index 788121c63..d48cb6552 100644 --- a/src/test/java/com/williamfiset/algorithms/strings/BoyerMooreStringSearchTest.java +++ b/src/test/java/com/williamfiset/algorithms/strings/BoyerMooreStringSearchTest.java @@ -115,6 +115,25 @@ private String generateRandomString(int upperLimitAscii, int length) { .toString(); } - // TODO(william): Add a test that compares this implementation of Boyermoore - // with that of KMP. + @Test + public void shouldReturnSameResultAsKMP(){ + int runLength = random.nextInt(MAX_ITERATION) + 1; + KMP kmp = new KMP(); + for(int run = 0; run < runLength; run++){ + int upperCharText = random.nextInt(3); + int upperCharPattern = random.nextInt(3); + int maxLengthText = random.nextInt(1000) + 100; + + // ensure maxLengthPattern is at least 1 + int maxLengthPattern = random.nextInt(10) + 1; + + String text = generateRandomString(upperCharText, maxLengthText); + String pattern = generateRandomString(upperCharPattern, maxLengthPattern); + + List boyerMooreResult = underTest.findOccurrences(text, pattern); + List kmpResult = KMP.kmp(text, pattern); + + assertThat(boyerMooreResult).containsExactlyElementsIn(kmpResult); + } + } }