PageRenderTime 100ms CodeModel.GetById 28ms app.highlight 65ms RepoModel.GetById 2ms app.codeStats 0ms

/Source Code/UnitTests/Searching and Indexing/SearcherTest.vb

#
Visual Basic | 597 lines | 379 code | 118 blank | 100 comment | 2 complexity | 82005c39491819fb7a5d7de9db04b22c MD5 | raw file
  1Imports System.Collections.Generic
  2Imports Microsoft.VisualStudio.TestTools.UnitTesting
  3Imports PowerSong.FastSearching
  4
  5''' <summary>
  6''' This is a test class for SearcherTest and is intended
  7''' to contain all SearcherTest Unit Tests
  8''' </summary>
  9<TestClass()> _
 10Public Class SearcherTest
 11    Inherits BaseSearchTester
 12
 13    <ClassCleanup()> _
 14    Public Shared Sub CleanUp()
 15        RemoveTemporaryFiles()
 16    End Sub
 17
 18    ''' <summary>
 19    ''' Verifies that searching for nothing in an empty index returns no documents.
 20    ''' </summary>
 21    <TestMethod()> _
 22    Public Sub SearchEmptyIndexTest1()
 23
 24        Dim Index As New Indexer
 25        Dim Searcher As New Searcher(Index)
 26        Dim Results As List(Of String) = Searcher.Search(String.Empty)
 27        Assert.AreEqual(0, Results.Count, "Some results were returned when they were not expected.")
 28
 29    End Sub
 30
 31    ''' <summary>
 32    ''' Verifies that searching for something in an empty index returns no documents.
 33    ''' </summary>
 34    <TestMethod()> _
 35    Public Sub SearchEmptyIndexTest2()
 36
 37        Dim Index As New Indexer
 38        Dim Searcher As New Searcher(Index)
 39
 40        Dim Results As List(Of String) = Searcher.Search("First")
 41        Assert.AreEqual(0, Results.Count, "Some results were returned when they were not expected.")
 42
 43        Results = Searcher.Search("TWO WORDS")
 44        Assert.AreEqual(0, Results.Count, "Some results were returned when they were not expected.")
 45
 46        Results = Searcher.Search("!!!")
 47        Assert.AreEqual(0, Results.Count, "Some results were returned when they were not expected.")
 48
 49    End Sub
 50
 51    ''' <summary>
 52    ''' Verifies that searching for nothing in a populated index returns no documents.
 53    ''' </summary>
 54    <TestMethod()> _
 55    Public Sub SearchPopulatedIndexTest()
 56
 57        Dim Index As Indexer = CreateBasicIndex()
 58        Dim Searcher As New Searcher(Index)
 59        Dim Results As List(Of String) = Searcher.Search(String.Empty)
 60        Assert.AreEqual(0, Results.Count, "Some results were returned when they were not expected.")
 61
 62    End Sub
 63
 64    ''' <summary>
 65    ''' Verifies that searching for a word existing in the sole document in an index 
 66    ''' returns that document.
 67    ''' </summary>
 68    <TestMethod()> _
 69    Public Sub SearchForSoleFileTest()
 70
 71        Dim Index As New Indexer
 72        Index.AddFileContentsToIndex("SOLE_FILE", "This is the sole file in the index.")
 73        Dim Searcher As New Searcher(Index)
 74
 75        For Each Terms As String In New String() {"sole", "SOLE", "index.", "the sole file", "this is the sole file in the index"}
 76
 77            Dim Results As List(Of String) = Searcher.Search(Terms)
 78            Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned when searching for '" + Terms + "'.")
 79            Assert.AreEqual("SOLE_FILE", Results(0), "The file was not returned.")
 80
 81        Next
 82        
 83    End Sub
 84
 85    ''' <summary>
 86    ''' Ensures that searching a moderately sized index works, for one word existing in 
 87    ''' a single document.
 88    ''' </summary>
 89    <TestMethod()> _
 90    Public Sub SearchForAFileTest()
 91
 92        Dim Index As Indexer = CreateModerateIndex()
 93        Dim Searcher As New Searcher(Index)
 94
 95        For Each Terms As String In New String() {"FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH", "SEVENTH", "ONE"}
 96            Dim Results As List(Of String) = Searcher.Search(Terms)
 97            Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned when searching for '" + Terms + "'.")
 98        Next
 99
100        Assert.AreEqual("A", Searcher.Search("FIRST")(0), "The file was not returned.")
101        Assert.AreEqual("B", Searcher.Search("SECOND")(0), "The file was not returned.")
102        Assert.AreEqual("C", Searcher.Search("THIRD")(0), "The file was not returned.")
103        Assert.AreEqual("D", Searcher.Search("FOURTH")(0), "The file was not returned.")
104        Assert.AreEqual("E", Searcher.Search("FIFTH")(0), "The file was not returned.")
105        Assert.AreEqual("F", Searcher.Search("SIXTH")(0), "The file was not returned.")
106        Assert.AreEqual("G", Searcher.Search("SEVENTH")(0), "The file was not returned.")
107        Assert.AreEqual("D", Searcher.Search("ONE")(0), "The file was not returned.")
108
109    End Sub
110
111    ''' <summary>
112    ''' Ensures that searching a moderately sized index works, for one word existing in 
113    ''' multiple documents.
114    ''' </summary>
115    <TestMethod()> _
116    Public Sub SearchForMultipleFilesTest()
117
118        Dim Index As Indexer = CreateModerateIndex()
119        Dim Searcher As New Searcher(Index)
120
121        Dim Results As List(Of String) = Searcher.Search("ONE")
122        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
123        Assert.AreEqual("D", Results(0), "An unexpected file was returned.")
124
125        Results = Searcher.Search("two")
126        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
127        Assert.IsTrue(Results.Contains("A"), "An unexpected file was returned.")
128        Assert.IsTrue(Results.Contains("D"), Results(0), "An unexpected file was returned.")
129
130        Results = Searcher.Search(" three")
131        Assert.AreEqual(3, Results.Count, "Incorrect number of documents returned.")
132        Assert.IsTrue(Results.Contains("A"), "An unexpected file was returned.")
133        Assert.IsTrue(Results.Contains("D"), "An unexpected file was returned.")
134        Assert.IsTrue(Results.Contains("G"), "An unexpected file was returned.")
135
136        Results = Searcher.Search(" four ")
137        Assert.AreEqual(4, Results.Count, "Incorrect number of documents returned.")
138        Assert.IsTrue(Results.Contains("C"), "An unexpected file was returned.")
139        Assert.IsTrue(Results.Contains("D"), "An unexpected file was returned.")
140        Assert.IsTrue(Results.Contains("E"), "An unexpected file was returned.")
141        Assert.IsTrue(Results.Contains("F"), "An unexpected file was returned.")
142
143        Results = Searcher.Search("five")
144        Assert.AreEqual(5, Results.Count, "Incorrect number of documents returned.")
145        Assert.IsTrue(Results.Contains("C"), "An unexpected file was returned.")
146        Assert.IsTrue(Results.Contains("D"), "An unexpected file was returned.")
147        Assert.IsTrue(Results.Contains("E"), "An unexpected file was returned.")
148        Assert.IsTrue(Results.Contains("F"), "An unexpected file was returned.")
149        Assert.IsTrue(Results.Contains("G"), "An unexpected file was returned.")
150
151        Results = Searcher.Search("SIX")
152        Assert.AreEqual(6, Results.Count, "Incorrect number of documents returned.")
153        Assert.IsTrue(Results.Contains("B"), "An unexpected file was returned.")
154        Assert.IsTrue(Results.Contains("C"), "An unexpected file was returned.")
155        Assert.IsTrue(Results.Contains("D"), "An unexpected file was returned.")
156        Assert.IsTrue(Results.Contains("E"), "An unexpected file was returned.")
157        Assert.IsTrue(Results.Contains("F"), "An unexpected file was returned.")
158        Assert.IsTrue(Results.Contains("G"), "An unexpected file was returned.")
159
160        Results = Searcher.Search("all")
161        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
162        Assert.IsTrue(Results.Contains("A"), "An unexpected file was returned.")
163        Assert.IsTrue(Results.Contains("B"), "An unexpected file was returned.")
164        Assert.IsTrue(Results.Contains("C"), "An unexpected file was returned.")
165        Assert.IsTrue(Results.Contains("D"), "An unexpected file was returned.")
166        Assert.IsTrue(Results.Contains("E"), "An unexpected file was returned.")
167        Assert.IsTrue(Results.Contains("F"), "An unexpected file was returned.")
168        Assert.IsTrue(Results.Contains("G"), "An unexpected file was returned.")
169
170    End Sub
171
172    ''' <summary>
173    ''' Ensures that searching a moderately sized index works, for multiple words existing 
174    ''' in a single document.
175    ''' </summary>
176    <TestMethod()> _
177    Public Sub SearchMultipleWordsSingleDocumentTest()
178
179        Dim Index As Indexer = CreateModerateIndex()
180        Dim Searcher As New Searcher(Index)
181
182        Dim Results As List(Of String) = Searcher.Search("ONE")
183        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
184        Assert.AreEqual("D", Results(0), "An unexpected file was returned.")
185
186        Results = Searcher.Search("ONE ONE ONE ONE")
187        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
188        Assert.AreEqual("D", Results(0), "An unexpected file was returned.")
189
190        Results = Searcher.Search("ONE fourth")
191        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
192        Assert.AreEqual("D", Results(0), "An unexpected file was returned.")
193
194    End Sub
195
196    ''' <summary>
197    ''' Ensures that searching a moderately sized index works, for multiple words existing 
198    ''' in multiple documents. (This involves various scenarios and is the most complex test)
199    ''' </summary>
200    <TestMethod()> _
201    Public Sub SearchMultipleWordsMultipleDocumentsTest()
202
203        Dim Index As Indexer = CreateModerateIndex()
204        Dim Searcher As New Searcher(Index)
205
206        Dim Results As List(Of String) = Searcher.Search("ONE two")
207        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
208        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
209        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
210
211        Results = Searcher.Search("three two")
212        Assert.AreEqual(3, Results.Count, "Incorrect number of documents returned.")
213        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
214        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
215        Assert.IsTrue(Results.Contains("G"), "An expected file was not returned.")
216
217        Results = Searcher.Search("first second")
218        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
219        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
220        Assert.IsTrue(Results.Contains("B"), "An expected file was not returned.")
221
222        Results = Searcher.Search("third seventh")
223        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
224        Assert.IsTrue(Results.Contains("C"), "An expected file was not returned.")
225        Assert.IsTrue(Results.Contains("G"), "An expected file was not returned.")
226
227        Results = Searcher.Search("second two")
228        Assert.AreEqual(3, Results.Count, "Incorrect number of documents returned.")
229        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
230        Assert.IsTrue(Results.Contains("B"), "An expected file was not returned.")
231        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
232
233        Results = Searcher.Search("all one two three")
234        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
235        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
236        Assert.IsTrue(Results.Contains("B"), "An expected file was not returned.")
237        Assert.IsTrue(Results.Contains("C"), "An expected file was not returned.")
238        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
239        Assert.IsTrue(Results.Contains("E"), "An expected file was not returned.")
240        Assert.IsTrue(Results.Contains("F"), "An expected file was not returned.")
241        Assert.IsTrue(Results.Contains("G"), "An expected file was not returned.")
242
243        Results = Searcher.Search("this is the first file")
244        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
245        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
246        Assert.IsTrue(Results.Contains("B"), "An expected file was not returned.")
247        Assert.IsTrue(Results.Contains("C"), "An expected file was not returned.")
248        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
249        Assert.IsTrue(Results.Contains("E"), "An expected file was not returned.")
250        Assert.IsTrue(Results.Contains("F"), "An expected file was not returned.")
251        Assert.IsTrue(Results.Contains("G"), "An expected file was not returned.")
252
253    End Sub
254
255    ''' <summary>
256    ''' Ensures that searching a moderately sized index works, for multiple words
257    ''' existing in all documents.
258    ''' </summary>
259    <TestMethod()> _
260    Public Sub SearchMultipleWordsAllDocumentsTest()
261
262        Dim Index As Indexer = CreateModerateIndex()
263        Dim Searcher As New Searcher(Index)
264
265        Dim Results As List(Of String) = Searcher.Search("this all two three second")
266        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
267        Assert.IsTrue(Results.Contains("A"), "An expected file was not returned.")
268        Assert.IsTrue(Results.Contains("B"), "An expected file was not returned.")
269        Assert.IsTrue(Results.Contains("C"), "An expected file was not returned.")
270        Assert.IsTrue(Results.Contains("D"), "An expected file was not returned.")
271        Assert.IsTrue(Results.Contains("E"), "An expected file was not returned.")
272        Assert.IsTrue(Results.Contains("F"), "An expected file was not returned.")
273        Assert.IsTrue(Results.Contains("G"), "An expected file was not returned.")
274
275    End Sub
276
277    ''' <summary>
278    ''' Ensures that searching a moderately sized index works, for all words not 
279    ''' existing in any documents.
280    ''' </summary>
281    <TestMethod()> _
282    Public Sub SearchWordsNotInAnyDocumentsTest()
283
284        Dim Index As Indexer = CreateModerateIndex()
285        Dim Searcher As New Searcher(Index)
286        Dim Results As List(Of String) = Searcher.Search("hooligans manchester welsh english")
287        Assert.AreEqual(0, Results.Count, "Incorrect number of documents returned.")
288
289    End Sub
290
291    ''' <summary>
292    ''' Check that the correct order of documents is returned when searching a moderately sized index.
293    ''' </summary>
294    <TestMethod()> _
295    Public Sub ConfirmDocumentOrderTest()
296
297        Dim Index As Indexer = CreateModerateIndex()
298        Dim Searcher As New Searcher(Index)
299
300        Dim Results As List(Of String) = Searcher.Search("ONE")
301        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
302        Assert.AreEqual("D", Results(0), "Incorrect document or document order.")
303
304        Results = Searcher.Search("TWO")
305        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
306        Assert.AreEqual("D", Results(0), "Incorrect document or document order.")
307        Assert.AreEqual("A", Results(1), "Incorrect document or document order.")
308
309        Results = Searcher.Search("TWO ONE")
310        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
311        Assert.AreEqual("D", Results(0), "Incorrect document or document order.")
312        Assert.AreEqual("A", Results(1), "Incorrect document or document order.")
313
314        Results = Searcher.Search("TWO forceTop")
315        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
316        Assert.AreEqual("A", Results(0), "Incorrect document or document order.")
317        Assert.AreEqual("D", Results(1), "Incorrect document or document order.")
318
319        Results = Searcher.Search("ALL SIX FIVE FOUR THREE TWO ONE")
320        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
321        Assert.AreEqual("D", Results(0), "Incorrect document or document order.")
322        Assert.AreEqual("A", Results(1), "Incorrect document or document order.")
323        Assert.AreEqual("G", Results(2), "Incorrect document or document order.")
324        Assert.AreEqual("F", Results(3), "Incorrect document or document order.")
325        Assert.AreEqual("E", Results(4), "Incorrect document or document order.")
326        Assert.AreEqual("C", Results(5), "Incorrect document or document order.")
327        Assert.AreEqual("B", Results(6), "Incorrect document or document order.")
328
329    End Sub
330
331    ''' <summary>
332    ''' Confirm searching works as expected after clearing the index.
333    ''' </summary>
334    <TestMethod()> _
335    Public Sub SearchClearedIndexTest()
336
337        Dim Index As Indexer = CreateModerateIndex()
338        Dim Searcher As New Searcher(Index)
339
340        Dim Results As List(Of String) = Searcher.Search("ALL")
341        Assert.AreEqual(7, Results.Count, "Incorrect number of documents returned.")
342
343        Index.Clear()
344
345        Assert.AreEqual(0, Searcher.Search("ALL").Count, "Documents were returned.")
346
347    End Sub
348
349    ''' <summary>
350    ''' Confirm searching works as expected after adding documents.
351    ''' </summary>
352    <TestMethod()> _
353    Public Sub SearchAfterAddingDocumentsTest()
354
355        ' Create a searcher
356        Dim Index As Indexer = CreateModerateIndex()
357        Dim Searcher As New Searcher(Index)
358
359        ' Perform a simple search
360        Dim Results As List(Of String) = Searcher.Search("TWO EIGHT")
361        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
362        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
363        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
364
365        ' Add a document with unique word "EIGHT"
366        Index.AddFileContentsToIndex("NEW1", "additional file EIGHT")
367
368        ' Confirm the new document can be returned by itself
369        Results = Searcher.Search("EIGHT")
370        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
371        Assert.IsTrue(Results.Contains("NEW1"), "Expected document was not returned.")
372
373        ' Confirm the new document can be returned along with other documents
374        Results = Searcher.Search("TWO EIGHT")
375        Assert.AreEqual(3, Results.Count, "Incorrect number of documents returned.")
376        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
377        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
378        Assert.IsTrue(Results.Contains("NEW1"), "Expected document was not returned.")
379
380        ' Confirm all documents can be returned, including the new one
381        Results = Searcher.Search("ALL EIGHT")
382        Assert.AreEqual(8, Results.Count, "Incorrect number of documents returned.")
383        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
384        Assert.IsTrue(Results.Contains("B"), "Expected document was not returned.")
385        Assert.IsTrue(Results.Contains("C"), "Expected document was not returned.")
386        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
387        Assert.IsTrue(Results.Contains("E"), "Expected document was not returned.")
388        Assert.IsTrue(Results.Contains("F"), "Expected document was not returned.")
389        Assert.IsTrue(Results.Contains("G"), "Expected document was not returned.")
390        Assert.IsTrue(Results.Contains("NEW1"), "Expected document was not returned.")
391
392        ' Confirm only old documents can be returned
393        Results = Searcher.Search("TWO")
394        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
395        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
396        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
397
398        ' Add another file, this time with a word that already exists in other documents
399        Index.AddFileContentsToIndex("NEW2", "YET MORE: TWO")
400
401        ' Confirm that searching for a word in old and new documents returns the correct results
402        Results = Searcher.Search("TWO")
403        Assert.AreEqual(3, Results.Count, "Incorrect number of documents returned.")
404        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
405        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
406        Assert.IsTrue(Results.Contains("NEW2"), "Expected document was not returned.")
407
408        ' Confirm that all documents can be returned
409        Results = Searcher.Search("ALL EIGHT TWO")
410        Assert.AreEqual(9, Results.Count, "Incorrect number of documents returned.")
411        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
412        Assert.IsTrue(Results.Contains("B"), "Expected document was not returned.")
413        Assert.IsTrue(Results.Contains("C"), "Expected document was not returned.")
414        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
415        Assert.IsTrue(Results.Contains("E"), "Expected document was not returned.")
416        Assert.IsTrue(Results.Contains("F"), "Expected document was not returned.")
417        Assert.IsTrue(Results.Contains("G"), "Expected document was not returned.")
418        Assert.IsTrue(Results.Contains("NEW1"), "Expected document was not returned.")
419        Assert.IsTrue(Results.Contains("NEW2"), "Expected document was not returned.")
420        
421    End Sub
422
423    ''' <summary>
424    ''' A simple test for proving no documents get returned when an index is emptied by
425    ''' removing existing document(s).
426    ''' </summary>
427    <TestMethod()> _
428    Public Sub SearchEmptiedIndexTest()
429
430        ' Create an index with one document
431        Dim Index As New Indexer
432        Index.AddFileContentsToIndex("A", "File contents go here.")
433
434        ' Search for the document
435        Dim Searcher As New Searcher(Index)
436        Dim Results As List(Of String) = Searcher.Search("contents")
437        Assert.AreEqual(1, Results.Count, "No documents returned when one was expected.")
438        Assert.AreEqual("A", Results(0), "Incorrect document returned.")
439
440        ' Remove the document
441        Index.RemoveDocumentFromIndex("A")
442
443        ' The same search should no longer work
444        Results = Searcher.Search("contents")
445        Assert.AreEqual(0, Results.Count, "One or more documents were returned.")
446
447    End Sub
448
449    ''' <summary>
450    ''' The simpler version of SearchAterRemovingDocumentsTest2.
451    ''' </summary>
452    <TestMethod()> _
453    Public Sub SearchAterRemovingDocumentsTest1()
454
455        ' Create an index with one document
456        Dim Index As New Indexer
457        Index.AddFileContentsToIndex("A", "File contents go here.")
458        Index.AddFileContentsToIndex("B", "File contents go here.")
459
460        ' Search for the document
461        Dim Searcher As New Searcher(Index)
462        Dim Results As List(Of String) = Searcher.Search("contents")
463        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
464        Assert.IsTrue(Results.Contains("A"), "Document not returned as expected.")
465        Assert.IsTrue(Results.Contains("B"), "Document not returned as expected.")
466
467        ' Remove a document, and prove only one gets returned now
468        Index.RemoveDocumentFromIndex("B")
469        Results = Searcher.Search("contents")
470        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
471        Assert.IsTrue(Results.Contains("A"), "Document not returned as expected.")
472
473        ' Remove a document, and prove only one gets returned now
474        Index.RemoveDocumentFromIndex("A")
475        Results = Searcher.Search("contents")
476        Assert.AreEqual(0, Results.Count, "One or more documents were returned.")
477
478    End Sub
479
480    ''' <summary>
481    ''' Confirm searching works as expected after removing documents.
482    ''' </summary>
483    <TestMethod()> _
484    Public Sub SearchAterRemovingDocumentsTest2()
485
486        ' Create a searcher
487        Dim Index As Indexer = CreateModerateIndex()
488        Dim Searcher As New Searcher(Index)
489
490        ' Perform a simple search
491        Dim Results As List(Of String) = Searcher.Search("TWO")
492        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
493        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
494        Assert.IsTrue(Results.Contains("D"), "Expected document was not returned.")
495
496        ' Remove a document
497        Index.RemoveDocumentFromIndex("D")
498
499        ' Confirm the same search returns only one document
500        Results = Searcher.Search("TWO")
501        Assert.AreEqual(1, Results.Count, "Incorrect number of documents returned.")
502        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
503
504        ' Confirm searching for all documents returns everything except the removed document
505        Results = Searcher.Search("ALL")
506        Assert.AreEqual(6, Results.Count, "Incorrect number of documents returned.")
507        Assert.IsTrue(Results.Contains("A"), "Expected document was not returned.")
508        Assert.IsTrue(Results.Contains("B"), "Expected document was not returned.")
509        Assert.IsTrue(Results.Contains("C"), "Expected document was not returned.")
510        Assert.IsTrue(Results.Contains("E"), "Expected document was not returned.")
511        Assert.IsTrue(Results.Contains("F"), "Expected document was not returned.")
512        Assert.IsTrue(Results.Contains("G"), "Expected document was not returned.")
513
514        ' Remove some more documents
515        Index.RemoveDocumentFromIndex("G")
516        Index.RemoveDocumentFromIndex("A")
517
518        ' Confirm previously valid terms no longer return documents
519        Results = Searcher.Search("TWO")
520        Assert.AreEqual(0, Results.Count, "Incorrect number of documents returned.")
521
522        ' Get all remaining documents
523        Results = Searcher.Search("ALL documents including ONE TWO THREE FOUR FIVE SIX")
524        Assert.AreEqual(4, Results.Count, "Incorrect number of documents returned.")
525        Assert.IsTrue(Results.Contains("B"), "Expected document was not returned.")
526        Assert.IsTrue(Results.Contains("C"), "Expected document was not returned.")
527        Assert.IsTrue(Results.Contains("E"), "Expected document was not returned.")
528        Assert.IsTrue(Results.Contains("F"), "Expected document was not returned.")
529
530    End Sub
531
532    ''' <summary>
533    ''' Confirm searching works as expected after both adding and removing documents.
534    ''' </summary>
535    <TestMethod()> _
536    Public Sub SearchAterAddingAndRemovingDocumentsTest()
537
538        ' Create an index
539        Dim Index As Indexer = CreateModerateIndex()
540
541        ' Remove two documents
542        Index.RemoveDocumentFromIndex("B")
543        Index.RemoveDocumentFromIndex("D")
544
545        ' Add a document
546        Index.AddFileContentsToIndex("X", "XXX YYY ZZZ ALL")
547
548        ' Save and load the index again
549        Dim FileName As String = GetTemporaryFileName()
550        Index.Save(FileName)
551        Index = Indexer.LoadIndex(FileName)
552
553        ' Perform some quick checks
554        Assert.AreEqual(6, Index.DocumentCount, "Incorrect number of documents indexed.")
555        Assert.AreEqual("A", Index.DocumentName(0), "Document not indexed properly.")
556        Assert.AreEqual("C", Index.DocumentName(2), "Document not indexed properly.")
557        Assert.AreEqual("E", Index.DocumentName(4), "Document not indexed properly.")
558        Assert.AreEqual("F", Index.DocumentName(5), "Document not indexed properly.")
559        Assert.AreEqual("G", Index.DocumentName(6), "Document not indexed properly.")
560        Assert.AreEqual("X", Index.DocumentName(7), "Document not indexed properly.")
561
562        ' Perform some searches
563        Dim Searcher As New Searcher(Index)
564        Assert.AreEqual("X", Searcher.Search("XXX")(0))
565        Assert.AreEqual("X", Searcher.Search("YYY")(0))
566        Assert.AreEqual("X", Searcher.Search("ZZZ")(0))
567        Assert.AreEqual("X", Searcher.Search("XXX YYY ZZZ")(0))
568        Assert.AreEqual("X", Searcher.Search("ZZZ")(0))
569
570        ' Get all documents and check that they are all returned
571        Dim Results As List(Of String) = Searcher.Search("ALL")
572        Assert.AreEqual(6, Results.Count, "Incorrect number of documents returned.")
573        Assert.IsTrue(Results.Contains("A"), "Result does not contain expected document.")
574        Assert.IsTrue(Results.Contains("C"), "Result does not contain expected document.")
575        Assert.IsTrue(Results.Contains("E"), "Result does not contain expected document.")
576        Assert.IsTrue(Results.Contains("F"), "Result does not contain expected document.")
577        Assert.IsTrue(Results.Contains("G"), "Result does not contain expected document.")
578        Assert.IsTrue(Results.Contains("X"), "Result does not contain expected document.")
579
580        ' Also, confirm that it is possible to not get the document
581        Results = Searcher.Search("THREE")
582        Assert.AreEqual(2, Results.Count, "Incorrect number of documents returned.")
583        Assert.IsTrue(Results.Contains("A"), "Result does not contain expected document.")
584        Assert.IsTrue(Results.Contains("G"), "Result does not contain expected document.")
585
586    End Sub
587
588    ''' <summary>
589    ''' A test for Searcher Constructor.
590    ''' </summary>
591    <TestMethod()> _
592    Public Sub SearcherConstructorTest()
593        Dim Index As New Indexer
594        Dim Search As Searcher = New Searcher(Index)
595    End Sub
596
597End Class