View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.text.similarity;
18  
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
21  
22  import org.junit.jupiter.api.BeforeAll;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link LongestCommonSubsequenceDistance}.
27   */
28  public class LongestCommonSubsequenceDistanceTest {
29  
30      private static LongestCommonSubsequenceDistance subject;
31  
32      @BeforeAll
33      public static void setup() {
34          subject = new LongestCommonSubsequenceDistance();
35      }
36  
37      @Test
38      public void testGettingLongestCommonSubsequenceDistance() {
39          assertThat(subject.apply("", "")).isEqualTo(0);
40          assertThat(subject.apply("left", "")).isEqualTo(4);
41          assertThat(subject.apply("", "right")).isEqualTo(5);
42          assertThat(subject.apply("frog", "fog")).isEqualTo(1);
43          assertThat(subject.apply("fly", "ant")).isEqualTo(6);
44          assertThat(subject.apply("elephant", "hippo")).isEqualTo(11);
45          assertThat(subject.apply("ABC Corporation", "ABC Corp")).isEqualTo(7);
46          assertThat(subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.")).isEqualTo(4);
47          assertThat(subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness")).isEqualTo(9);
48          assertThat(subject.apply("PENNSYLVANIA", "PENNCISYLVNIA")).isEqualTo(3);
49          assertThat(subject.apply("left", "right")).isEqualTo(7);
50          assertThat(subject.apply("leettteft", "ritttght")).isEqualTo(9);
51          assertThat(subject.apply("the same string", "the same string")).isEqualTo(0);
52      }
53  
54      @Test
55      public void testGettingLongestCommonSubsequenceDistanceNullNull() {
56          assertThatIllegalArgumentException().isThrownBy(() -> subject.apply(null, null));
57      }
58  
59      @Test
60      public void testGettingLongestCommonSubsequenceDistanceNullString() {
61          assertThatIllegalArgumentException().isThrownBy(() -> subject.apply(null, "right"));
62      }
63  
64      @Test
65      public void testGettingLongestCommonSubsequenceDistanceStringNull() {
66          assertThatIllegalArgumentException().isThrownBy(() -> subject.apply(" ", null));
67      }
68  
69  }