1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
4+
5+ namespace SuffixTreeSharp . Test
6+ {
7+ [ TestClass ]
8+ public class SuffixTreeTest
9+ {
10+ public static void AssertEmpty < T > ( ICollection < T > collection )
11+ {
12+ Assert . IsTrue ( collection . Count == 0 , "Expected empty collection." ) ;
13+ }
14+
15+ [ TestMethod ]
16+ public void TestBasicTreeGeneration ( )
17+ {
18+ var input = new GeneralizedSuffixTree ( ) ;
19+
20+ var word = "cacao" ;
21+ input . Put ( word , 0 ) ;
22+
23+ /* Test that every substring is contained within the tree */
24+ foreach ( var s in word . GetSubstrings ( ) )
25+ {
26+ Assert . IsTrue ( input . Search ( s ) . Contains ( 0 ) ) ;
27+ }
28+
29+ AssertEmpty ( input . Search ( "caco" ) ) ;
30+ AssertEmpty ( input . Search ( "cacaoo" ) ) ;
31+ AssertEmpty ( input . Search ( "ccacao" ) ) ;
32+
33+ input = new GeneralizedSuffixTree ( ) ;
34+ word = "bookkeeper" ;
35+ input . Put ( word , 0 ) ;
36+ foreach ( var s in word . GetSubstrings ( ) )
37+ {
38+ Assert . IsTrue ( input . Search ( s ) . Contains ( 0 ) ) ;
39+ }
40+
41+ AssertEmpty ( input . Search ( "books" ) ) ;
42+ AssertEmpty ( input . Search ( "boke" ) ) ;
43+ AssertEmpty ( input . Search ( "ookepr" ) ) ;
44+ }
45+
46+ [ TestMethod ]
47+ public void TestWeirdword ( )
48+ {
49+ var input = new GeneralizedSuffixTree ( ) ;
50+
51+ var word = "cacacato" ;
52+ input . Put ( word , 0 ) ;
53+
54+ /* Test that every substring is contained within the tree */
55+ foreach ( var s in word . GetSubstrings ( ) )
56+ {
57+ Assert . IsTrue ( input . Search ( s ) . Contains ( 0 ) ) ;
58+ }
59+ }
60+
61+ [ TestMethod ]
62+ public void TestDouble ( )
63+ {
64+ // Test whether the tree can handle repetitions
65+ var input = new GeneralizedSuffixTree ( ) ;
66+ var word = "cacao" ;
67+ input . Put ( word , 0 ) ;
68+ input . Put ( word , 1 ) ;
69+
70+ foreach ( var s in word . GetSubstrings ( ) )
71+ {
72+ Assert . IsTrue ( input . Search ( s ) . Contains ( 0 ) ) ;
73+ Assert . IsTrue ( input . Search ( s ) . Contains ( 1 ) ) ;
74+ }
75+ }
76+
77+ [ TestMethod ]
78+ public void TestBananaAddition ( )
79+ {
80+ var input = new GeneralizedSuffixTree ( ) ;
81+ var words = new [ ] { "banana" , "bano" , "ba" } ;
82+ for ( var i = 0 ; i < words . Length ; ++ i )
83+ {
84+ input . Put ( words [ i ] , i ) ;
85+
86+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
87+ {
88+ var result = input . Search ( s ) ;
89+ Assert . IsNotNull ( result , "result null for string " + s + " after adding " + words [ i ] ) ;
90+ Assert . IsTrue ( result . Contains ( i ) , "substring " + s + " not found after adding " + words [ i ] ) ;
91+ }
92+ }
93+
94+ // verify post-addition
95+ for ( var i = 0 ; i < words . Length ; ++ i )
96+ {
97+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
98+ {
99+ Assert . IsTrue ( input . Search ( s ) . Contains ( i ) ) ;
100+ }
101+ }
102+
103+ // add again, to see if it's stable
104+ for ( var i = 0 ; i < words . Length ; ++ i )
105+ {
106+ input . Put ( words [ i ] , i + words . Length ) ;
107+
108+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
109+ {
110+ Assert . IsTrue ( input . Search ( s ) . Contains ( i + words . Length ) ) ;
111+ }
112+ }
113+ }
114+
115+ [ TestMethod ]
116+ public void TestAddition ( )
117+ {
118+ var input = new GeneralizedSuffixTree ( ) ;
119+ var words = new [ ] { "cacaor" , "caricato" , "cacato" , "cacata" , "caricata" , "cacao" , "banana" } ;
120+ for ( var i = 0 ; i < words . Length ; ++ i )
121+ {
122+ input . Put ( words [ i ] , i ) ;
123+
124+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
125+ {
126+ var result = input . Search ( s ) ;
127+ Assert . IsNotNull ( result , "result null for string " + s + " after adding " + words [ i ] ) ;
128+ Assert . IsTrue ( result . Contains ( i ) , "substring " + s + " not found after adding " + words [ i ] ) ;
129+ }
130+ }
131+
132+ // verify post-addition
133+ for ( var i = 0 ; i < words . Length ; ++ i )
134+ {
135+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
136+ {
137+ var result = input . Search ( s ) ;
138+ Assert . IsNotNull ( result , "result null for string " + s + " after adding " + words [ i ] ) ;
139+ Assert . IsTrue ( result . Contains ( i ) , "substring " + s + " not found after adding " + words [ i ] ) ;
140+ }
141+ }
142+
143+ // add again, to see if it's stable
144+ for ( var i = 0 ; i < words . Length ; ++ i )
145+ {
146+ input . Put ( words [ i ] , i + words . Length ) ;
147+
148+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
149+ {
150+ Assert . IsTrue ( input . Search ( s ) . Contains ( i + words . Length ) ) ;
151+ }
152+ }
153+
154+ // input.computeCount();
155+ // TestResultsCount(input.getRoot());
156+
157+ AssertEmpty ( input . Search ( "aoca" ) ) ;
158+ }
159+
160+ [ TestMethod ]
161+ public void TestSampleAddition ( )
162+ {
163+ var input = new GeneralizedSuffixTree ( ) ;
164+ var words = new [ ]
165+ {
166+ "libertypike" ,
167+ "franklintn" ,
168+ "carothersjohnhenryhouse" ,
169+ "carothersezealhouse" ,
170+ "acrossthetauntonriverfromdightonindightonrockstatepark" ,
171+ "dightonma" ,
172+ "dightonrock" ,
173+ "6mineoflowgaponlowgapfork" ,
174+ "lowgapky" ,
175+ "lemasterjohnjandellenhouse" ,
176+ "lemasterhouse" ,
177+ "70wilburblvd" ,
178+ "poughkeepsieny" ,
179+ "freerhouse" ,
180+ "701laurelst" ,
181+ "conwaysc" ,
182+ "hollidayjwjrhouse" ,
183+ "mainandappletonsts" ,
184+ "menomoneefallswi" ,
185+ "mainstreethistoricdistrict" ,
186+ "addressrestricted" ,
187+ "brownsmillsnj" ,
188+ "hanoverfurnace" ,
189+ "hanoverbogironfurnace" ,
190+ "sofsavannahatfergusonaveandbethesdard" ,
191+ "savannahga" ,
192+ "bethesdahomeforboys" ,
193+ "bethesda"
194+ } ;
195+ for ( var i = 0 ; i < words . Length ; ++ i )
196+ {
197+ input . Put ( words [ i ] , i ) ;
198+
199+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
200+ {
201+ var result = input . Search ( s ) ;
202+ Assert . IsNotNull ( result , "result null for string " + s + " after adding " + words [ i ] ) ;
203+ Assert . IsTrue ( result . Contains ( i ) , "substring " + s + " not found after adding " + words [ i ] ) ;
204+ }
205+ }
206+
207+ // verify post-addition
208+ for ( var i = 0 ; i < words . Length ; ++ i )
209+ {
210+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
211+ {
212+ Assert . IsTrue ( input . Search ( s ) . Contains ( i ) ) ;
213+ }
214+ }
215+
216+ // add again, to see if it's stable
217+ for ( var i = 0 ; i < words . Length ; ++ i )
218+ {
219+ input . Put ( words [ i ] , i + words . Length ) ;
220+
221+ foreach ( var s in words [ i ] . GetSubstrings ( ) )
222+ {
223+ Assert . IsTrue ( input . Search ( s ) . Contains ( i + words . Length ) ) ;
224+ }
225+ }
226+
227+ // input.computeCount();
228+ // TestResultsCount(input.getRoot());
229+
230+ AssertEmpty ( input . Search ( "aoca" ) ) ;
231+ }
232+
233+ // private void TestResultsCount(Node n) {
234+ // for (Edge e : n.getEdges().values()) {
235+ // assertEquals(n.getData(-1).size(), n.getResultCount());
236+ // TestResultsCount(e.getDest());
237+ // }
238+ // }
239+
240+ /* Testing a Test method :) */
241+ [ TestMethod ]
242+ public void TestGetSubstrings ( )
243+ {
244+ var exp = new [ ] { "w" , "r" , "d" , "wr" , "rd" , "wrd" } . ToHashSet ( ) ;
245+ var ret = "wrd" . GetSubstrings ( ) ;
246+ Assert . IsTrue ( ret . SetEquals ( exp ) ) ;
247+ }
248+ }
249+ }
0 commit comments