Skip to content

Commit

Permalink
Merge pull request #196 from Divyeshhhh/fuzzytest
Browse files Browse the repository at this point in the history
write tests for methods in the FuzzySearching class
  • Loading branch information
creme332 authored Aug 7, 2024
2 parents 9bf21e2 + 3ee414e commit 58d6b1e
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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));

}
}

0 comments on commit 58d6b1e

Please sign in to comment.