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 import android.test.suitebuilder.annotation.SmallTest; 21 22 @SmallTest 23 public class EditDistanceTests extends AndroidTestCase { 24 @Override 25 protected void setUp() throws Exception { 26 super.setUp(); 27 } 28 29 @Override 30 protected void tearDown() throws Exception { 31 super.tearDown(); 32 } 33 34 /* 35 * dist(kitten, sitting) == 3 36 * 37 * kitten- 38 * .|||.| 39 * sitting 40 */ 41 public void testExample1() { 42 final int dist = BinaryDictionary.editDistance("kitten", "sitting"); 43 assertEquals("edit distance between 'kitten' and 'sitting' is 3", 44 3, dist); 45 } 46 47 /* 48 * dist(Sunday, Saturday) == 3 49 * 50 * Saturday 51 * | |.||| 52 * S--unday 53 */ 54 public void testExample2() { 55 final int dist = BinaryDictionary.editDistance("Saturday", "Sunday"); 56 assertEquals("edit distance between 'Saturday' and 'Sunday' is 3", 57 3, dist); 58 } 59 60 public void testBothEmpty() { 61 final int dist = BinaryDictionary.editDistance("", ""); 62 assertEquals("when both string are empty, no edits are needed", 63 0, dist); 64 } 65 66 public void testFirstArgIsEmpty() { 67 final int dist = BinaryDictionary.editDistance("", "aaaa"); 68 assertEquals("when only one string of the arguments is empty," 69 + " the edit distance is the length of the other.", 70 4, dist); 71 } 72 73 public void testSecoondArgIsEmpty() { 74 final int dist = BinaryDictionary.editDistance("aaaa", ""); 75 assertEquals("when only one string of the arguments is empty," 76 + " the edit distance is the length of the other.", 77 4, dist); 78 } 79 80 public void testSameStrings() { 81 final String arg1 = "The quick brown fox jumps over the lazy dog."; 82 final String arg2 = "The quick brown fox jumps over the lazy dog."; 83 final int dist = BinaryDictionary.editDistance(arg1, arg2); 84 assertEquals("when same strings are passed, distance equals 0.", 85 0, dist); 86 } 87 88 public void testSameReference() { 89 final String arg = "The quick brown fox jumps over the lazy dog."; 90 final int dist = BinaryDictionary.editDistance(arg, arg); 91 assertEquals("when same string references are passed, the distance equals 0.", 92 0, dist); 93 } 94 95 public void testNullArg() { 96 try { 97 BinaryDictionary.editDistance(null, "aaa"); 98 fail("IllegalArgumentException should be thrown."); 99 } catch (Exception e) { 100 assertTrue(e instanceof IllegalArgumentException); 101 } 102 try { 103 BinaryDictionary.editDistance("aaa", null); 104 fail("IllegalArgumentException should be thrown."); 105 } catch (Exception e) { 106 assertTrue(e instanceof IllegalArgumentException); 107 } 108 } 109 } 110