Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin;
     18 
     19 import android.test.AndroidTestCase;
     20 
     21 public class EditDistanceTests extends AndroidTestCase {
     22     @Override
     23     protected void setUp() throws Exception {
     24         super.setUp();
     25     }
     26 
     27     @Override
     28     protected void tearDown() throws Exception {
     29         super.tearDown();
     30     }
     31 
     32     /*
     33      * dist(kitten, sitting) == 3
     34      *
     35      * kitten-
     36      * .|||.|
     37      * sitting
     38      */
     39     public void testExample1() {
     40         final int dist = Utils.editDistance("kitten", "sitting");
     41         assertEquals("edit distance between 'kitten' and 'sitting' is 3",
     42                 3, dist);
     43     }
     44 
     45     /*
     46      * dist(Sunday, Saturday) == 3
     47      *
     48      * Saturday
     49      * |  |.|||
     50      * S--unday
     51      */
     52     public void testExample2() {
     53         final int dist = Utils.editDistance("Saturday", "Sunday");
     54         assertEquals("edit distance between 'Saturday' and 'Sunday' is 3",
     55                 3, dist);
     56     }
     57 
     58     public void testBothEmpty() {
     59         final int dist = Utils.editDistance("", "");
     60         assertEquals("when both string are empty, no edits are needed",
     61                 0, dist);
     62     }
     63 
     64     public void testFirstArgIsEmpty() {
     65         final int dist = Utils.editDistance("", "aaaa");
     66         assertEquals("when only one string of the arguments is empty,"
     67                  + " the edit distance is the length of the other.",
     68                  4, dist);
     69     }
     70 
     71     public void testSecoondArgIsEmpty() {
     72         final int dist = Utils.editDistance("aaaa", "");
     73         assertEquals("when only one string of the arguments is empty,"
     74                  + " the edit distance is the length of the other.",
     75                  4, dist);
     76     }
     77 
     78     public void testSameStrings() {
     79         final String arg1 = "The quick brown fox jumps over the lazy dog.";
     80         final String arg2 = "The quick brown fox jumps over the lazy dog.";
     81         final int dist = Utils.editDistance(arg1, arg2);
     82         assertEquals("when same strings are passed, distance equals 0.",
     83                 0, dist);
     84     }
     85 
     86     public void testSameReference() {
     87         final String arg = "The quick brown fox jumps over the lazy dog.";
     88         final int dist = Utils.editDistance(arg, arg);
     89         assertEquals("when same string references are passed, the distance equals 0.",
     90                 0, dist);
     91     }
     92 
     93     public void testNullArg() {
     94         try {
     95             Utils.editDistance(null, "aaa");
     96             fail("IllegalArgumentException should be thrown.");
     97         } catch (Exception e) {
     98             assertTrue(e instanceof IllegalArgumentException);
     99         }
    100         try {
    101             Utils.editDistance("aaa", null);
    102             fail("IllegalArgumentException should be thrown.");
    103         } catch (Exception e) {
    104             assertTrue(e instanceof IllegalArgumentException);
    105         }
    106     }
    107 }
    108