mumboe-amatch 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit/ui/console/testrunner'
4
+ require 'test/unit/testsuite'
5
+ $:.unshift File.expand_path(File.dirname($0))
6
+ require 'test_levenshtein'
7
+ require 'test_sellers'
8
+ require 'test_pair_distance'
9
+ require 'test_hamming'
10
+ require 'test_longest_subsequence'
11
+ require 'test_longest_substring'
12
+
13
+ class TS_AllTests
14
+ def self.suite
15
+ suite = Test::Unit::TestSuite.new 'All tests'
16
+ suite << TC_Levenshtein.suite
17
+ suite << TC_Sellers.suite
18
+ suite << TC_PairDistance.suite
19
+ suite << TC_Hamming.suite
20
+ suite << TC_LongestSubsequence.suite
21
+ suite << TC_LongestSubstring.suite
22
+ suite
23
+ end
24
+ end
25
+ Test::Unit::UI::Console::TestRunner.run(TS_AllTests)
26
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,59 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+
4
+ class TC_Hamming < Test::Unit::TestCase
5
+ include Amatch
6
+
7
+ D = 0.000001
8
+
9
+ def setup
10
+ @small = Hamming.new('test')
11
+ @empty = Hamming.new('')
12
+ @long = Hamming.new('A' * 160)
13
+ end
14
+
15
+ def test_empty
16
+ assert_in_delta 0, @empty.match(''), D
17
+ assert_in_delta 9, @empty.match('not empty'), D
18
+ assert_in_delta 1, @empty.similar(''), D
19
+ assert_in_delta 0, @empty.similar('not empty'), D
20
+ end
21
+
22
+ def test_small_match
23
+ assert_in_delta 4, @small.match(''), D
24
+ assert_in_delta 0, @small.match('test'), D
25
+ assert_in_delta 1, @small.match('testa'), D
26
+ assert_in_delta 5, @small.match('atest'), D
27
+ assert_in_delta 3, @small.match('teast'), D
28
+ assert_in_delta 4, @small.match('est'), D
29
+ assert_in_delta 1, @small.match('tes'), D
30
+ assert_in_delta 3, @small.match('tst'), D
31
+ assert_in_delta 1, @small.match('best'), D
32
+ assert_in_delta 1, @small.match('tost'), D
33
+ assert_in_delta 1, @small.match('tesa'), D
34
+ assert_in_delta 3, @small.match('taex'), D
35
+ assert_in_delta 9, @small.match('aaatestbbb'), D
36
+ end
37
+
38
+ def test_small_similar
39
+ assert_in_delta 0.0, @small.similar(''), D
40
+ assert_in_delta 1.0, @small.similar('test'), D
41
+ assert_in_delta 0.8, @small.similar('testa'), D
42
+ assert_in_delta 0.0, @small.similar('atest'), D
43
+ assert_in_delta 0.4, @small.similar('teast'), D
44
+ assert_in_delta 0, @small.similar('est'), D
45
+ assert_in_delta 0.75, @small.similar('tes'), D
46
+ assert_in_delta 0.25, @small.similar('tst'), D
47
+ assert_in_delta 0.75, @small.similar('best'), D
48
+ assert_in_delta 0.75, @small.similar('tost'), D
49
+ assert_in_delta 0.75, @small.similar('tesa'), D
50
+ assert_in_delta 0.25, @small.similar('taex'), D
51
+ assert_in_delta 0.1, @small.similar('aaatestbbb'), D
52
+ assert_in_delta 0.8, @small.pattern.hamming_similar('testa'), D
53
+ end
54
+
55
+ def test_long
56
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
57
+ end
58
+ end
59
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,85 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+
4
+ class TC_Levenshtein < Test::Unit::TestCase
5
+ include Amatch
6
+
7
+ D = 0.000001
8
+
9
+ def setup
10
+ @empty = Levenshtein.new('')
11
+ @simple = Levenshtein.new('test')
12
+ @long = Levenshtein.new('A' * 160)
13
+ end
14
+
15
+ def test_long
16
+ a = "lost this fantasy, this fantasy, this fantasy, this fantasy, this fantasy, this fantasy\r\n\r\nGood love Neat work\r\n\r\nSuper job Fancy work\r\n\r\nPants job Cool work"
17
+ b = "lost\r\n\r\nGood love Neat work\r\n\r\nSuper job Fancy work\r\n\r\nPants job Cool work"
18
+ p a.levenshtein_similar b
19
+ end
20
+
21
+ def test_match
22
+ assert_equal 4, @simple.match('')
23
+ assert_equal 0, @simple.match('test')
24
+ assert_equal 0, @simple.match('test')
25
+ assert_equal 1, @simple.match('testa')
26
+ assert_equal 1, @simple.match('atest')
27
+ assert_equal 1, @simple.match('teast')
28
+ assert_equal 1, @simple.match('est')
29
+ assert_equal 1, @simple.match('tes')
30
+ assert_equal 1, @simple.match('tst')
31
+ assert_equal 1, @simple.match('best')
32
+ assert_equal 1, @simple.match('tost')
33
+ assert_equal 1, @simple.match('tesa')
34
+ assert_equal 3, @simple.match('taex')
35
+ assert_equal 6, @simple.match('aaatestbbb')
36
+ end
37
+
38
+ def test_search
39
+ assert_equal 4, @simple.search('')
40
+ assert_equal 0, @empty.search('')
41
+ assert_equal 0, @empty.search('test')
42
+ assert_equal 0, @simple.search('aaatestbbb')
43
+ assert_equal 3, @simple.search('aaataexbbb')
44
+ assert_equal 4, @simple.search('aaaaaaaaa')
45
+ end
46
+
47
+ def test_array_result
48
+ assert_equal [2, 0], @simple.match(["tets", "test"])
49
+ assert_equal [1, 0], @simple.search(["tetsaaa", "testaaa"])
50
+ assert_raises(TypeError) { @simple.match([:foo, "bar"]) }
51
+ end
52
+
53
+ def test_pattern_setting
54
+ assert_raises(TypeError) { @simple.pattern = :something }
55
+ assert_equal 0, @simple.match('test')
56
+ @simple.pattern = ''
57
+ assert_equal 4, @simple.match('test')
58
+ @simple.pattern = 'test'
59
+ assert_equal 0, @simple.match('test')
60
+ end
61
+
62
+ def test_similar
63
+ assert_in_delta 1, @empty.similar(''), D
64
+ assert_in_delta 0, @empty.similar('not empty'), D
65
+ assert_in_delta 0.0, @simple.similar(''), D
66
+ assert_in_delta 1.0, @simple.similar('test'), D
67
+ assert_in_delta 0.8, @simple.similar('testa'), D
68
+ assert_in_delta 0.8, @simple.similar('atest'), D
69
+ assert_in_delta 0.8, @simple.similar('teast'), D
70
+ assert_in_delta 0.75, @simple.similar('est'), D
71
+ assert_in_delta 0.75, @simple.similar('tes'), D
72
+ assert_in_delta 0.75, @simple.similar('tst'), D
73
+ assert_in_delta 0.75, @simple.similar('best'), D
74
+ assert_in_delta 0.75, @simple.similar('tost'), D
75
+ assert_in_delta 0.75, @simple.similar('tesa'), D
76
+ assert_in_delta 0.25, @simple.similar('taex'), D
77
+ assert_in_delta 0.4, @simple.similar('aaatestbbb'), D
78
+ assert_in_delta 0.75, @simple.pattern.levenshtein_similar('est'), D
79
+ end
80
+
81
+ def test_long
82
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
83
+ end
84
+ end
85
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,62 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+
4
+ class TC_LongestSubsequence < Test::Unit::TestCase
5
+ include Amatch
6
+
7
+ D = 0.000001
8
+
9
+ def setup
10
+ @small = LongestSubsequence.new('test')
11
+ @empty = LongestSubsequence.new('')
12
+ @long = LongestSubsequence.new('A' * 160)
13
+ end
14
+
15
+ def test_empty_subsequence
16
+ assert_equal 0, @empty.match('')
17
+ assert_equal 0, @empty.match('a')
18
+ assert_equal 0, @small.match('')
19
+ assert_equal 0, @empty.match('not empty')
20
+ end
21
+
22
+ def test_small_subsequence
23
+ assert_equal 4, @small.match('test')
24
+ assert_equal 4, @small.match('testa')
25
+ assert_equal 4, @small.match('atest')
26
+ assert_equal 4, @small.match('teast')
27
+ assert_equal 3, @small.match('est')
28
+ assert_equal 3, @small.match('tes')
29
+ assert_equal 3, @small.match('tst')
30
+ assert_equal 3, @small.match('best')
31
+ assert_equal 3, @small.match('tost')
32
+ assert_equal 3, @small.match('tesa')
33
+ assert_equal 2, @small.match('taex')
34
+ assert_equal 1, @small.match('aaatbbb')
35
+ assert_equal 1, @small.match('aaasbbb')
36
+ assert_equal 4, @small.match('aaatestbbb')
37
+ end
38
+
39
+ def test_similar
40
+ assert_in_delta 1, @empty.similar(''), D
41
+ assert_in_delta 0, @empty.similar('not empty'), D
42
+ assert_in_delta 0.0, @small.similar(''), D
43
+ assert_in_delta 1.0, @small.similar('test'), D
44
+ assert_in_delta 0.8, @small.similar('testa'), D
45
+ assert_in_delta 0.8, @small.similar('atest'), D
46
+ assert_in_delta 0.8, @small.similar('teast'), D
47
+ assert_in_delta 0.75, @small.similar('est'), D
48
+ assert_in_delta 0.75, @small.similar('tes'), D
49
+ assert_in_delta 0.75, @small.similar('tst'), D
50
+ assert_in_delta 0.75, @small.similar('best'), D
51
+ assert_in_delta 0.75, @small.similar('tost'), D
52
+ assert_in_delta 0.75, @small.similar('tesa'), D
53
+ assert_in_delta 0.50, @small.similar('taex'), D
54
+ assert_in_delta 0.4, @small.similar('aaatestbbb'), D
55
+ assert_in_delta 0.75, @small.pattern.longest_subsequence_similar('est'), D
56
+ end
57
+
58
+ def test_long
59
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
60
+ end
61
+ end
62
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,62 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+
4
+ class TC_LongestSubstring < Test::Unit::TestCase
5
+ include Amatch
6
+
7
+ D = 0.000001
8
+
9
+ def setup
10
+ @small = LongestSubstring.new('test')
11
+ @empty = LongestSubstring.new('')
12
+ @long = LongestSubstring.new('A' * 160)
13
+ end
14
+
15
+ def test_empty_substring
16
+ assert_in_delta 0, @empty.match(''), D
17
+ assert_in_delta 0, @empty.match('a'), D
18
+ assert_in_delta 0, @small.match(''), D
19
+ assert_in_delta 0, @empty.match('not empty'), D
20
+ end
21
+
22
+ def test_small_substring
23
+ assert_in_delta 4, @small.match('test'), D
24
+ assert_in_delta 4, @small.match('testa'), D
25
+ assert_in_delta 4, @small.match('atest'), D
26
+ assert_in_delta 2, @small.match('teast'), D
27
+ assert_in_delta 3, @small.match('est'), D
28
+ assert_in_delta 3, @small.match('tes'), D
29
+ assert_in_delta 2, @small.match('tst'), D
30
+ assert_in_delta 3, @small.match('best'), D
31
+ assert_in_delta 2, @small.match('tost'), D
32
+ assert_in_delta 3, @small.match('tesa'), D
33
+ assert_in_delta 1, @small.match('taex'), D
34
+ assert_in_delta 1, @small.match('aaatbbb'), D
35
+ assert_in_delta 1, @small.match('aaasbbb'), D
36
+ assert_in_delta 4, @small.match('aaatestbbb'), D
37
+ end
38
+
39
+ def test_similar
40
+ assert_in_delta 1, @empty.similar(''), D
41
+ assert_in_delta 0, @empty.similar('not empty'), D
42
+ assert_in_delta 0.0, @small.similar(''), D
43
+ assert_in_delta 1.0, @small.similar('test'), D
44
+ assert_in_delta 0.8, @small.similar('testa'), D
45
+ assert_in_delta 0.8, @small.similar('atest'), D
46
+ assert_in_delta 0.4, @small.similar('teast'), D
47
+ assert_in_delta 0.75, @small.similar('est'), D
48
+ assert_in_delta 0.75, @small.similar('tes'), D
49
+ assert_in_delta 0.5, @small.similar('tst'), D
50
+ assert_in_delta 0.75, @small.similar('best'), D
51
+ assert_in_delta 0.5, @small.similar('tost'), D
52
+ assert_in_delta 0.75, @small.similar('tesa'), D
53
+ assert_in_delta 0.25, @small.similar('taex'), D
54
+ assert_in_delta 0.4, @small.similar('aaatestbbb'), D
55
+ assert_in_delta 0.75, @small.pattern.longest_substring_similar('est'), D
56
+ end
57
+
58
+ def test_long
59
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
60
+ end
61
+ end
62
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+
4
+ class TC_PairDistance < Test::Unit::TestCase
5
+ include Amatch
6
+
7
+ D = 0.000001
8
+
9
+ def setup
10
+ @single = PairDistance.new('test')
11
+ @empty = PairDistance.new('')
12
+ @france = PairDistance.new('republic of france')
13
+ @germany = PairDistance.new('federal republic of germany')
14
+ @csv = PairDistance.new('foo,bar,baz')
15
+ @long = PairDistance.new('A' * 160)
16
+ end
17
+
18
+ def test_empty
19
+ assert_in_delta 1, @empty.match(''), D
20
+ assert_in_delta 0, @empty.match('not empty'), D
21
+ assert_in_delta 1, @empty.similar(''), D
22
+ assert_in_delta 0, @empty.similar('not empty'), D
23
+ end
24
+
25
+ def test_countries
26
+ assert_in_delta 0.5555555, @france.match('france'), D
27
+ assert_in_delta 0.1052631, @france.match('germany'), D
28
+ assert_in_delta 0.4615384, @germany.match('germany'), D
29
+ assert_in_delta 0.16, @germany.match('france'), D
30
+ assert_in_delta 0.6829268,
31
+ @germany.match('german democratic republic'), D
32
+ assert_in_delta 0.72,
33
+ @france.match('french republic'), D
34
+ assert_in_delta 0.4375,
35
+ @germany.match('french republic'), D
36
+ assert_in_delta 0.5294117,
37
+ @france.match('german democratic republic'), D
38
+ end
39
+
40
+ def test_single
41
+ assert_in_delta 0, @single.match(''), D
42
+ assert_in_delta 1, @single.match('test'), D
43
+ assert_in_delta 0.8571428, @single.match('testa'), D
44
+ assert_in_delta 0.8571428, @single.match('atest'), D
45
+ assert_in_delta 0.5714285, @single.match('teast'), D
46
+ assert_in_delta 0.8, @single.match('est'), D
47
+ assert_in_delta 0.8, @single.match('tes'), D
48
+ assert_in_delta 0.4, @single.match('tst'), D
49
+ assert_in_delta 0.6666666, @single.match('best'), D
50
+ assert_in_delta 0.3333333, @single.match('tost'), D
51
+ assert_in_delta 0.6666666, @single.match('tesa'), D
52
+ assert_in_delta 0.0, @single.match('taex'), D
53
+ assert_in_delta 0.5, @single.match('aaatestbbb'), D
54
+ assert_in_delta 0.6, @single.match('aaa test bbb'), D
55
+ assert_in_delta 0.6, @single.match('test aaa bbb'), D
56
+ assert_in_delta 0.6, @single.match('bbb aaa test'), D
57
+ assert_in_delta 0.8571428, @single.pattern.pair_distance_similar('atest'), D
58
+ end
59
+
60
+ def test_csv
61
+ assert_in_delta 0, @csv.match('', /,/), D
62
+ assert_in_delta 0.5, @csv.match('foo', /,/), D
63
+ assert_in_delta 0.5, @csv.match('bar', /,/), D
64
+ assert_in_delta 0.5, @csv.match('baz', /,/), D
65
+ assert_in_delta 0.8, @csv.match('foo,bar', /,/), D
66
+ assert_in_delta 0.8, @csv.match('bar,foo', /,/), D
67
+ assert_in_delta 0.8, @csv.match('bar,baz', /,/), D
68
+ assert_in_delta 0.8, @csv.match('baz,bar', /,/), D
69
+ assert_in_delta 0.8, @csv.match('foo,baz', /,/), D
70
+ assert_in_delta 0.8, @csv.match('baz,foo', /,/), D
71
+ assert_in_delta 1, @csv.match('foo,bar,baz', /,/), D
72
+ assert_in_delta 1, @csv.match('foo,baz,bar', /,/), D
73
+ assert_in_delta 1, @csv.match('baz,foo,bar', /,/), D
74
+ assert_in_delta 1, @csv.match('baz,bar,foo', /,/), D
75
+ assert_in_delta 1, @csv.match('bar,foo,baz', /,/), D
76
+ assert_in_delta 1, @csv.match('bar,baz,foo', /,/), D
77
+ assert_in_delta 1, @csv.match('foo,bar,baz', nil), D
78
+ assert_in_delta 0.9, @csv.match('foo,baz,bar', nil), D
79
+ assert_in_delta 0.9, @csv.match('foo,baz,bar'), D
80
+ assert_in_delta 0.9, @csv.similar('foo,baz,bar'), D
81
+ end
82
+
83
+ def test_long
84
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
85
+ end
86
+ end
87
+ # vim: set et sw=2 ts=2:
@@ -0,0 +1,98 @@
1
+ require 'test/unit'
2
+ require 'amatch'
3
+ require 'test_levenshtein'
4
+
5
+ class TC_Sellers < TC_Levenshtein
6
+ include Amatch
7
+
8
+ D = 0.000001
9
+
10
+ def setup
11
+ @empty = Sellers.new('')
12
+ @simple = Sellers.new('test')
13
+ @long = Sellers.new('A' * 160)
14
+ end
15
+
16
+ def test_weights
17
+ assert_in_delta 1, @simple.substitution, D
18
+ assert_in_delta 1, @simple.insertion, D
19
+ assert_in_delta 1, @simple.deletion, D
20
+ @simple.insertion = 1
21
+ @simple.substitution = @simple.deletion = 1000
22
+ assert_in_delta 1, @simple.match('tst'), D
23
+ assert_in_delta 1, @simple.search('bbbtstccc'), D
24
+ @simple.deletion = 1
25
+ @simple.substitution = @simple.insertion = 1000
26
+ assert_in_delta 1, @simple.match('tedst'), D
27
+ assert_in_delta 1, @simple.search('bbbtedstccc'), D
28
+ @simple.substitution = 1
29
+ @simple.deletion = @simple.insertion = 1000
30
+ assert_in_delta 1, @simple.match('tast'), D
31
+ assert_in_delta 1, @simple.search('bbbtastccc'), D
32
+ @simple.insertion = 0.5
33
+ @simple.substitution = @simple.deletion = 1000
34
+ assert_in_delta 0.5, @simple.match('tst'), D
35
+ assert_in_delta 0.5, @simple.search('bbbtstccc'), D
36
+ @simple.deletion = 0.5
37
+ @simple.substitution = @simple.insertion = 1000
38
+ assert_in_delta 0.5, @simple.match('tedst'), D
39
+ assert_in_delta 0.5, @simple.search('bbbtedstccc'), D
40
+ @simple.substitution = 0.5
41
+ @simple.deletion = @simple.insertion = 1000
42
+ assert_in_delta 0.5, @simple.match('tast'), D
43
+ assert_in_delta 0.5, @simple.search('bbbtastccc'), D
44
+ @simple.reset_weights
45
+ assert_in_delta 1, @simple.substitution, D
46
+ assert_in_delta 1, @simple.insertion, D
47
+ assert_in_delta 1, @simple.deletion, D
48
+ end
49
+
50
+ def test_weight_exceptions
51
+ assert_raises(TypeError) { @simple.substitution = :something }
52
+ assert_raises(TypeError) { @simple.insertion = :something }
53
+ assert_raises(TypeError) { @simple.deletion = :something }
54
+ end
55
+
56
+ def test_similar
57
+ assert_in_delta 0.0, @simple.similar(''), D
58
+ assert_in_delta 1.0, @simple.similar('test'), D
59
+ assert_in_delta 0.8, @simple.similar('testa'), D
60
+ assert_in_delta 0.8, @simple.similar('atest'), D
61
+ assert_in_delta 0.8, @simple.similar('teast'), D
62
+ assert_in_delta 0.75, @simple.similar('est'), D
63
+ assert_in_delta 0.75, @simple.similar('tes'), D
64
+ assert_in_delta 0.75, @simple.similar('tst'), D
65
+ assert_in_delta 0.75, @simple.similar('best'), D
66
+ assert_in_delta 0.75, @simple.similar('tost'), D
67
+ assert_in_delta 0.75, @simple.similar('tesa'), D
68
+ assert_in_delta 0.25, @simple.similar('taex'), D
69
+ assert_in_delta 0.4, @simple.similar('aaatestbbb'), D
70
+ assert_in_delta 0.75, @simple.pattern.levenshtein_similar('est'), D
71
+ end
72
+
73
+ def test_similar
74
+ assert_in_delta 1, @empty.similar(''), D
75
+ assert_in_delta 0, @empty.similar('not empty'), D
76
+ assert_in_delta 0.0, @simple.similar(''), D
77
+ assert_in_delta 1.0, @simple.similar('test'), D
78
+ assert_in_delta 0.8, @simple.similar('testa'), D
79
+ assert_in_delta 0.8, @simple.similar('atest'), D
80
+ assert_in_delta 0.8, @simple.similar('teast'), D
81
+ assert_in_delta 0.75, @simple.similar('est'), D
82
+ assert_in_delta 0.75, @simple.similar('tes'), D
83
+ assert_in_delta 0.75, @simple.similar('tst'), D
84
+ assert_in_delta 0.75, @simple.similar('best'), D
85
+ assert_in_delta 0.75, @simple.similar('tost'), D
86
+ assert_in_delta 0.75, @simple.similar('tesa'), D
87
+ assert_in_delta 0.25, @simple.similar('taex'), D
88
+ assert_in_delta 0.4, @simple.similar('aaatestbbb'), D
89
+ @simple.insertion = 1
90
+ @simple.substitution = @simple.deletion = 2
91
+ assert_in_delta 0.875, @simple.similar('tst'), D
92
+ end
93
+
94
+ def test_long
95
+ assert_in_delta 1.0, @long.similar(@long.pattern), D
96
+ end
97
+ end
98
+ # vim: set et sw=2 ts=2: