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