blob: 5ff97e87154fbda7e1b1ed5fed338cf82cc52cfe [file] [log] [blame]
Laurent Le Brun9bf2da42016-06-01 16:36:41 +00001// Copyright 2016 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.util;
15
16import static com.google.common.truth.Truth.assertThat;
17
Laurent Le Brund8b80c32016-06-03 14:38:19 +000018import com.google.common.collect.Lists;
19
Laurent Le Brun9bf2da42016-06-01 16:36:41 +000020import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
Laurent Le Brund8b80c32016-06-03 14:38:19 +000024import java.util.List;
25
Laurent Le Brun9bf2da42016-06-01 16:36:41 +000026/**
27 * Tests for {@link SpellChecker}.
28 */
29@RunWith(JUnit4.class)
30public class SpellCheckerTest {
31
32 private void assertDistance(String s1, String s2, int distance) {
33 assertThat(SpellChecker.editDistance(s1, s2, 100)).isEqualTo(distance);
34 assertThat(SpellChecker.editDistance(s1, s2, distance)).isEqualTo(distance);
35
36 // Symmetry
37 assertThat(SpellChecker.editDistance(s2, s1, 100)).isEqualTo(distance);
38 assertThat(SpellChecker.editDistance(s2, s1, distance)).isEqualTo(distance);
39 }
40
41 @Test
42 public void editDistance_1() throws Exception {
43 // Deletion
44 assertDistance("abcdef", "abdef", 1);
45 assertDistance("abcdef", "abcde", 1);
46 assertDistance("abcdef", "bcdef", 1);
47
48 // Replacement
49 assertDistance("abcdef", "_bcdef", 1);
50 assertDistance("abcdef", "abc_ef", 1);
51 assertDistance("abcdef", "abcde_", 1);
52
53 // Insertion
54 assertDistance("abcdef", "_abcdef", 1);
55 assertDistance("abcdef", "abcd_ef", 1);
56 assertDistance("abcdef", "abcdef_", 1);
57 }
58
59 @Test
60 public void editDistance_general() throws Exception {
61 assertDistance("", "", 0);
62 assertDistance("abcd", "abcd", 0);
63 assertDistance("abcde", "", 5);
64 assertDistance("abcde", "12345", 5);
65 assertDistance("ab", "ba", 2);
66 assertDistance("abba", "acca", 2);
67 assertDistance("abaa", "aaca", 2);
68 assertDistance("kitten", "sitting", 3);
69 assertDistance("kitten kitten", "sitting sitting", 6);
70 assertDistance("flaw", "lawn", 2);
71 }
72
73 @Test
74 public void editDistance_maxDistance() throws Exception {
75 assertThat(SpellChecker.editDistance("kitten", "sitting", 0)).isEqualTo(-1);
76 assertThat(SpellChecker.editDistance("kitten", "sitting", 1)).isEqualTo(-1);
77 assertThat(SpellChecker.editDistance("kitten", "sitting", 2)).isEqualTo(-1);
78 assertThat(SpellChecker.editDistance("kitten", "sitting", 3)).isEqualTo(3);
79 assertThat(SpellChecker.editDistance("kitten", "sitting", 4)).isEqualTo(3);
80
81 assertThat(SpellChecker.editDistance("abcdefg", "s", 2)).isEqualTo(-1);
82 }
Laurent Le Brund8b80c32016-06-03 14:38:19 +000083
84 @Test
85 public void suggest() throws Exception {
86 List<String> dict = Lists.newArrayList(
Laurent Le Brun20424c42017-03-20 16:59:12 +000087 "isalnum", "isalpha", "isdigit", "islower", "isupper", "find", "join", "range",
88 "rsplit", "rstrip", "split", "splitlines", "startswith", "strip", "title", "upper",
89 "x", "xyz");
Laurent Le Brund8b80c32016-06-03 14:38:19 +000090
91 assertThat(SpellChecker.suggest("isdfit", dict)).isEqualTo("isdigit");
92 assertThat(SpellChecker.suggest("rspit", dict)).isEqualTo("rsplit");
93 assertThat(SpellChecker.suggest("IS_LOWER", dict)).isEqualTo("islower");
94 assertThat(SpellChecker.suggest("sartwigh", dict)).isEqualTo("startswith");
95 assertThat(SpellChecker.suggest("SplitAllLines", dict)).isEqualTo("splitlines");
96 assertThat(SpellChecker.suggest("fird", dict)).isEqualTo("find");
97 assertThat(SpellChecker.suggest("stip", dict)).isEqualTo("strip");
98 assertThat(SpellChecker.suggest("isAln", dict)).isEqualTo("isalnum");
Laurent Le Brun20424c42017-03-20 16:59:12 +000099 assertThat(SpellChecker.suggest("targe", dict)).isEqualTo("range");
100 assertThat(SpellChecker.suggest("rarget", dict)).isEqualTo("range");
101 assertThat(SpellChecker.suggest("xyw", dict)).isEqualTo("xyz");
Laurent Le Brund8b80c32016-06-03 14:38:19 +0000102
Laurent Le Brun20424c42017-03-20 16:59:12 +0000103 assertThat(SpellChecker.suggest("target", dict)).isNull();
104 assertThat(SpellChecker.suggest("isAl", dict)).isNull();
105 assertThat(SpellChecker.suggest("", dict)).isNull();
106 assertThat(SpellChecker.suggest("f", dict)).isNull();
107 assertThat(SpellChecker.suggest("fir", dict)).isNull();
108 assertThat(SpellChecker.suggest("wqevxc", dict)).isNull();
109 assertThat(SpellChecker.suggest("ialsnuaip", dict)).isNull();
110 assertThat(SpellChecker.suggest("xy", dict)).isNull();
Laurent Le Brund8b80c32016-06-03 14:38:19 +0000111 }
Laurent Le Brun9bf2da42016-06-01 16:36:41 +0000112}