aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/inputmethod/latin/utils/EditDistanceTests.java
blob: 58312264bc476d3b2d064bec63d7c39d9f7a7712 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin.utils;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

@SmallTest
public class EditDistanceTests extends AndroidTestCase {
    /*
     * dist(kitten, sitting) == 3
     *
     * kitten-
     * .|||.|
     * sitting
     */
    public void testExample1() {
        final int dist = BinaryDictionaryUtils.editDistance("kitten", "sitting");
        assertEquals("edit distance between 'kitten' and 'sitting' is 3",
                3, dist);
    }

    /*
     * dist(Sunday, Saturday) == 3
     *
     * Saturday
     * |  |.|||
     * S--unday
     */
    public void testExample2() {
        final int dist = BinaryDictionaryUtils.editDistance("Saturday", "Sunday");
        assertEquals("edit distance between 'Saturday' and 'Sunday' is 3",
                3, dist);
    }

    public void testBothEmpty() {
        final int dist = BinaryDictionaryUtils.editDistance("", "");
        assertEquals("when both string are empty, no edits are needed",
                0, dist);
    }

    public void testFirstArgIsEmpty() {
        final int dist = BinaryDictionaryUtils.editDistance("", "aaaa");
        assertEquals("when only one string of the arguments is empty,"
                 + " the edit distance is the length of the other.",
                 4, dist);
    }

    public void testSecoondArgIsEmpty() {
        final int dist = BinaryDictionaryUtils.editDistance("aaaa", "");
        assertEquals("when only one string of the arguments is empty,"
                 + " the edit distance is the length of the other.",
                 4, dist);
    }

    public void testSameStrings() {
        final String arg1 = "The quick brown fox jumps over the lazy dog.";
        final String arg2 = "The quick brown fox jumps over the lazy dog.";
        final int dist = BinaryDictionaryUtils.editDistance(arg1, arg2);
        assertEquals("when same strings are passed, distance equals 0.",
                0, dist);
    }

    public void testSameReference() {
        final String arg = "The quick brown fox jumps over the lazy dog.";
        final int dist = BinaryDictionaryUtils.editDistance(arg, arg);
        assertEquals("when same string references are passed, the distance equals 0.",
                0, dist);
    }

    public void testNullArg() {
        try {
            BinaryDictionaryUtils.editDistance(null, "aaa");
            fail("IllegalArgumentException should be thrown.");
        } catch (Exception e) {
            assertTrue(e instanceof IllegalArgumentException);
        }
        try {
            BinaryDictionaryUtils.editDistance("aaa", null);
            fail("IllegalArgumentException should be thrown.");
        } catch (Exception e) {
            assertTrue(e instanceof IllegalArgumentException);
        }
    }
}