You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/06/24 18:11:56 UTC

[lucenenet] 02/26: addresses #295 optimised some Assert calls using long, long. NUnit seems to not treat these as numerics or at least elongates test times.

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit a49ff752e14c59d6fa4a28aa647cf7ab33966e2f
Author: Michael <mi...@hotmail.com>
AuthorDate: Tue Jun 23 13:04:51 2020 +0700

    addresses #295 optimised some Assert calls using long, long. NUnit seems to not treat these as numerics or at least elongates test times.
    
    Did the same for byte comparisons.
---
 .../Support/TestFramework/Assert.cs                | 64 +++++++++++++++++++++-
 .../Support/TestFramework/SeedAttribute.cs         | 54 ++++++++++++++++++
 src/Lucene.Net.Tests/Util/Fst/TestBytesStore.cs    |  2 +
 src/Lucene.Net.Tests/Util/Fst/TestFSTs.cs          |  2 +
 .../Util/Packed/TestEliasFanoSequence.cs           |  2 +
 src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs |  2 +
 src/Lucene.Net.Tests/Util/TestBroadWord.cs         |  1 +
 src/Lucene.Net.Tests/Util/TestByteBlockPool.cs     |  1 +
 src/Lucene.Net.Tests/Util/TestIdentityHashSet.cs   |  2 +
 src/Lucene.Net.Tests/Util/TestNumericUtils.cs      |  3 +-
 src/Lucene.Net.Tests/Util/TestOpenBitSet.cs        |  1 +
 src/Lucene.Net.Tests/Util/TestPagedBytes.cs        |  9 ++-
 src/Lucene.Net.Tests/Util/TestWAH8DocIdSet.cs      |  3 +
 src/Lucene.Net/Util/Packed/EliasFanoDecoder.cs     |  2 +-
 14 files changed, 140 insertions(+), 8 deletions(-)

diff --git a/src/Lucene.Net.TestFramework.NUnit/Support/TestFramework/Assert.cs b/src/Lucene.Net.TestFramework.NUnit/Support/TestFramework/Assert.cs
index 428e476..2c0af47 100644
--- a/src/Lucene.Net.TestFramework.NUnit/Support/TestFramework/Assert.cs
+++ b/src/Lucene.Net.TestFramework.NUnit/Support/TestFramework/Assert.cs
@@ -4,6 +4,7 @@ using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
+using NUnit.Framework.Constraints;
 using _NUnit = NUnit.Framework;
 
 namespace Lucene.Net.TestFramework
@@ -163,6 +164,63 @@ namespace Lucene.Net.TestFramework
             _NUnit.Assert.AreEqual(expected, actual, delta);
         }
 
+        //
+        // Summary:
+        //     Verifies that two objects are equal. Two objects are considered equal if both
+        //     are null, or if both have the same value. NUnit has special semantics for some
+        //     object types. If they are not equal an NUnit.Framework.AssertionException is
+        //     thrown.
+        //
+        // Parameters:
+        //   expected:
+        //     The value that is expected
+        //
+        //   actual:
+        //     The actual value
+        public static void AreEqual(long expected, long actual)
+        {
+            _NUnit.Assert.True(expected == actual);
+        }
+
+        //
+        // Summary:
+        //     Verifies that two objects are equal. Two objects are considered equal if both
+        //     are null, or if both have the same value. NUnit has special semantics for some
+        //     object types. If they are not equal an NUnit.Framework.AssertionException is
+        //     thrown.
+        //
+        // Parameters:
+        //   expected:
+        //     The value that is expected
+        //
+        //   message:
+        //     The message to display in case of failure
+        //
+        //   actual:
+        //     The actual value
+        public static void AreEqual(long expected, long actual, string message)
+        {
+            _NUnit.Assert.True(expected == actual, message);
+        }
+
+        //
+        // Summary:
+        //     Verifies that two objects are equal. Two objects are considered equal if both
+        //     are null, or if both have the same value. NUnit has special semantics for some
+        //     object types. If they are not equal an NUnit.Framework.AssertionException is
+        //     thrown.
+        //
+        // Parameters:
+        //   expected:
+        //     The value that is expected
+        //
+        //   actual:
+        //     The actual value
+        public static void AreEqual(byte expected, byte actual)
+        {
+            _NUnit.Assert.True(expected == actual);
+        }
+
         // From CollectionAssert
         public static void AreEqual<T>(T[] expected, T[] actual)
         {
@@ -362,7 +420,7 @@ namespace Lucene.Net.TestFramework
         //     The evaluated condition
         public static void False(bool condition)
         {
-            _NUnit.Assert.False(condition);
+            _NUnit.Assert.That(condition, _NUnit.Is.False);
         }
 
         //
@@ -375,7 +433,7 @@ namespace Lucene.Net.TestFramework
         //     The evaluated condition
         public static void IsFalse(bool condition)
         {
-            _NUnit.Assert.IsFalse(condition);
+            _NUnit.Assert.That(condition, _NUnit.Is.False);
         }
 
         //
@@ -394,7 +452,7 @@ namespace Lucene.Net.TestFramework
         //     Array of objects to be used in formatting the message
         public static void IsFalse(bool condition, string message, params object[] args)
         {
-            _NUnit.Assert.IsFalse(condition, message, args);
+            _NUnit.Assert.That(condition, _NUnit.Is.False, message, args);
         }
 
         //
diff --git a/src/Lucene.Net.TestFramework/Support/TestFramework/SeedAttribute.cs b/src/Lucene.Net.TestFramework/Support/TestFramework/SeedAttribute.cs
new file mode 100644
index 0000000..5cdea8a
--- /dev/null
+++ b/src/Lucene.Net.TestFramework/Support/TestFramework/SeedAttribute.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Reflection;
+using NUnit.Framework.Interfaces;
+using NUnit.Framework.Internal;
+using NUnit.Framework.Internal.Commands;
+
+[AttributeUsage(AttributeTargets.Method)]
+public sealed class SeedAttribute : Attribute, IWrapSetUpTearDown
+{
+    public SeedAttribute(int randomSeed)
+    {
+        RandomSeed = randomSeed;
+    }
+
+    public int RandomSeed { get; }
+
+    public TestCommand Wrap(TestCommand command)
+    {
+        return new SeedCommand(command, RandomSeed);
+    }
+
+    private sealed class SeedCommand : DelegatingTestCommand
+    {
+        private readonly int randomSeed;
+
+        public SeedCommand(TestCommand innerCommand, int randomSeed) : base(innerCommand)
+        {
+            this.randomSeed = randomSeed;
+        }
+
+        public override TestResult Execute(TestExecutionContext context)
+        {
+            ResetRandomSeed(context, randomSeed);
+            try
+            {
+                return innerCommand.Execute(context);
+            }
+            finally
+            {
+                if (context.CurrentTest.Seed != randomSeed)
+                    throw new InvalidOperationException($"{nameof(SeedAttribute)} cannot be used together with an attribute or test that changes the seed.");
+            }
+        }
+    }
+
+    private static void ResetRandomSeed(TestExecutionContext context, int seed)
+    {
+        context.CurrentTest.Seed = seed;
+
+        typeof(TestExecutionContext)
+            .GetField("_randomGenerator", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
+            .SetValue(context, null);
+    }
+}
\ No newline at end of file
diff --git a/src/Lucene.Net.Tests/Util/Fst/TestBytesStore.cs b/src/Lucene.Net.Tests/Util/Fst/TestBytesStore.cs
index 7ab90b1..3c54f7c 100644
--- a/src/Lucene.Net.Tests/Util/Fst/TestBytesStore.cs
+++ b/src/Lucene.Net.Tests/Util/Fst/TestBytesStore.cs
@@ -2,6 +2,7 @@ using System;
 using Lucene.Net.Randomized.Generators;
 using NUnit.Framework;
 using Console = Lucene.Net.Util.SystemConsole;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util.Fst
 {
@@ -34,6 +35,7 @@ namespace Lucene.Net.Util.Fst
     {
 
         [Test, LongRunningTest]
+        [Seed(1249648971)]
         public virtual void TestRandom()
         {
 
diff --git a/src/Lucene.Net.Tests/Util/Fst/TestFSTs.cs b/src/Lucene.Net.Tests/Util/Fst/TestFSTs.cs
index 56bfe33..dbce10d 100644
--- a/src/Lucene.Net.Tests/Util/Fst/TestFSTs.cs
+++ b/src/Lucene.Net.Tests/Util/Fst/TestFSTs.cs
@@ -14,6 +14,7 @@ using System.Text;
 using JCG = J2N.Collections.Generic;
 using Console = Lucene.Net.Util.SystemConsole;
 using J2N.Collections.Generic.Extensions;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util.Fst
 {
@@ -272,6 +273,7 @@ namespace Lucene.Net.Util.Fst
 
 
         [Test, LongRunningTest] // Can take up to 15 minutes
+        [Seed(1249648971)]
         public virtual void TestRandomWords()
         {
             // LUCENENET specific: NUnit will crash with an OOM if we do the full test
diff --git a/src/Lucene.Net.Tests/Util/Packed/TestEliasFanoSequence.cs b/src/Lucene.Net.Tests/Util/Packed/TestEliasFanoSequence.cs
index 8bbd490..20bad81 100644
--- a/src/Lucene.Net.Tests/Util/Packed/TestEliasFanoSequence.cs
+++ b/src/Lucene.Net.Tests/Util/Packed/TestEliasFanoSequence.cs
@@ -2,6 +2,8 @@ using Lucene.Net.Attributes;
 using NUnit.Framework;
 using System;
 using System.Diagnostics;
+using Assert = Lucene.Net.TestFramework.Assert;
+
 
 namespace Lucene.Net.Util.Packed
 {
diff --git a/src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs b/src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs
index b3b6ad0..5975a0b 100644
--- a/src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs
+++ b/src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs
@@ -38,6 +38,7 @@ namespace Lucene.Net.Util.Packed
     using RAMDirectory = Lucene.Net.Store.RAMDirectory;
     //using Slow = Lucene.Net.Util.LuceneTestCase.Slow;
     using Reader = Lucene.Net.Util.Packed.PackedInt32s.Reader;
+    using Assert = Lucene.Net.TestFramework.Assert;
     using Attributes;
 
     [TestFixture]
@@ -1127,6 +1128,7 @@ namespace Lucene.Net.Util.Packed
 
 
         [Test, LongRunningTest]
+        [Seed(1249648971)]
         public virtual void TestAppendingLongBuffer()
         {
 
diff --git a/src/Lucene.Net.Tests/Util/TestBroadWord.cs b/src/Lucene.Net.Tests/Util/TestBroadWord.cs
index 1bd519d..b1ed9a2 100644
--- a/src/Lucene.Net.Tests/Util/TestBroadWord.cs
+++ b/src/Lucene.Net.Tests/Util/TestBroadWord.cs
@@ -2,6 +2,7 @@ using J2N.Numerics;
 using Lucene.Net.Attributes;
 using Lucene.Net.Support;
 using NUnit.Framework;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
diff --git a/src/Lucene.Net.Tests/Util/TestByteBlockPool.cs b/src/Lucene.Net.Tests/Util/TestByteBlockPool.cs
index a47c35c..b5e30e9 100644
--- a/src/Lucene.Net.Tests/Util/TestByteBlockPool.cs
+++ b/src/Lucene.Net.Tests/Util/TestByteBlockPool.cs
@@ -1,6 +1,7 @@
 using Lucene.Net.Randomized.Generators;
 using NUnit.Framework;
 using System.Collections.Generic;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
diff --git a/src/Lucene.Net.Tests/Util/TestIdentityHashSet.cs b/src/Lucene.Net.Tests/Util/TestIdentityHashSet.cs
index db62dff..6bccb73 100644
--- a/src/Lucene.Net.Tests/Util/TestIdentityHashSet.cs
+++ b/src/Lucene.Net.Tests/Util/TestIdentityHashSet.cs
@@ -3,6 +3,7 @@ using NUnit.Framework;
 using System;
 using System.Collections.Generic;
 using JCG = J2N.Collections.Generic;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
@@ -28,6 +29,7 @@ namespace Lucene.Net.Util
     {
 
         [Test]
+        [Seed(1249648971)]
         public virtual void TestCheck()
         {
             Random rnd = Random;
diff --git a/src/Lucene.Net.Tests/Util/TestNumericUtils.cs b/src/Lucene.Net.Tests/Util/TestNumericUtils.cs
index e6c8f7c..0995959 100644
--- a/src/Lucene.Net.Tests/Util/TestNumericUtils.cs
+++ b/src/Lucene.Net.Tests/Util/TestNumericUtils.cs
@@ -3,6 +3,7 @@ using Lucene.Net.Attributes;
 using NUnit.Framework;
 using System;
 using System.Collections.Generic;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
@@ -358,8 +359,8 @@ namespace Lucene.Net.Util
             AssertLongRangeSplit(long.MinValue, long.MinValue + 0xfL, 4, true, new long[] { 0x000000000000000L, 0x000000000000000L }, new int[] { 4 });
             AssertLongRangeSplit(long.MinValue, long.MinValue + 0x10L, 4, true, new long[] { 0x0000000000000010L, 0x0000000000000010L, 0x000000000000000L, 0x000000000000000L }, new int[] { 0, 4 });
         }
-
         [Test, LongRunningTest]
+        [Seed(1249648971)]
         public virtual void TestRandomSplit()
         {
             long num = (long)AtLeast(10);
diff --git a/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs b/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
index abfc039..e3b8e73 100644
--- a/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
+++ b/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
@@ -21,6 +21,7 @@ using Lucene.Net.Randomized.Generators;
 using Lucene.Net.Support;
 using NUnit.Framework;
 using System.Collections;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
diff --git a/src/Lucene.Net.Tests/Util/TestPagedBytes.cs b/src/Lucene.Net.Tests/Util/TestPagedBytes.cs
index 7467891..1c6e307 100644
--- a/src/Lucene.Net.Tests/Util/TestPagedBytes.cs
+++ b/src/Lucene.Net.Tests/Util/TestPagedBytes.cs
@@ -21,6 +21,9 @@ using Lucene.Net.Store;
 using Lucene.Net.Support;
 using NUnit.Framework;
 using System;
+using System.Collections;
+using System.Runtime.InteropServices;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
@@ -35,11 +38,11 @@ namespace Lucene.Net.Util
     [TestFixture]
     public class TestPagedBytes : LuceneTestCase
     {
-
         // Writes random byte/s to "normal" file in dir, then
         // copies into PagedBytes and verifies with
         // PagedBytes.Reader:
         [Test, LongRunningTest]
+        [Seed(1249648971)]
         public virtual void TestDataInputOutput()
         {
             Random random = Random;
@@ -180,7 +183,7 @@ namespace Lucene.Net.Util
             }
         }
 
-        [Ignore("// memory hole")] 
+        [Ignore("// memory hole")]
         [Test, LongRunningTest]
         public virtual void TestOverflow()
         {
@@ -199,7 +202,7 @@ namespace Lucene.Net.Util
             long numBytes = (1L << 31) + TestUtil.NextInt32(Random, 1, blockSize * 3);
             var p = new PagedBytes(blockBits);
             var @out = dir.CreateOutput("foo", IOContext.DEFAULT);
-            for (long i = 0; i < numBytes; )
+            for (long i = 0; i < numBytes;)
             {
                 Assert.AreEqual(i, @out.GetFilePointer());
                 int len = (int)Math.Min(arr.Length, numBytes - i);
diff --git a/src/Lucene.Net.Tests/Util/TestWAH8DocIdSet.cs b/src/Lucene.Net.Tests/Util/TestWAH8DocIdSet.cs
index eb842c8..24f036e 100644
--- a/src/Lucene.Net.Tests/Util/TestWAH8DocIdSet.cs
+++ b/src/Lucene.Net.Tests/Util/TestWAH8DocIdSet.cs
@@ -2,6 +2,7 @@ using Lucene.Net.Support;
 using NUnit.Framework;
 using System.Collections;
 using System.Collections.Generic;
+using Assert = Lucene.Net.TestFramework.Assert;
 
 namespace Lucene.Net.Util
 {
@@ -43,6 +44,7 @@ namespace Lucene.Net.Util
         }
 
         [Test]
+        [Seed(1249648971)]
         public virtual void TestUnion()
         {
             int numBits = TestUtil.NextInt32(Random, 100, 1 << 20);
@@ -71,6 +73,7 @@ namespace Lucene.Net.Util
         }
 
         [Test]
+        [Seed(1249648971)]
         public virtual void TestIntersection()
         {
             int numBits = TestUtil.NextInt32(Random, 100, 1 << 20);
diff --git a/src/Lucene.Net/Util/Packed/EliasFanoDecoder.cs b/src/Lucene.Net/Util/Packed/EliasFanoDecoder.cs
index a720b02..b188d0c 100644
--- a/src/Lucene.Net/Util/Packed/EliasFanoDecoder.cs
+++ b/src/Lucene.Net/Util/Packed/EliasFanoDecoder.cs
@@ -134,7 +134,7 @@ namespace Lucene.Net.Util.Packed
         ///  <returns> The low value for the current decoding index. </returns>
         private long CurrentLowValue()
         {
-            Debug.Assert(((efIndex >= 0) && (efIndex < numEncoded)), "efIndex " + efIndex);
+            Debug.Assert(((efIndex >= 0) && (efIndex < numEncoded)), $"efIndex {efIndex.ToString()}");
             return UnPackValue(efEncoder.lowerLongs, efEncoder.numLowBits, efIndex, efEncoder.lowerBitsMask);
         }