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      void testLogestCommonSubsequence() {
88          assertEquals("", subject.logestCommonSubsequence("", ""));
89          assertEquals("", subject.logestCommonSubsequence("left", ""));
90          assertEquals("", subject.logestCommonSubsequence("", "right"));
91          assertEquals("", subject.logestCommonSubsequence("l", "a"));
92          assertEquals("", subject.logestCommonSubsequence("left", "a"));
93          assertEquals("fog", subject.logestCommonSubsequence("frog", "fog"));
94          assertEquals("", subject.logestCommonSubsequence("fly", "ant"));
95          assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo"));
96          assertEquals("ABC Corp", subject.logestCommonSubsequence("ABC Corporation", "ABC Corp"));
97          assertEquals("D  H Enterprises Inc", subject.logestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc."));
98          assertEquals("My Gym Childrens Fitness", subject.logestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
99          assertEquals("PENNSYLVNIA", subject.logestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA"));
100         assertEquals("t", subject.logestCommonSubsequence("left", "right"));
101         assertEquals("tttt", subject.logestCommonSubsequence("leettteft", "ritttght"));
102         assertEquals("the same string", subject.logestCommonSubsequence("the same string", "the same string"));
103     }
104 
105     @Test
106     void testLongestCommonSubsequence() {
107         assertEquals("", subject.longestCommonSubsequence("", ""));
108         assertEquals("", subject.longestCommonSubsequence("left", ""));
109         assertEquals("", subject.longestCommonSubsequence("", "right"));
110         assertEquals("", subject.longestCommonSubsequence("l", "a"));
111         assertEquals("", subject.longestCommonSubsequence("left", "a"));
112         assertEquals("fog", subject.longestCommonSubsequence("frog", "fog"));
113         assertEquals("", subject.longestCommonSubsequence("fly", "ant"));
114         assertEquals("h", subject.longestCommonSubsequence("elephant", "hippo"));
115         assertEquals("ABC Corp", subject.longestCommonSubsequence("ABC Corporation", "ABC Corp"));
116         assertEquals("D  H Enterprises Inc", subject.longestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc."));
117         assertEquals("My Gym Childrens Fitness", subject.longestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
118         assertEquals("PENNSYLVNIA", subject.longestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA"));
119         assertEquals("t", subject.longestCommonSubsequence("left", "right"));
120         assertEquals("tttt", subject.longestCommonSubsequence("leettteft", "ritttght"));
121         assertEquals("the same string", subject.longestCommonSubsequence("the same string", "the same string"));
122     }
123 
124     @Test
125     void testLongestCommonSubsequenceApply() {
126         assertEquals(0, subject.apply("", ""));
127         assertEquals(0, subject.apply("left", ""));
128         assertEquals(0, subject.apply("", "right"));
129         assertEquals(3, subject.apply("frog", "fog"));
130         assertEquals(0, subject.apply("fly", "ant"));
131         assertEquals(1, subject.apply("elephant", "hippo"));
132         assertEquals(8, subject.apply("ABC Corporation", "ABC Corp"));
133         assertEquals(20, subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
134         assertEquals(24, subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
135         assertEquals(11, subject.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
136         assertEquals(1, subject.apply("left", "right"));
137         assertEquals(4, subject.apply("leettteft", "ritttght"));
138         assertEquals(15, subject.apply("the same string", "the same string"));
139     }
140 
141     @Test
142     void testLongestCommonSubstringLengthArray() {
143         assertArrayEquals(new int[][]{ {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 2, 2}}, subject.longestCommonSubstringLengthArray("ab", "abc"));
144     }
145 
146 }