-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathcast_to.cs
804 lines (624 loc) · 29 KB
/
cast_to.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using Xunit;
namespace System.Numerics.Tests
{
public class cast_toTest
{
public delegate void ExceptionGenerator();
private const int NumberOfRandomIterations = 10;
private static Random s_random = new Random(100);
[Fact]
public static void RunByteImplicitCastToBigIntegerTests()
{
// Byte Implicit Cast to BigInteger: Byte.MinValue
VerifyByteImplicitCastToBigInteger(byte.MinValue);
// Byte Implicit Cast to BigInteger: 0
VerifyByteImplicitCastToBigInteger(0);
// Byte Implicit Cast to BigInteger: 1
VerifyByteImplicitCastToBigInteger(1);
// Byte Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyByteImplicitCastToBigInteger((byte)s_random.Next(1, byte.MaxValue));
}
// Byte Implicit Cast to BigInteger: Byte.MaxValue
VerifyByteImplicitCastToBigInteger(byte.MaxValue);
}
[Fact]
public static void RunSByteImplicitCastToBigIntegerTests()
{
// SByte Implicit Cast to BigInteger: SByte.MinValue
VerifySByteImplicitCastToBigInteger(sbyte.MinValue);
// SByte Implicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySByteImplicitCastToBigInteger((sbyte)s_random.Next(sbyte.MinValue, 0));
}
// SByte Implicit Cast to BigInteger: -1
VerifySByteImplicitCastToBigInteger(-1);
// SByte Implicit Cast to BigInteger: 0
VerifySByteImplicitCastToBigInteger(0);
// SByte Implicit Cast to BigInteger: 1
VerifySByteImplicitCastToBigInteger(1);
// SByte Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySByteImplicitCastToBigInteger((sbyte)s_random.Next(1, sbyte.MaxValue));
}
// SByte Implicit Cast to BigInteger: SByte.MaxValue
VerifySByteImplicitCastToBigInteger(sbyte.MaxValue);
}
[Fact]
public static void RunUInt16ImplicitCastToBigIntegerTests()
{
// UInt16 Implicit Cast to BigInteger: UInt16.MinValue
VerifyUInt16ImplicitCastToBigInteger(ushort.MinValue);
// UInt16 Implicit Cast to BigInteger: 0
VerifyUInt16ImplicitCastToBigInteger(0);
// UInt16 Implicit Cast to BigInteger: 1
VerifyUInt16ImplicitCastToBigInteger(1);
// UInt16 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyUInt16ImplicitCastToBigInteger((ushort)s_random.Next(1, ushort.MaxValue));
}
// UInt16 Implicit Cast to BigInteger: UInt16.MaxValue
VerifyUInt16ImplicitCastToBigInteger(ushort.MaxValue);
}
[Fact]
public static void RunInt16ImplicitCastToBigIntegerTests()
{
// Int16 Implicit Cast to BigInteger: Int16.MinValue
VerifyInt16ImplicitCastToBigInteger(short.MinValue);
// Int16 Implicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt16ImplicitCastToBigInteger((short)s_random.Next(short.MinValue, 0));
}
// Int16 Implicit Cast to BigInteger: -1
VerifyInt16ImplicitCastToBigInteger(-1);
// Int16 Implicit Cast to BigInteger: 0
VerifyInt16ImplicitCastToBigInteger(0);
// Int16 Implicit Cast to BigInteger: 1
VerifyInt16ImplicitCastToBigInteger(1);
// Int16 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt16ImplicitCastToBigInteger((short)s_random.Next(1, short.MaxValue));
}
// Int16 Implicit Cast to BigInteger: Int16.MaxValue
VerifyInt16ImplicitCastToBigInteger(short.MaxValue);
}
[Fact]
public static void RunUInt32ImplicitCastToBigIntegerTests()
{
// UInt32 Implicit Cast to BigInteger: UInt32.MinValue
VerifyUInt32ImplicitCastToBigInteger(uint.MinValue);
// UInt32 Implicit Cast to BigInteger: 0
VerifyUInt32ImplicitCastToBigInteger(0);
// UInt32 Implicit Cast to BigInteger: 1
VerifyUInt32ImplicitCastToBigInteger(1);
// UInt32 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyUInt32ImplicitCastToBigInteger((uint)(uint.MaxValue * s_random.NextDouble()));
}
// UInt32 Implicit Cast to BigInteger: UInt32.MaxValue
VerifyUInt32ImplicitCastToBigInteger(uint.MaxValue);
}
[Fact]
public static void RunInt32ImplicitCastToBigIntegerTests()
{
// Int32 Implicit Cast to BigInteger: Int32.MinValue
VerifyInt32ImplicitCastToBigInteger(int.MinValue);
// Int32 Implicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt32ImplicitCastToBigInteger((int)s_random.Next(int.MinValue, 0));
}
// Int32 Implicit Cast to BigInteger: -1
VerifyInt32ImplicitCastToBigInteger(-1);
// Int32 Implicit Cast to BigInteger: 0
VerifyInt32ImplicitCastToBigInteger(0);
// Int32 Implicit Cast to BigInteger: 1
VerifyInt32ImplicitCastToBigInteger(1);
// Int32 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt32ImplicitCastToBigInteger((int)s_random.Next(1, int.MaxValue));
}
// Int32 Implicit Cast to BigInteger: Int32.MaxValue
VerifyInt32ImplicitCastToBigInteger(int.MaxValue);
}
[Fact]
public static void RunUInt64ImplicitCastToBigIntegerTests()
{
// UInt64 Implicit Cast to BigInteger: UInt64.MinValue
VerifyUInt64ImplicitCastToBigInteger(ulong.MinValue);
// UInt64 Implicit Cast to BigInteger: 0
VerifyUInt64ImplicitCastToBigInteger(0);
// UInt64 Implicit Cast to BigInteger: 1
VerifyUInt64ImplicitCastToBigInteger(1);
// UInt64 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyUInt64ImplicitCastToBigInteger((ulong)(ulong.MaxValue * s_random.NextDouble()));
}
// UInt64 Implicit Cast to BigInteger: UInt64.MaxValue
VerifyUInt64ImplicitCastToBigInteger(ulong.MaxValue);
}
[Fact]
public static void RunInt64ImplicitCastToBigIntegerTests()
{
// Int64 Implicit Cast to BigInteger: Int64.MinValue
VerifyInt64ImplicitCastToBigInteger(long.MinValue);
// Int64 Implicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt64ImplicitCastToBigInteger(((long)(long.MaxValue * s_random.NextDouble())) - long.MaxValue);
}
// Int64 Implicit Cast to BigInteger: -1
VerifyInt64ImplicitCastToBigInteger(-1);
// Int64 Implicit Cast to BigInteger: 0
VerifyInt64ImplicitCastToBigInteger(0);
// Int64 Implicit Cast to BigInteger: 1
VerifyInt64ImplicitCastToBigInteger(1);
// Int64 Implicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt64ImplicitCastToBigInteger((long)(long.MaxValue * s_random.NextDouble()));
}
// Int64 Implicit Cast to BigInteger: Int64.MaxValue
VerifyInt64ImplicitCastToBigInteger(long.MaxValue);
}
[Fact]
public static void RunSingleExplicitCastToBigIntegerTests()
{
float value;
// Single Explicit Cast to BigInteger: Single.NegativeInfinity
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)float.NegativeInfinity; });
// Single Explicit Cast to BigInteger: Single.MinValue
VerifySingleExplicitCastToBigInteger(float.MinValue);
// Single Explicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySingleExplicitCastToBigInteger(((float)(float.MaxValue * s_random.NextDouble())) - float.MaxValue);
}
// Single Explicit Cast to BigInteger: Random Non-Integral Negative > -100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySingleExplicitCastToBigInteger((float)(-100 * s_random.NextDouble()));
}
// Single Explicit Cast to BigInteger: -1
VerifySingleExplicitCastToBigInteger(-1);
// Single Explicit Cast to BigInteger: 0
VerifySingleExplicitCastToBigInteger(0);
// Single Explicit Cast to BigInteger: 1
VerifySingleExplicitCastToBigInteger(1);
// Single Explicit Cast to BigInteger: Random Non-Integral Positive < 100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySingleExplicitCastToBigInteger((float)(100 * s_random.NextDouble()));
}
// Single Explicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifySingleExplicitCastToBigInteger((float)(float.MaxValue * s_random.NextDouble()));
}
// Single Explicit Cast to BigInteger: Single.MaxValue
VerifySingleExplicitCastToBigInteger(float.MaxValue);
// Single Explicit Cast to BigInteger: Single.PositiveInfinity
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)float.PositiveInfinity; });
// double.IsInfinity(float.MaxValue * 2.0f) == false, but we don't want this odd behavior here
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)(float.MaxValue * 2.0f); });
// Single Explicit Cast to BigInteger: Single.Epsilon
VerifySingleExplicitCastToBigInteger(float.Epsilon);
// Single Explicit Cast to BigInteger: Single.NaN
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)float.NaN; });
//There are multiple ways to represent a NaN just try another one
// Single Explicit Cast to BigInteger: Single.NaN 2
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)ConvertInt32ToSingle(0x7FC00000); });
// Single Explicit Cast to BigInteger: Smallest Exponent
VerifySingleExplicitCastToBigInteger((float)Math.Pow(2, -126));
// Single Explicit Cast to BigInteger: Largest Exponent
VerifySingleExplicitCastToBigInteger((float)Math.Pow(2, 127));
// Single Explicit Cast to BigInteger: Largest number less then 1
value = 0;
for (int i = 1; i <= 24; ++i)
{
value += (float)(Math.Pow(2, -i));
}
VerifySingleExplicitCastToBigInteger(value);
// Single Explicit Cast to BigInteger: Smallest number greater then 1
value = (float)(1 + Math.Pow(2, -23));
VerifySingleExplicitCastToBigInteger(value);
// Single Explicit Cast to BigInteger: Largest number less then 2
value = 0;
for (int i = 1; i <= 23; ++i)
{
value += (float)(Math.Pow(2, -i));
}
value += 1;
VerifySingleExplicitCastToBigInteger(value);
}
[Fact]
public static void RunDoubleExplicitCastToBigIntegerTests()
{
double value;
// Double Explicit Cast to BigInteger: Double.NegativeInfinity
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)double.NegativeInfinity; });
// Double Explicit Cast to BigInteger: Double.MinValue
VerifyDoubleExplicitCastToBigInteger(double.MinValue);
// Double Explicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDoubleExplicitCastToBigInteger(((double)(double.MaxValue * s_random.NextDouble())) - double.MaxValue);
}
// Double Explicit Cast to BigInteger: Random Non-Integral Negative > -100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDoubleExplicitCastToBigInteger((double)(-100 * s_random.NextDouble()));
}
// Double Explicit Cast to BigInteger: -1
VerifyDoubleExplicitCastToBigInteger(-1);
// Double Explicit Cast to BigInteger: 0
VerifyDoubleExplicitCastToBigInteger(0);
// Double Explicit Cast to BigInteger: 1
VerifyDoubleExplicitCastToBigInteger(1);
// Double Explicit Cast to BigInteger: Random Non-Integral Positive < 100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDoubleExplicitCastToBigInteger((double)(100 * s_random.NextDouble()));
}
// Double Explicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDoubleExplicitCastToBigInteger((double)(double.MaxValue * s_random.NextDouble()));
}
// Double Explicit Cast to BigInteger: Double.MaxValue
VerifyDoubleExplicitCastToBigInteger(double.MaxValue);
// Double Explicit Cast to BigInteger: Double.PositiveInfinity
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)double.PositiveInfinity; });
// Double Explicit Cast to BigInteger: Double.Epsilon
VerifyDoubleExplicitCastToBigInteger(double.Epsilon);
// Double Explicit Cast to BigInteger: Double.NaN
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)double.NaN; });
//There are multiple ways to represent a NaN just try another one
// Double Explicit Cast to BigInteger: Double.NaN 2
Assert.Throws<OverflowException>(() => { BigInteger temp = (BigInteger)ConvertInt64ToDouble(0x7FF8000000000000); });
// Double Explicit Cast to BigInteger: Smallest Exponent
VerifyDoubleExplicitCastToBigInteger((double)Math.Pow(2, -1022));
// Double Explicit Cast to BigInteger: Largest Exponent
VerifyDoubleExplicitCastToBigInteger((double)Math.Pow(2, 1023));
// Double Explicit Cast to BigInteger: Largest number less then 1
value = 0;
for (int i = 1; i <= 53; ++i)
{
value += (double)(Math.Pow(2, -i));
}
VerifyDoubleExplicitCastToBigInteger(value);
// Double Explicit Cast to BigInteger: Smallest number greater then 1
value = (double)(1 + Math.Pow(2, -52));
VerifyDoubleExplicitCastToBigInteger(value);
// Double Explicit Cast to BigInteger: Largest number less then 2
value = 0;
for (int i = 1; i <= 52; ++i)
{
value += (double)(Math.Pow(2, -i));
}
value += 1;
VerifyDoubleExplicitCastToBigInteger(value);
}
[Fact]
public static void RunDecimalExplicitCastToBigIntegerTests()
{
decimal value;
// Decimal Explicit Cast to BigInteger: Decimal.MinValue
VerifyDecimalExplicitCastToBigInteger(decimal.MinValue);
// Decimal Explicit Cast to BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
value = new decimal(
s_random.Next(int.MinValue, int.MaxValue),
s_random.Next(int.MinValue, int.MaxValue),
s_random.Next(int.MinValue, int.MaxValue),
true,
(byte)s_random.Next(0, 29));
VerifyDecimalExplicitCastToBigInteger(value);
}
// Decimal Explicit Cast to BigInteger: -1
VerifyDecimalExplicitCastToBigInteger(-1);
// Decimal Explicit Cast to BigInteger: 0
VerifyDecimalExplicitCastToBigInteger(0);
// Decimal Explicit Cast to BigInteger: 1
VerifyDecimalExplicitCastToBigInteger(1);
// Decimal Explicit Cast to BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
value = new decimal(
s_random.Next(int.MinValue, int.MaxValue),
s_random.Next(int.MinValue, int.MaxValue),
s_random.Next(int.MinValue, int.MaxValue),
false,
(byte)s_random.Next(0, 29));
VerifyDecimalExplicitCastToBigInteger(value);
}
// Decimal Explicit Cast to BigInteger: Decimal.MaxValue
VerifyDecimalExplicitCastToBigInteger(decimal.MaxValue);
// Decimal Explicit Cast to BigInteger: Smallest Exponent
unchecked
{
value = new decimal(1, 0, 0, false, 0);
}
VerifyDecimalExplicitCastToBigInteger(value);
// Decimal Explicit Cast to BigInteger: Largest Exponent and zero integer
unchecked
{
value = new decimal(0, 0, 0, false, 28);
}
VerifyDecimalExplicitCastToBigInteger(value);
// Decimal Explicit Cast to BigInteger: Largest Exponent and non zero integer
unchecked
{
value = new decimal(1, 0, 0, false, 28);
}
VerifyDecimalExplicitCastToBigInteger(value);
// Decimal Explicit Cast to BigInteger: Largest number less then 1
value = 1 - new decimal(1, 0, 0, false, 28);
VerifyDecimalExplicitCastToBigInteger(value);
// Decimal Explicit Cast to BigInteger: Smallest number greater then 1
value = 1 + new decimal(1, 0, 0, false, 28);
VerifyDecimalExplicitCastToBigInteger(value);
// Decimal Explicit Cast to BigInteger: Largest number less then 2
value = 2 - new decimal(1, 0, 0, false, 28);
VerifyDecimalExplicitCastToBigInteger(value);
}
private static float ConvertInt32ToSingle(int value)
{
return BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
}
private static double ConvertInt64ToDouble(long value)
{
return BitConverter.ToDouble(BitConverter.GetBytes(value), 0);
}
private static void VerifyByteImplicitCastToBigInteger(byte value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (byte)bigInteger);
if (value != byte.MaxValue)
{
Assert.Equal((byte)(value + 1), (byte)(bigInteger + 1));
}
if (value != byte.MinValue)
{
Assert.Equal((byte)(value - 1), (byte)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifySByteImplicitCastToBigInteger(sbyte value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (sbyte)bigInteger);
if (value != sbyte.MaxValue)
{
Assert.Equal((sbyte)(value + 1), (sbyte)(bigInteger + 1));
}
if (value != sbyte.MinValue)
{
Assert.Equal((sbyte)(value - 1), (sbyte)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyUInt16ImplicitCastToBigInteger(ushort value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (ushort)bigInteger);
if (value != ushort.MaxValue)
{
Assert.Equal((ushort)(value + 1), (ushort)(bigInteger + 1));
}
if (value != ushort.MinValue)
{
Assert.Equal((ushort)(value - 1), (ushort)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyInt16ImplicitCastToBigInteger(short value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (short)bigInteger);
if (value != short.MaxValue)
{
Assert.Equal((short)(value + 1), (short)(bigInteger + 1));
}
if (value != short.MinValue)
{
Assert.Equal((short)(value - 1), (short)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyUInt32ImplicitCastToBigInteger(uint value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (uint)bigInteger);
if (value != uint.MaxValue)
{
Assert.Equal((uint)(value + 1), (uint)(bigInteger + 1));
}
if (value != uint.MinValue)
{
Assert.Equal((uint)(value - 1), (uint)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyInt32ImplicitCastToBigInteger(int value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (int)bigInteger);
if (value != int.MaxValue)
{
Assert.Equal((int)(value + 1), (int)(bigInteger + 1));
}
if (value != int.MinValue)
{
Assert.Equal((int)(value - 1), (int)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyUInt64ImplicitCastToBigInteger(ulong value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (ulong)bigInteger);
if (value != ulong.MaxValue)
{
Assert.Equal((ulong)(value + 1), (ulong)(bigInteger + 1));
}
if (value != ulong.MinValue)
{
Assert.Equal((ulong)(value - 1), (ulong)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifyInt64ImplicitCastToBigInteger(long value)
{
BigInteger bigInteger = value;
Assert.Equal(value, bigInteger);
Assert.Equal(value.ToString(), bigInteger.ToString());
Assert.Equal(value, (long)bigInteger);
if (value != long.MaxValue)
{
Assert.Equal((long)(value + 1), (long)(bigInteger + 1));
}
if (value != long.MinValue)
{
Assert.Equal((long)(value - 1), (long)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == value);
}
private static void VerifySingleExplicitCastToBigInteger(float value)
{
float expectedValue;
BigInteger bigInteger;
if (value < 0)
{
expectedValue = (float)Math.Ceiling(value);
}
else
{
expectedValue = (float)Math.Floor(value);
}
bigInteger = (BigInteger)value;
Assert.Equal(expectedValue, (float)bigInteger);
// Single can only accurately represent integers between -16777216 and 16777216 exclusive.
// ToString starts to become inaccurate at this point.
if (expectedValue < 16777216 && -16777216 < expectedValue)
{
Assert.Equal(expectedValue.ToString("G9"), bigInteger.ToString());
}
if (expectedValue != Math.Floor(float.MaxValue))
{
Assert.Equal((float)(expectedValue + 1), (float)(bigInteger + 1));
}
if (expectedValue != Math.Ceiling(float.MinValue))
{
Assert.Equal((float)(expectedValue - 1), (float)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == expectedValue);
}
private static void VerifyDoubleExplicitCastToBigInteger(double value)
{
double expectedValue;
BigInteger bigInteger;
if (value < 0)
{
expectedValue = Math.Ceiling(value);
}
else
{
expectedValue = Math.Floor(value);
}
bigInteger = (BigInteger)value;
Assert.Equal(expectedValue, (double)bigInteger);
// Double can only accurately represent integers between -9007199254740992 and 9007199254740992 exclusive.
// ToString starts to become inaccurate at this point.
if (expectedValue < 9007199254740992 && -9007199254740992 < expectedValue)
{
Assert.Equal(expectedValue.ToString(), bigInteger.ToString());
}
if (!float.IsInfinity((float)expectedValue))
{
Assert.Equal((double)(expectedValue + 1), (double)(bigInteger + 1));
Assert.Equal((double)(expectedValue - 1), (double)(bigInteger - 1));
}
VerifyBigIntegerUsingIdentities(bigInteger, 0 == expectedValue);
}
private static void VerifyDecimalExplicitCastToBigInteger(decimal value)
{
decimal expectedValue;
BigInteger bigInteger;
if (value < 0)
{
expectedValue = Math.Ceiling(value);
}
else
{
expectedValue = Math.Floor(value);
}
bigInteger = (BigInteger)value;
Assert.Equal(expectedValue.ToString(), bigInteger.ToString());
Assert.Equal(expectedValue, (decimal)bigInteger);
VerifyBigIntegerUsingIdentities(bigInteger, 0 == expectedValue);
if (expectedValue != Math.Floor(decimal.MaxValue))
{
Assert.Equal((decimal)(expectedValue + 1), (decimal)(bigInteger + 1));
}
if (expectedValue != Math.Ceiling(decimal.MinValue))
{
Assert.Equal((decimal)(expectedValue - 1), (decimal)(bigInteger - 1));
}
}
private static void VerifyBigIntegerUsingIdentities(BigInteger bigInteger, bool isZero)
{
BigInteger tempBigInteger = new BigInteger(bigInteger.ToByteArray());
Assert.Equal(bigInteger, tempBigInteger);
if (isZero)
{
Assert.Equal(BigInteger.Zero, bigInteger);
}
else
{
Assert.NotEqual(BigInteger.Zero, bigInteger);
// x/x = 1
Assert.Equal(BigInteger.One, bigInteger / bigInteger);
}
// (x + 1) - 1 = x
Assert.Equal(bigInteger, (bigInteger + BigInteger.One) - BigInteger.One);
// (x + 1) - x = 1
Assert.Equal(BigInteger.One, (bigInteger + BigInteger.One) - bigInteger);
// x - x = 0
Assert.Equal(BigInteger.Zero, bigInteger - bigInteger);
// x + x = 2x
Assert.Equal(2 * bigInteger, bigInteger + bigInteger);
// x/1 = x
Assert.Equal(bigInteger, bigInteger / BigInteger.One);
// 1 * x = x
Assert.Equal(bigInteger, BigInteger.One * bigInteger);
}
}
}