From 3ee414e6ba7b676b68a63470413c14f02f597bf7 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Fri, 2 Aug 2024 19:22:30 +0400 Subject: [PATCH] write tests for methods in the FuzzySearching class --- .../tests/utils/FuzzySearchingTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/com/github/creme332/tests/utils/FuzzySearchingTest.java diff --git a/src/test/java/com/github/creme332/tests/utils/FuzzySearchingTest.java b/src/test/java/com/github/creme332/tests/utils/FuzzySearchingTest.java new file mode 100644 index 00000000..83dce594 --- /dev/null +++ b/src/test/java/com/github/creme332/tests/utils/FuzzySearchingTest.java @@ -0,0 +1,62 @@ +package com.github.creme332.tests.utils; + +import static org.junit.Assert.*; +import org.junit.Test; +import com.github.creme332.utils.FuzzySearching; + +public class FuzzySearchingTest { + + @Test + public void testLevenshteinDistance() { + // Identical strings + assertEquals(0, FuzzySearching.levenshteinDistance("kitten", "kitten")); + + // Completely different strings + assertEquals(6, FuzzySearching.levenshteinDistance("kitten", "puppy")); + + // One empty string + assertEquals(6, FuzzySearching.levenshteinDistance("kitten", "")); + assertEquals(6, FuzzySearching.levenshteinDistance("", "kitten")); + + // Strings differing by one character + assertEquals(1, FuzzySearching.levenshteinDistance("kitten", "sitten")); + + // Strings of different lengths + assertEquals(3, FuzzySearching.levenshteinDistance("kitten", "kit")); + } + + @Test + public void testIsSimilar() { + // Identical strings + assertTrue(FuzzySearching.isSimilar("kitten", "kitten")); + + // Small difference (distance ≤ 3) + assertTrue(FuzzySearching.isSimilar("kitten", "sitten")); + assertTrue(FuzzySearching.isSimilar("kitten", "kitt")); + + // Larger difference (distance > 3) + assertFalse(FuzzySearching.isSimilar("kitten", "puppy")); + + // One empty string + assertFalse(FuzzySearching.isSimilar("kitten", "")); + assertFalse(FuzzySearching.isSimilar("", "kitten")); + } + + @Test + public void testMatch() { + String[] keywords = {"kitten", "puppy", "dog"}; + + // Exact match + assertTrue(FuzzySearching.match("kitten", keywords)); + + // Similar match + assertTrue(FuzzySearching.match("kitt", keywords)); + + // No match + assertFalse(FuzzySearching.match("elephant", keywords)); + + // Case insensitivity + assertTrue(FuzzySearching.match("KITTEN", keywords)); + + } +}