1+ import * as fc from 'fast-check' ;
12import longestCommonSubstring from '../longestCommonSubstring' ;
23
34describe ( 'longestCommonSubstring' , ( ) => {
@@ -8,4 +9,73 @@ describe('longestCommonSubstring', () => {
89 expect ( longestCommonSubstring ( 'ABABC' , 'BABCA' ) ) . toBe ( 'BABC' ) ;
910 expect ( longestCommonSubstring ( 'BABCA' , 'ABCBA' ) ) . toBe ( 'ABC' ) ;
1011 } ) ;
12+ it ( 'should find same length whatever the ordering of parameters [property]' , ( ) => {
13+ fc . assert (
14+ fc . property (
15+ fc . fullUnicodeString ( ) ,
16+ fc . fullUnicodeString ( ) ,
17+ ( s1 , s2 ) => longestCommonSubstring ( s1 , s2 ) . length === longestCommonSubstring ( s2 , s1 ) . length
18+ )
19+ ) ;
20+ } ) ;
21+ it ( 'should be a substring of both strings [property]' , ( ) => {
22+ fc . assert (
23+ fc . property (
24+ fc . fullUnicodeString ( ) ,
25+ fc . fullUnicodeString ( ) ,
26+ ( s1 , s2 ) => {
27+ const longest = longestCommonSubstring ( s1 , s2 ) ;
28+ return s1 . includes ( longest ) && s2 . includes ( longest ) ;
29+ }
30+ )
31+ ) ;
32+ } ) ;
33+ it ( 'should find the substring when its the whole string [property]' , ( ) => {
34+ fc . assert (
35+ fc . property (
36+ fc . fullUnicodeString ( ) ,
37+ fc . fullUnicodeString ( ) ,
38+ fc . fullUnicodeString ( ) ,
39+ ( s , prefix , suffix ) => longestCommonSubstring ( prefix + s + suffix , s ) === s
40+ )
41+ ) ;
42+ } ) ;
43+ it ( 'should find a substring greater or equal to the one we know [property]' , ( ) => {
44+ fc . assert (
45+ fc . property (
46+ fc . fullUnicodeString ( ) ,
47+ fc . fullUnicodeString ( ) ,
48+ fc . fullUnicodeString ( ) ,
49+ fc . fullUnicodeString ( ) ,
50+ fc . fullUnicodeString ( ) ,
51+ ( s , prefix1 , suffix1 , prefix2 , suffix2 ) => {
52+ const s1 = prefix1 + s + suffix1 ;
53+ const s2 = prefix2 + s + suffix2 ;
54+ const match = longestCommonSubstring ( s1 , s2 ) ;
55+ return [ ...s ] . length <= [ ...match ] . length ;
56+ }
57+ )
58+ ) ;
59+ } ) ;
60+ } ) ;
61+ it ( 'should find the longest common when two distinct areas match [property]' , ( ) => {
62+ fc . assert (
63+ fc . property (
64+ fc . fullUnicodeString ( 1 , 10 ) ,
65+ fc . fullUnicodeString ( 1 , 10 ) ,
66+ fc . fullUnicodeString ( ) ,
67+ fc . fullUnicodeString ( ) ,
68+ fc . fullUnicodeString ( ) ,
69+ fc . fullUnicodeString ( ) ,
70+ fc . fullUnicodeString ( ) ,
71+ fc . fullUnicodeString ( ) ,
72+ fc . fullUnicodeString ( ) ,
73+ ( c1 , c2 , prefix1 , mid1 , suffix1 , prefix2 , mid2 , suffix2 ) => {
74+ const s1 = prefix1 + c1 + mid1 + c2 + suffix1 ;
75+ const s2 = prefix2 + c1 + mid2 + c2 + suffix2 ;
76+ const match = longestCommonSubstring ( s1 , s2 ) ;
77+ return Math . max ( [ ...c1 ] . length , [ ...c2 ] . length ) <= [ ...match ] . length ;
78+ }
79+ )
80+ ) ;
1181} ) ;
0 commit comments