diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs index e4d8845dd05e8..f58c9877d3d53 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs @@ -544,7 +544,7 @@ private BigInteger(Span value) isNegative = true; length = value.LastIndexOfAnyExcept(uint.MaxValue) + 1; - if ((length == 0) || ((int)value[length - 1] > 0)) + if ((length == 0) || ((int)value[length - 1] >= 0)) { // We ne need to preserve the sign bit length++; diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs index cd4a578fab910..58d4afdc819f7 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs @@ -877,6 +877,11 @@ public static List GetBytes(BitArray ba) } public static string Print(byte[] bytes) + { + return Print(bytes.AsSpan()); + } + + public static string Print(ReadOnlySpan bytes) { string ret = "make "; diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/SampleGeneration.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/SampleGeneration.cs new file mode 100644 index 0000000000000..7d778be2141f8 --- /dev/null +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/SampleGeneration.cs @@ -0,0 +1,81 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System.Numerics.Tests +{ + public static partial class SampleGeneration + { + public static IEnumerable> EnumerateSequence(IEnumerable elementSource, int minLength, int maxLengthExclusive) + { + return EnumerateSequence(elementSource.ToArray(), minLength, maxLengthExclusive); + } + + public static IEnumerable> EnumerateSequence(T[] elementSource, int minLength, int maxLengthExclusive) + { + for (var i = minLength; maxLengthExclusive > i; ++i) + { + foreach (var item in EnumerateSequence(elementSource, i)) + { + yield return item; + } + } + } + + public static IEnumerable> EnumerateSequence(IEnumerable elementSource, int length) + { + return EnumerateSequence(elementSource.ToArray(), length); + } + + public static IEnumerable> EnumerateSequence(T[] elementSource, int length) + { + var a = new T[length]; + var r = new ReadOnlyMemory(a); + foreach (var _ in EnumerateSequenceYieldsCurrentCount(elementSource, a)) + { + yield return r; + } + } + + private static IEnumerable EnumerateSequenceYieldsCurrentCount(T[] elementSource, T[] buffer) + { + var c = 0L; + var b = elementSource.Length; + if (b != 0) + { + var stack = new int[buffer.Length]; + for (var i = 0; i < buffer.Length; ++i) + { + buffer[i] = elementSource[0]; + } + { + L:; + yield return c++; + for (var i = 0; stack.Length != i; ++i) + { + var en = ++stack[i]; + if (b == en) + { + } + else + { + buffer[i] = elementSource[en]; + for (; 0 <= --i;) + { + buffer[i] = elementSource[0]; + stack[i] = 0; + } + goto L; + } + } + } + } + } + } +} diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/UInt32Samples.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/UInt32Samples.cs new file mode 100644 index 0000000000000..658c68ae2c981 --- /dev/null +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/UInt32Samples.cs @@ -0,0 +1,138 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Xunit; + +namespace System.Numerics.Tests +{ + + public static partial class UInt32Samples + { + private static readonly uint[] set1 = new uint[] { + 0B00000000000000000000000000000000U, + 0B00000000000000000000000000000001U, + 0B00111111111111111111111111111110U, + 0B00111111111111111111111111111111U, + + 0B01000000000000000000000000000000U, + 0B01000000000000000000000000000001U, + 0B01111111111111111111111111111110U, + 0B01111111111111111111111111111111U, + + 0B10000000000000000000000000000000U, + 0B10000000000000000000000000000001U, + 0B10111111111111111111111111111110U, + 0B10111111111111111111111111111111U, + + 0B11000000000000000000000000000000U, + 0B11000000000000000000000000000001U, + 0B11111111111111111111111111111110U, + 0B11111111111111111111111111111111U, + }; + + private static IEnumerable GetSet1() + { + foreach (var item in set1) + { + yield return item; + } + } + + public static readonly IEnumerable Set1 = GetSet1(); + + private static readonly uint[] set2 = new uint[] { + 0B00000000000000000000000000000000U, + 0B00000000000000000000000000000001U, + 0B00000000000000000100000000000000U, + 0B00000000000000000100000000000001U, + + 0B00000000000000010000000000000000U, + 0B00000000000000010000000000000001U, + 0B00000000000000010100000000000000U, + 0B00000000000000010100000000000001U, + + 0B00111111111111111111111111111110U, + 0B00111111111111111111111111111111U, + 0B00111111111111111011111111111110U, + 0B00111111111111111011111111111111U, + + 0B00111111111111101111111111111110U, + 0B00111111111111101111111111111111U, + 0B00111111111111101011111111111110U, + 0B00111111111111101011111111111111U, + + 0B01000000000000000000000000000000U, + 0B01000000000000000000000000000001U, + 0B01000000000000000100000000000000U, + 0B01000000000000000100000000000001U, + + 0B01000000000000010000000000000000U, + 0B01000000000000010000000000000001U, + 0B01000000000000010100000000000000U, + 0B01000000000000010100000000000001U, + + 0B01111111111111111111111111111110U, + 0B01111111111111111111111111111111U, + 0B01111111111111111011111111111110U, + 0B01111111111111111011111111111111U, + + 0B01111111111111101111111111111110U, + 0B01111111111111101111111111111111U, + 0B01111111111111101011111111111110U, + 0B01111111111111101011111111111111U, + + 0B10000000000000000000000000000000U, + 0B10000000000000000000000000000001U, + 0B10000000000000000100000000000000U, + 0B10000000000000000100000000000001U, + + 0B10000000000000010000000000000000U, + 0B10000000000000010000000000000001U, + 0B10000000000000010100000000000000U, + 0B10000000000000010100000000000001U, + + 0B10111111111111111111111111111110U, + 0B10111111111111111111111111111111U, + 0B10111111111111111011111111111110U, + 0B10111111111111111011111111111111U, + + 0B10111111111111101111111111111110U, + 0B10111111111111101111111111111111U, + 0B10111111111111101011111111111110U, + 0B10111111111111101011111111111111U, + + 0B11000000000000000000000000000000U, + 0B11000000000000000000000000000001U, + 0B11000000000000000100000000000000U, + 0B11000000000000000100000000000001U, + + 0B11000000000000010000000000000000U, + 0B11000000000000010000000000000001U, + 0B11000000000000010100000000000000U, + 0B11000000000000010100000000000001U, + + 0B11111111111111111111111111111110U, + 0B11111111111111111111111111111111U, + 0B11111111111111111011111111111110U, + 0B11111111111111111011111111111111U, + + 0B11111111111111101111111111111110U, + 0B11111111111111101111111111111111U, + 0B11111111111111101011111111111110U, + 0B11111111111111101011111111111111U, + }; + + private static IEnumerable GetSet2() + { + foreach (var item in set2) + { + yield return item; + } + } + + public static readonly IEnumerable Set2 = GetSet2(); + } +} diff --git a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs index ed25708e2d0c6..86bc8b59c0d29 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs @@ -1,6 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; using Xunit; namespace System.Numerics.Tests @@ -117,6 +120,26 @@ public static void RunAndTests() } } + [Fact] + public static void RunAndTestsForSampleSet1() + { + var s = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2); + var t = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2); + + foreach (var i in s) + { + foreach (var j in t) + { + var a = MemoryMarshal.AsBytes(i.Span); + var b = MemoryMarshal.AsBytes(j.Span); + + VerifyAndString(Print(a) + Print(b) + "b&"); + + VerifyAndString(Print(b) + Print(a) + "b&"); + } + } + } + private static void VerifyAndString(string opstring) { StackCalc sc = new StackCalc(opstring); @@ -139,5 +162,10 @@ private static string Print(byte[] bytes) { return MyBigIntImp.Print(bytes); } + + private static string Print(ReadOnlySpan bytes) + { + return MyBigIntImp.Print(bytes); + } } } diff --git a/src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj b/src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj index 8e2c0ce29543b..4224f0ac83414 100644 --- a/src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj +++ b/src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj @@ -48,10 +48,12 @@ + +