You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ar...@apache.org on 2009/11/03 19:06:38 UTC
svn commit: r832486 [29/29] - in /incubator/lucene.net/trunk/C#/src: ./
Demo/DeleteFiles/ Demo/DemoLib/ Demo/IndexFiles/ Demo/IndexHtml/
Demo/SearchFiles/ Lucene.Net/ Lucene.Net/Analysis/ Lucene.Net/Document/
Lucene.Net/Index/ Lucene.Net/Search/ Lucene...
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestOpenBitSet.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestOpenBitSet.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestOpenBitSet.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestOpenBitSet.cs Tue Nov 3 18:06:27 2009
@@ -1,13 +1,13 @@
-/**
+/*
* 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
- *
+ *
+ * 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.
@@ -15,318 +15,257 @@
* limitations under the License.
*/
+using System;
+
using NUnit.Framework;
-using BitArray = System.Collections.BitArray;
+using DocIdSetIterator = Lucene.Net.Search.DocIdSetIterator;
namespace Lucene.Net.Util
{
- /**
- * @version $Id$
- */
- [TestFixture]
- public class TestOpenBitSet
- {
- static System.Random rand = new System.Random();
-
- void DoGet(BitArray a, OpenBitSet b)
- {
- int max = a.Count;
- for (int i = 0; i < max; i++)
- {
- if (a.Get(i) != b.Get(i))
- {
- Assert.Fail("mismatch: BitArray[" + i + "]=" + a.Get(i));
- }
- }
- }
-
- void DoNextSetBit(BitArray a, OpenBitSet b)
- {
- int aa = -1, bb = -1;
- do
- {
- aa = SupportClass.Number.NextSetBit(a, aa + 1);
- bb = b.NextSetBit(bb + 1);
- Assert.AreEqual(aa, bb);
- } while (aa >= 0);
- }
-
- // test interleaving different BitSetIterator.next()
- void DoIterate(BitArray a, OpenBitSet b)
- {
- int aa = -1, bb = -1;
- OpenBitSetIterator iterator = new OpenBitSetIterator(b);
- do
- {
- aa = SupportClass.Number.NextSetBit(a, aa + 1);
- if (rand.Next(2) == 0)
- iterator.Next();
- else
- iterator.SkipTo(bb + 1);
- bb = iterator.Doc();
- Assert.AreEqual(aa, bb);
- } while (aa >= 0);
- }
-
-
- void DoRandomSets(int maxSize, int iter)
- {
- BitArray a0 = null;
- OpenBitSet b0 = null;
-
- for (int i = 0; i < iter; i++)
- {
- int sz = rand.Next(maxSize);
- BitArray a = new BitArray(sz);
- OpenBitSet b = new OpenBitSet(sz);
-
- // test the various ways of setting bits
- if (sz > 0)
- {
- int nOper = rand.Next(sz);
- for (int j = 0; j < nOper; j++)
- {
- int idx;
-
- idx = rand.Next(sz);
- a.Set(idx, true);
- b.FastSet(idx);
- idx = rand.Next(sz);
- a.Set(idx, false);
- b.FastClear(idx);
- idx = rand.Next(sz);
- a.Set(idx, !a.Get(idx));
- b.FastFlip(idx);
-
- bool val = b.FlipAndGet(idx);
- bool val2 = b.FlipAndGet(idx);
- Assert.IsTrue(val != val2);
-
- val = b.GetAndSet(idx);
- Assert.IsTrue(val2 == val);
- Assert.IsTrue(b.Get(idx));
-
- if (!val) b.FastClear(idx);
- Assert.IsTrue(b.Get(idx) == val);
- }
- }
-
- // test that the various ways of accessing the bits are equivalent
- DoGet(a, b);
-
- // {{dougsale-2.4.0}}
- //
- // Java's java.util.BitSet automatically grows as needed - i.e., when a bit is referenced beyond
- // the size of the BitSet, an exception isn't thrown - rather, the set grows to the size of the
- // referenced bit.
- //
- // System.Collections.BitArray does not have this feature, and thus I've faked it here by
- // "growing" the array explicitly when necessary (creating a new instance of the appropriate size
- // and setting the appropriate bits).
- //
-
- // test ranges, including possible extension
- int fromIndex, toIndex;
- fromIndex = rand.Next(sz + 80);
- toIndex = fromIndex + rand.Next((sz >> 1) + 1);
-
- // {{dougsale-2.4.0}}:
- // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSets 'a'
- // and 'aa' to the same cardinality as 'j+1' when 'a.Count < j+1' and 'fromIndex < toIndex':
- //BitArray aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, !a.Get(j));
- // So, if necessary, lets explicitly grow 'a' now; then 'a' and its clone, 'aa', will be of the required size.
- if (a.Count < toIndex && fromIndex < toIndex)
- {
- BitArray tmp = new BitArray(toIndex, false);
- for (int k = 0; k < a.Count; k++)
- tmp.Set(k, a.Get(k));
- a = tmp;
- }
- // {{dougsale-2.4.0}}: now we can invoke this statement without going 'out-of-bounds'
- BitArray aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, !a.Get(j));
- OpenBitSet bb = (OpenBitSet)b.Clone(); bb.Flip(fromIndex, toIndex);
-
- DoIterate(aa, bb); // a problem here is from flip or DoIterate
-
- fromIndex = rand.Next(sz + 80);
- toIndex = fromIndex + rand.Next((sz >> 1) + 1);
- // {{dougsale-2.4.0}}:
- // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSet 'aa'
- // when 'a.Count < j+1' and 'fromIndex < toIndex'
- //aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
- // So, if necessary, lets explicitly grow 'aa' now
- if (a.Count < toIndex && fromIndex < toIndex)
- {
- aa = new BitArray(toIndex);
- for (int k = 0; k < a.Count; k++)
- aa.Set(k, a.Get(k));
- }
- else
- {
- aa = (BitArray)a.Clone();
- }
- for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
- bb = (OpenBitSet)b.Clone(); bb.Clear(fromIndex, toIndex);
-
- DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit
-
- fromIndex = rand.Next(sz + 80);
- toIndex = fromIndex + rand.Next((sz >> 1) + 1);
- // {{dougsale-2.4.0}}:
- // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSet 'aa'
- // when 'a.Count < j+1' and 'fromIndex < toIndex'
- //aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
- // So, if necessary, lets explicitly grow 'aa' now
- if (a.Count < toIndex && fromIndex < toIndex)
- {
- aa = new BitArray(toIndex);
- for (int k = 0; k < a.Count; k++)
- aa.Set(k, a.Get(k));
- }
- else
- {
- aa = (BitArray)a.Clone();
- }
- for (int j = fromIndex; j < toIndex; j++) aa.Set(j, true);
- bb = (OpenBitSet)b.Clone(); bb.Set(fromIndex, toIndex);
-
- DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit
-
-
- if (a0 != null)
- {
- Assert.AreEqual(a.Equals(a0), b.Equals(b0));
-
- Assert.AreEqual(SupportClass.Number.Cardinality(a), b.Cardinality());
-
- // {{dougsale-2.4.0}}
- //
- // The Java code used java.util.BitSet, which grows as needed.
- // When a bit, outside the dimension of the set is referenced,
- // the set automatically grows to the necessary size. The
- // new entries default to false.
- //
- // BitArray does not grow automatically and is not growable.
- // Thus when BitArray instances of mismatched cardinality
- // interact, we must first explicitly "grow" the smaller one.
- //
- // This growth is acheived by creating a new instance of the
- // required size and copying the appropriate values.
- //
-
- //BitArray a_and = (BitArray)a.Clone(); a_and.And(a0);
- //BitArray a_or = (BitArray)a.Clone(); a_or.Or(a0);
- //BitArray a_xor = (BitArray)a.Clone(); a_xor.Xor(a0);
- //BitArray a_andn = (BitArray)a.Clone(); for (int j = 0; j < a_andn.Count; j++) if (a0.Get(j)) a_andn.Set(j, false);
-
- BitArray a_and;
- BitArray a_or;
- BitArray a_xor;
- BitArray a_andn;
-
- if (a.Count < a0.Count)
- {
- // the Java code would have implicitly resized 'a_and', 'a_or', 'a_xor', and 'a_andn'
- // in this case, so we explicitly create a resized stand-in for 'a' here, allowing for
- // a to keep its original size while 'a_and', 'a_or', 'a_xor', and 'a_andn' are resized
- BitArray tmp = new BitArray(a0.Count, false);
- for (int z = 0; z < a.Count; z++)
- tmp.Set(z, a.Get(z));
-
- a_and = (BitArray)tmp.Clone(); a_and.And(a0);
- a_or = (BitArray)tmp.Clone(); a_or.Or(a0);
- a_xor = (BitArray)tmp.Clone(); a_xor.Xor(a0);
- a_andn = (BitArray)tmp.Clone(); for (int j = 0; j < a_andn.Count; j++) if (a0.Get(j)) a_andn.Set(j, false);
- }
- else if (a.Count > a0.Count)
- {
- // the Java code would have implicitly resized 'a0' in this case, so
- // we explicitly do so here:
- BitArray tmp = new BitArray(a.Count, false);
- for (int z = 0; z < a0.Count; z++)
- tmp.Set(z, a0.Get(z));
- a0 = tmp;
-
- a_and = (BitArray)a.Clone(); a_and.And(a0);
- a_or = (BitArray)a.Clone(); a_or.Or(a0);
- a_xor = (BitArray)a.Clone(); a_xor.Xor(a0);
- a_andn = (BitArray)a.Clone(); for (int j = 0; j < a_andn.Count; j++) if (a0.Get(j)) a_andn.Set(j, false);
- }
- else
- {
- // 'a' and 'a0' are the same size, no explicit growing necessary
- a_and = (BitArray)a.Clone(); a_and.And(a0);
- a_or = (BitArray)a.Clone(); a_or.Or(a0);
- a_xor = (BitArray)a.Clone(); a_xor.Xor(a0);
- a_andn = (BitArray)a.Clone(); for (int j = 0; j < a_andn.Count; j++) if (a0.Get(j)) a_andn.Set(j, false);
- }
-
- OpenBitSet b_and = (OpenBitSet)b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0);
- OpenBitSet b_or = (OpenBitSet)b.Clone(); b_or.Or(b0);
- OpenBitSet b_xor = (OpenBitSet)b.Clone(); b_xor.Xor(b0);
- OpenBitSet b_andn = (OpenBitSet)b.Clone(); b_andn.AndNot(b0);
-
- DoIterate(a_and, b_and);
- DoIterate(a_or, b_or);
- DoIterate(a_xor, b_xor);
- DoIterate(a_andn, b_andn);
-
- Assert.AreEqual(SupportClass.Number.Cardinality(a_and), b_and.Cardinality());
- Assert.AreEqual(SupportClass.Number.Cardinality(a_or), b_or.Cardinality());
- Assert.AreEqual(SupportClass.Number.Cardinality(a_xor), b_xor.Cardinality());
- Assert.AreEqual(SupportClass.Number.Cardinality(a_andn), b_andn.Cardinality());
-
- // test non-mutating popcounts
- Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0));
- Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0));
- Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0));
- Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0));
- }
-
- a0 = a;
- b0 = b;
- }
- }
-
- // large enough to flush obvious bugs, small enough to run in <.5 sec as part of a
- // larger testsuite.
- [Test]
- public void TestSmall()
- {
- DoRandomSets(1200, 1000);
- }
-
- [Test]
- public void TestBig()
- {
- // uncomment to run a bigger test (~2 minutes).
- //DoRandomSets(2000,200000);
- }
-
- [Test]
- public void TestEquals()
- {
- OpenBitSet b1 = new OpenBitSet(1111);
- OpenBitSet b2 = new OpenBitSet(2222);
- Assert.IsTrue(b1.Equals(b2));
- Assert.IsTrue(b2.Equals(b1));
- b1.Set(10);
- Assert.IsFalse(b1.Equals(b2));
- Assert.IsFalse(b2.Equals(b1));
- b2.Set(10);
- Assert.IsTrue(b1.Equals(b2));
- Assert.IsTrue(b2.Equals(b1));
- b2.Set(2221);
- Assert.IsFalse(b1.Equals(b2));
- Assert.IsFalse(b2.Equals(b1));
- b1.Set(2221);
- Assert.IsTrue(b1.Equals(b2));
- Assert.IsTrue(b2.Equals(b1));
-
- // try different type of object
- Assert.IsFalse(b1.Equals(new object()));
- }
-
- }
-}
+
+ /// <version> $Id$
+ /// </version>
+ [TestFixture]
+ public class TestOpenBitSet:LuceneTestCase
+ {
+ internal System.Random rand;
+
+ internal virtual void DoGet(System.Collections.BitArray a, OpenBitSet b)
+ {
+ int max = a.Count;
+ for (int i = 0; i < max; i++)
+ {
+ if (a.Get(i) != b.Get(i))
+ {
+ Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.Get(i));
+ }
+ }
+ }
+
+ internal virtual void DoNextSetBit(System.Collections.BitArray a, OpenBitSet b)
+ {
+ int aa = - 1, bb = - 1;
+ do
+ {
+ aa = SupportClass.BitSetSupport.NextSetBit(a, aa + 1);
+ bb = b.NextSetBit(bb + 1);
+ Assert.AreEqual(aa, bb);
+ }
+ while (aa >= 0);
+ }
+
+ // test interleaving different OpenBitSetIterator.next()/skipTo()
+ internal virtual void DoIterate(System.Collections.BitArray a, OpenBitSet b, int mode)
+ {
+ if (mode == 1)
+ DoIterate1(a, b);
+ if (mode == 2)
+ DoIterate2(a, b);
+ }
+
+ internal virtual void DoIterate1(System.Collections.BitArray a, OpenBitSet b)
+ {
+ int aa = - 1, bb = - 1;
+ OpenBitSetIterator iterator = new OpenBitSetIterator(b);
+ do
+ {
+ aa = SupportClass.BitSetSupport.NextSetBit(a, aa + 1);
+ bb = rand.NextDouble() > 0.5 ? iterator.NextDoc() : iterator.Advance(bb + 1);
+ Assert.AreEqual(aa == - 1?DocIdSetIterator.NO_MORE_DOCS:aa, bb);
+ }
+ while (aa >= 0);
+ }
+
+ internal virtual void DoIterate2(System.Collections.BitArray a, OpenBitSet b)
+ {
+ int aa = - 1, bb = - 1;
+ OpenBitSetIterator iterator = new OpenBitSetIterator(b);
+ do
+ {
+ aa = SupportClass.BitSetSupport.NextSetBit(a, aa + 1);
+ bb = rand.NextDouble() > 0.5 ? iterator.NextDoc() : iterator.Advance(bb + 1);
+ Assert.AreEqual(aa == - 1?DocIdSetIterator.NO_MORE_DOCS:aa, bb);
+ }
+ while (aa >= 0);
+ }
+
+ internal virtual void DoRandomSets(int maxSize, int iter, int mode)
+ {
+ System.Collections.BitArray a0 = null;
+ OpenBitSet b0 = null;
+
+ for (int i = 0; i < iter; i++)
+ {
+ int sz = rand.Next(maxSize);
+ System.Collections.BitArray a = new System.Collections.BitArray((sz % 64 == 0?sz / 64:sz / 64 + 1) * 64);
+ OpenBitSet b = new OpenBitSet(sz);
+
+ // test the various ways of setting bits
+ if (sz > 0)
+ {
+ int nOper = rand.Next(sz);
+ for (int j = 0; j < nOper; j++)
+ {
+ int idx;
+
+ idx = rand.Next(sz);
+ a.Set(idx, true);
+ b.FastSet(idx);
+ idx = rand.Next(sz);
+ a.Set(idx, false);
+ b.FastClear(idx);
+ idx = rand.Next(sz);
+ a.Set(idx, !a.Get(idx));
+ b.FastFlip(idx);
+
+ bool val = b.FlipAndGet(idx);
+ bool val2 = b.FlipAndGet(idx);
+ Assert.IsTrue(val != val2);
+
+ val = b.GetAndSet(idx);
+ Assert.IsTrue(val2 == val);
+ Assert.IsTrue(b.Get(idx));
+
+ if (!val)
+ b.FastClear(idx);
+ Assert.IsTrue(b.Get(idx) == val);
+ }
+ }
+
+ // test that the various ways of accessing the bits are equivalent
+ DoGet(a, b);
+
+ // test ranges, including possible extension
+ int fromIndex, toIndex;
+ fromIndex = rand.Next(sz + 80);
+ toIndex = fromIndex + rand.Next((sz >> 1) + 1);
+ System.Collections.BitArray aa = (System.Collections.BitArray) a.Clone(); for (int j = fromIndex; j < toIndex; i++) aa.Set(j, !aa.Get(j));
+ OpenBitSet bb = (OpenBitSet) b.Clone(); bb.Flip(fromIndex, toIndex);
+
+ DoIterate(aa, bb, mode); // a problem here is from flip or doIterate
+
+ fromIndex = rand.Next(sz + 80);
+ toIndex = fromIndex + rand.Next((sz >> 1) + 1);
+ aa = (System.Collections.BitArray) a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
+ bb = (OpenBitSet) b.Clone(); bb.Clear(fromIndex, toIndex);
+
+ DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit
+
+ fromIndex = rand.Next(sz + 80);
+ toIndex = fromIndex + rand.Next((sz >> 1) + 1);
+ aa = (System.Collections.BitArray) a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, true);
+ bb = (OpenBitSet) b.Clone(); bb.Set(fromIndex, toIndex);
+
+ DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit
+
+
+ if (a0 != null)
+ {
+ Assert.AreEqual(a.Equals(a0), b.Equals(b0));
+
+ Assert.AreEqual(SupportClass.BitSetSupport.Cardinality(a), b.Cardinality());
+
+ System.Collections.BitArray a_and = (System.Collections.BitArray) a.Clone();
+ a_and.And(a0);
+ System.Collections.BitArray a_or = (System.Collections.BitArray) a.Clone();
+ a_or.Or(a0);
+ System.Collections.BitArray a_xor = (System.Collections.BitArray) a.Clone();
+ a_xor.Xor(a0);
+ System.Collections.BitArray a_andn = (System.Collections.BitArray) a.Clone(); a_andn.And(a0.Not());
+
+ OpenBitSet b_and = (OpenBitSet) b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0);
+ OpenBitSet b_or = (OpenBitSet) b.Clone(); b_or.Or(b0);
+ OpenBitSet b_xor = (OpenBitSet) b.Clone(); b_xor.Xor(b0);
+ OpenBitSet b_andn = (OpenBitSet) b.Clone(); b_andn.AndNot(b0);
+
+ DoIterate(a_and, b_and, mode);
+ DoIterate(a_or, b_or, mode);
+ DoIterate(a_xor, b_xor, mode);
+ DoIterate(a_andn, b_andn, mode);
+
+ Assert.AreEqual(SupportClass.BitSetSupport.Cardinality(a_and), b_and.Cardinality());
+ Assert.AreEqual(SupportClass.BitSetSupport.Cardinality(a_or), b_or.Cardinality());
+ Assert.AreEqual(SupportClass.BitSetSupport.Cardinality(a_xor), b_xor.Cardinality());
+ Assert.AreEqual(SupportClass.BitSetSupport.Cardinality(a_andn), b_andn.Cardinality());
+
+ // test non-mutating popcounts
+ Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0));
+ Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0));
+ Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0));
+ Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0));
+ }
+
+ a0 = a;
+ b0 = b;
+ }
+ }
+
+ // large enough to flush obvious bugs, small enough to run in <.5 sec as part of a
+ // larger testsuite.
+ [Test]
+ public virtual void TestSmall()
+ {
+ rand = NewRandom();
+ DoRandomSets(1200, 1000, 1);
+ DoRandomSets(1200, 1000, 2);
+ }
+
+ [Test]
+ public virtual void TestBig()
+ {
+ // uncomment to run a bigger test (~2 minutes).
+ // rand = newRandom();
+ // doRandomSets(2000,200000, 1);
+ // doRandomSets(2000,200000, 2);
+ }
+
+ [Test]
+ public virtual void TestEquals()
+ {
+ rand = NewRandom();
+ OpenBitSet b1 = new OpenBitSet(1111);
+ OpenBitSet b2 = new OpenBitSet(2222);
+ Assert.IsTrue(b1.Equals(b2));
+ Assert.IsTrue(b2.Equals(b1));
+ b1.Set(10);
+ Assert.IsFalse(b1.Equals(b2));
+ Assert.IsFalse(b2.Equals(b1));
+ b2.Set(10);
+ Assert.IsTrue(b1.Equals(b2));
+ Assert.IsTrue(b2.Equals(b1));
+ b2.Set(2221);
+ Assert.IsFalse(b1.Equals(b2));
+ Assert.IsFalse(b2.Equals(b1));
+ b1.Set(2221);
+ Assert.IsTrue(b1.Equals(b2));
+ Assert.IsTrue(b2.Equals(b1));
+
+ // try different type of object
+ Assert.IsFalse(b1.Equals(new System.Object()));
+ }
+
+ [Test]
+ public virtual void TestBitUtils()
+ {
+ rand = NewRandom();
+ long num = 100000;
+ Assert.AreEqual(5, BitUtil.Ntz(num));
+ Assert.AreEqual(5, BitUtil.Ntz2(num));
+ Assert.AreEqual(5, BitUtil.Ntz3(num));
+
+ num = 10;
+ Assert.AreEqual(1, BitUtil.Ntz(num));
+ Assert.AreEqual(1, BitUtil.Ntz2(num));
+ Assert.AreEqual(1, BitUtil.Ntz3(num));
+
+ for (int i = 0; i < 64; i++)
+ {
+ num = 1L << i;
+ Assert.AreEqual(i, BitUtil.Ntz(num));
+ Assert.AreEqual(i, BitUtil.Ntz2(num));
+ Assert.AreEqual(i, BitUtil.Ntz3(num));
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestPriorityQueue.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestPriorityQueue.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestPriorityQueue.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestPriorityQueue.cs Tue Nov 3 18:06:27 2009
@@ -1,4 +1,4 @@
-/*
+/*
* 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.
@@ -21,14 +21,15 @@
namespace Lucene.Net.Util
{
+
[TestFixture]
- public class TestPriorityQueue : LuceneTestCase
+ public class TestPriorityQueue:LuceneTestCase
{
- //public TestPriorityQueue(System.String name) : base(name)
- //{
- //}
+ public TestPriorityQueue(System.String name):base(name)
+ {
+ }
- private class IntegerQueue : PriorityQueue
+ private class IntegerQueue:PriorityQueue
{
public IntegerQueue(int count):base()
{
@@ -44,13 +45,12 @@
[Test]
public virtual void TestPQ()
{
- TestPQ(10000);
+ TestPQ(10000, NewRandom());
}
- public static void TestPQ(int count)
+ public static void TestPQ(int count, System.Random gen)
{
PriorityQueue pq = new IntegerQueue(count);
- System.Random gen = new System.Random();
int sum = 0, sum2 = 0;
for (int i = 0; i < count; i++)
@@ -125,8 +125,8 @@
Assert.IsNull(pq.InsertWithOverflow((System.Object) i2));
Assert.IsNull(pq.InsertWithOverflow((System.Object) i3));
Assert.IsNull(pq.InsertWithOverflow((System.Object) i4));
- Assert.IsTrue((int)pq.InsertWithOverflow((System.Object)i5) == i3); // i3 should have been dropped
- Assert.IsTrue((int)pq.InsertWithOverflow((System.Object)i6) == i6); // i6 should not have been inserted
+ Assert.IsTrue(pq.InsertWithOverflow((System.Object) i5) == (System.Object) i3); // i3 should have been dropped
+ Assert.IsTrue(pq.InsertWithOverflow((System.Object) i6) == (System.Object) i6); // i6 should not have been inserted
Assert.AreEqual(size, pq.Size());
Assert.AreEqual(2, ((System.Int32) pq.Top()));
}
Added: incubator/lucene.net/trunk/C#/src/Test/Util/TestRamUsageEstimator.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestRamUsageEstimator.cs?rev=832486&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestRamUsageEstimator.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestRamUsageEstimator.cs Tue Nov 3 18:06:27 2009
@@ -0,0 +1,68 @@
+/*
+ * 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 System;
+
+using NUnit.Framework;
+
+namespace Lucene.Net.Util
+{
+
+ [TestFixture]
+ public class TestRamUsageEstimator
+ {
+
+ [Test]
+ public virtual void TestBasic()
+ {
+ System.String string_Renamed = new System.Text.StringBuilder("test str").ToString();
+ RamUsageEstimator rue = new RamUsageEstimator();
+ long size = rue.EstimateRamUsage(string_Renamed);
+ System.Console.Out.WriteLine("size:" + size);
+
+ string_Renamed = new System.Text.StringBuilder("test strin").ToString();
+ size = rue.EstimateRamUsage(string_Renamed);
+ System.Console.Out.WriteLine("size:" + size);
+
+ Holder holder = new Holder();
+ holder.holder = new Holder("string2", 5000L);
+ size = rue.EstimateRamUsage(holder);
+ System.Console.Out.WriteLine("size:" + size);
+
+ System.String[] strings = new System.String[]{new System.Text.StringBuilder("test strin").ToString(), new System.Text.StringBuilder("hollow").ToString(), new System.Text.StringBuilder("catchmaster").ToString()};
+ size = rue.EstimateRamUsage(strings);
+ System.Console.Out.WriteLine("size:" + size);
+ }
+
+ private sealed class Holder
+ {
+ internal long field1 = 5000L;
+ internal System.String name = "name";
+ internal Holder holder;
+
+ internal Holder()
+ {
+ }
+
+ internal Holder(System.String name, long field1)
+ {
+ this.name = name;
+ this.field1 = field1;
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestSmallFloat.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestSmallFloat.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestSmallFloat.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestSmallFloat.cs Tue Nov 3 18:06:27 2009
@@ -1,4 +1,4 @@
-/*
+/*
* 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.
@@ -25,7 +25,7 @@
/// <version> $Id$
/// </version>
[TestFixture]
- public class TestSmallFloat : LuceneTestCase
+ public class TestSmallFloat:LuceneTestCase
{
// original lucene byteToFloat
@@ -37,7 +37,7 @@
int mantissa = b & 7;
int exponent = (b >> 3) & 31;
int bits = ((exponent + (63 - 15)) << 24) | (mantissa << 21);
- return (BitConverter.ToSingle(BitConverter.GetBytes(bits), 0));
+ return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);
}
// original lucene floatToByte
@@ -51,7 +51,7 @@
// zero is a special case
return 0;
- int bits = BitConverter.ToInt32(BitConverter.GetBytes(f), 0); // parse float into parts
+ int bits = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
int mantissa = (bits & 0xffffff) >> 21;
int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
@@ -92,7 +92,7 @@
[Test]
public virtual void TestFloatToByte()
{
- System.Random rand = new System.Random((System.Int32) 0);
+ System.Random rand = NewRandom();
// up iterations for more exhaustive test after changing something
for (int i = 0; i < 100000; i++)
{
@@ -122,7 +122,7 @@
/// byte b1 = orig_floatToByte(f);
/// byte b2 = SmallFloat.floatToByte315(f);
/// if (b1!=b2) {
- /// TestCase.Fail("Failed floatToByte315 for float " + f);
+ /// TestCase.fail("Failed floatToByte315 for float " + f);
/// }
/// }
/// if (i==Integer.MAX_VALUE) break;
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestSortedVIntList.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestSortedVIntList.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestSortedVIntList.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestSortedVIntList.cs Tue Nov 3 18:06:27 2009
@@ -1,13 +1,13 @@
-/**
+/*
* 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
- *
+ *
+ * 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.
@@ -15,241 +15,243 @@
* limitations under the License.
*/
-using NUnit.Framework;
+using System;
-using BitArray = System.Collections.BitArray;
+using NUnit.Framework;
using DocIdSetIterator = Lucene.Net.Search.DocIdSetIterator;
namespace Lucene.Net.Util
{
- [TestFixture]
- public class TestSortedVIntList
- {
-
- void tstIterator(
- SortedVIntList vintList,
- int[] ints)
- {
- for (int i = 0; i < ints.Length; i++)
- {
- if ((i > 0) && (ints[i - 1] == ints[i]))
- {
- return; // DocNrSkipper should not skip to same document.
- }
- }
- DocIdSetIterator m = vintList.Iterator();
- for (int i = 0; i < ints.Length; i++)
- {
- Assert.IsTrue(m.Next(), "No end of Matcher at: " + i);
- Assert.AreEqual(ints[i], m.Doc());
- }
- Assert.IsTrue((!m.Next()), "End of Matcher");
- }
-
- void tstVIntList(
- SortedVIntList vintList,
- int[] ints,
- int expectedByteSize)
- {
- Assert.AreEqual(ints.Length, vintList.Size(), "Size");
- Assert.AreEqual(expectedByteSize, vintList.GetByteSize(), "Byte size");
- tstIterator(vintList, ints);
- }
-
- public void tstViaBitSet(int[] ints, int expectedByteSize)
- {
- int MAX_INT_FOR_BITSET = 1024 * 1024;
- SupportClass.CollectionsSupport.BitSet bs = new SupportClass.CollectionsSupport.BitSet(ints.Length);
- //BitArray bs = new BitArray(ints.Length);
- for (int i = 0; i < ints.Length; i++)
- {
- if (ints[i] > MAX_INT_FOR_BITSET)
- {
- return; // BitArray takes too much memory
- }
- if ((i > 0) && (ints[i - 1] == ints[i]))
- {
- return; // BitArray cannot store duplicate.
- }
- bs.Set(ints[i]);
- }
- SortedVIntList svil = new SortedVIntList(bs);
- tstVIntList(svil, ints, expectedByteSize);
- tstVIntList(new SortedVIntList(svil.Iterator()), ints, expectedByteSize);
- }
-
- private static int VB1 = 0x7F;
- private static int BIT_SHIFT = 7;
- private static int VB2 = (VB1 << BIT_SHIFT) | VB1;
- private static int VB3 = (VB2 << BIT_SHIFT) | VB1;
- private static int VB4 = (VB3 << BIT_SHIFT) | VB1;
-
- private int vIntByteSize(int i)
- {
- System.Diagnostics.Debug.Assert(i >= 0);
- if (i <= VB1) return 1;
- if (i <= VB2) return 2;
- if (i <= VB3) return 3;
- if (i <= VB4) return 4;
- return 5;
- }
-
- private int vIntListByteSize(int[] ints)
- {
- int byteSize = 0;
- int last = 0;
- for (int i = 0; i < ints.Length; i++)
- {
- byteSize += vIntByteSize(ints[i] - last);
- last = ints[i];
- }
- return byteSize;
- }
-
- public void tstInts(int[] ints)
- {
- int expectedByteSize = vIntListByteSize(ints);
- try
- {
- tstVIntList(new SortedVIntList(ints), ints, expectedByteSize);
- tstViaBitSet(ints, expectedByteSize);
- }
- catch (System.IO.IOException ioe)
- {
- throw new System.Exception(null, ioe);
- }
- }
-
- public void tstIllegalArgExc(int[] ints)
- {
- try
- {
- new SortedVIntList(ints);
- }
- catch (System.ArgumentException)
- {
- return;
- }
- Assert.Fail("Expected ArgumentException");
- }
-
- private int[] fibArray(int a, int b, int size)
- {
- int[] fib = new int[size];
- fib[0] = a;
- fib[1] = b;
- for (int i = 2; i < size; i++)
- {
- fib[i] = fib[i - 1] + fib[i - 2];
- }
- return fib;
- }
-
- private int[] reverseDiffs(int[] ints)
- { // reverse the order of the successive differences
- int[] res = new int[ints.Length];
- for (int i = 0; i < ints.Length; i++)
- {
- res[i] = ints[ints.Length - 1] + (ints[0] - ints[ints.Length - 1 - i]);
- }
- return res;
- }
-
- [Test]
- public void Test01()
- {
- tstInts(new int[] { });
- }
- [Test]
- public void Test02()
- {
- tstInts(new int[] { 0 });
- }
- [Test]
- public void Test03()
- {
- tstInts(new int[] { 0, int.MaxValue });
- }
- [Test]
- public void Test04a()
- {
- tstInts(new int[] { 0, VB2 - 1 });
- }
- [Test]
- public void Test04b()
- {
- tstInts(new int[] { 0, VB2 });
- }
- [Test]
- public void Test04c()
- {
- tstInts(new int[] { 0, VB2 + 1 });
- }
- [Test]
- public void Test05()
- {
- tstInts(fibArray(0, 1, 7)); // includes duplicate value 1
- }
- [Test]
- public void Test05b()
- {
- tstInts(reverseDiffs(fibArray(0, 1, 7)));
- }
- [Test]
- public void Test06()
- {
- tstInts(fibArray(1, 2, 45)); // no duplicates, size 46 exceeds max int.
- }
- [Test]
- public void Test06b()
- {
- tstInts(reverseDiffs(fibArray(1, 2, 45)));
- }
- [Test]
- public void Test07a()
- {
- tstInts(new int[] { 0, VB3 });
- }
- [Test]
- public void Test07b()
- {
- tstInts(new int[] { 1, VB3 + 2 });
- }
- [Test]
- public void Test07c()
- {
- tstInts(new int[] { 2, VB3 + 4 });
- }
- [Test]
- public void Test08a()
- {
- tstInts(new int[] { 0, VB4 + 1 });
- }
- [Test]
- public void Test08b()
- {
- tstInts(new int[] { 1, VB4 + 1 });
- }
- [Test]
- public void Test08c()
- {
- tstInts(new int[] { 2, VB4 + 1 });
- }
- [Test]
- public void Test10()
- {
- tstIllegalArgExc(new int[] { -1 });
- }
- [Test]
- public void Test11()
- {
- tstIllegalArgExc(new int[] { 1, 0 });
- }
- [Test]
- public void Test12()
- {
- tstIllegalArgExc(new int[] { 0, 1, 1, 2, 3, 5, 8, 0 });
- }
- }
-}
+
+ [TestFixture]
+ public class TestSortedVIntList:LuceneTestCase
+ {
+ /// <summary>Main for running test case by itself. </summary>
+ [STAThread]
+ public static void Main(System.String[] args)
+ {
+ // TestRunner.run(new TestSuite(typeof(TestSortedVIntList))); // {{Aroush-2.9}} how is this done in NUnit?
+ }
+
+ internal virtual void TstIterator(SortedVIntList vintList, int[] ints)
+ {
+ for (int i = 0; i < ints.Length; i++)
+ {
+ if ((i > 0) && (ints[i - 1] == ints[i]))
+ {
+ return ; // DocNrSkipper should not skip to same document.
+ }
+ }
+ DocIdSetIterator m = vintList.Iterator();
+ for (int i = 0; i < ints.Length; i++)
+ {
+ Assert.IsTrue(m.NextDoc() != DocIdSetIterator.NO_MORE_DOCS, "No end of Matcher at: " + i);
+ Assert.AreEqual(ints[i], m.DocID());
+ }
+ Assert.IsTrue(m.NextDoc() == DocIdSetIterator.NO_MORE_DOCS, "End of Matcher");
+ }
+
+ internal virtual void TstVIntList(SortedVIntList vintList, int[] ints, int expectedByteSize)
+ {
+ Assert.AreEqual(ints.Length, vintList.Size(), "Size");
+ Assert.AreEqual(expectedByteSize, vintList.GetByteSize(), "Byte size");
+ TstIterator(vintList, ints);
+ }
+
+ public virtual void TstViaBitSet(int[] ints, int expectedByteSize)
+ {
+ int MAX_INT_FOR_BITSET = 1024 * 1024;
+ System.Collections.BitArray bs = new System.Collections.BitArray(64);
+ for (int i = 0; i < ints.Length; i++)
+ {
+ if (ints[i] > MAX_INT_FOR_BITSET)
+ {
+ return ; // BitSet takes too much memory
+ }
+ if ((i > 0) && (ints[i - 1] == ints[i]))
+ {
+ return ; // BitSet cannot store duplicate.
+ }
+ bs.Set(ints[i], true);
+ }
+ SortedVIntList svil = new SortedVIntList(bs);
+ TstVIntList(svil, ints, expectedByteSize);
+ TstVIntList(new SortedVIntList(svil.Iterator()), ints, expectedByteSize);
+ }
+
+ private const int VB1 = 0x7F;
+ private const int BIT_SHIFT = 7;
+ private static readonly int VB2 = (VB1 << BIT_SHIFT) | VB1;
+ private static readonly int VB3 = (VB2 << BIT_SHIFT) | VB1;
+ private static readonly int VB4 = (VB3 << BIT_SHIFT) | VB1;
+
+ private int VIntByteSize(int i)
+ {
+ System.Diagnostics.Debug.Assert(i >= 0);
+ if (i <= VB1)
+ return 1;
+ if (i <= VB2)
+ return 2;
+ if (i <= VB3)
+ return 3;
+ if (i <= VB4)
+ return 4;
+ return 5;
+ }
+
+ private int VIntListByteSize(int[] ints)
+ {
+ int byteSize = 0;
+ int last = 0;
+ for (int i = 0; i < ints.Length; i++)
+ {
+ byteSize += VIntByteSize(ints[i] - last);
+ last = ints[i];
+ }
+ return byteSize;
+ }
+
+ public virtual void TstInts(int[] ints)
+ {
+ int expectedByteSize = VIntListByteSize(ints);
+ try
+ {
+ TstVIntList(new SortedVIntList(ints), ints, expectedByteSize);
+ TstViaBitSet(ints, expectedByteSize);
+ }
+ catch (System.IO.IOException ioe)
+ {
+ throw new System.SystemException("", ioe);
+ }
+ }
+
+ public virtual void TstIllegalArgExc(int[] ints)
+ {
+ try
+ {
+ new SortedVIntList(ints);
+ }
+ catch (System.ArgumentException e)
+ {
+ return ;
+ }
+ Assert.Fail("Expected IllegalArgumentException");
+ }
+
+ private int[] FibArray(int a, int b, int size)
+ {
+ int[] fib = new int[size];
+ fib[0] = a;
+ fib[1] = b;
+ for (int i = 2; i < size; i++)
+ {
+ fib[i] = fib[i - 1] + fib[i - 2];
+ }
+ return fib;
+ }
+
+ private int[] ReverseDiffs(int[] ints)
+ {
+ // reverse the order of the successive differences
+ int[] res = new int[ints.Length];
+ for (int i = 0; i < ints.Length; i++)
+ {
+ res[i] = ints[ints.Length - 1] + (ints[0] - ints[ints.Length - 1 - i]);
+ }
+ return res;
+ }
+
+ [Test]
+ public virtual void Test01()
+ {
+ TstInts(new int[]{});
+ }
+ [Test]
+ public virtual void Test02()
+ {
+ TstInts(new int[]{0});
+ }
+ [Test]
+ public virtual void Test04a()
+ {
+ TstInts(new int[]{0, VB2 - 1});
+ }
+ [Test]
+ public virtual void Test04b()
+ {
+ TstInts(new int[]{0, VB2});
+ }
+ [Test]
+ public virtual void Test04c()
+ {
+ TstInts(new int[]{0, VB2 + 1});
+ }
+ [Test]
+ public virtual void Test05()
+ {
+ TstInts(FibArray(0, 1, 7)); // includes duplicate value 1
+ }
+ [Test]
+ public virtual void Test05b()
+ {
+ TstInts(ReverseDiffs(FibArray(0, 1, 7)));
+ }
+ [Test]
+ public virtual void Test06()
+ {
+ TstInts(FibArray(1, 2, 45)); // no duplicates, size 46 exceeds max int.
+ }
+ [Test]
+ public virtual void Test06b()
+ {
+ TstInts(ReverseDiffs(FibArray(1, 2, 45)));
+ }
+ [Test]
+ public virtual void Test07a()
+ {
+ TstInts(new int[]{0, VB3});
+ }
+ [Test]
+ public virtual void Test07b()
+ {
+ TstInts(new int[]{1, VB3 + 2});
+ }
+ [Test]
+ public virtual void Test07c()
+ {
+ TstInts(new int[]{2, VB3 + 4});
+ }
+ [Test]
+ public virtual void Test08a()
+ {
+ TstInts(new int[]{0, VB4 + 1});
+ }
+ [Test]
+ public virtual void Test08b()
+ {
+ TstInts(new int[]{1, VB4 + 1});
+ }
+ [Test]
+ public virtual void Test08c()
+ {
+ TstInts(new int[]{2, VB4 + 1});
+ }
+
+ [Test]
+ public virtual void Test10()
+ {
+ TstIllegalArgExc(new int[]{- 1});
+ }
+ [Test]
+ public virtual void Test11()
+ {
+ TstIllegalArgExc(new int[]{1, 0});
+ }
+ [Test]
+ public virtual void Test12()
+ {
+ TstIllegalArgExc(new int[]{0, 1, 1, 2, 3, 5, 8, 0});
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestStringHelper.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestStringHelper.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestStringHelper.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestStringHelper.cs Tue Nov 3 18:06:27 2009
@@ -1,4 +1,4 @@
-/*
+/*
* 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.
@@ -21,10 +21,12 @@
namespace Lucene.Net.Util
{
+
[TestFixture]
- public class TestStringHelper : LuceneTestCase
+ public class TestStringHelper:LuceneTestCase
{
+
[Test]
public virtual void TestStringDifference()
{
Added: incubator/lucene.net/trunk/C#/src/Test/Util/TestStringIntern.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestStringIntern.cs?rev=832486&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestStringIntern.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestStringIntern.cs Tue Nov 3 18:06:27 2009
@@ -0,0 +1,143 @@
+/*
+ * 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 System;
+
+using NUnit.Framework;
+
+namespace Lucene.Net.Util
+{
+
+ [TestFixture]
+ public class TestStringIntern:LuceneTestCase
+ {
+ public TestStringIntern()
+ {
+ InitBlock();
+ }
+ private class AnonymousClassThread:SupportClass.ThreadClass
+ {
+ public AnonymousClassThread(System.Int32 seed, int iter, bool newStrings, TestStringIntern enclosingInstance)
+ {
+ InitBlock(seed, iter, newStrings, enclosingInstance);
+ }
+ private void InitBlock(System.Int32 seed, int iter, bool newStrings, TestStringIntern enclosingInstance)
+ {
+ this.seed = seed;
+ this.iter = iter;
+ this.newStrings = newStrings;
+ this.enclosingInstance = enclosingInstance;
+ }
+ private System.Int32 seed;
+ private int iter;
+ private bool newStrings;
+ private TestStringIntern enclosingInstance;
+ public TestStringIntern Enclosing_Instance
+ {
+ get
+ {
+ return enclosingInstance;
+ }
+
+ }
+ override public void Run()
+ {
+ System.Random rand = new Random(seed);
+ System.String[] myInterned = new System.String[Enclosing_Instance.testStrings.Length];
+ for (int j = 0; j < iter; j++)
+ {
+ int idx = rand.Next(Enclosing_Instance.testStrings.Length);
+ System.String s = Enclosing_Instance.testStrings[idx];
+ if (newStrings == true && (new System.Random().NextDouble()) > 0.5)
+ s = new System.Text.StringBuilder(s).ToString(); // make a copy half of the time
+ System.String interned = StringHelper.Intern(s);
+ System.String prevInterned = myInterned[idx];
+ System.String otherInterned = Enclosing_Instance.internedStrings[idx];
+
+ // test against other threads
+ if (otherInterned != null && (System.Object) otherInterned != (System.Object) interned)
+ {
+ Assert.Fail(); // TestCase.fail();
+ }
+ Enclosing_Instance.internedStrings[idx] = interned;
+
+ // test against local copy
+ if (prevInterned != null && (System.Object) prevInterned != (System.Object) interned)
+ {
+ Assert.Fail(); // TestCase.fail();
+ }
+ myInterned[idx] = interned;
+ }
+ }
+ }
+ private void InitBlock()
+ {
+ r = NewRandom();
+ }
+ internal System.String[] testStrings;
+ internal System.String[] internedStrings;
+ internal System.Random r;
+
+ private System.String RandStr(int len)
+ {
+ char[] arr = new char[len];
+ for (int i = 0; i < len; i++)
+ {
+ arr[i] = (char) ('a' + r.Next(26));
+ }
+ return new System.String(arr);
+ }
+
+ private void MakeStrings(int sz)
+ {
+ testStrings = new System.String[sz];
+ internedStrings = new System.String[sz];
+ for (int i = 0; i < sz; i++)
+ {
+ testStrings[i] = RandStr(r.Next(8) + 3);
+ }
+ }
+
+ [Test]
+ public virtual void TestStringIntern_Renamed()
+ {
+ MakeStrings(1024 * 10); // something greater than the capacity of the default cache size
+ // makeStrings(100); // realistic for perf testing
+ int nThreads = 20;
+ // final int iter=100000;
+ int iter = 1000000;
+ bool newStrings = true;
+
+ // try native intern
+ // StringHelper.interner = new StringInterner();
+
+ SupportClass.ThreadClass[] threads = new SupportClass.ThreadClass[nThreads];
+ for (int i = 0; i < nThreads; i++)
+ {
+ int seed = i;
+ threads[i] = new AnonymousClassThread(seed, iter, newStrings, this);
+
+ threads[i].Start();
+ }
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ threads[i].Join();
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Util/_TestUtil.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/_TestUtil.cs?rev=832486&r1=832485&r2=832486&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/_TestUtil.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/_TestUtil.cs Tue Nov 3 18:06:27 2009
@@ -1,4 +1,4 @@
-/*
+/*
* 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.
@@ -17,6 +17,8 @@
using System;
+using NUnit.Framework;
+
using CheckIndex = Lucene.Net.Index.CheckIndex;
using ConcurrentMergeScheduler = Lucene.Net.Index.ConcurrentMergeScheduler;
using IndexWriter = Lucene.Net.Index.IndexWriter;
@@ -29,6 +31,17 @@
public class _TestUtil
{
+ /// <summary>Returns temp dir, containing String arg in its name;
+ /// does not create the directory.
+ /// </summary>
+ public static System.IO.FileInfo GetTempDir(System.String desc)
+ {
+ System.String tempDir = System.IO.Path.GetTempPath();
+ if (tempDir == null)
+ throw new System.SystemException("java.io.tmpdir undefined, cannot run test");
+ return new System.IO.FileInfo(System.IO.Path.Combine(tempDir, desc + "." + (new System.Random()).Next(System.Int32.MaxValue)));
+ }
+
public static void RmDir(System.IO.FileInfo dir)
{
bool tmpBool;
@@ -91,28 +104,76 @@
if (ms is ConcurrentMergeScheduler)
((ConcurrentMergeScheduler) ms).Sync();
}
-
- /// <summary>
- /// This runs the CheckIndex tool on the index in dir.
- /// If any issues are hit, a System.Exception is thrown; else,
- /// true is returned.
- /// </summary>
- /// <param name="dir"></param>
- /// <returns></returns>
- public static bool CheckIndex(Directory dir)
- {
- System.IO.StringWriter sw = new System.IO.StringWriter();
- CheckIndex checker = new CheckIndex(dir);
- checker.SetInfoStream(sw);
- CheckIndex.Status indexStatus = checker.CheckIndex_Renamed();
- if (indexStatus == null || !indexStatus.clean)
- {
- System.Console.WriteLine("CheckIndex failed");
- System.Console.WriteLine(sw.ToString());
- throw new System.Exception("CheckIndex failed");
- }
- else
- return true;
- }
+
+ /// <summary>This runs the CheckIndex tool on the index in. If any
+ /// issues are hit, a RuntimeException is thrown; else,
+ /// true is returned.
+ /// </summary>
+ public static bool CheckIndex(Directory dir)
+ {
+ System.IO.MemoryStream bos = new System.IO.MemoryStream(1024);
+
+ CheckIndex checker = new CheckIndex(dir);
+ checker.SetInfoStream(new System.IO.StreamWriter(bos));
+ CheckIndex.Status indexStatus = checker.CheckIndex_Renamed_Method();
+ if (indexStatus == null || indexStatus.clean == false)
+ {
+ System.Console.Out.WriteLine("CheckIndex failed");
+ char[] tmpChar;
+ byte[] tmpByte;
+ tmpByte = bos.GetBuffer();
+ tmpChar = new char[bos.Length];
+ System.Array.Copy(tmpByte, 0, tmpChar, 0, tmpChar.Length);
+ System.Console.Out.WriteLine(new System.String(tmpChar));
+ throw new System.SystemException("CheckIndex failed");
+ }
+ else
+ return true;
+ }
+
+ /// <summary>Use only for testing.</summary>
+ /// <deprecated> -- in 3.0 we can use Arrays.toString
+ /// instead
+ /// </deprecated>
+ public static System.String ArrayToString(int[] array)
+ {
+ System.Text.StringBuilder buf = new System.Text.StringBuilder();
+ buf.Append("[");
+ for (int i = 0; i < array.Length; i++)
+ {
+ if (i > 0)
+ {
+ buf.Append(" ");
+ }
+ buf.Append(array[i]);
+ }
+ buf.Append("]");
+ return buf.ToString();
+ }
+
+ /// <summary>Use only for testing.</summary>
+ /// <deprecated> -- in 3.0 we can use Arrays.toString
+ /// instead
+ /// </deprecated>
+ public static System.String ArrayToString(System.Object[] array)
+ {
+ System.Text.StringBuilder buf = new System.Text.StringBuilder();
+ buf.Append("[");
+ for (int i = 0; i < array.Length; i++)
+ {
+ if (i > 0)
+ {
+ buf.Append(" ");
+ }
+ buf.Append(array[i]);
+ }
+ buf.Append("]");
+ return buf.ToString();
+ }
+
+ public static int GetRandomSocketPort()
+ {
+ return 1024 + new System.Random().Next(64512);
+ }
}
}
\ No newline at end of file