1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }