[Adapdev-commits] Adapdev/src/Adapdev.Tests/Text/Indexing FullTextSearchTests.cs,1.2,1.3 RegExFilter
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-11-16 07:02:03
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.Tests/Text/Indexing In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv909/src/Adapdev.Tests/Text/Indexing Added Files: FullTextSearchTests.cs RegExFilterTest.cs SearchResultTest.cs SpecialCharactersFilterTest.cs StringTokenizerTest.cs TokenAssertions.cs TokenLengthFilterTest.cs WordFilterTest.cs Log Message: --- NEW FILE: TokenAssertions.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Tokenizers; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Summary description for TokenAssertions. /// </summary> public class TokenAssertions { public static void AssertTokens(string text, ITokenFilter filter, params Token[] tokens) { AssertTokens(new StringTokenizer(text), filter, tokens); } public static void AssertTokens(ITokenizer tokenizer, ITokenFilter filter, params Token[] tokens) { ITokenizer actual = filter.Clone(tokenizer); foreach (Token expected in tokens) { Assertion.AssertEquals(expected, actual.NextToken()); } } public static void AssertTokens(ITokenizer tokenizer, params Token[] tokens) { foreach (Token expected in tokens) { Assertion.AssertEquals(expected, tokenizer.NextToken()); } } public static void AssertTokenValues(ITokenizer tokenizer, params string[] expectedValues) { foreach (string value in expectedValues) { Assertion.AssertEquals(value, tokenizer.NextToken().Value); } Assertion.AssertNull(tokenizer.NextToken()); } public static void AssertTokenValues(string text, ITokenFilter filter, params string[] expectedValues) { ITokenizer tokenizer = filter.Clone(new StringTokenizer(text)); AssertTokenValues(tokenizer, expectedValues); } public static object SerializeDeserialize(object graph) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, graph); stream.Position = 0; return formatter.Deserialize(stream); } } } --- NEW FILE: SearchResultTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing; using Adapdev.Text.Indexing.Records; namespace Adapdev.Text.Indexing.Tests { class AgeFilter : ISearchHitFilter { int _min; int _max; public AgeFilter(int min, int max) { _min = min; _max = max; } public bool Test(SearchHit hit) { int age = (int)hit.Record["Age"]; return (age >= _min && age <= _max); } } class NameComparer : System.Collections.IComparer { public int Compare(object a, object b) { HashtableRecord r1 = (HashtableRecord)a; HashtableRecord r2 = (HashtableRecord)b; return ((IComparable)r1["Name"]).CompareTo(r2["Name"]); } } /// <summary> /// Summary description for SearchResultTest. /// </summary> [TestFixture] public class SearchResultTest : Assertion { HashtableRecord _record1; HashtableRecord _record2; HashtableRecord _record3; SearchResult _result; [SetUp] public void SetUp() { _record1 = CreateRecord("Teresa", 43); _record2 = CreateRecord("M�rcia", 26); _record3 = CreateRecord("Bamboo", 27); _result = new SearchResult(); _result.Add(new SearchHit(_record1)); _result.Add(new SearchHit(_record2)); _result.Add(new SearchHit(_record3)); } [Test] public void TestToRecordArray() { HashtableRecord[] records = (HashtableRecord[])_result.ToRecordArray(typeof(HashtableRecord)); AssertEquals(3, records.Length); AssertSame(_record1, records[0]); AssertSame(_record2, records[1]); AssertSame(_record3, records[2]); } [Test] public void TestSortByField() { _result.SortByField("Name"); AssertSearchHits(_result, _record3, _record2, _record1); _result.SortByField("Age"); AssertSearchHits(_result, _record2, _record3, _record1); } [Test] public void TestSort() { _result.Sort(new NameComparer()); AssertSearchHits(_result, _record3, _record2, _record1); } [Test] public void TestFilter() { AssertSearchHits(_result.Filter(new AgeFilter(25, 28)), _record2, _record3); } [Test] public void TestIntersect() { SearchResult other = new SearchResult(); other.Add(new SearchHit(_record3)); other.Add(new SearchHit(_record1)); AssertSearchHits(_result.Intersect(other), _record1, _record3); other = new SearchResult(); other.Add(new SearchHit(_record2)); AssertSearchHits(_result.Intersect(other), _record2); AssertEquals(0, _result.Intersect(new SearchResult()).Count); AssertSearchHits(_result.Intersect(_result), _record1, _record2, _record3); } [Test] public void TestForEach() { int i=0; IRecord[] expected = { _record1, _record2, _record3 }; foreach (SearchHit hit in _result) { AssertEquals(expected[i++], hit.Record); } } void AssertSearchHits(SearchResult result, params IRecord[] expectedRecords) { AssertEquals(expectedRecords.Length, result.Count); for (int i=0; i<expectedRecords.Length; ++i) { IRecord expected = expectedRecords[i]; IRecord actual = result[i].Record; AssertEquals(string.Format("{0} != {1}", expected["Name"], actual["Name"]), expected, actual); } } HashtableRecord CreateRecord(string name, int age) { HashtableRecord record = new HashtableRecord(); record["Name"] = name; record["Age"] = age; return record; } } } --- NEW FILE: WordFilterTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Filters; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Summary description for WordFilterTest. /// </summary> [TestFixture] public class WordFilterTest : Assertion { [Test] public void TestFilter() { string[] filteredWords = { "de", "com" }; string text = "Bolo de chocolate com calda de morango"; TokenAssertions.AssertTokens(text, new WordFilter(filteredWords), new Token("Bolo", 0), new Token("chocolate", 8), new Token("calda", 22), new Token("morango", 31), null); } [Test] public void TestWordFilterChaining() { string[] filteredWords = { "de", "com" }; string text = "Bolo dé Açafrão com Rúcula"; TokenAssertions.AssertTokens(text, new WordFilter(new SpecialCharactersFilter(), filteredWords), new Token("bolo", 0), new Token("acafrao", 8), new Token("rucula", 20), null ); } } } --- NEW FILE: RegExFilterTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Filters; using Adapdev.Text.Indexing.FullText.Tokenizers; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Summary description for RegExFilterTest. /// </summary> [TestFixture] public class RegExFilterTest { RegexTokenFilter _filter; [SetUp] public void SetUp() { _filter = new RegexTokenFilter(@"[0-9]+\w*"); } [Test] public void TestSerializable() { _filter = (RegexTokenFilter)TokenAssertions.SerializeDeserialize(_filter); TestSimpleRegex(); } [Test] public void TestSimpleRegex() { TokenAssertions.AssertTokens("a token, 100 23 1g 144kg other token", _filter, new Token("a", 0), new Token("token", 2), new Token("other", 25), new Token("token", 31), null ); } } } --- NEW FILE: StringTokenizerTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Tokenizers; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Tests for the StringTokenizer class. /// </summary> [TestFixture] public class StringTokenizerTest : Assertion { [Test] public void TestSimpleStrings() { string text = "a foo Bar a�a\n45\n\n\n"; TokenAssertions.AssertTokens(new StringTokenizer(text), new Token("a", 0), new Token("foo", 2), new Token("Bar", 6), new Token("a�a", 10), new Token("45", 14), null ); TokenAssertions.AssertTokens(new StringTokenizer(""), (Token)null); TokenAssertions.AssertTokens(new StringTokenizer("\n\t "), (Token)null); TokenAssertions.AssertTokens(new StringTokenizer("\n\t a"), new Token("a", 4), null); } [Test] public void TestPunctuation() { string text = "A foo,bar goest! flu? Oh, yes, flu!!! really? yep.\n.\tdidn't think [so..(yep)"; TokenAssertions.AssertTokenValues(new StringTokenizer(text), "A", "foo", "bar", "goest", "flu", "Oh", "yes", "flu", "really", "yep", "didn", "t", "think", "so", "yep" ); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void TestNullValues() { StringTokenizer tokenizer = new StringTokenizer(null); } } } --- NEW FILE: TokenLengthFilterTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Filters; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Summary description for TokenLengthFilterTest. /// </summary> [TestFixture] public class TokenLengthFilterTest { [Test] public void TestFilter() { string text = "a bc dado de o"; TokenAssertions.AssertTokenValues(text, new TokenLengthFilter(1), "a", "bc", "dado", "de", "o" ); TokenAssertions.AssertTokenValues(text, new TokenLengthFilter(2), "bc", "dado", "de" ); TokenAssertions.AssertTokenValues(text, new TokenLengthFilter(3), "dado" ); TokenAssertions.AssertTokenValues(text, new TokenLengthFilter(4), "dado" ); } } } --- NEW FILE: SpecialCharactersFilterTest.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.FullText.Filters; using Adapdev.Text.Indexing.FullText.Tokenizers; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Summary description for SpecialCharactersFilterTest. /// </summary> [TestFixture] public class SpecialCharactersFilterTest { [Test] public void TestReplacements() { string text = "áéÃóú calção aviões aiouao"; TokenAssertions.AssertTokens(text, new SpecialCharactersFilter(), new Token("aeiou", 0), new Token("calcao", 6), new Token("avioes", 13), new Token("aiouao", 20), null ); } } } --- NEW FILE: FullTextSearchTests.cs --- #region license // Bamboo.Prevalence - a .NET object prevalence engine // Copyright (C) 2004 Rodrigo B. de Oliveira // // Based on the original concept and implementation of Prevayler (TM) // by Klaus Wuestefeld. Visit http://www.prevayler.org for details. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Contact Information // // http://bbooprevalence.sourceforge.net // mailto:rod...@us... #endregion using System; using NUnit.Framework; using Adapdev.Text.Indexing; using Adapdev.Text.Indexing.FullText; using Adapdev.Text.Indexing.Records; namespace Adapdev.Text.Indexing.Tests { /// <summary> /// Test cases for the fulltext indexing/search support. /// </summary> [TestFixture] public class FullTextSearchTests : Assertion { IIndex _index; IIndex _multipleFieldIndex; HashtableRecord _record1; HashtableRecord _record2; HashtableRecord _record3; [SetUp] public void SetUp() { FullTextSearchIndex index = new FullTextSearchIndex(); index.Fields.Add("Title"); FullTextSearchIndex multipleFieldIndex = new FullTextSearchIndex(); multipleFieldIndex.Fields.Add("Title"); multipleFieldIndex.Fields.Add("Ingredients"); _index = index; _multipleFieldIndex = multipleFieldIndex; _record1 = new HashtableRecord(); _record1["Title"] = "Bolo de Chocolate"; _record1["Calories"] = 300; _record1["Ingredients"] = "3 colheres de açucar\n1 lata de nescau\nfermento"; _index.Add(_record1); _multipleFieldIndex.Add(_record1); DumpPostings(index.Postings); _record2 = new HashtableRecord(); _record2["Title"] = "Bolo de Açafrão"; _record2["Calories"] = 100; _record2["Ingredients"] = "10 folhas de açafrão\n1 colher de fermento em pó"; _index.Add(_record2); _multipleFieldIndex.Add(_record2); DumpPostings(index.Postings); _record3 = new HashtableRecord(); _record3["Title"] = "Torta de Chocolate"; _record3["Calories"] = 400; _record3["Ingredients"] = "1 lata de nescau\nchocolate granulado\naçucar"; _index.Add(_record3); _multipleFieldIndex.Add(_record3); DumpPostings(index.Postings); } [Test] public void TestClear() { _index.Clear(); AssertEquals(0, ((FullTextSearchIndex)_index).Records.Length); AssertEquals(0, ((FullTextSearchIndex)_index).Postings.Length); AssertSearchContains(_index.Search(new FullTextSearchExpression("chocolate"))); } [Test] public void TestSimpleSearch() { ISearchExpression expression = new FullTextSearchExpression("bolo"); AssertSearchContains(_index.Search(expression), _record1, _record2); expression = new FullTextSearchExpression("chocolate"); AssertSearchContains(_index.Search(expression), _record1, _record3); expression = new FullTextSearchExpression("acafrão"); AssertSearchContains(_index.Search(expression), _record2); expression = new FullTextSearchExpression("bolo AcaFrao"); AssertSearchContains(_index.Search(expression), _record1, _record2); } [Test] public void TestMultiIndexSimpleSearch() { ISearchExpression expression = new FullTextSearchExpression("nescau"); AssertSearchContains(_multipleFieldIndex.Search(expression), _record1, _record3); expression = new FullTextSearchExpression("chocolate"); AssertSearchContains(_multipleFieldIndex.Search(expression), _record1, _record3); expression = new FullTextSearchExpression("fermento"); AssertSearchContains(_multipleFieldIndex.Search(expression), _record1, _record2); } [Test] public void TestIncludeAllSearch() { ISearchExpression expression = new FullTextSearchExpression("Bolo Chocolate", FullTextSearchMode.IncludeAll); AssertSearchContains(_index.Search(expression), _record1); AssertSearchContains(_multipleFieldIndex.Search(expression), _record1); expression = new FullTextSearchExpression("Bolo Açafrão", FullTextSearchMode.IncludeAll); AssertSearchContains(_index.Search(expression), _record2); AssertSearchContains(_multipleFieldIndex.Search(expression), _record2); expression = new FullTextSearchExpression("Torta Chocolate", FullTextSearchMode.IncludeAll); AssertSearchContains(_index.Search(expression), _record3); AssertSearchContains(_multipleFieldIndex.Search(expression), _record3); } [Test] public void TestMultiIndexIncludeAllSearch() { AssertSearchContains( _multipleFieldIndex.Search(new FullTextSearchExpression("bolo nescau", FullTextSearchMode.IncludeAll)), _record1 ); AssertSearchContains( _multipleFieldIndex.Search(new FullTextSearchExpression("torta nescau", FullTextSearchMode.IncludeAll)), _record3 ); AssertSearchContains( _multipleFieldIndex.Search(new FullTextSearchExpression("bolo fermento", FullTextSearchMode.IncludeAll)), _record1, _record2 ); } [Test] public void TestRemove() { _index.Remove(_record1); _multipleFieldIndex.Remove(_record1); AssertSearchContains( _index.Search(new FullTextSearchExpression("bolo chocolate")), _record2, _record3 ); AssertSearchContains( _multipleFieldIndex.Search(new FullTextSearchExpression("açucar")), _record3 ); } [Test] [ExpectedException(typeof(ArgumentException))] public void TestInvalidSearchExpression() { // this search is invalid because // the default filter should only let // pass tokens longer // than 2 characters _index.Search(new FullTextSearchExpression("de")); } [Test] public void TestUpdate() { _record1["Title"] = "Torta de Salsinha"; _index.Update(_record1); _multipleFieldIndex.Update(_record1); ISearchExpression expression = new FullTextSearchExpression("torta salsinha"); AssertSearchContains(_index.Search(expression), _record1, _record3); AssertSearchContains(_multipleFieldIndex.Search(expression), _record1, _record3); } [Test] public void TestIndexSerialization() { _index = TokenAssertions.SerializeDeserialize(_index) as IIndex; FullTextSearchIndex index = _index as FullTextSearchIndex; IRecord[] records = index.Records; AssertEquals(3, records.Length); _record1 = FindByTitle(records, (string)_record1["Title"]); _record2 = FindByTitle(records, (string)_record2["Title"]); _record3 = FindByTitle(records, (string)_record3["Title"]); TestSimpleSearch(); } HashtableRecord FindByTitle(IRecord[] records, string title) { foreach (HashtableRecord record in records) { if (((string)record["Title"]) == title) { return record; } } throw new ArgumentException("Record not found!"); } void AssertSearchContains(SearchResult result, params IRecord[] expected) { foreach (IRecord record in expected) { if (!result.Contains(record)) { Fail(string.Format("Expected record \"{0}\"!", record["Title"])); } } AssertEquals("result.Count", expected.Length, result.Count); } void DumpPostings(Postings[] postings) { System.Diagnostics.Trace.WriteLine("\n**************"); foreach (Postings p in postings) { System.Diagnostics.Trace.WriteLine(p.ToString()); } System.Diagnostics.Trace.WriteLine("**************"); } } } |