Skip to content

Commit db0c284

Browse files
committed
RunWithFakeThreshold hack
1 parent b890959 commit db0c284

File tree

6 files changed

+30
-61
lines changed

6 files changed

+30
-61
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Numerics;
6-
using System.Reflection;
5+
using System.Runtime.CompilerServices;
76
using System.Text;
87

98
namespace BigIntTools
@@ -34,20 +33,30 @@ public static string BuildRandomNumber(int maxdigits, int seed)
3433
}
3534
}
3635

37-
#if DEBUG
38-
public static void RunWithFakeThreshold(ref int field, int value, Action action)
36+
public static void RunWithFakeThreshold(in int field, int value, Action action)
3937
{
4038
int lastValue = field;
39+
40+
// This is tricky hack. If only DEBUG build is targeted,
41+
// `RunWithFakeThreshold(ref int field, int value, Action action action)` would be more appropriate.
42+
// However, in RELEASE build, the code should be like this.
43+
// This is because const fields cannot be passed as ref arguments.
44+
// When a const field is passed to the in argument, a local
45+
// variable reference is implicitly passed to the in argument
46+
// so that the original const field value is never rewritten.
47+
ref int reference = ref Unsafe.AsRef(in field);
48+
4149
try
4250
{
43-
field = value;
51+
reference = value;
52+
if (field != value)
53+
return; // In release build, the test will be skipped.
4454
action();
4555
}
4656
finally
4757
{
48-
field = lastValue;
58+
reference = lastValue;
4959
}
5060
}
51-
#endif
5261
}
5362
}

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,8 @@ public static void ModPow1Large2SmallInt()
141141
[Fact]
142142
public static void ModPow1Large2SmallInt_Threshold()
143143
{
144-
#if DEBUG
145144
// Again, with lower threshold
146-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.ReducerThreshold, 8, ModPow1Large2SmallInt);
147-
#endif
145+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.ReducerThreshold, 8, ModPow1Large2SmallInt);
148146
}
149147

150148
[Fact]
@@ -167,10 +165,8 @@ public static void ModPow2Large1SmallInt()
167165
[Fact]
168166
public static void ModPow2Large1SmallInt_Threshold()
169167
{
170-
#if DEBUG
171168
// Again, with lower threshold
172-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.ReducerThreshold, 8, ModPow2Large1SmallInt);
173-
#endif
169+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.ReducerThreshold, 8, ModPow2Large1SmallInt);
174170
}
175171

176172
// InlineData randomly generated using a new Random(0) and the same logic as is used in MyBigIntImp
@@ -197,13 +193,11 @@ public static void ModPow3LargeInt(string value, string exponent, string modulus
197193
// Once with default threshold
198194
Assert.Equal(resultInt, BigInteger.ModPow(valueInt, exponentInt, modulusInt));
199195

200-
#if DEBUG
201196
// Once with reduced threshold
202-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.ReducerThreshold, 8, () =>
197+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.ReducerThreshold, 8, () =>
203198
{
204199
Assert.Equal(resultInt, BigInteger.ModPow(valueInt, exponentInt, modulusInt));
205200
});
206-
#endif
207201
}
208202

209203
[Fact]

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,17 @@ public static void RunMultiply_TwoLargeBigIntegers()
3636
[Fact]
3737
public static void RunMultiply_TwoLargeBigIntegers_Threshold()
3838
{
39-
#if DEBUG
4039
// Again, with lower threshold
41-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.SquareThreshold, 8, () =>
42-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.MultiplyThreshold, 8, RunMultiply_TwoLargeBigIntegers)
40+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.SquareThreshold, 8, () =>
41+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.MultiplyThreshold, 8, RunMultiply_TwoLargeBigIntegers)
4342
);
4443

4544
// Again, with lower threshold
46-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.SquareThreshold, 8, () =>
47-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.MultiplyThreshold, 8, () =>
48-
BigIntTools.Utils.RunWithFakeThreshold(ref BigIntegerCalculator.StackAllocThreshold, 8, RunMultiply_TwoLargeBigIntegers)
45+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.SquareThreshold, 8, () =>
46+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.MultiplyThreshold, 8, () =>
47+
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.StackAllocThreshold, 8, RunMultiply_TwoLargeBigIntegers)
4948
)
5049
);
51-
#endif
5250
}
5351

5452
[Fact]

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public static IEnumerable<object[]> Cultures
3737
public static void RunParseToStringTests(CultureInfo culture)
3838
{
3939
Test();
40-
#if DEBUG
41-
BigNumberTools.Utils.RunWithFakeThreshold(ref Number.s_naiveThreshold, 0, Test);
42-
#endif
40+
BigIntTools.Utils.RunWithFakeThreshold(Number.s_naiveThreshold, 0, Test);
41+
4342
void Test()
4443
{
4544
byte[] tempByteArray1 = new byte[0];
@@ -104,9 +103,8 @@ public void Parse_Subspan_Success(string input, int offset, int length, string e
104103
{
105104
Test();
106105

107-
#if DEBUG
108-
BigNumberTools.Utils.RunWithFakeThreshold(ref Number.s_naiveThreshold, 0, Test);
109-
#endif
106+
BigIntTools.Utils.RunWithFakeThreshold(Number.s_naiveThreshold, 0, Test);
107+
110108
void Test()
111109
{
112110
Eval(BigInteger.Parse(input.AsSpan(offset, length)), expected);
@@ -119,9 +117,8 @@ void Test()
119117
public void Parse_EmptySubspan_Fails()
120118
{
121119
Test();
122-
#if DEBUG
123-
BigNumberTools.Utils.RunWithFakeThreshold(ref Number.s_naiveThreshold, 0, Test);
124-
#endif
120+
BigIntTools.Utils.RunWithFakeThreshold(Number.s_naiveThreshold, 0, Test);
121+
125122
void Test()
126123
{
127124
Assert.False(BigInteger.TryParse("12345".AsSpan(0, 0), out BigInteger result));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<Compile Include="BigInteger\BigIntegerToStringTests.cs" />
1111
<Compile Include="BigInteger\BigInteger.AddTests.cs" />
1212
<Compile Include="BigInteger\BigInteger.SubtractTests.cs" />
13-
<Compile Include="BigInteger\BigNumberTools.cs" />
1413
<Compile Include="BigInteger\BigIntTools.cs" />
1514
<Compile Include="BigInteger\cast_from.cs" />
1615
<Compile Include="BigInteger\cast_to.cs" />

0 commit comments

Comments
 (0)