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 LongestCommonSubsequence}.
27   */
28  class LongestCommonSubsequenceTest {
29  
30      private static LongestCommonSubsequence subject;
31  
32      @BeforeAll
33      public static void setup() {
34          subject = new LongestCommonSubsequence();
35      }
36  
37      @Test
38      @SuppressWarnings("deprecation")
39      void testGettingLogestCommonSubsequenceNullNull() {
40          assertThrows(IllegalArgumentException.class, () -> subject.logestCommonSubsequence(null, null));
41      }
42  
43      @Test
44      @SuppressWarnings("deprecation")
45      void testGettingLogestCommonSubsequenceNullString() {
46          assertThrows(IllegalArgumentException.class, () -> subject.logestCommonSubsequence(null, "right"));
47      }
48  
49      @Test
50      @SuppressWarnings("deprecation")
51      void testGettingLogestCommonSubsequenceStringNull() {
52          assertThrows(IllegalArgumentException.class, () -> subject.logestCommonSubsequence(" ", null));
53      }
54  
55      @Test
56      void testGettingLongestCommonSubsequenceApplyNullNull() {
57          assertThrows(IllegalArgumentException.class, () -> subject.apply(null, null));
58      }
59  
60      @Test
61      void testGettingLongestCommonSubsequenceApplyNullString() {
62          assertThrows(IllegalArgumentException.class, () -> subject.apply(null, "right"));
63      }
64  
65      @Test
66      void testGettingLongestCommonSubsequenceApplyStringNull() {
67          assertThrows(IllegalArgumentException.class, () -> subject.apply(" ", null));
68      }
69  
70      @Test
71      void testGettingLongestCommonSubsequenceNullNull() {
72          assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubsequence(null, null));
73      }
74  
75      @Test
76      void testGettingLongestCommonSubsequenceNullString() {
77          assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubsequence(null, "right"));
78      }
79  
80      @Test
81      void testGettingLongestCommonSubsequenceStringNull() {
82          assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubsequence(" ", null));
83      }
84  
85      @Test
86      @Deprecated
87      void testLogestCommonSubsequence() {
88          assertEquals("", subject.logestCommonSubsequence("", ""));
89          assertEquals("", subject.logestCommonSubsequence("left", ""));
90          assertEquals("", subject.logestCommonSubsequence("", "right"));
91          assertEquals("fog", subject.logestCommonSubsequence("frog", "fog"));
92          assertEquals("", subject.logestCommonSubsequence("fly", "ant"));
93          assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo"));
94          assertEquals("ABC Corp", subject.logestCommonSubsequence("ABC Corporation", "ABC Corp"));
95          assertEquals("D  H Enterprises Inc", subject.logestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc."));
96          assertEquals("My Gym Childrens Fitness", subject.logestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
97          assertEquals("PENNSYLVNIA", subject.logestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA"));
98          assertEquals("t", subject.logestCommonSubsequence("left", "right"));
99          assertEquals("tttt", subject.logestCommonSubsequence("leettteft", "ritttght"));
100         assertEquals("the same string", subject.logestCommonSubsequence("the same string", "the same string"));
101     }
102 
103     @Test
104     void testLongestCommonSubsequence() {
105         assertEquals("", subject.longestCommonSubsequence("", ""));
106         assertEquals("", subject.longestCommonSubsequence("left", ""));
107         assertEquals("", subject.longestCommonSubsequence("", "right"));
108         assertEquals("fog", subject.longestCommonSubsequence("frog", "fog"));
109         assertEquals("", subject.longestCommonSubsequence("fly", "ant"));
110         assertEquals("h", subject.longestCommonSubsequence("elephant", "hippo"));
111         assertEquals("ABC Corp", subject.longestCommonSubsequence("ABC Corporation", "ABC Corp"));
112         assertEquals("D  H Enterprises Inc", subject.longestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc."));
113         assertEquals("My Gym Childrens Fitness", subject.longestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
114         assertEquals("PENNSYLVNIA", subject.longestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA"));
115         assertEquals("t", subject.longestCommonSubsequence("left", "right"));
116         assertEquals("tttt", subject.longestCommonSubsequence("leettteft", "ritttght"));
117         assertEquals("the same string", subject.longestCommonSubsequence("the same string", "the same string"));
118     }
119 
120     @Test
121     void testLongestCommonSubsequenceApply() {
122         assertEquals(0, subject.apply("", ""));
123         assertEquals(0, subject.apply("left", ""));
124         assertEquals(0, subject.apply("", "right"));
125         assertEquals(3, subject.apply("frog", "fog"));
126         assertEquals(0, subject.apply("fly", "ant"));
127         assertEquals(1, subject.apply("elephant", "hippo"));
128         assertEquals(8, subject.apply("ABC Corporation", "ABC Corp"));
129         assertEquals(20, subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
130         assertEquals(24, subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
131         assertEquals(11, subject.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
132         assertEquals(1, subject.apply("left", "right"));
133         assertEquals(4, subject.apply("leettteft", "ritttght"));
134         assertEquals(15, subject.apply("the same string", "the same string"));
135     }
136 }