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