2010-03-06 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / System.Numerics / Test / System.Numerics / BigIntegerTest.cs
1 // BigIntegerTest.cs
2 //
3 // Authors:
4 // Rodrigo Kumpera <rkumpera@novell.com>
5 //
6 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
7 //
8
9 using System;
10 using System.Numerics;
11 using NUnit.Framework;
12
13
14 namespace MonoTests.System.Numerics
15 {
16         [TestFixture]
17         public class BigIntegerTest
18         {
19                 static byte[] huge_a = new byte[] {0x1D, 0x33, 0xFB, 0xFE, 0xB1, 0x2, 0x85, 0x44, 0xCA, 0xDC, 0xFB, 0x70, 0xD, 0x39, 0xB1, 0x47, 0xB6, 0xE6, 0xA2, 0xD1, 0x19, 0x1E, 0x9F, 0xE4, 0x3C, 0x1E, 0x16, 0x56, 0x13, 0x9C, 0x4D, 0xD3, 0x5C, 0x74, 0xC9, 0xBD, 0xFA, 0x56, 0x40, 0x58, 0xAC, 0x20, 0x6B, 0x55, 0xA2, 0xD5, 0x41, 0x38, 0xA4, 0x6D, 0xF6, 0x8C, };
20
21                 static byte[] huge_b = new byte[] {0x96, 0x5, 0xDA, 0xFE, 0x93, 0x17, 0xC1, 0x93, 0xEC, 0x2F, 0x30, 0x2D, 0x8F, 0x28, 0x13, 0x99, 0x70, 0xF4, 0x4C, 0x60, 0xA6, 0x49, 0x24, 0xF9, 0xB3, 0x4A, 0x41, 0x67, 0xDC, 0xDD, 0xB1, 0xA5, 0xA6, 0xC0, 0x3D, 0x57, 0x9A, 0xCB, 0x29, 0xE2, 0x94, 0xAC, 0x6C, 0x7D, 0xEF, 0x3E, 0xC6, 0x7A, 0xC1, 0xA8, 0xC8, 0xB0, 0x20, 0x95, 0xE6, 0x4C, 0xE1, 0xE0, 0x4B, 0x49, 0xD5, 0x5A, 0xB7, };
22
23                 static byte[] huge_add = new byte[] {0xB3, 0x38, 0xD5, 0xFD, 0x45, 0x1A, 0x46, 0xD8, 0xB6, 0xC, 0x2C, 0x9E, 0x9C, 0x61, 0xC4, 0xE0, 0x26, 0xDB, 0xEF, 0x31, 0xC0, 0x67, 0xC3, 0xDD, 0xF0, 0x68, 0x57, 0xBD, 0xEF, 0x79, 0xFF, 0x78, 0x3, 0x35, 0x7, 0x15, 0x95, 0x22, 0x6A, 0x3A, 0x41, 0xCD, 0xD7, 0xD2, 0x91, 0x14, 0x8, 0xB3, 0x65, 0x16, 0xBF, 0x3D, 0x20, 0x95, 0xE6, 0x4C, 0xE1, 0xE0, 0x4B, 0x49, 0xD5, 0x5A, 0xB7, };
24
25                 static byte[] a_m_b = new byte[] { 0x87, 0x2D, 0x21, 0x0, 0x1E, 0xEB, 0xC3, 0xB0, 0xDD, 0xAC, 0xCB, 0x43, 0x7E, 0x10, 0x9E, 0xAE, 0x45, 0xF2, 0x55, 0x71, 0x73, 0xD4, 0x7A, 0xEB, 0x88, 0xD3, 0xD4, 0xEE, 0x36, 0xBE, 0x9B, 0x2D, 0xB6, 0xB3, 0x8B, 0x66, 0x60, 0x8B, 0x16, 0x76, 0x17, 0x74, 0xFE, 0xD7, 0xB2, 0x96, 0x7B, 0xBD, 0xE2, 0xC4, 0x2D, 0xDC, 0xDE, 0x6A, 0x19, 0xB3, 0x1E, 0x1F, 0xB4, 0xB6, 0x2A, 0xA5, 0x48, };\r
26                 static byte[] b_m_a = new byte[] { 0x79, 0xD2, 0xDE, 0xFF, 0xE1, 0x14, 0x3C, 0x4F, 0x22, 0x53, 0x34, 0xBC, 0x81, 0xEF, 0x61, 0x51, 0xBA, 0xD, 0xAA, 0x8E, 0x8C, 0x2B, 0x85, 0x14, 0x77, 0x2C, 0x2B, 0x11, 0xC9, 0x41, 0x64, 0xD2, 0x49, 0x4C, 0x74, 0x99, 0x9F, 0x74, 0xE9, 0x89, 0xE8, 0x8B, 0x1, 0x28, 0x4D, 0x69, 0x84, 0x42, 0x1D, 0x3B, 0xD2, 0x23, 0x21, 0x95, 0xE6, 0x4C, 0xE1, 0xE0, 0x4B, 0x49, 0xD5, 0x5A, 0xB7, };\r
27
28                 static byte[] huge_mul = new byte[] { 0xFE, 0x83, 0xE1, 0x9B, 0x8D, 0x61, 0x40, 0xD1, 0x60, 0x19, 0xBD, 0x38, 0xF0, 0xFF, 0x90, 0xAE, 0xDD, 0xAE, 0x73, 0x2C, 0x20, 0x23, 0xCF, 0x6, 0x7A, 0xB4, 0x1C, 0xE7, 0xD9, 0x64, 0x96, 0x2C, 0x87, 0x7E, 0x1D, 0xB3, 0x8F, 0xD4, 0x33, 0xBA, 0xF4, 0x22, 0xB4, 0xDB, 0xC0, 0x5B, 0xA5, 0x64, 0xA0, 0xBC, 0xCA, 0x3E, 0x94, 0x95, 0xDA, 0x49, 0xE2, 0xA8, 0x33, 0xA2, 0x6A, 0x33, 0xB1, 0xF2, 0xEA, 0x99, 0x32, 0xD0, 0xB2, 0xAE, 0x55, 0x75, 0xBD, 0x19, 0xFC, 0x9A, 0xEC, 0x54, 0x87, 0x2A, 0x6, 0xCC, 0x78, 0xDA, 0x88, 0xBB, 0xAB, 0xA5, 0x47, 0xEF, 0xC7, 0x2B, 0xC7, 0x5B, 0x32, 0x31, 0xCD, 0xD9, 0x53, 0x96, 0x1A, 0x9D, 0x9A, 0x57, 0x40, 0x51, 0xB6, 0x5D, 0xC, 0x17, 0xD1, 0x86, 0xE9, 0xA4, 0x20, };
29
30                 static byte[] huge_div = new byte[] { 0x0, };\r
31                 static byte[] huge_rem = new byte[] { 0x1D, 0x33, 0xFB, 0xFE, 0xB1, 0x2, 0x85, 0x44, 0xCA, 0xDC, 0xFB, 0x70, 0xD, 0x39, 0xB1, 0x47, 0xB6, 0xE6, 0xA2, 0xD1, 0x19, 0x1E, 0x9F, 0xE4, 0x3C, 0x1E, 0x16, 0x56, 0x13, 0x9C, 0x4D, 0xD3, 0x5C, 0x74, 0xC9, 0xBD, 0xFA, 0x56, 0x40, 0x58, 0xAC, 0x20, 0x6B, 0x55, 0xA2, 0xD5, 0x41, 0x38, 0xA4, 0x6D, 0xF6, 0x8C, };\r
32                 static byte[][] add_a = new byte[][] {
33                         new byte[] {1},
34                         new byte[] {0xFF},
35                         huge_a
36                 };
37
38                 static byte[][] add_b = new byte[][] {
39                         new byte[] {1},
40                         new byte[] {1},
41                         huge_b
42                 };
43
44                 static byte[][] add_c = new byte[][] {
45                         new byte[] {2},
46                         new byte[] {0},
47                         huge_add
48                 };
49
50                 [Test]
51                 public void Mul () {
52                         long[] values = new long [] { -1000000000L, -1000, -1, 0, 1, 1000, 100000000L };
53                         for (int i = 0; i < values.Length; ++i) {
54                                 for (int j = 0; j < values.Length; ++j) {
55                                         var a = new BigInteger (values [i]);
56                                         var b = new BigInteger (values [j]);
57                                         var c = a * b;
58                                         Assert.AreEqual (values [i] * values [j], (long)c, "#_" + i + "_" + j);
59                                 }
60                         }
61                 }
62
63                 [Test]
64                 public void TestHugeMul () {
65                         var a = new BigInteger (huge_a);
66                         var b = new BigInteger (huge_b);
67                         Assert.AreEqual (huge_mul, (a * b).ToByteArray (), "#1");
68                 }
69
70
71                 [Test]
72                 public void DivRem () {
73                         long[] values = new long [] { -10000000330L, -5000, -1, 0, 1, 1000, 333, 10234544400L };
74                         for (int i = 0; i < values.Length; ++i) {
75                                 for (int j = 0; j < values.Length; ++j) {
76                                         if (values [j] == 0)
77                                                 continue;
78                                         var a = new BigInteger (values [i]);
79                                         var b = new BigInteger (values [j]);
80                                         BigInteger c, d;
81                                         c = BigInteger.DivRem (a, b, out d);
82
83                                         Assert.AreEqual (values [i] / values [j], (long)c, "#a_" + i + "_" + j);
84                                         Assert.AreEqual (values [i] % values [j], (long)d, "#b_" + i + "_" + j);
85                                 }
86                         }
87                 }
88
89                 [Test]
90                 public void TestHugeDivRem () {
91                         var a = new BigInteger (huge_a);
92                         var b = new BigInteger (huge_b);
93                         BigInteger c, d;
94                         c = BigInteger.DivRem (a, b, out d);
95
96                         Assert.AreEqual (huge_div, c.ToByteArray (), "#1");
97                         Assert.AreEqual (huge_rem, d.ToByteArray (), "#2");
98                 }
99
100                 [Test]
101                 public void Pow () {
102                         try {
103                                 BigInteger.Pow (1, -1);
104                                 Assert.Fail ("#1");
105                         } catch (ArgumentOutOfRangeException) {}
106                         
107                         Assert.AreEqual (1, (int)BigInteger.Pow (99999, 0), "#2");
108                         Assert.AreEqual (99999, (int)BigInteger.Pow (99999, 1), "#5");
109                         Assert.AreEqual (59049, (int)BigInteger.Pow (3, 10), "#4");
110                         Assert.AreEqual (177147, (int)BigInteger.Pow (3, 11), "#5");
111                 }
112
113
114                 [Test]
115                 public void DivRemByZero () {
116                         try {
117                                 BigInteger c, d;
118                                 c = BigInteger.DivRem (100, 0, out d);
119                                 Assert.Fail ("#1");
120                         } catch (DivideByZeroException) {}
121                 }
122
123                 [Test]
124                 public void TestAdd () {
125                         for (int i = 0; i < add_a.Length; ++i) {
126                                 var a = new BigInteger (add_a [i]);
127                                 var b = new BigInteger (add_b [i]);
128                                 var c = new BigInteger (add_c [i]);
129
130                                 Assert.AreEqual (c, a + b, "#" + i + "a");
131                                 Assert.AreEqual (c, b + a, "#" + i + "b");
132                                 Assert.AreEqual (c, BigInteger.Add (a, b), "#" + i + "c");
133                                 Assert.AreEqual (add_c [i], (a + b).ToByteArray (), "#" + i + "d");
134                         }
135                 }
136
137                 [Test]
138                 public void TestAdd2 () {
139                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
140                         for (int i = 0; i < values.Length; ++i) {
141                                 for (int j = 0; j < values.Length; ++j) {
142                                         var a = new BigInteger (values [i]);
143                                         var b = new BigInteger (values [j]);
144                                         var c = a + b;
145                                         Assert.AreEqual (values [i] + values [j], (long)c, "#_" + i + "_" + j);
146                                 }
147                         }
148                 }
149
150                 [Test]
151                 public void TestHugeSub () {
152                         var a = new BigInteger (huge_a);
153                         var b = new BigInteger (huge_b);
154                         Assert.AreEqual (a_m_b, (a - b).ToByteArray (), "#1");
155                         Assert.AreEqual (b_m_a, (b - a).ToByteArray (), "#2");
156                 }
157
158                 [Test]
159                 public void TestSub () {
160                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
161                         for (int i = 0; i < values.Length; ++i) {
162                                 for (int j = 0; j < values.Length; ++j) {
163                                         var a = new BigInteger (values [i]);
164                                         var b = new BigInteger (values [j]);
165                                         var c = a - b;
166                                         var d = BigInteger.Subtract (a, b);
167
168                                         Assert.AreEqual (values [i] - values [j], (long)c, "#_" + i + "_" + j);
169                                         Assert.AreEqual (values [i] - values [j], (long)d, "#_" + i + "_" + j);
170                                 }
171                         }
172                 }
173
174                 [Test]
175                 public void TestMin () {
176                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
177                         for (int i = 0; i < values.Length; ++i) {
178                                 for (int j = 0; j < values.Length; ++j) {
179                                         var a = new BigInteger (values [i]);
180                                         var b = new BigInteger (values [j]);
181                                         var c = BigInteger.Min (a, b);
182
183                                         Assert.AreEqual (Math.Min (values [i], values [j]), (long)c, "#_" + i + "_" + j);
184                                 }
185                         }
186                 }
187
188                 [Test]
189                 public void TestMax () {
190                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
191                         for (int i = 0; i < values.Length; ++i) {
192                                 for (int j = 0; j < values.Length; ++j) {
193                                         var a = new BigInteger (values [i]);
194                                         var b = new BigInteger (values [j]);
195                                         var c = BigInteger.Max (a, b);
196
197                                         Assert.AreEqual (Math.Max (values [i], values [j]), (long)c, "#_" + i + "_" + j);
198                                 }
199                         }
200                 }
201
202                 [Test]
203                 public void TestAbs () {
204                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
205                         for (int i = 0; i < values.Length; ++i) {
206                                 var a = new BigInteger (values [i]);
207                                 var c = BigInteger.Abs (a);
208
209                                 Assert.AreEqual (Math.Abs (values [i]), (long)c, "#_" + i);
210                         }
211                 }
212
213                 [Test]
214                 public void TestNegate () {
215                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
216                         for (int i = 0; i < values.Length; ++i) {
217                                 var a = new BigInteger (values [i]);
218                                 var c = -a;
219                                 var d = BigInteger.Negate (a);
220
221                                 Assert.AreEqual (-values [i], (long)c, "#_" + i);
222                                 Assert.AreEqual (-values [i], (long)d, "#_" + i);
223                         }
224                 }
225
226                 [Test]
227                 public void TestInc () {
228                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
229                         for (int i = 0; i < values.Length; ++i) {
230                                 var a = new BigInteger (values [i]);
231                                 var b = ++a;
232
233                                 Assert.AreEqual (++values [i], (long)b, "#_" + i);
234                         }
235                 }
236
237                 [Test]
238                 public void TestDec () {
239                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
240                         for (int i = 0; i < values.Length; ++i) {
241                                 var a = new BigInteger (values [i]);
242                                 var b = --a;
243
244                                 Assert.AreEqual (--values [i], (long)b, "#_" + i);
245                         }
246                 }
247
248                 [Test]
249                 public void TestBitwiseOps () {
250                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L, 0xFFFF00000000L };
251                         for (int i = 0; i < values.Length; ++i) {
252                                 for (int j = 0; j < values.Length; ++j) {
253                                         var a = new BigInteger (values [i]);
254                                         var b = new BigInteger (values [j]);
255
256
257                                         Assert.AreEqual (values [i] | values [j], (long)(a | b) , "#b_" + i + "_" + j);
258                                         Assert.AreEqual (values [i] & values [j], (long)(a & b) , "#a_" + i + "_" + j);
259                                         Assert.AreEqual (values [i] ^ values [j], (long)(a ^ b) , "#c_" + i + "_" + j);
260                                         Assert.AreEqual (~values [i], (long)~a , "#d_" + i + "_" + j);
261                                 }
262                         }
263                 }
264
265                 [Test]
266                 public void TestLeftShift () {
267                         Assert.AreEqual (new byte[] {0x00, 0x28},
268                                 (new BigInteger(0x0A) << 10).ToByteArray (), "#1");
269                         Assert.AreEqual (new byte[] {0x00, 0xD8},
270                                 (new BigInteger(-10) << 10).ToByteArray (), "#2");
271                         Assert.AreEqual (new byte[] {0x00, 0x00, 0xFF},
272                                 (new BigInteger(-1) << 16).ToByteArray (), "#3");
273                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A},
274                                 (new BigInteger(0x0A) << 80).ToByteArray (), "#4");
275                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6},
276                                 (new BigInteger(-10) << 80).ToByteArray (), "#5");
277                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF},
278                                 (new BigInteger(-1) << 80).ToByteArray (), "#6");
279                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xD9},
280                                 (new BigInteger(-1234) << 75).ToByteArray (), "#7");
281                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x91, 0x00},
282                                 (new BigInteger(0x1234) << 75).ToByteArray (), "#8");
283
284                         Assert.AreEqual (new byte[] {0xFF, 0x00}, (new BigInteger(0xFF00) << -8).ToByteArray (), "#9");
285                 }
286
287                 [Test]
288                 public void TestRightShift () {
289                         Assert.AreEqual (new byte[] {0x16, 0xB0, 0x4C, 0x02},
290                                 (new BigInteger(1234567899L) >> 5).ToByteArray (), "#1");
291
292                         Assert.AreEqual (new byte[] {0x2C, 0x93, 0x00},
293                                 (new BigInteger(1234567899L) >> 15).ToByteArray (), "#2");
294
295                         Assert.AreEqual (new byte[] {0xFF, 0xFF, 0x7F},
296                                 (new BigInteger(long.MaxValue - 100) >> 40).ToByteArray (), "#3");
297
298                         Assert.AreEqual (new byte[] {0xE9, 0x4F, 0xB3, 0xFD},
299                                 (new BigInteger(-1234567899L) >> 5).ToByteArray (), "#4");
300
301                         Assert.AreEqual (new byte[] {0xD3, 0x6C, 0xFF},
302                                 (new BigInteger(-1234567899L) >> 15).ToByteArray (), "#5");
303
304                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x80},
305                                 (new BigInteger(long.MinValue + 100) >> 40).ToByteArray (), "#6");
306
307                         Assert.AreEqual (new byte[] { 0xFF },
308                                 (new BigInteger(-1234567899L) >> 90).ToByteArray (), "#7");
309
310                         Assert.AreEqual (new byte[] {0x00},
311                                 (new BigInteger(999999) >> 90).ToByteArray (), "#8");
312
313                         Assert.AreEqual (new byte[] {0x00, 0x00, 0xFF, 0x00}, (new BigInteger(0xFF00) >> -8).ToByteArray (), "#9");
314                 }
315
316                 [Test]
317                 public void CompareOps () {
318                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
319                         for (int i = 0; i < values.Length; ++i) {
320                                 for (int j = 0; j < values.Length; ++j) {
321                                         var a = new BigInteger (values [i]);
322                                         var b = new BigInteger (values [j]);
323                                         
324                                         Assert.AreEqual (values [i].CompareTo (values [j]), a.CompareTo (b), "#a_" + i + "_" + j);
325                                         Assert.AreEqual (values [i].CompareTo (values [j]), BigInteger.Compare (a, b), "#b_" + i + "_" + j);
326
327                                         Assert.AreEqual (values [i] < values [j], a < b, "#c_" + i + "_" + j);
328                                         Assert.AreEqual (values [i] <= values [j], a <= b, "#d_" + i + "_" + j);
329                                         Assert.AreEqual (values [i] == values [j], a == b, "#e_" + i + "_" + j);
330                                         Assert.AreEqual (values [i] != values [j], a != b, "#f_" + i + "_" + j);
331                                         Assert.AreEqual (values [i] >= values [j], a >= b, "#g_" + i + "_" + j);
332                                         Assert.AreEqual (values [i] > values [j], a > b, "#h_" + i + "_" + j);
333                                 }
334                         }
335                 }
336
337                 [Test]
338                 public void CompareOps2 () {
339                         BigInteger a = new BigInteger (100000000000L);
340                         BigInteger b = new BigInteger (28282828282UL);
341
342                         Assert.IsTrue (a >= b, "#1");
343                         Assert.IsTrue (a >= b, "#2");
344                         Assert.IsFalse (a < b, "#3");
345                         Assert.IsFalse (a <= b, "#4");
346                         Assert.AreEqual (1, a.CompareTo (b), "#5");
347                 }
348
349                 [Test]
350                 public void CompareULong () {
351                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L, 0xAA00000000L };
352                         ulong[] uvalues = new ulong [] {0, 1, 1000, 100000000000L, 999999, 28282828282, 0xAA00000000, ulong.MaxValue };
353                         for (int i = 0; i < values.Length; ++i) {
354                                 for (int j = 0; j < uvalues.Length; ++j) {
355                                         var a = new BigInteger (values [i]);
356                                         var b = uvalues [j];
357                                         var c = new BigInteger (b);
358                                         
359                                         Assert.AreEqual (a.CompareTo (c), a.CompareTo (b), "#a_" + i + "_" + j);
360
361                                         Assert.AreEqual (a > c, a > b, "#b_" + i + "_" + j);
362                                         Assert.AreEqual (a < c, a < b, "#c_" + i + "_" + j);
363                                         Assert.AreEqual (a <= c, a <= b, "#d_" + i + "_" + j);
364                                         Assert.AreEqual (a == c, a == b, "#e_" + i + "_" + j);
365                                         Assert.AreEqual (a != c, a != b, "#f_" + i + "_" + j);
366                                         Assert.AreEqual (a >= c, a >= b, "#g_" + i + "_" + j);
367
368                                         Assert.AreEqual (c > a, b > a, "#ib_" + i + "_" + j);
369                                         Assert.AreEqual (c < a, b < a, "#ic_" + i + "_" + j);
370                                         Assert.AreEqual (c <= a, b <= a, "#id_" + i + "_" + j);
371                                         Assert.AreEqual (c == a, b == a, "#ie_" + i + "_" + j);
372                                         Assert.AreEqual (c != a, b != a, "#if_" + i + "_" + j);
373                                         Assert.AreEqual (c >= a, b >= a, "#ig_" + i + "_" + j);
374                                 }
375                         }
376                 }
377
378                 [Test]
379                 public void CompareLong () {
380                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 9999999, 100000000000L, 0xAA00000000, long.MaxValue, long.MinValue };
381
382                         for (int i = 0; i < values.Length; ++i) {
383                                 for (int j = 0; j < values.Length; ++j) {
384                                         var a = new BigInteger (values [i]);
385                                         var b = values [j];
386                                         var c = new BigInteger (b);
387                                         
388                                         Assert.AreEqual (a.CompareTo (c), a.CompareTo (b), "#a_" + i + "_" + j);
389
390                                         Assert.AreEqual (a > c, a > b, "#b_" + i + "_" + j);
391                                         Assert.AreEqual (a < c, a < b, "#c_" + i + "_" + j);
392                                         Assert.AreEqual (a <= c, a <= b, "#d_" + i + "_" + j);
393                                         Assert.AreEqual (a == c, a == b, "#e_" + i + "_" + j);
394                                         Assert.AreEqual (a != c, a != b, "#f_" + i + "_" + j);
395                                         Assert.AreEqual (a >= c, a >= b, "#g_" + i + "_" + j);
396
397                                         Assert.AreEqual (c > a, b > a, "#ib_" + i + "_" + j);
398                                         Assert.AreEqual (c < a, b < a, "#ic_" + i + "_" + j);
399                                         Assert.AreEqual (c <= a, b <= a, "#id_" + i + "_" + j);
400                                         Assert.AreEqual (c == a, b == a, "#ie_" + i + "_" + j);
401                                         Assert.AreEqual (c != a, b != a, "#if_" + i + "_" + j);
402                                         Assert.AreEqual (c >= a, b >= a, "#ig_" + i + "_" + j);
403                                 }
404                         }
405                 }
406
407                 [Test]
408                 public void TestEquals () {
409                                 var a = new BigInteger (10);
410                                 var b = new BigInteger (10);
411                                 var c = new BigInteger (-10);
412
413                                 Assert.AreEqual (a, b, "#1");
414                                 Assert.AreNotEqual (a, c, "#2");
415                                 Assert.AreNotEqual (a, 10, "#3");
416                 }
417
418                 [Test]
419                 public void ByteArrayCtor ()
420                 {
421                         try {
422                                 new BigInteger (null);
423                                 Assert.Fail ("#1");
424                         } catch (ArgumentNullException) {}
425
426                         Assert.AreEqual (0, (int)new BigInteger (new byte [0]), "#2");
427                 }
428
429                 [Test]
430                 public void IntCtorRoundTrip ()
431                 {
432                         int[] values = new int [] {
433                                 int.MinValue, -0x2F33BB, -0x1F33, -0x33, 0, 0x33,
434                                 0x80, 0x8190, 0xFF0011, 0x1234, 0x11BB99, 0x44BB22CC,
435                                 int.MaxValue };
436                         foreach (var val in values) {
437                                 var a = new BigInteger (val);
438                                 var b = new BigInteger (a.ToByteArray ());
439
440                                 Assert.AreEqual (val, (int)a, "#a_" + val);
441                                 Assert.AreEqual (val, (int)b, "#b_" + val);
442                         }
443                 }
444
445                 [Test]
446                 public void LongCtorRoundTrip ()
447                 {
448                         long[] values = new long [] {
449                                 0, long.MinValue, long.MaxValue, -1, 1L + int.MaxValue, -1L + int.MinValue, 0x1234, 0xFFFFFFFFL, 0x1FFFFFFFFL, -0xFFFFFFFFL, -0x1FFFFFFFFL,
450                                 0x100000000L, -0x100000000L, 0x100000001L, -0x100000001L };
451                         foreach (var val in values) {
452                                 var a = new BigInteger (val);
453                                 var b = new BigInteger (a.ToByteArray ());
454
455                                 Assert.AreEqual (val, (long)a, "#a_" + val);
456                                 Assert.AreEqual (val, (long)b, "#b_" + val);
457                         }
458                 }
459
460                 [Test]
461                 public void ByteArrayCtorRoundTrip ()
462                 {
463                         var arr = new byte [] { 1,2,3,4,5,6,7,8,9 };
464                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#1");
465
466                         arr = new byte [] { 1,2,3,4,5,6,7,8,0xFF, 0x0};
467                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#2");
468
469                         arr = new byte [] { 1,2,3,4,5,6,7,8,9, 0xF0 };
470                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#3");
471
472                         arr = new byte [] { 1};
473                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#4");
474
475                         arr = new byte [] { 1,2 };
476                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#5");
477
478                         arr = new byte [] { 1,2,3 };
479                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#6");
480
481                         arr = new byte [] { 1,2,3,4 };
482                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#7");
483
484                         arr = new byte [] { 1,2,3,4,5 };
485                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#8");
486
487                         arr = new byte [] { 1,2,3,4,5,6 };
488                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#9");
489
490                         arr = new byte [] { 1,2,3,4,5,6,7 };
491                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#10");
492
493                         arr = new byte [] { 1,2,3,4,5 };
494                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#11");
495
496                         arr = new byte [] { 0 };
497                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#12");
498
499                         arr = new byte [] { 0xFF, 00 };
500                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#13");
501
502                         arr = new byte [] { 1, 0, 0, 0, 0, 0, };
503                         Assert.AreEqual (new byte [1] {1}, new BigInteger (arr).ToByteArray (), "#14");
504                 }
505
506                 [Test]
507                 public void TestIntCtorProperties ()
508                 {
509                         BigInteger a = new BigInteger (10);
510                         Assert.IsTrue (a.IsEven, "#1");
511                         Assert.IsFalse (a.IsOne, "#2");
512                         Assert.IsFalse (a.IsPowerOfTwo, "#3");
513                         Assert.IsFalse (a.IsZero, "#4");
514                         Assert.AreEqual (1, a.Sign, "#5");
515
516                         Assert.IsFalse (new BigInteger (11).IsEven, "#6");
517                         Assert.IsTrue (new BigInteger (1).IsOne, "#7");
518                         Assert.IsTrue (new BigInteger (32).IsPowerOfTwo, "#8");
519                         Assert.IsTrue (new BigInteger (0).IsZero, "#9");
520                         Assert.AreEqual (0, new BigInteger (0).Sign, "#10");
521                         Assert.AreEqual (-1, new BigInteger (-99999).Sign, "#11");
522
523                         Assert.IsFalse (new BigInteger (0).IsPowerOfTwo, "#12");
524                         Assert.IsFalse (new BigInteger (-16).IsPowerOfTwo, "#13");
525                         Assert.IsTrue (new BigInteger (1).IsPowerOfTwo, "#14");
526                 }
527
528                 /*[Test]
529                 public void TestIntCtorToString ()
530                 {
531                         Assert.AreEqual ("5555", new BigInteger (5555).ToString (), "#1");
532                         Assert.AreEqual ("-99999", new BigInteger (-99999).ToString (), "#2");
533                 }*/
534
535                 [Test]
536                 public void TestToIntOperator ()
537                 {
538                         try {
539                                 int v = (int)new BigInteger (huge_a);
540                                 Assert.Fail ("#1");
541                         } catch (OverflowException) {}
542
543                         try {
544                                 int v = (int)new BigInteger (1L + int.MaxValue);
545                                 Assert.Fail ("#2");
546                         } catch (OverflowException) {}
547
548                         try {
549                                 int v = (int)new BigInteger (-1L + int.MinValue);
550                                 Assert.Fail ("#3");
551                         } catch (OverflowException) {}
552
553                         Assert.AreEqual (int.MaxValue, (int)new BigInteger (int.MaxValue), "#4");
554                         Assert.AreEqual (int.MinValue, (int)new BigInteger (int.MinValue), "#5");
555                 }
556
557
558                 [Test]
559                 public void TestToLongOperator ()
560                 {
561                         try {
562                                 long v = (long)new BigInteger (huge_a);
563                                 Assert.Fail ("#1");
564                         } catch (OverflowException) {}
565
566                         //long.MaxValue + 1
567                         try {
568                                 long v = (long)new BigInteger (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00 });
569                                 Assert.Fail ("#2");
570                         } catch (OverflowException) {}
571
572                         //TODO long.MinValue - 1
573                         try {
574                                 long v = (long)new BigInteger (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF });
575                                 Assert.Fail ("#3");
576                         } catch (OverflowException) {}
577
578                         Assert.AreEqual (long.MaxValue, (long)new BigInteger (long.MaxValue), "#4");
579                         Assert.AreEqual (long.MinValue, (long)new BigInteger (long.MinValue), "#5");
580                 }
581
582                 [Test]
583                 public void TestIntCtorToByteArray ()
584                 {
585                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-1).ToByteArray (), "#1");
586                         Assert.AreEqual (new byte[] { 0xD4, 0xFE }, new BigInteger (-300).ToByteArray (), "#2");
587                         Assert.AreEqual (new byte[] { 0x80, 0x00 }, new BigInteger (128).ToByteArray (), "#3");
588                         Assert.AreEqual (new byte[] { 0x00, 0x60 }, new BigInteger (0x6000).ToByteArray (), "#4");
589                         Assert.AreEqual (new byte[] { 0x00, 0x80, 0x00 }, new BigInteger (0x8000).ToByteArray (), "#5");
590                         Assert.AreEqual (new byte[] { 0xDD, 0xBC, 0x00, 0x7A }, new BigInteger (0x7A00BCDD).ToByteArray (), "#6");
591                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (int.MaxValue).ToByteArray (), "#7");
592                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue).ToByteArray (), "#8");
593                         Assert.AreEqual (new byte[] { 0x01, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue + 1).ToByteArray (), "#9");
594                         Assert.AreEqual (new byte[] { 0x7F }, new BigInteger (0x7F).ToByteArray (), "#10");
595                         Assert.AreEqual (new byte[] { 0x45, 0xCC, 0xD0 }, new BigInteger (-0x2F33BB).ToByteArray (), "#11");
596                         Assert.AreEqual (new byte[] { 0 }, new BigInteger (0).ToByteArray (), "#12");
597                 }
598
599                 [Test]
600                 public void TestLongCtorToByteArray ()
601                 {
602                         Assert.AreEqual (new byte[] { 0x01 }, new BigInteger (0x01L).ToByteArray (), "#1");
603                         Assert.AreEqual (new byte[] { 0x02, 0x01 }, new BigInteger (0x0102L).ToByteArray (), "#2");
604                         Assert.AreEqual (new byte[] { 0x03, 0x02, 0x01 }, new BigInteger (0x010203L).ToByteArray (), "#3");
605                         Assert.AreEqual (new byte[] { 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304L).ToByteArray (), "#4");
606                         Assert.AreEqual (new byte[] { 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405L).ToByteArray (), "#5");
607                         Assert.AreEqual (new byte[] { 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x010203040506L).ToByteArray (), "#6");
608                         Assert.AreEqual (new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304050607L).ToByteArray (), "#7");
609                         Assert.AreEqual (new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405060708L).ToByteArray (), "#8");
610
611                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-0x01L).ToByteArray (), "#1m");
612                         Assert.AreEqual (new byte[] { 0xFE, 0xFE}, new BigInteger (-0x0102L).ToByteArray (), "#2m");
613                         Assert.AreEqual (new byte[] { 0xFD, 0xFD, 0xFE }, new BigInteger (-0x010203L).ToByteArray (), "#3m");
614                         Assert.AreEqual (new byte[] { 0xFC, 0xFC, 0xFD, 0xFE}, new BigInteger (-0x01020304L).ToByteArray (), "#4m");
615                         Assert.AreEqual (new byte[] { 0xFB, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405L).ToByteArray (), "#5m");
616                         Assert.AreEqual (new byte[] { 0xFA, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x010203040506L).ToByteArray (), "#6m");
617                         Assert.AreEqual (new byte[] { 0xF9, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x01020304050607L).ToByteArray (), "#7m");
618                         Assert.AreEqual (new byte[] { 0xF8, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405060708L).ToByteArray (), "#8m");
619
620
621                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (long.MaxValue).ToByteArray (), "#9");
622                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, new BigInteger (long.MinValue).ToByteArray (), "#10");
623
624                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F, 0xFF }, new BigInteger (-2147483649L).ToByteArray (), "11");
625                 }
626
627                 [Test]
628                 public void IComparable () {
629                         var a = new BigInteger (99);
630                         Assert.AreEqual (-1, a.CompareTo (100), "#1");
631                         Assert.AreEqual (1, a.CompareTo (null), "#2");
632                 }
633
634                 [Test]
635                 public void ShortOperators () {
636                         Assert.AreEqual (22, (int)new BigInteger ((short)22), "#1");
637                         Assert.AreEqual (-22, (int)new BigInteger ((short)-22), "#2");
638
639                         try {
640                                 short x = (short)new BigInteger (10000000);
641                                 Assert.Fail ("#3");
642                         } catch (OverflowException) {}
643
644                         try {
645                                 short x = (short)new BigInteger (-10000000);
646                                 Assert.Fail ("#4");
647                         } catch (OverflowException) {}
648                 }
649         }
650 }