-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
multiply.cs
296 lines (254 loc) · 11 KB
/
multiply.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.Numerics.Tests
{
public class multiplyTest
{
private static int s_samples = 10;
private static int s_seed = 100;
[Fact]
public static void RunMultiply_TwoLargeBigIntegers()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - One Large BigInteger
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomByteArray(random);
VerifyMultiplyString(Print(tempByteArray1) + "uMultiply");
}
// Multiply Method - Two Large BigIntegers
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomByteArray(random);
tempByteArray2 = GetRandomByteArray(random);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
}
[Fact]
public static void RunMultiply_TwoLargeBigIntegers_Threshold()
{
// Again, with lower threshold
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.SquareThreshold, 8, () =>
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.MultiplyKaratsubaThreshold, 8, RunMultiply_TwoLargeBigIntegers)
);
// Again, with lower threshold
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.SquareThreshold, 8, () =>
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.MultiplyKaratsubaThreshold, 8, () =>
BigIntTools.Utils.RunWithFakeThreshold(BigIntegerCalculator.StackAllocThreshold, 8, RunMultiply_TwoLargeBigIntegers)
)
);
}
[Fact]
public static void RunMultiply_TwoSmallBigIntegers()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - Two Small BigIntegers
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomByteArray(random, 2);
tempByteArray2 = GetRandomByteArray(random, 2);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
}
[Fact]
public static void RunMultiply_OneLargeOneSmall()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - One large and one small BigIntegers
for (int i = 0; i < s_samples; i++)
{
try
{
tempByteArray1 = GetRandomByteArray(random);
tempByteArray2 = GetRandomByteArray(random, 2);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
tempByteArray1 = GetRandomByteArray(random, 2);
tempByteArray2 = GetRandomByteArray(random);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Array1: " + Print(tempByteArray1));
Console.WriteLine("Array2: " + Print(tempByteArray2));
throw;
}
}
}
[Fact]
public static void RunMultiply_OneLargeOneZero()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - One large BigIntegers and zero
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomByteArray(random);
tempByteArray2 = new byte[] { 0 };
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
tempByteArray1 = new byte[] { 0 };
tempByteArray2 = GetRandomByteArray(random);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
}
[Fact]
public static void RunMultiply_OneSmallOneZero()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - One small BigIntegers and zero
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomByteArray(random, 2);
tempByteArray2 = new byte[] { 0 };
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
tempByteArray1 = new byte[] { 0 };
tempByteArray2 = GetRandomByteArray(random, 2);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
}
[Fact]
public static void RunMultiply_AxiomXX1()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Axiom: X*1 = X
VerifyIdentityString(int.MaxValue + " " + BigInteger.One + " bMultiply", Int32.MaxValue.ToString());
VerifyIdentityString(long.MaxValue + " " + BigInteger.One + " bMultiply", Int64.MaxValue.ToString());
for (int i = 0; i < s_samples; i++)
{
string randBigInt = Print(GetRandomByteArray(random));
VerifyIdentityString(randBigInt + BigInteger.One + " bMultiply", randBigInt + "u+");
}
}
[Fact]
public static void RunMultiply_AxiomXX0()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Axiom: X*0 = 0
VerifyIdentityString(int.MaxValue + " " + BigInteger.Zero + " bMultiply", BigInteger.Zero.ToString());
VerifyIdentityString(long.MaxValue + " " + BigInteger.Zero + " bMultiply", BigInteger.Zero.ToString());
for (int i = 0; i < s_samples; i++)
{
string randBigInt = Print(GetRandomByteArray(random));
VerifyIdentityString(randBigInt + BigInteger.Zero + " bMultiply", BigInteger.Zero.ToString());
}
}
[Fact]
public static void RunMultiply_Commutat()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Axiom: a*b = b*a
VerifyIdentityString(int.MaxValue + " " + long.MaxValue + " bMultiply", long.MaxValue + " " + int.MaxValue + " bMultiply");
for (int i = 0; i < s_samples; i++)
{
string randBigInt1 = Print(GetRandomByteArray(random));
string randBigInt2 = Print(GetRandomByteArray(random));
VerifyIdentityString(randBigInt1 + randBigInt2 + "bMultiply", randBigInt2 + randBigInt1 + "bMultiply");
}
}
[Fact]
public static void RunMultiply_Boundary()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Check interesting cases for boundary conditions
// You'll either be shifting a 0 or 1 across the boundary
// 32 bit boundary n2=0
VerifyMultiplyString(Math.Pow(2, 32) + " 2 bMultiply");
// 32 bit boundary n1=0 n2=1
VerifyMultiplyString(Math.Pow(2, 33) + " 2 bMultiply");
}
[Fact]
public static void RunMultiplyKaratsubaBoundary()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
byte[] tempByteArray2 = new byte[0];
// Multiply Method - One Large BigInteger
for (int i = 0; i < s_samples; i++)
{
for (int d1 = -2; d1 <= 2; d1++)
{
tempByteArray1 = GetRandomByteArray(random, BigIntegerCalculator.MultiplyKaratsubaThreshold + d1);
for (int d2 = -4; d2 <= 4; d2++)
{
tempByteArray2 = GetRandomByteArray(random, (BigIntegerCalculator.MultiplyKaratsubaThreshold + 1) * 2 + d2);
VerifyMultiplyString(Print(tempByteArray1) + Print(tempByteArray2) + "bMultiply");
}
}
}
}
[Fact]
public static void RunMultiply_OnePositiveOneNegative()
{
Random random = new Random(s_seed);
byte[] tempByteArray1 = new byte[0];
// Multiply Method - One positive and one negative BigIntegers
for (int i = 0; i < s_samples; i++)
{
try
{
tempByteArray1 = GetRandomByteArray(random);
string randBigInt = Print(tempByteArray1);
VerifyMultiplyString(randBigInt + randBigInt + "uNegate bMultiply");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Array1: " + Print(tempByteArray1));
throw;
}
}
}
private static void VerifyMultiplyString(string opstring)
{
StackCalc sc = new StackCalc(opstring);
while (sc.DoNextOperation())
{
Assert.Equal(sc.snCalc.Peek().ToString(), sc.myCalc.Peek().ToString());
}
}
private static void VerifyIdentityString(string opstring1, string opstring2)
{
StackCalc sc1 = new StackCalc(opstring1);
while (sc1.DoNextOperation())
{
//Run the full calculation
sc1.DoNextOperation();
}
StackCalc sc2 = new StackCalc(opstring2);
while (sc2.DoNextOperation())
{
//Run the full calculation
sc2.DoNextOperation();
}
Assert.Equal(sc1.snCalc.Peek().ToString(), sc2.snCalc.Peek().ToString());
}
private static byte[] GetRandomByteArray(Random random)
{
return GetRandomByteArray(random, random.Next(0, 100));
}
private static byte[] GetRandomByteArray(Random random, int size)
{
return MyBigIntImp.GetRandomByteArray(random, size);
}
private static string Print(byte[] bytes)
{
return MyBigIntImp.Print(bytes);
}
}
}