test/test.rb in levenshtein-0.2.1 vs test/test.rb in levenshtein-0.2.2

- old
+ new

@@ -1,39 +1,42 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 + require "test/unit" require "levenshtein" module Levenshtein class TestSequence def initialize(o) @sequence = o end - def length - @sequence.length + def each + @sequence.length.times do |pos| + yield(@sequence[pos]) + end end - - def [](pos) - raise "type not allowed [#{pos.inspect}]" unless pos.kind_of?(Fixnum) - - @sequence[pos] - end end class TestElement attr_reader :object def initialize(o) @object = o end - - def ==(other) - @object == other.object + + def hash + @object.hash end + + def eql?(other) + @object.eql?(other.object) + end end end -class TestLevenshteinString < Test::Unit::TestCase +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) @@ -77,61 +80,75 @@ 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) assert_in_delta(0.625, Levenshtein.normalized_distance("123cd123", "123"), 0.01) end -end -class TestLevenshteinArray < Test::Unit::TestCase - def test_erik_veenstra - x = lambda{|s| s.scan(/./).collect{|e| Levenshtein::TestElement.new(e)}} + def test_interface + seq1 = Levenshtein::TestSequence.new("erik".scan(/./).collect{|e| Levenshtein::TestElement.new(e)}) + seq2 = Levenshtein::TestSequence.new("veenstra".scan(/./).collect{|e| Levenshtein::TestElement.new(e)}) - assert_equal(7, Levenshtein.distance(x["erik"], x["veenstra"])) + assert_equal(7, Levenshtein.distance(seq1, seq2)) end end -class TestLevenshteinArrayOfStrings < Test::Unit::TestCase +class TestLevenshteinFast < Test::Unit::TestCase def test_erik_veenstra - x = lambda{|s| s.scan(/./)} + assert_equal(7, Levenshtein.distance("erik", "veenstra", nil, :force_slow=>false)) + assert_equal(7, Levenshtein.distance("veenstra", "erik", nil, :force_slow=>false)) + end - assert_equal(7, Levenshtein.distance(x["erik"], x["veenstra"])) + def test_empty_string + assert_equal(0, Levenshtein.distance("", "", nil, :force_slow=>false)) + assert_equal(3, Levenshtein.distance("", "foo", nil, :force_slow=>false)) + assert_equal(3, Levenshtein.distance("foo", "", nil, :force_slow=>false)) end -end -class TestLevenshteinGeneric < Test::Unit::TestCase - def test_erik_veenstra - x = lambda{|s| Levenshtein::TestSequence.new(s.scan(/./).collect{|e| Levenshtein::TestElement.new(e)})} + def test_same_string + assert_equal(0, Levenshtein.distance("", "", nil, :force_slow=>false)) + assert_equal(0, Levenshtein.distance("foo", "foo", nil, :force_slow=>false)) + end - assert_equal(7, Levenshtein.distance(x["erik"], x["veenstra"])) + def test_threshold + assert_equal(3, Levenshtein.distance("foo", "foobar", nil, :force_slow=>false)) + assert_equal(3, Levenshtein.distance("foo", "foobar", 4, :force_slow=>false)) + assert_equal(nil, Levenshtein.distance("foo", "foobar", 2, :force_slow=>false)) end + + def test_same_head_and_or_tail + assert_equal(3, Levenshtein.distance("ab123cd", "abxyzcd", nil, :force_slow=>false)) + assert_equal(3, Levenshtein.distance("ab123", "abxyz", nil, :force_slow=>false)) + assert_equal(3, Levenshtein.distance("123cd", "xyzcd", nil, :force_slow=>false)) + assert_equal(5, Levenshtein.distance("123cd123", "123", nil, :force_slow=>false)) + end end class TestLevenshteinSlow < Test::Unit::TestCase def test_erik_veenstra - assert_equal(7, Levenshtein.distance_slow("erik", "veenstra", nil)) - assert_equal(7, Levenshtein.distance_slow("veenstra", "erik", nil)) + assert_equal(7, Levenshtein.distance("erik", "veenstra", nil, :force_slow=>true)) + assert_equal(7, Levenshtein.distance("veenstra", "erik", nil, :force_slow=>true)) end def test_empty_string - assert_equal(0, Levenshtein.distance_slow("", "", nil)) - assert_equal(3, Levenshtein.distance_slow("", "foo", nil)) - assert_equal(3, Levenshtein.distance_slow("foo", "", nil)) + assert_equal(0, Levenshtein.distance("", "", nil, :force_slow=>true)) + assert_equal(3, Levenshtein.distance("", "foo", nil, :force_slow=>true)) + assert_equal(3, Levenshtein.distance("foo", "", nil, :force_slow=>true)) end def test_same_string - assert_equal(0, Levenshtein.distance_slow("", "", nil)) - assert_equal(0, Levenshtein.distance_slow("foo", "foo", nil)) + assert_equal(0, Levenshtein.distance("", "", nil, :force_slow=>true)) + assert_equal(0, Levenshtein.distance("foo", "foo", nil, :force_slow=>true)) end def test_threshold - assert_equal(3, Levenshtein.distance_slow("foo", "foobar", nil)) - assert_equal(3, Levenshtein.distance_slow("foo", "foobar", 4)) - assert_equal(nil, Levenshtein.distance_slow("foo", "foobar", 2)) + assert_equal(3, Levenshtein.distance("foo", "foobar", nil, :force_slow=>true)) + assert_equal(3, Levenshtein.distance("foo", "foobar", 4, :force_slow=>true)) + assert_equal(nil, Levenshtein.distance("foo", "foobar", 2, :force_slow=>true)) end def test_same_head_and_or_tail - assert_equal(3, Levenshtein.distance_slow("ab123cd", "abxyzcd", nil)) - assert_equal(3, Levenshtein.distance_slow("ab123", "abxyz", nil)) - assert_equal(3, Levenshtein.distance_slow("123cd", "xyzcd", nil)) - assert_equal(5, Levenshtein.distance_slow("123cd123", "123", nil)) + assert_equal(3, Levenshtein.distance("ab123cd", "abxyzcd", nil, :force_slow=>true)) + assert_equal(3, Levenshtein.distance("ab123", "abxyz", nil, :force_slow=>true)) + assert_equal(3, Levenshtein.distance("123cd", "xyzcd", nil, :force_slow=>true)) + assert_equal(5, Levenshtein.distance("123cd123", "123", nil, :force_slow=>true)) end end