You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by pn...@apache.org on 2011/05/07 07:29:38 UTC

[Lucene.Net] svn commit: r1100454 - in /incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core: Lucene.Net.Test.csproj Search/Regex/ Search/Regex/TestRegexpQuery.cs

Author: pnasser
Date: Sat May  7 05:29:37 2011
New Revision: 1100454

URL: http://svn.apache.org/viewvc?rev=1100454&view=rev
Log:
LUCENENET-371 Unit Tests for Search.Regex Min Port (LUCENENET-330)

Added:
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/TestRegexpQuery.cs
Modified:
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj

Modified: incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj?rev=1100454&r1=1100453&r2=1100454&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj (original)
+++ incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj Sat May  7 05:29:37 2011
@@ -386,6 +386,7 @@
     <Compile Include="Search\TestPrefixQuery.cs" />
     <Compile Include="Search\TestQueryTermVector.cs" />
     <Compile Include="Search\TestQueryWrapperFilter.cs" />
+    <Compile Include="Search\Regex\TestRegexpQuery.cs" />
     <Compile Include="Search\TestScoreCachingWrappingScorer.cs" />
     <Compile Include="Search\TestScorerPerf.cs" />
     <Compile Include="Search\TestSearchHitsWithDeletions.cs" />

Added: incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/TestRegexpQuery.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/TestRegexpQuery.cs?rev=1100454&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/TestRegexpQuery.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Search/Regex/TestRegexpQuery.cs Sat May  7 05:29:37 2011
@@ -0,0 +1,129 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Search;
+using Lucene.Net.Search.Regex;
+using Lucene.Net.Store;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+
+namespace Lucene.Net.Search.Regex
+{
+    /// <summary>
+    /// See http://svn.eu.apache.org/repos/asf/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/TestRegexpQuery.java
+    /// </summary>
+    [TestFixture]
+    public class TestRegexpQuery : LuceneTestCase
+    {
+        private IndexSearcher _searcher;
+        private const string FIELDNAME = "field";
+
+        public override void SetUp()
+        {
+            base.SetUp();
+
+            RAMDirectory directory = new RAMDirectory();
+            IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
+
+            Document doc = new Document();
+            doc.Add(new Field(FIELDNAME, "the quick brown fox jumps over the lazy crazy pretty dog",
+                Field.Store.NO, Field.Index.ANALYZED));
+            writer.AddDocument(doc);
+            writer.Optimize();
+            writer.Close();
+            _searcher = new IndexSearcher(directory, true);
+        }
+
+        public override void TearDown()
+        {
+            _searcher.Close();
+            base.TearDown();
+        }
+
+        private static Term NewTerm(string sValue)
+        {
+            return new Term(FIELDNAME, sValue);
+        }
+
+        private int GetRegexQueryHitsCount(string sRegex)
+        {
+            RegexQuery query = new RegexQuery(NewTerm(sRegex));
+            return _searcher.Search(query, 5).totalHits;
+        }
+
+        [Test]
+        public void TestLiterals()
+        {
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"nothing"));
+
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"brown"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"ver"));
+
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"the"));
+        }
+
+        [Test]
+        public void TestExpressions()
+        {
+            // wildcards
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"j.mps"));
+
+            // repetitions
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"dogs?"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"pret+y"));
+
+            // classes
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"q.[aeiou]c.*"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"br?own?"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"z.[aeiou]c.*"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"c?[rl]azy"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\bc?[rl]azy\b"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"c[^lmn]azy"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"c[^r]azy"));
+
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\p{L}+"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\p{L}{6,}"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"\p{L}{7,}"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\p{L}{6,7}"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"\p{L}{7,9}"));
+
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\D+"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"\d+"));
+
+            // position
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"^q"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"q$"));
+
+            // alternatives
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"brown|red"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"yellow|red"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"(l|cr)azy"));
+
+            // lookaround
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\b\w+(?=zy)"));
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"\b\w+(?=zu)"));
+
+            Assert.AreEqual(0, GetRegexQueryHitsCount(@"\b\w*q[^u]\w*\b"));
+            Assert.AreEqual(1, GetRegexQueryHitsCount(@"\b\w*qu\w*\b"));
+        }
+    }
+
+}