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