PageRenderTime 69ms CodeModel.GetById 14ms app.highlight 49ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Visual Basic | 526 lines | 311 code | 112 blank | 103 comment | 22 complexity | 628308ebe866d9cb5b4693f76d5d22f0 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 IndexerTest and is intended
  7''' to contain all IndexerTest Unit Tests
  8'''</summary>
  9<TestClass()> _
 10Public Class IndexerTest
 11    Inherits BaseSearchTester
 12
 13    Private testContextInstance As TestContext
 14
 15    '''<summary>
 16    ''' Gets or sets the test context which provides
 17    ''' information about and functionality for the current test run.
 18    '''</summary>
 19    Public Property TestContext() As TestContext
 20        Get
 21            Return testContextInstance
 22        End Get
 23        Set(ByVal value As TestContext)
 24            testContextInstance = Value
 25        End Set
 26    End Property
 27
 28    <ClassCleanup()> _
 29    Public Shared Sub CleanUp()
 30        RemoveTemporaryFiles()
 31    End Sub
 32
 33    ''' <summary>
 34    ''' Tests the number of documents in the index.
 35    ''' </summary>
 36    <TestMethod()> _
 37    Public Sub DocumentsIndexedTest()
 38
 39        Dim Index As Indexer = New Indexer
 40        Assert.AreEqual(0, Index.DocumentCount, "Expected no documents in the index.")
 41
 42        Index.AddFileContentsToIndex("A", "Contents of first file.")
 43        Index.AddFileContentsToIndex("B", "Contents of second file.")
 44        Index.AddFileContentsToIndex("C", "Contents of third file.")
 45        Assert.AreEqual(3, Index.DocumentCount, "Expected 3 documents in the index.")
 46
 47        Index.AddFileContentsToIndex("D", "Contents of fourth file.")
 48        Assert.AreEqual(4, Index.DocumentCount, "Expected 4 documents in the index.")
 49
 50        Index.RemoveDocumentFromIndex("A")
 51        Index.RemoveDocumentFromIndex("D")
 52        Assert.AreEqual(2, Index.DocumentCount, "Expected 2 documents in the index.")
 53
 54    End Sub
 55
 56    ''' <summary>
 57    ''' Tests document names in the index.
 58    ''' </summary>
 59    <TestMethod()> _
 60    Public Sub DocumentNameTest()
 61
 62        Dim Index As Indexer = CreateBasicIndex()
 63
 64        Assert.AreEqual("A", Index.DocumentName(0), "Document 1 has an invalid name.")
 65        Assert.AreEqual("B", Index.DocumentName(1), "Document 2 has an invalid name.")
 66        Assert.AreEqual("C", Index.DocumentName(2), "Document 3 has an invalid name.")
 67
 68    End Sub
 69
 70    ''' <summary>
 71    ''' Tests document names in the index, after removing a document.
 72    ''' </summary>
 73    <TestMethod()> _
 74    Public Sub DocumentNameAfterRemovalTest()
 75
 76        Dim Index As Indexer = CreateBasicIndex
 77
 78        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents indexed.")
 79        Assert.AreEqual("A", Index.DocumentName(0), "Document 1 has an invalid name.")
 80        Assert.AreEqual("B", Index.DocumentName(1), "Document 2 has an invalid name.")
 81        Assert.AreEqual("C", Index.DocumentName(2), "Document 3 has an invalid name.")
 82
 83        Index.RemoveDocumentFromIndex("B")
 84        Assert.AreEqual(2, Index.DocumentCount, "Incorrect number of documents indexed.")
 85        Assert.AreEqual("A", Index.DocumentName(0), "Document 1 has an invalid name.")
 86        Assert.AreEqual("C", Index.DocumentName(2), "Document 3 has an invalid name.")
 87
 88        Index.RemoveDocumentFromIndex("A")
 89        Assert.AreEqual(1, Index.DocumentCount, "Incorrect number of documents indexed.")
 90        Assert.AreEqual("C", Index.DocumentName(2), "Document 3 has an invalid name.")
 91
 92        Index.RemoveDocumentFromIndex("C")
 93        Assert.AreEqual(0, Index.DocumentCount, "Expected no documents in index.")
 94
 95    End Sub
 96
 97    ''' <summary>
 98    ''' Tests document names accessed with an invalid index.
 99    ''' </summary>
100    <TestMethod(), ExpectedException(GetType(ApplicationException))> _
101    Public Sub DocumentNameInvalidIndexTest()
102
103        Dim Index As Indexer = CreateBasicIndex()
104        Dim Result As Integer = Index.DocumentName(3)
105
106    End Sub
107
108    ''' <summary>
109    ''' Tests document numbers in the index.
110    ''' </summary>
111    <TestMethod()> _
112    Public Sub DocumentNumbersTest()
113
114        Dim Index As Indexer = CreateBasicIndex()
115
116        Assert.AreEqual(0, Index.DocumentNumber("A"), "Document A has an invalid index.")
117        Assert.AreEqual(1, Index.DocumentNumber("B"), "Document B has an invalid index.")
118        Assert.AreEqual(2, Index.DocumentNumber("C"), "Document C has an invalid index.")
119
120    End Sub
121
122    ''' <summary>
123    ''' Tests document numbers in the index, after removing a document.
124    ''' </summary>
125    <TestMethod()> _
126    Public Sub DocumentNumbersAfterRemovalTest()
127
128        Dim Index As Indexer = CreateBasicIndex()
129
130        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents indexed.")
131        Assert.AreEqual(0, Index.DocumentNumber("A"), "Document A has an invalid index.")
132        Assert.AreEqual(1, Index.DocumentNumber("B"), "Document B has an invalid index.")
133        Assert.AreEqual(2, Index.DocumentNumber("C"), "Document C has an invalid index.")
134
135        Index.RemoveDocumentFromIndex("B")
136        Assert.AreEqual(2, Index.DocumentCount, "Incorrect number of documents indexed.")
137        Assert.AreEqual(0, Index.DocumentNumber("A"), "Document A has an invalid index.")
138        Assert.AreEqual(2, Index.DocumentNumber("C"), "Document C has an invalid index.")
139
140        Index.RemoveDocumentFromIndex("A")
141        Assert.AreEqual(1, Index.DocumentCount, "Incorrect number of documents indexed.")
142        Assert.AreEqual(2, Index.DocumentNumber("C"), "Document C has an invalid index.")
143
144        Index.RemoveDocumentFromIndex("C")
145        Assert.AreEqual(0, Index.DocumentCount, "Expected no documents in index.")
146
147    End Sub
148
149    ''' <summary>
150    ''' Tests document numbers accessed with an invalid index.
151    ''' </summary>
152    <TestMethod(), ExpectedException(GetType(ApplicationException))> _
153    Public Sub DocumentNumbersInvalidNameTest()
154
155        Dim Index As Indexer = CreateBasicIndex()
156        Dim Result As Integer = Index.DocumentNumber("Z")
157
158    End Sub
159
160    ''' <summary>
161    ''' A test for ToString
162    ''' </summary>
163    <TestMethod()> _
164    Public Sub ToStringTest()
165
166        Dim Index As Indexer = CreateBasicIndex()
167        Dim ActualString As String = "CONTENTS: 3 {A|1,B|1,C|1}" + Environment.NewLine + _
168                                     "FILE: 3 {A|1,B|1,C|1}" + Environment.NewLine + _
169                                     "FIRST: 1 {A|1}" + Environment.NewLine + _
170                                     "OF: 3 {A|1,B|1,C|1}" + Environment.NewLine + _
171                                     "SECOND: 1 {B|1}" + Environment.NewLine + _
172                                     "THIRD: 1 {C|1}" + Environment.NewLine
173        Assert.AreEqual(ActualString, Index.ToString, "Index cannot be rendered as a string.")
174
175    End Sub
176
177    ''' <summary>
178    ''' Check that term frequencies can be determined.
179    ''' </summary>
180    <TestMethod()> _
181    Public Sub TermFrequencyTest()
182
183        ' Create a basic index
184        Dim Index As New Indexer
185        Index.AddFileContentsToIndex("A", "ALL TWO First First")
186        Index.AddFileContentsToIndex("B", "ALL ONE Second Second")
187        Index.AddFileContentsToIndex("C", "ALL TWO Third Third")
188
189        ' Check first document
190        Assert.AreEqual(1, Index.TermFrequency(0, "ALL"), "Incorrect term frequency.")
191        Assert.AreEqual(1, Index.TermFrequency(0, "TWO"), "Incorrect term frequency.")
192        Assert.AreEqual(0, Index.TermFrequency(0, "ONE"), "Incorrect term frequency.")
193        Assert.AreEqual(2, Index.TermFrequency(0, "First"), "Incorrect term frequency.")
194        Assert.AreEqual(0, Index.TermFrequency(0, "Second"), "Incorrect term frequency.")
195        Assert.AreEqual(0, Index.TermFrequency(0, "Third"), "Incorrect term frequency.")
196
197        ' Check second document
198        Assert.AreEqual(1, Index.TermFrequency(1, "ALL"), "Incorrect term frequency.")
199        Assert.AreEqual(0, Index.TermFrequency(1, "TWO"), "Incorrect term frequency.")
200        Assert.AreEqual(1, Index.TermFrequency(1, "ONE"), "Incorrect term frequency.")
201        Assert.AreEqual(0, Index.TermFrequency(1, "First"), "Incorrect term frequency.")
202        Assert.AreEqual(2, Index.TermFrequency(1, "Second"), "Incorrect term frequency.")
203        Assert.AreEqual(0, Index.TermFrequency(1, "Third"), "Incorrect term frequency.")
204
205        ' Check third document
206        Assert.AreEqual(1, Index.TermFrequency(2, "ALL"), "Incorrect term frequency.")
207        Assert.AreEqual(1, Index.TermFrequency(2, "TWO"), "Incorrect term frequency.")
208        Assert.AreEqual(0, Index.TermFrequency(2, "ONE"), "Incorrect term frequency.")
209        Assert.AreEqual(0, Index.TermFrequency(2, "First"), "Incorrect term frequency.")
210        Assert.AreEqual(0, Index.TermFrequency(2, "Second"), "Incorrect term frequency.")
211        Assert.AreEqual(2, Index.TermFrequency(2, "Third"), "Incorrect term frequency.")
212
213    End Sub
214
215    ''' <summary>
216    ''' Check that term frequencies can be determined, even after removing documents.
217    ''' </summary>
218    <TestMethod()> _
219    Public Sub TermFrequencyAfterDocumentRemovalTest()
220
221        ' Create a basic index
222        Dim Index As New Indexer
223        Index.AddFileContentsToIndex("A", "ALL TWO First First")
224        Index.AddFileContentsToIndex("B", "ALL ONE Second Second")
225        Index.AddFileContentsToIndex("C", "ALL TWO Third Third")
226
227        ' Remove a document
228        Index.RemoveDocumentFromIndex("B")
229
230        ' Check first document
231        Assert.AreEqual(1, Index.TermFrequency(0, "ALL"), "Incorrect term frequency.")
232        Assert.AreEqual(1, Index.TermFrequency(0, "TWO"), "Incorrect term frequency.")
233        Assert.AreEqual(0, Index.TermFrequency(0, "ONE"), "Incorrect term frequency.")
234        Assert.AreEqual(2, Index.TermFrequency(0, "First"), "Incorrect term frequency.")
235        Assert.AreEqual(0, Index.TermFrequency(0, "Second"), "Incorrect term frequency.")
236        Assert.AreEqual(0, Index.TermFrequency(0, "Third"), "Incorrect term frequency.")
237
238        ' Check third document
239        Assert.AreEqual(1, Index.TermFrequency(2, "ALL"), "Incorrect term frequency.")
240        Assert.AreEqual(1, Index.TermFrequency(2, "TWO"), "Incorrect term frequency.")
241        Assert.AreEqual(0, Index.TermFrequency(2, "ONE"), "Incorrect term frequency.")
242        Assert.AreEqual(0, Index.TermFrequency(2, "First"), "Incorrect term frequency.")
243        Assert.AreEqual(0, Index.TermFrequency(2, "Second"), "Incorrect term frequency.")
244        Assert.AreEqual(2, Index.TermFrequency(2, "Third"), "Incorrect term frequency.")
245
246        ' Remove a document
247        Index.RemoveDocumentFromIndex("A")
248
249        ' Check third document
250        Assert.AreEqual(1, Index.TermFrequency(2, "ALL"), "Incorrect term frequency.")
251        Assert.AreEqual(1, Index.TermFrequency(2, "TWO"), "Incorrect term frequency.")
252        Assert.AreEqual(0, Index.TermFrequency(2, "ONE"), "Incorrect term frequency.")
253        Assert.AreEqual(0, Index.TermFrequency(2, "First"), "Incorrect term frequency.")
254        Assert.AreEqual(0, Index.TermFrequency(2, "Second"), "Incorrect term frequency.")
255        Assert.AreEqual(2, Index.TermFrequency(2, "Third"), "Incorrect term frequency.")
256
257        ' Remove a document
258        Index.RemoveDocumentFromIndex("C")
259
260    End Sub
261
262    ''' <summary>
263    ''' Prove an index can be saved and loaded, but don't do any advanced tests.
264    ''' </summary>
265    <TestMethod()> _
266    Public Sub SaveLoadTest()
267
268        ' Create the index
269        Dim Index As Indexer = CreateBasicIndex()
270
271        ' Save the index
272        Dim FileName As String = GetTemporaryFileName()
273        Index.Save(FileName)
274        Assert.IsTrue(IO.File.Exists(FileName), "File does not exist.")
275
276        ' Load the index
277        Dim Result As Indexer = Indexer.LoadIndex(FileName)
278        Assert.AreEqual(3, Result.DocumentCount, "Index not loaded.")
279
280    End Sub
281
282    ''' <summary>
283    ''' Check that documents can be removed from the index.
284    ''' </summary>
285    <TestMethod()> _
286    Public Sub RemoveFileFromIndexTest()
287
288        Dim Index As Indexer = CreateBasicIndex()
289        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents indexed.")
290
291        Index.RemoveDocumentFromIndex("A")
292        Assert.AreEqual(2, Index.DocumentCount, "Incorrect number of documents indexed.")
293
294        Index.RemoveDocumentFromIndex("B")
295        Assert.AreEqual(1, Index.DocumentCount, "Incorrect number of documents indexed.")
296
297        Index.RemoveDocumentFromIndex("C")
298        Assert.AreEqual(0, Index.DocumentCount, "Incorrect number of documents indexed.")
299
300    End Sub
301
302    ''' <summary>
303    ''' Check that documents can be removed from the index, when it does not exist.
304    ''' </summary>
305    <TestMethod(), ExpectedException(GetType(ApplicationException))> _
306    Public Sub RemoveNonExistingFileFromIndexTest()
307
308        Dim Index As Indexer = CreateBasicIndex()
309        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents indexed.")
310
311        Index.RemoveDocumentFromIndex("Z")
312
313    End Sub
314
315    ''' <summary>
316    ''' Test that the index can be cleared.
317    ''' </summary>
318    <TestMethod()> _
319    Public Sub ClearTest()
320
321        Dim Index As Indexer = CreateBasicIndex()
322        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents in index.")
323        Index.Clear()
324        Assert.AreEqual(0, Index.DocumentCount, "Incorrect number of documents in index.")
325
326    End Sub
327
328    ''' <summary>
329    ''' Tests that files can be added to the index.
330    ''' </summary>
331    <TestMethod()> _
332    Public Sub AddFileToIndexTest()
333
334        ' First create some temporary files
335        Dim FileName1 As String = GetTemporaryFileName()
336        Dim FileName2 As String = GetTemporaryFileName()
337        IO.File.WriteAllLines(FileName1, New String() {"First line", "Second line"})
338        IO.File.WriteAllLines(FileName2, New String() {"Third line", "Fourth line"})
339
340        ' Check can add a file without virtual file name
341        Dim Index As New Indexer
342        Index.AddFileToIndex(FileName1)
343        Assert.AreEqual(Index.DocumentCount, 1, "Incorrect number of documents indexed.")
344        Assert.AreEqual(FileName1, Index.DocumentName(0), "Incorrect file name for document in index.")
345
346        ' Check can add a file with virtual file name
347        Index.AddFileToIndex("VIRTUAL", FileName2)
348        Assert.AreEqual(Index.DocumentCount, 2, "Incorrect number of documents indexed.")
349        Assert.AreEqual(FileName1, Index.DocumentName(0), "Incorrect file name for document in index.")
350        Assert.AreEqual("VIRTUAL", Index.DocumentName(1), "Incorrect file name for document in index.")
351
352    End Sub
353
354    ''' <summary>
355    ''' A test for AddFileContentsToIndex
356    ''' </summary>
357    <TestMethod()> _
358    Public Sub AddFileContentsToIndexTest()
359
360        Dim Index As New Indexer
361        Assert.AreEqual(0, Index.DocumentCount, "Incorrect number of documents in the index.")
362
363        Index.AddFileContentsToIndex("Filename1.txt", "This is the contents of the first file.")
364        Assert.AreEqual(1, Index.DocumentCount, "Incorrect number of documents in the index.")
365        Assert.AreEqual("Filename1.txt", Index.DocumentName(0), "Incorrect file name for file in index.")
366
367        Index.AddFileContentsToIndex("Filename2.txt", "This is the contents of the second file.")
368        Assert.AreEqual(2, Index.DocumentCount, "Incorrect number of documents in the index.")
369        Assert.AreEqual("Filename1.txt", Index.DocumentName(0), "Incorrect file name for file in index.")
370        Assert.AreEqual("Filename2.txt", Index.DocumentName(1), "Incorrect file name for file in index.")
371
372        Index.AddFileContentsToIndex("Filename3.txt", "This is the contents of the third file.")
373        Assert.AreEqual(3, Index.DocumentCount, "Incorrect number of documents in the index.")
374        Assert.AreEqual("Filename1.txt", Index.DocumentName(0), "Incorrect file name for file in index.")
375        Assert.AreEqual("Filename2.txt", Index.DocumentName(1), "Incorrect file name for file in index.")
376        Assert.AreEqual("Filename3.txt", Index.DocumentName(2), "Incorrect file name for file in index.")
377
378    End Sub
379
380    ' TODO: Test adding duplicate files with same name
381
382    ''' <summary>
383    ''' A test for Indexer Constructor
384    ''' </summary>
385    <TestMethod()> _
386    Public Sub IndexerConstructorTest()
387
388        Dim Index As Indexer = New Indexer
389        Assert.AreEqual(0, Index.DocumentCount, "There are documents in the index.")
390
391    End Sub
392
393    ''' <summary>
394    ''' Tests the saving and loading of an empty index.
395    ''' </summary>
396    Public Sub SaveEmptyIndexTest()
397
398        Dim Index As New Indexer
399        Dim FileName As String = GetTemporaryFileName()
400        Index.Save(FileName)
401        Index = Indexer.LoadIndex(FileName)
402        Assert.AreEqual(0, Index.DocumentCount, "Incorrect number of documents in index.")
403
404    End Sub
405
406    ''' <summary>
407    ''' Tests that removing all but one document from the index, and then loading a saved version of
408    ''' that index, works as expected.
409    ''' </summary>
410    Public Sub SaveAndLoadAfterDeletingAllButOneTest()
411
412        Dim Index As Indexer = CreateBasicIndex()
413        Index.RemoveDocumentFromIndex("A")
414        Index.RemoveDocumentFromIndex("B")
415        Dim FileName As String = GetTemporaryFileName()
416        Index.Save(FileName)
417        Index = Indexer.LoadIndex(FileName)
418        Assert.AreEqual(1, Index.DocumentCount, "Incorrect number of documents in index.")
419        Assert.AreEqual(2, Index.DocumentNumber("C"), "Incorrect document number for sole document in index.")
420
421    End Sub
422
423    ''' <summary>
424    ''' Tests that files saved and subsequently loaded are identical to in-memory representation of index.
425    ''' (Simple test)
426    ''' </summary>
427    <TestMethod()> _
428    Public Sub SaveAndLoadTest1()
429
430        ' Create a basic index, and remove the first document
431        Dim Index1 As New Indexer
432        Index1.AddFileContentsToIndex("A", "ALL TWO First First")
433        Index1.AddFileContentsToIndex("B", "ALL ONE Second Second")
434        Index1.RemoveDocumentFromIndex("A")
435
436        ' Save the index
437        Dim FileName As String = GetTemporaryFileName()
438        Index1.Save(FileName)
439
440        ' Load the index
441        Dim Index2 As Indexer = Indexer.LoadIndex(FileName)
442        Assert.AreEqual(1, Index1.DocumentCount, "Incorrect number of documents indexed.")
443        Assert.AreEqual(1, Index2.DocumentCount, "Incorrect number of documents indexed.")
444        Assert.AreEqual(Index1.DocumentCount, Index2.DocumentCount, "Incorrect number of documents in index.")
445        Assert.AreEqual(Index1.DocumentName(1), Index2.DocumentName(1), "Mismatch in saved index.")
446
447        ' Check the documents containing the word "ALL"
448        Dim AllDocs1 As List(Of Integer) = Index1.DocumentsContainingWord("ALL")
449        Dim AllDocs2 As List(Of Integer) = Index2.DocumentsContainingWord("ALL")
450        Assert.AreEqual(AllDocs1.Count, AllDocs2.Count, "Invalid number of documents in loaded index.")
451        Assert.AreEqual(AllDocs1(0), AllDocs2(0), "Incorrect document reference.")
452
453        ' Test that the term frequency for each document had been recorded and loaded correctly
454        Const I As Integer = 1
455        Assert.AreEqual(Index1.TermFrequency(I, "ALL"), Index2.TermFrequency(I, "ALL"), "Incorrect term frequency for document " + I.ToString + ".")
456        Assert.AreEqual(Index1.TermFrequency(I, "ONE"), Index2.TermFrequency(I, "ONE"), "Incorrect term frequency for document " + I.ToString + ".")
457        Assert.AreEqual(Index1.TermFrequency(I, "TWO"), Index2.TermFrequency(I, "TWO"), "Incorrect term frequency for document " + I.ToString + ".")
458        Assert.AreEqual(Index1.TermFrequency(I, "First"), Index2.TermFrequency(I, "First"), "Incorrect term frequency for document " + I.ToString + ".")
459        Assert.AreEqual(Index1.TermFrequency(I, "Second"), Index2.TermFrequency(I, "Second"), "Incorrect term frequency for document " + I.ToString + ".")
460        Assert.AreEqual(Index1.TermFrequency(I, "Third"), Index2.TermFrequency(I, "Third"), "Incorrect term frequency for document " + I.ToString + ".")
461
462    End Sub
463
464    ''' <summary>
465    ''' Tests that files saved and subsequently loaded are identical to in-memory representation of index.
466    ''' </summary>
467    <TestMethod()> _
468    Public Sub SaveAndLoadTest2()
469
470        ' Create a basic index, and remove one document in the middle to make it interesting
471        Dim Index1 As New Indexer
472        Index1.AddFileContentsToIndex("A", "ALL TWO First First")
473        Index1.AddFileContentsToIndex("REMOVE_ME", "First Second Third")
474        Index1.AddFileContentsToIndex("B", "ALL ONE Second Second")
475        Index1.AddFileContentsToIndex("C", "ALL TWO Third Third")
476        Index1.RemoveDocumentFromIndex("REMOVE_ME")
477
478        ' Save the index
479        Dim FileName As String = GetTemporaryFileName()
480        Index1.Save(FileName)
481
482        ' Load the index
483        Dim Index2 As Indexer = Indexer.LoadIndex(FileName)
484        Assert.AreEqual(3, Index1.DocumentCount, "Incorrect number of documents indexed.")
485        Assert.AreEqual(3, Index2.DocumentCount, "Incorrect number of documents indexed.")
486        Assert.AreEqual(Index1.DocumentCount, Index2.DocumentCount, "Incorrect number of documents in index.")
487        Assert.AreEqual(Index1.DocumentName(0), Index2.DocumentName(0), "Mismatch in saved index.")
488        Assert.AreEqual(Index1.DocumentName(2), Index2.DocumentName(2), "Mismatch in saved index.")
489        Assert.AreEqual(Index1.DocumentName(3), Index2.DocumentName(3), "Mismatch in saved index.")
490
491        ' Check the documents containing the word "ALL"
492        Dim AllDocs1 As List(Of Integer) = Index1.DocumentsContainingWord("ALL")
493        Dim AllDocs2 As List(Of Integer) = Index2.DocumentsContainingWord("ALL")
494        Assert.AreEqual(AllDocs1.Count, AllDocs2.Count, "Invalid number of documents in loaded index.")
495        Assert.AreEqual(AllDocs1(0), AllDocs2(0), "Incorrect document reference.")
496        Assert.AreEqual(AllDocs1(1), AllDocs2(1), "Incorrect document reference.")
497        Assert.AreEqual(AllDocs1(2), AllDocs2(2), "Incorrect document reference.")
498
499        ' Check the documents containing the word "TWO"
500        Dim TwoDocs1 As List(Of Integer) = Index1.DocumentsContainingWord("TWO")
501        Dim TwoDocs2 As List(Of Integer) = Index2.DocumentsContainingWord("TWO")
502        Assert.AreEqual(TwoDocs1.Count, TwoDocs2.Count, "Invalid number of documents in loaded index.")
503        Assert.AreEqual(TwoDocs1(0), TwoDocs2(0), "Incorrect document reference.")
504        Assert.AreEqual(TwoDocs1(1), TwoDocs2(1), "Incorrect document reference.")
505
506        ' Check the documents containing the word "ONE"
507        Dim OneDocs1 As List(Of Integer) = Index1.DocumentsContainingWord("ONE")
508        Dim OneDocs2 As List(Of Integer) = Index2.DocumentsContainingWord("ONE")
509        Assert.AreEqual(OneDocs1.Count, OneDocs2.Count, "Invalid number of documents in loaded index.")
510        Assert.AreEqual(OneDocs1(0), OneDocs2(0), "Incorrect document reference.")
511
512        ' Test that the term frequency for each document had been recorded and loaded correctly
513        For I As Integer = 0 To 3
514            If I <> 1 Then
515                Assert.AreEqual(Index1.TermFrequency(I, "ALL"), Index2.TermFrequency(I, "ALL"), "Incorrect term frequency for document " + I.ToString + ".")
516                Assert.AreEqual(Index1.TermFrequency(I, "ONE"), Index2.TermFrequency(I, "ONE"), "Incorrect term frequency for document " + I.ToString + ".")
517                Assert.AreEqual(Index1.TermFrequency(I, "TWO"), Index2.TermFrequency(I, "TWO"), "Incorrect term frequency for document " + I.ToString + ".")
518                Assert.AreEqual(Index1.TermFrequency(I, "First"), Index2.TermFrequency(I, "First"), "Incorrect term frequency for document " + I.ToString + ".")
519                Assert.AreEqual(Index1.TermFrequency(I, "Second"), Index2.TermFrequency(I, "Second"), "Incorrect term frequency for document " + I.ToString + ".")
520                Assert.AreEqual(Index1.TermFrequency(I, "Third"), Index2.TermFrequency(I, "Third"), "Incorrect term frequency for document " + I.ToString + ".")
521            End If
522        Next
523
524    End Sub
525
526End Class