Skip to content

Commit

Permalink
Tests: Review T-Z Tests, #259 (#897)
Browse files Browse the repository at this point in the history
* Tests: Review T-Z Tests, #259

- This addresses some formatting disparities between the Java and C#
  tests to help make them more line-to-line matching.
- Comments added in places where our tests differ from upstream.
- RandomizedTesting.Generators extension method import moved behind a
  `#if !NET6_0_OR_GREATER` check, as the namespace was being ignored on
  .NET 6+ in a few files since .NET 6 added a Random.NextInt64 method.
  This will help prevent other contributors from thinking this namespace
  is not needed.
- A unit test assertion bug on arm64 with division by zero is fixed.
- The `build` file has been chmod'ed +x for ease of use
- Rider IDE files added

* Update AssertSorted method to better match Lucene, reduce allocations, #259

* Add feature for .NET 6+ Random methods and change #if to use it, #259

* Fix XML docs for TestBytesRefHash, #259

* Update .editorconfig to use new lines at end of file
  • Loading branch information
paulirwin committed Jan 22, 2024
1 parent 6868c38 commit 3729f4c
Show file tree
Hide file tree
Showing 75 changed files with 346 additions and 248 deletions.
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[*]
charset = utf-8-bom
trim_trailing_whitespace = true
insert_final_newline = true
resharper_enforce_empty_line_at_end_of_file = true

[*.md]
indent_style = space
Expand Down Expand Up @@ -189,4 +191,4 @@ dotnet_diagnostic.IDE0070.severity = none
### SonarCloud Issues ###

# S907: Remove this use of 'goto'
dotnet_diagnostic.S907.severity = none
dotnet_diagnostic.S907.severity = none
13 changes: 13 additions & 0 deletions .idea/.idea.Lucene.Net/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.Lucene.Net/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.Lucene.Net/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Lucene.Net/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Lucene.Net/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PropertyGroup Condition=" $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) ">

<DefineConstants>$(DefineConstants);FEATURE_SPANFORMATTABLE</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_RANDOM_NEXTINT64_NEXTSINGLE</DefineConstants>

</PropertyGroup>

Expand Down
Empty file modified build
100644 → 100755
Empty file.
19 changes: 15 additions & 4 deletions src/Lucene.Net.Tests.Expressions/JS/TestJavascriptOperations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Runtime.InteropServices;
using Assert = Lucene.Net.TestFramework.Assert;

namespace Lucene.Net.Expressions.JS
Expand All @@ -23,7 +25,7 @@ namespace Lucene.Net.Expressions.JS

public class TestJavascriptOperations : LuceneTestCase
{

private void AssertEvaluatesTo(string expression, long expected)
{
Expression evaluator = JavascriptCompiler.Compile(expression);
Expand Down Expand Up @@ -91,8 +93,17 @@ public virtual void TestDivisionOperation()
AssertEvaluatesTo("10/5/2", 1);
AssertEvaluatesTo("(27/9)/3", 1);
AssertEvaluatesTo("27/(9/3)", 9);
//.NET Port division overflow cast to double evals to long.MinValue
AssertEvaluatesTo("1/0", -9223372036854775808);
// LUCENENET: division overflow cast to double then to long evaluates to long.MinValue, except on arm64
// where it matches Java's behavior and Lucene's assertion. This only happens with the conv.i8 opcode with
// positive infinity on the stack. 1.0 / 0.0 == double.PositiveInfinity. In C#, if you cast
// double.PositiveInfinity to long in an unchecked context it returns 0. The C# spec for conversion in an
// unchecked context from double to long states "If the value of the operand is NaN or infinite, the result
// of the conversion is an unspecified value of the destination type." Likewise, the docs for conv.i8 state
// "If overflow occurs converting a floating-point type to an integer the value returned is unspecified."
// Essentially this is undefined behavior, so we are going to assert an architecture-specific value
// primarily to ensure it produces something rather than throws. CPU architectures other than arm64, x86,
// and x64 may produce different results.
AssertEvaluatesTo("1/0", RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? 9223372036854775807 : -9223372036854775808);
}

[Test]
Expand Down Expand Up @@ -373,7 +384,7 @@ public virtual void TestHexConst2()
AssertEvaluatesTo("0X0", 0);
AssertEvaluatesTo("0X1", 1);
AssertEvaluatesTo("0XF", 15);
AssertEvaluatesTo("0X1234ABCDEF", 78193085935L);
AssertEvaluatesTo("0X1234ABCDEF", 78193085935L);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Tests/Util/Automaton/TestBasicOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ public virtual void TestGetRandomAcceptedString()
}
}
}
}
}
3 changes: 1 addition & 2 deletions src/Lucene.Net.Tests/Util/Automaton/TestCompiledAutomaton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;
using JCG = J2N.Collections.Generic;
Expand Down Expand Up @@ -148,4 +147,4 @@ public virtual void TestBasic()
TestFloor(c, "zzz", "goo");
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Util/Automaton/TestDeterminism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public virtual void TestRegexps()
int num = AtLeast(500);
for (int i = 0; i < num; i++)
{
AssertAutomaton((new RegExp(AutomatonTestUtil.RandomRegexp(Random), RegExpSyntax.NONE)).ToAutomaton());
AssertAutomaton(new RegExp(AutomatonTestUtil.RandomRegexp(Random), RegExpSyntax.NONE).ToAutomaton());
}
}

Expand Down Expand Up @@ -89,4 +89,4 @@ private static void AssertAutomaton(Automaton a)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ public void AssertLexicon()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual void TestLev2()
[Test]
public virtual void TestNoWastedStates()
{
AutomatonTestUtil.AssertNoDetachedStates((new LevenshteinAutomata("abc", false)).ToAutomaton(1));
AutomatonTestUtil.AssertNoDetachedStates(new LevenshteinAutomata("abc", false).ToAutomaton(1));
}

/// <summary>
Expand Down Expand Up @@ -435,4 +435,4 @@ private int GetTDistance(string target, string other)
return Math.Abs(d[n][m]);
}
}
}
}
5 changes: 2 additions & 3 deletions src/Lucene.Net.Tests/Util/Automaton/TestMinimize.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NUnit.Framework;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;

namespace Lucene.Net.Util.Automaton
{
Expand Down Expand Up @@ -250,7 +249,7 @@ public virtual void TestAgainstBrzozowski()
[Test]
public virtual void TestMinimizeHuge()
{
(new RegExp("+-*(A|.....|BC)*]", RegExpSyntax.NONE)).ToAutomaton();
new RegExp("+-*(A|.....|BC)*]", RegExpSyntax.NONE).ToAutomaton();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public virtual void TestFiniteStrings()
Assert.IsTrue(strings.Contains(duck));
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Util/Automaton/TestUTF32ToUTF8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void TestRandomRegexes()
int num = AtLeast(250);
for (int i = 0; i < num; i++)
{
AssertAutomaton((new RegExp(AutomatonTestUtil.RandomRegexp(Random), RegExpSyntax.NONE)).ToAutomaton());
AssertAutomaton(new RegExp(AutomatonTestUtil.RandomRegexp(Random), RegExpSyntax.NONE).ToAutomaton());
}
}

Expand Down Expand Up @@ -286,4 +286,4 @@ private static void AssertAutomaton(Automaton automaton)
}
}
}
}
}
58 changes: 28 additions & 30 deletions src/Lucene.Net.Tests/Util/BaseSortTestCase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Lucene.Net.Support;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -44,18 +43,19 @@ public virtual int CompareTo(Entry other)
}

private readonly bool stable;
private readonly Random random;

protected BaseSortTestCase(bool stable)
{
this.stable = stable;
this.random = Random;
}

public abstract Sorter NewSorter(Entry[] arr);

// LUCENENET specific
public class StableEntryComparer : IComparer<Entry>
{
public static readonly StableEntryComparer Default = new StableEntryComparer();

public int Compare(Entry a, Entry b)
{
if (a.Value < b.Value) return -1;
Expand All @@ -69,7 +69,8 @@ public int Compare(Entry a, Entry b)
public virtual void AssertSorted(Entry[] original, Entry[] sorted)
{
Assert.AreEqual(original.Length, sorted.Length);
Entry[] stableSorted = original.OrderBy(e => e, new StableEntryComparer()).ToArray();
Entry[] stableSorted = Arrays.CopyOf(original, original.Length);
Array.Sort(stableSorted, StableEntryComparer.Default); // LUCENENET: use StableEntryComparer
for (int i = 0; i < original.Length; ++i)
{
Assert.AreEqual(stableSorted[i].Value, sorted[i].Value);
Expand All @@ -82,8 +83,8 @@ public virtual void AssertSorted(Entry[] original, Entry[] sorted)

public virtual void SortTest(Entry[] arr)
{
int o = random.Next(1000);
var toSort = new Entry[o + arr.Length + random.Next(3)];
int o = Random.Next(1000);
var toSort = new Entry[o + arr.Length + Random.Next(3)];
Arrays.Copy(arr, 0, toSort, o, arr.Length);
Sorter sorter = NewSorter(toSort);
sorter.Sort(o, o + arr.Length);
Expand All @@ -94,50 +95,47 @@ public virtual void SortTest(Entry[] arr)

private void RandomStrategy(Entry[] arr, int i)
{
arr[i] = new Entry(random.Next(), i);
arr[i] = new Entry(Random.Next(), i);
}

private void RandomLowCardinalityStrategy(Entry[] arr, int i)
{
arr[i] = new Entry(random.nextInt(6), i);
arr[i] = new Entry(Random.nextInt(6), i);
}

private void AscendingStrategy(Entry[] arr, int i)
{
arr[i] = i == 0
? new Entry(random.nextInt(6), 0)
: new Entry(arr[i - 1].Value + random.nextInt(6), i);
? new Entry(Random.nextInt(6), 0)
: new Entry(arr[i - 1].Value + Random.nextInt(6), i);
}

private void DescendingStrategy(Entry[] arr, int i)
{
arr[i] = i == 0
? new Entry(random.nextInt(6), 0)
: new Entry(arr[i - 1].Value - random.nextInt(6), i);
? new Entry(Random.nextInt(6), 0)
: new Entry(arr[i - 1].Value - Random.nextInt(6), i);
}

private void StrictlyDescendingStrategy(Entry[] arr, int i)
{
arr[i] = i == 0
? new Entry(random.nextInt(6), 0)
: new Entry(arr[i - 1].Value - TestUtil.NextInt32(random, 1, 5), i);

? new Entry(Random.nextInt(6), 0)
: new Entry(arr[i - 1].Value - TestUtil.NextInt32(Random, 1, 5), i);
}

private void AscendingSequencesStrategy(Entry[] arr, int i)
{
arr[i] = i == 0
? new Entry(random.nextInt(6), 0)
: new Entry(Rarely(random) ? random.nextInt(1000) : arr[i - 1].Value + random.nextInt(6), i);

? new Entry(Random.nextInt(6), 0)
: new Entry(Rarely(Random) ? Random.nextInt(1000) : arr[i - 1].Value + Random.nextInt(6), i);
}

private void MostlyAscendingStrategy(Entry[] arr, int i)
{
arr[i] = i == 0
? new Entry(random.nextInt(6), 0)
: new Entry(arr[i - 1].Value + TestUtil.NextInt32(random, -8, 10), i);

? new Entry(Random.nextInt(6), 0)
: new Entry(arr[i - 1].Value + TestUtil.NextInt32(Random, -8, 10), i);
}

private void DoTest(Strategy strategy, int length)
Expand Down Expand Up @@ -169,7 +167,7 @@ public virtual void TestOne()
[Test]
public virtual void TestTwo()
{
DoTest(RandomStrategy, 2);
DoTest(RandomLowCardinalityStrategy, 2);
}

[Test]
Expand All @@ -181,31 +179,31 @@ public virtual void TestRandom()
[Test]
public virtual void TestRandomLowCardinality()
{
DoTest(RandomLowCardinalityStrategy, 2);
DoTest(RandomLowCardinalityStrategy);
}

[Test]
public virtual void TestAscending()
{
DoTest(AscendingStrategy, 2);
DoTest(AscendingStrategy);
}

[Test]
public virtual void TestAscendingSequences()
{
DoTest(AscendingSequencesStrategy, 2);
DoTest(AscendingSequencesStrategy);
}

[Test]
public virtual void TestDescending()
{
DoTest(DescendingStrategy, 2);
DoTest(DescendingStrategy);
}

[Test]
public virtual void TestStrictlyDescendingStrategy()
{
DoTest(StrictlyDescendingStrategy, 2);
DoTest(StrictlyDescendingStrategy);
}
}
}
}
7 changes: 5 additions & 2 deletions src/Lucene.Net.Tests/Util/Fst/Test2BFST.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Lucene.Net.Support;
using NUnit.Framework;
using RandomizedTesting.Generators;
using System;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;
using Int64 = J2N.Numerics.Int64;

#if !FEATURE_RANDOM_NEXTINT64_NEXTSINGLE
using RandomizedTesting.Generators; // for Random.NextInt64 extension method
#endif

namespace Lucene.Net.Util.Fst
{
/*
Expand Down Expand Up @@ -338,4 +341,4 @@ private void NextInput(Random r, int[] ints)
}
}
}
}
}

0 comments on commit 3729f4c

Please sign in to comment.