Skip to content

Commit

Permalink
Add a missing = in BigInteger.cs (dotnet#105456)
Browse files Browse the repository at this point in the history
* Add a missing =

* Add some tests for bigint AND.

* Add some tests for bigint AND.

* Update src/libraries/System.Runtime.Numerics/tests/BigInteger/SampleGeneration.cs

Co-authored-by: Dan Moseley <danmose@microsoft.com>

---------

Co-authored-by: Dan Moseley <danmose@microsoft.com>
  • Loading branch information
2 people authored and mikelle-rogers committed Dec 4, 2024
1 parent a905114 commit 8b7a685
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private BigInteger(Span<uint> 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++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,11 @@ public static List<byte> GetBytes(BitArray ba)
}

public static string Print(byte[] bytes)
{
return Print(bytes.AsSpan());
}

public static string Print(ReadOnlySpan<byte> bytes)
{
string ret = "make ";

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int minLength, int maxLengthExclusive)
{
return EnumerateSequence(elementSource.ToArray(), minLength, maxLengthExclusive);
}

public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(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<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int length)
{
return EnumerateSequence(elementSource.ToArray(), length);
}

public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(T[] elementSource, int length)
{
var a = new T[length];
var r = new ReadOnlyMemory<T>(a);
foreach (var _ in EnumerateSequenceYieldsCurrentCount(elementSource, a))
{
yield return r;
}
}

private static IEnumerable<long> EnumerateSequenceYieldsCurrentCount<T>(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;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<uint> GetSet1()
{
foreach (var item in set1)
{
yield return item;
}
}

public static readonly IEnumerable<uint> 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<uint> GetSet2()
{
foreach (var item in set2)
{
yield return item;
}
}

public static readonly IEnumerable<uint> Set2 = GetSet2();
}
}
28 changes: 28 additions & 0 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -139,5 +162,10 @@ private static string Print(byte[] bytes)
{
return MyBigIntImp.Print(bytes);
}

private static string Print(ReadOnlySpan<byte> bytes)
{
return MyBigIntImp.Print(bytes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
<Compile Include="BigInteger\pow.cs" />
<Compile Include="BigInteger\properties.cs" />
<Compile Include="BigInteger\remainder.cs" />
<Compile Include="BigInteger\SampleGeneration.cs" />
<Compile Include="BigInteger\sign.cs" />
<Compile Include="BigInteger\stackcalculator.cs" />
<Compile Include="BigInteger\ToByteArray.cs" />
<Compile Include="BigInteger\TryWriteBytes.cs" />
<Compile Include="BigInteger\UInt32Samples.cs" />
<Compile Include="ComplexTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 8b7a685

Please sign in to comment.