Skip to content

Commit 49c46f3

Browse files
Add a missing = in BigInteger.cs (#105456)
* 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 <[email protected]> --------- Co-authored-by: Dan Moseley <[email protected]>
1 parent ab7e123 commit 49c46f3

File tree

6 files changed

+255
-1
lines changed

6 files changed

+255
-1
lines changed

src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ private BigInteger(Span<uint> value)
544544
isNegative = true;
545545
length = value.LastIndexOfAnyExcept(uint.MaxValue) + 1;
546546

547-
if ((length == 0) || ((int)value[length - 1] > 0))
547+
if ((length == 0) || ((int)value[length - 1] >= 0))
548548
{
549549
// We ne need to preserve the sign bit
550550
length++;

src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ public static List<byte> GetBytes(BitArray ba)
877877
}
878878

879879
public static string Print(byte[] bytes)
880+
{
881+
return Print(bytes.AsSpan());
882+
}
883+
884+
public static string Print(ReadOnlySpan<byte> bytes)
880885
{
881886
string ret = "make ";
882887

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace System.Numerics.Tests
12+
{
13+
public static partial class SampleGeneration
14+
{
15+
public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int minLength, int maxLengthExclusive)
16+
{
17+
return EnumerateSequence(elementSource.ToArray(), minLength, maxLengthExclusive);
18+
}
19+
20+
public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(T[] elementSource, int minLength, int maxLengthExclusive)
21+
{
22+
for (var i = minLength; maxLengthExclusive > i; ++i)
23+
{
24+
foreach (var item in EnumerateSequence(elementSource, i))
25+
{
26+
yield return item;
27+
}
28+
}
29+
}
30+
31+
public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int length)
32+
{
33+
return EnumerateSequence(elementSource.ToArray(), length);
34+
}
35+
36+
public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(T[] elementSource, int length)
37+
{
38+
var a = new T[length];
39+
var r = new ReadOnlyMemory<T>(a);
40+
foreach (var _ in EnumerateSequenceYieldsCurrentCount(elementSource, a))
41+
{
42+
yield return r;
43+
}
44+
}
45+
46+
private static IEnumerable<long> EnumerateSequenceYieldsCurrentCount<T>(T[] elementSource, T[] buffer)
47+
{
48+
var c = 0L;
49+
var b = elementSource.Length;
50+
if (b != 0)
51+
{
52+
var stack = new int[buffer.Length];
53+
for (var i = 0; i < buffer.Length; ++i)
54+
{
55+
buffer[i] = elementSource[0];
56+
}
57+
{
58+
L:;
59+
yield return c++;
60+
for (var i = 0; stack.Length != i; ++i)
61+
{
62+
var en = ++stack[i];
63+
if (b == en)
64+
{
65+
}
66+
else
67+
{
68+
buffer[i] = elementSource[en];
69+
for (; 0 <= --i;)
70+
{
71+
buffer[i] = elementSource[0];
72+
stack[i] = 0;
73+
}
74+
goto L;
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.Linq;
7+
using Xunit;
8+
9+
namespace System.Numerics.Tests
10+
{
11+
12+
public static partial class UInt32Samples
13+
{
14+
private static readonly uint[] set1 = new uint[] {
15+
0B00000000000000000000000000000000U,
16+
0B00000000000000000000000000000001U,
17+
0B00111111111111111111111111111110U,
18+
0B00111111111111111111111111111111U,
19+
20+
0B01000000000000000000000000000000U,
21+
0B01000000000000000000000000000001U,
22+
0B01111111111111111111111111111110U,
23+
0B01111111111111111111111111111111U,
24+
25+
0B10000000000000000000000000000000U,
26+
0B10000000000000000000000000000001U,
27+
0B10111111111111111111111111111110U,
28+
0B10111111111111111111111111111111U,
29+
30+
0B11000000000000000000000000000000U,
31+
0B11000000000000000000000000000001U,
32+
0B11111111111111111111111111111110U,
33+
0B11111111111111111111111111111111U,
34+
};
35+
36+
private static IEnumerable<uint> GetSet1()
37+
{
38+
foreach (var item in set1)
39+
{
40+
yield return item;
41+
}
42+
}
43+
44+
public static readonly IEnumerable<uint> Set1 = GetSet1();
45+
46+
private static readonly uint[] set2 = new uint[] {
47+
0B00000000000000000000000000000000U,
48+
0B00000000000000000000000000000001U,
49+
0B00000000000000000100000000000000U,
50+
0B00000000000000000100000000000001U,
51+
52+
0B00000000000000010000000000000000U,
53+
0B00000000000000010000000000000001U,
54+
0B00000000000000010100000000000000U,
55+
0B00000000000000010100000000000001U,
56+
57+
0B00111111111111111111111111111110U,
58+
0B00111111111111111111111111111111U,
59+
0B00111111111111111011111111111110U,
60+
0B00111111111111111011111111111111U,
61+
62+
0B00111111111111101111111111111110U,
63+
0B00111111111111101111111111111111U,
64+
0B00111111111111101011111111111110U,
65+
0B00111111111111101011111111111111U,
66+
67+
0B01000000000000000000000000000000U,
68+
0B01000000000000000000000000000001U,
69+
0B01000000000000000100000000000000U,
70+
0B01000000000000000100000000000001U,
71+
72+
0B01000000000000010000000000000000U,
73+
0B01000000000000010000000000000001U,
74+
0B01000000000000010100000000000000U,
75+
0B01000000000000010100000000000001U,
76+
77+
0B01111111111111111111111111111110U,
78+
0B01111111111111111111111111111111U,
79+
0B01111111111111111011111111111110U,
80+
0B01111111111111111011111111111111U,
81+
82+
0B01111111111111101111111111111110U,
83+
0B01111111111111101111111111111111U,
84+
0B01111111111111101011111111111110U,
85+
0B01111111111111101011111111111111U,
86+
87+
0B10000000000000000000000000000000U,
88+
0B10000000000000000000000000000001U,
89+
0B10000000000000000100000000000000U,
90+
0B10000000000000000100000000000001U,
91+
92+
0B10000000000000010000000000000000U,
93+
0B10000000000000010000000000000001U,
94+
0B10000000000000010100000000000000U,
95+
0B10000000000000010100000000000001U,
96+
97+
0B10111111111111111111111111111110U,
98+
0B10111111111111111111111111111111U,
99+
0B10111111111111111011111111111110U,
100+
0B10111111111111111011111111111111U,
101+
102+
0B10111111111111101111111111111110U,
103+
0B10111111111111101111111111111111U,
104+
0B10111111111111101011111111111110U,
105+
0B10111111111111101011111111111111U,
106+
107+
0B11000000000000000000000000000000U,
108+
0B11000000000000000000000000000001U,
109+
0B11000000000000000100000000000000U,
110+
0B11000000000000000100000000000001U,
111+
112+
0B11000000000000010000000000000000U,
113+
0B11000000000000010000000000000001U,
114+
0B11000000000000010100000000000000U,
115+
0B11000000000000010100000000000001U,
116+
117+
0B11111111111111111111111111111110U,
118+
0B11111111111111111111111111111111U,
119+
0B11111111111111111011111111111110U,
120+
0B11111111111111111011111111111111U,
121+
122+
0B11111111111111101111111111111110U,
123+
0B11111111111111101111111111111111U,
124+
0B11111111111111101011111111111110U,
125+
0B11111111111111101011111111111111U,
126+
};
127+
128+
private static IEnumerable<uint> GetSet2()
129+
{
130+
foreach (var item in set2)
131+
{
132+
yield return item;
133+
}
134+
}
135+
136+
public static readonly IEnumerable<uint> Set2 = GetSet2();
137+
}
138+
}

src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
47
using Xunit;
58

69
namespace System.Numerics.Tests
@@ -117,6 +120,26 @@ public static void RunAndTests()
117120
}
118121
}
119122

123+
[Fact]
124+
public static void RunAndTestsForSampleSet1()
125+
{
126+
var s = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2);
127+
var t = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2);
128+
129+
foreach (var i in s)
130+
{
131+
foreach (var j in t)
132+
{
133+
var a = MemoryMarshal.AsBytes(i.Span);
134+
var b = MemoryMarshal.AsBytes(j.Span);
135+
136+
VerifyAndString(Print(a) + Print(b) + "b&");
137+
138+
VerifyAndString(Print(b) + Print(a) + "b&");
139+
}
140+
}
141+
}
142+
120143
private static void VerifyAndString(string opstring)
121144
{
122145
StackCalc sc = new StackCalc(opstring);
@@ -139,5 +162,10 @@ private static string Print(byte[] bytes)
139162
{
140163
return MyBigIntImp.Print(bytes);
141164
}
165+
166+
private static string Print(ReadOnlySpan<byte> bytes)
167+
{
168+
return MyBigIntImp.Print(bytes);
169+
}
142170
}
143171
}

src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@
4848
<Compile Include="BigInteger\pow.cs" />
4949
<Compile Include="BigInteger\properties.cs" />
5050
<Compile Include="BigInteger\remainder.cs" />
51+
<Compile Include="BigInteger\SampleGeneration.cs" />
5152
<Compile Include="BigInteger\sign.cs" />
5253
<Compile Include="BigInteger\stackcalculator.cs" />
5354
<Compile Include="BigInteger\ToByteArray.cs" />
5455
<Compile Include="BigInteger\TryWriteBytes.cs" />
56+
<Compile Include="BigInteger\UInt32Samples.cs" />
5557
<Compile Include="ComplexTests.cs" />
5658
</ItemGroup>
5759
<ItemGroup>

0 commit comments

Comments
 (0)