require "test/unit" require "levenshtein" class TestLevenshtein < Test::Unit::TestCase def test_erik_veenstra assert_equal(7, Levenshtein.distance("erik", "veenstra")) assert_equal(7, Levenshtein.distance("veenstra", "erik")) assert_in_delta(0.875, Levenshtein.normalized_distance("erik", "veenstra"), 0.01) assert_in_delta(0.875, Levenshtein.normalized_distance("veenstra", "erik"), 0.01) end def test_empty_string assert_equal(0, Levenshtein.distance("", "")) assert_equal(3, Levenshtein.distance("", "foo")) assert_equal(3, Levenshtein.distance("foo", "")) assert_in_delta(0.0, Levenshtein.normalized_distance("", ""), 0.01) assert_in_delta(1.0, Levenshtein.normalized_distance("", "foo"), 0.01) assert_in_delta(1.0, Levenshtein.normalized_distance("foo", ""), 0.01) end def test_same_string assert_equal(0, Levenshtein.distance("", "")) assert_equal(0, Levenshtein.distance("foo", "foo")) assert_in_delta(0.0, Levenshtein.normalized_distance("", ""), 0.01) assert_in_delta(0.0, Levenshtein.normalized_distance("foo", "foo"), 0.01) end def test_threshold assert_equal(3, Levenshtein.distance("foo", "foobar")) assert_equal(nil, Levenshtein.distance("foo", "foobar", 2)) assert_in_delta(0.5, Levenshtein.normalized_distance("foo", "foobar"), 0.01) assert_equal(nil, Levenshtein.normalized_distance("foo", "foobar", 0.30)) end def test_same_head_and_or_tail assert_equal(3, Levenshtein.distance("ab123cd", "abxyzcd")) assert_equal(3, Levenshtein.distance("ab123", "abxyz")) assert_equal(3, Levenshtein.distance("123cd", "xyzcd")) assert_in_delta(0.42, Levenshtein.normalized_distance("ab123cd", "abxyzcd"), 0.01) assert_in_delta(0.6, Levenshtein.normalized_distance("ab123", "abxyz"), 0.01) assert_in_delta(0.6, Levenshtein.normalized_distance("123cd", "xyzcd"), 0.01) end end class TestLevenshteinPart2Slow < Test::Unit::TestCase def test_erik_veenstra assert_equal(7, Levenshtein.distance_part2_slow("erik", "veenstra", nil)) end def test_empty_string assert_equal(0, Levenshtein.distance_part2_slow("", "", nil)) assert_equal(3, Levenshtein.distance_part2_slow("", "foo", nil)) end def test_same_string assert_equal(0, Levenshtein.distance_part2_slow("", "", nil)) assert_equal(0, Levenshtein.distance_part2_slow("foo", "foo", nil)) end def test_threshold assert_equal(3, Levenshtein.distance_part2_slow("foo", "foobar", nil)) assert_equal(nil, Levenshtein.distance_part2_slow("foo", "foobar", 2)) end end class TestLevenshteinPart2Fast < Test::Unit::TestCase def test_erik_veenstra assert_equal(7, Levenshtein.distance_part2_fast("erik", "veenstra", nil)) end def test_empty_string assert_equal(0, Levenshtein.distance_part2_fast("", "", nil)) assert_equal(3, Levenshtein.distance_part2_fast("", "foo", nil)) end def test_same_string assert_equal(0, Levenshtein.distance_part2_fast("", "", nil)) assert_equal(0, Levenshtein.distance_part2_fast("foo", "foo", nil)) end def test_threshold assert_equal(3, Levenshtein.distance_part2_fast("foo", "foobar", nil)) assert_equal(nil, Levenshtein.distance_part2_fast("foo", "foobar", 2)) end end