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    *      https://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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.junit.jupiter.api.BeforeAll;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link LongestCommonSubsequenceDistance}.
27   */
28  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      void testGettingLongestCommonSubsequenceDistance() {
39          assertEquals(0, subject.apply("", ""));
40          assertEquals(4, subject.apply("left", ""));
41          assertEquals(5, subject.apply("", "right"));
42          assertEquals(1, subject.apply("frog", "fog"));
43          assertEquals(6, subject.apply("fly", "ant"));
44          assertEquals(11, subject.apply("elephant", "hippo"));
45          assertEquals(7, subject.apply("ABC Corporation", "ABC Corp"));
46          assertEquals(4, subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
47          assertEquals(9, subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
48          assertEquals(3, subject.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
49          assertEquals(7, subject.apply("left", "right"));
50          assertEquals(9, subject.apply("leettteft", "ritttght"));
51          assertEquals(0, subject.apply("the same string", "the same string"));
52      }
53  
54      @Test
55      void testGettingLongestCommonSubsequenceDistanceNullNull() {
56          assertThrows(IllegalArgumentException.class, () -> subject.apply(null, null));
57      }
58  
59      @Test
60      void testGettingLongestCommonSubsequenceDistanceNullString() {
61          assertThrows(IllegalArgumentException.class, () -> subject.apply(null, "right"));
62      }
63  
64      @Test
65      void testGettingLongestCommonSubsequenceDistanceStringNull() {
66          assertThrows(IllegalArgumentException.class, () -> subject.apply(" ", null));
67      }
68  
69  }