2010-03-05 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[][] add_a = new byte[][] {
31                         new byte[] {1},
32                         new byte[] {0xFF},
33                         huge_a
34                 };
35
36                 static byte[][] add_b = new byte[][] {
37                         new byte[] {1},
38                         new byte[] {1},
39                         huge_b
40                 };
41
42                 static byte[][] add_c = new byte[][] {
43                         new byte[] {2},
44                         new byte[] {0},
45                         huge_add
46                 };
47
48                 [Test]
49                 public void Mul () {
50                         long[] values = new long [] { -1000000000L, -1000, -1, 0, 1, 1000, 100000000L };
51                         for (int i = 0; i < values.Length; ++i) {
52                                 for (int j = 0; j < values.Length; ++j) {
53                                         var a = new BigInteger (values [i]);
54                                         var b = new BigInteger (values [j]);
55                                         var c = a * b;
56                                         Assert.AreEqual (values [i] * values [j], (long)c, "#_" + i + "_" + j);
57                                 }
58                         }
59                 }
60
61                 [Test]
62                 public void TestHugeMul () {
63                         var a = new BigInteger (huge_a);
64                         var b = new BigInteger (huge_b);
65                         Assert.AreEqual (huge_mul, (a * b).ToByteArray (), "#1");
66                 }
67
68                 [Test]
69                 public void TestAdd () {
70                         for (int i = 0; i < add_a.Length; ++i) {
71                                 var a = new BigInteger (add_a [i]);
72                                 var b = new BigInteger (add_b [i]);
73                                 var c = new BigInteger (add_c [i]);
74
75                                 Assert.AreEqual (c, a + b, "#" + i + "a");
76                                 Assert.AreEqual (c, b + a, "#" + i + "b");
77                                 Assert.AreEqual (c, BigInteger.Add (a, b), "#" + i + "c");
78                                 Assert.AreEqual (add_c [i], (a + b).ToByteArray (), "#" + i + "d");
79                         }
80                 }
81
82                 [Test]
83                 public void TestAdd2 () {
84                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
85                         for (int i = 0; i < values.Length; ++i) {
86                                 for (int j = 0; j < values.Length; ++j) {
87                                         var a = new BigInteger (values [i]);
88                                         var b = new BigInteger (values [j]);
89                                         var c = a + b;
90                                         Assert.AreEqual (values [i] + values [j], (long)c, "#_" + i + "_" + j);
91                                 }
92                         }
93                 }
94
95                 [Test]
96                 public void TestHugeSub () {
97                         var a = new BigInteger (huge_a);
98                         var b = new BigInteger (huge_b);
99                         Assert.AreEqual (a_m_b, (a - b).ToByteArray (), "#1");
100                         Assert.AreEqual (b_m_a, (b - a).ToByteArray (), "#2");
101                 }
102
103                 [Test]
104                 public void TestSub () {
105                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
106                         for (int i = 0; i < values.Length; ++i) {
107                                 for (int j = 0; j < values.Length; ++j) {
108                                         var a = new BigInteger (values [i]);
109                                         var b = new BigInteger (values [j]);
110                                         var c = a - b;
111                                         var d = BigInteger.Subtract (a, b);
112
113                                         Assert.AreEqual (values [i] - values [j], (long)c, "#_" + i + "_" + j);
114                                         Assert.AreEqual (values [i] - values [j], (long)d, "#_" + i + "_" + j);
115                                 }
116                         }
117                 }
118
119                 [Test]
120                 public void TestMin () {
121                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
122                         for (int i = 0; i < values.Length; ++i) {
123                                 for (int j = 0; j < values.Length; ++j) {
124                                         var a = new BigInteger (values [i]);
125                                         var b = new BigInteger (values [j]);
126                                         var c = BigInteger.Min (a, b);
127
128                                         Assert.AreEqual (Math.Min (values [i], values [j]), (long)c, "#_" + i + "_" + j);
129                                 }
130                         }
131                 }
132
133                 [Test]
134                 public void TestMax () {
135                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
136                         for (int i = 0; i < values.Length; ++i) {
137                                 for (int j = 0; j < values.Length; ++j) {
138                                         var a = new BigInteger (values [i]);
139                                         var b = new BigInteger (values [j]);
140                                         var c = BigInteger.Max (a, b);
141
142                                         Assert.AreEqual (Math.Max (values [i], values [j]), (long)c, "#_" + i + "_" + j);
143                                 }
144                         }
145                 }
146
147                 [Test]
148                 public void TestAbs () {
149                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
150                         for (int i = 0; i < values.Length; ++i) {
151                                 var a = new BigInteger (values [i]);
152                                 var c = BigInteger.Abs (a);
153
154                                 Assert.AreEqual (Math.Abs (values [i]), (long)c, "#_" + i);
155                         }
156                 }
157
158                 [Test]
159                 public void TestNegate () {
160                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
161                         for (int i = 0; i < values.Length; ++i) {
162                                 var a = new BigInteger (values [i]);
163                                 var c = -a;
164                                 var d = BigInteger.Negate (a);
165
166                                 Assert.AreEqual (-values [i], (long)c, "#_" + i);
167                                 Assert.AreEqual (-values [i], (long)d, "#_" + i);
168                         }
169                 }
170
171                 [Test]
172                 public void TestInc () {
173                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
174                         for (int i = 0; i < values.Length; ++i) {
175                                 var a = new BigInteger (values [i]);
176                                 var b = ++a;
177
178                                 Assert.AreEqual (++values [i], (long)b, "#_" + i);
179                         }
180                 }
181
182                 [Test]
183                 public void TestDec () {
184                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
185                         for (int i = 0; i < values.Length; ++i) {
186                                 var a = new BigInteger (values [i]);
187                                 var b = --a;
188
189                                 Assert.AreEqual (--values [i], (long)b, "#_" + i);
190                         }
191                 }
192
193                 [Test]
194                 public void TestBitwiseOps () {
195                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L, 0xFFFF00000000L };
196                         for (int i = 0; i < values.Length; ++i) {
197                                 for (int j = 0; j < values.Length; ++j) {
198                                         var a = new BigInteger (values [i]);
199                                         var b = new BigInteger (values [j]);
200
201
202                                         Assert.AreEqual (values [i] | values [j], (long)(a | b) , "#b_" + i + "_" + j);
203                                         Assert.AreEqual (values [i] & values [j], (long)(a & b) , "#a_" + i + "_" + j);
204                                         Assert.AreEqual (values [i] ^ values [j], (long)(a ^ b) , "#c_" + i + "_" + j);
205                                         Assert.AreEqual (~values [i], (long)~a , "#d_" + i + "_" + j);
206                                 }
207                         }
208                 }
209
210                 [Test]
211                 public void TestLeftShift () {
212                         Assert.AreEqual (new byte[] {0x00, 0x28},
213                                 (new BigInteger(0x0A) << 10).ToByteArray (), "#1");
214                         Assert.AreEqual (new byte[] {0x00, 0xD8},
215                                 (new BigInteger(-10) << 10).ToByteArray (), "#2");
216                         Assert.AreEqual (new byte[] {0x00, 0x00, 0xFF},
217                                 (new BigInteger(-1) << 16).ToByteArray (), "#3");
218                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A},
219                                 (new BigInteger(0x0A) << 80).ToByteArray (), "#4");
220                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6},
221                                 (new BigInteger(-10) << 80).ToByteArray (), "#5");
222                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF},
223                                 (new BigInteger(-1) << 80).ToByteArray (), "#6");
224                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xD9},
225                                 (new BigInteger(-1234) << 75).ToByteArray (), "#7");
226                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x91, 0x00},
227                                 (new BigInteger(0x1234) << 75).ToByteArray (), "#8");
228
229                         Assert.AreEqual (new byte[] {0xFF, 0x00}, (new BigInteger(0xFF00) << -8).ToByteArray (), "#9");
230                 }
231
232                 [Test]
233                 public void TestRightShift () {
234                         Assert.AreEqual (new byte[] {0x16, 0xB0, 0x4C, 0x02},
235                                 (new BigInteger(1234567899L) >> 5).ToByteArray (), "#1");
236
237                         Assert.AreEqual (new byte[] {0x2C, 0x93, 0x00},
238                                 (new BigInteger(1234567899L) >> 15).ToByteArray (), "#2");
239
240                         Assert.AreEqual (new byte[] {0xFF, 0xFF, 0x7F},
241                                 (new BigInteger(long.MaxValue - 100) >> 40).ToByteArray (), "#3");
242
243                         Assert.AreEqual (new byte[] {0xE9, 0x4F, 0xB3, 0xFD},
244                                 (new BigInteger(-1234567899L) >> 5).ToByteArray (), "#4");
245
246                         Assert.AreEqual (new byte[] {0xD3, 0x6C, 0xFF},
247                                 (new BigInteger(-1234567899L) >> 15).ToByteArray (), "#5");
248
249                         Assert.AreEqual (new byte[] {0x00, 0x00, 0x80},
250                                 (new BigInteger(long.MinValue + 100) >> 40).ToByteArray (), "#6");
251
252                         Assert.AreEqual (new byte[] { 0xFF },
253                                 (new BigInteger(-1234567899L) >> 90).ToByteArray (), "#7");
254
255                         Assert.AreEqual (new byte[] {0x00},
256                                 (new BigInteger(999999) >> 90).ToByteArray (), "#8");
257
258                         Assert.AreEqual (new byte[] {0x00, 0x00, 0xFF, 0x00}, (new BigInteger(0xFF00) >> -8).ToByteArray (), "#9");
259                 }
260
261                 [Test]
262                 public void CompareOps () {
263                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
264                         for (int i = 0; i < values.Length; ++i) {
265                                 for (int j = 0; j < values.Length; ++j) {
266                                         var a = new BigInteger (values [i]);
267                                         var b = new BigInteger (values [j]);
268                                         
269                                         Assert.AreEqual (values [i].CompareTo (values [j]), a.CompareTo (b), "#a_" + i + "_" + j);
270                                         Assert.AreEqual (values [i].CompareTo (values [j]), BigInteger.Compare (a, b), "#b_" + i + "_" + j);
271
272                                         Assert.AreEqual (values [i] < values [j], a < b, "#c_" + i + "_" + j);
273                                         Assert.AreEqual (values [i] <= values [j], a <= b, "#d_" + i + "_" + j);
274                                         Assert.AreEqual (values [i] == values [j], a == b, "#e_" + i + "_" + j);
275                                         Assert.AreEqual (values [i] != values [j], a != b, "#f_" + i + "_" + j);
276                                         Assert.AreEqual (values [i] >= values [j], a >= b, "#g_" + i + "_" + j);
277                                         Assert.AreEqual (values [i] > values [j], a > b, "#h_" + i + "_" + j);
278                                 }
279                         }
280                 }
281
282                 [Test]
283                 public void CompareOps2 () {
284                         BigInteger a = new BigInteger (100000000000L);
285                         BigInteger b = new BigInteger (28282828282UL);
286
287                         Assert.IsTrue (a >= b, "#1");
288                         Assert.IsTrue (a >= b, "#2");
289                         Assert.IsFalse (a < b, "#3");
290                         Assert.IsFalse (a <= b, "#4");
291                         Assert.AreEqual (1, a.CompareTo (b), "#5");
292                 }
293
294                 [Test]
295                 public void CompareULong () {
296                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L, 0xAA00000000L };
297                         ulong[] uvalues = new ulong [] {0, 1, 1000, 100000000000L, 999999, 28282828282, 0xAA00000000, ulong.MaxValue };
298                         for (int i = 0; i < values.Length; ++i) {
299                                 for (int j = 0; j < uvalues.Length; ++j) {
300                                         var a = new BigInteger (values [i]);
301                                         var b = uvalues [j];
302                                         var c = new BigInteger (b);
303                                         
304                                         Assert.AreEqual (a.CompareTo (c), a.CompareTo (b), "#a_" + i + "_" + j);
305
306                                         Assert.AreEqual (a > c, a > b, "#b_" + i + "_" + j);
307                                         Assert.AreEqual (a < c, a < b, "#c_" + i + "_" + j);
308                                         Assert.AreEqual (a <= c, a <= b, "#d_" + i + "_" + j);
309                                         Assert.AreEqual (a == c, a == b, "#e_" + i + "_" + j);
310                                         Assert.AreEqual (a != c, a != b, "#f_" + i + "_" + j);
311                                         Assert.AreEqual (a >= c, a >= b, "#g_" + i + "_" + j);
312
313                                         Assert.AreEqual (c > a, b > a, "#ib_" + i + "_" + j);
314                                         Assert.AreEqual (c < a, b < a, "#ic_" + i + "_" + j);
315                                         Assert.AreEqual (c <= a, b <= a, "#id_" + i + "_" + j);
316                                         Assert.AreEqual (c == a, b == a, "#ie_" + i + "_" + j);
317                                         Assert.AreEqual (c != a, b != a, "#if_" + i + "_" + j);
318                                         Assert.AreEqual (c >= a, b >= a, "#ig_" + i + "_" + j);
319                                 }
320                         }
321                 }
322
323                 [Test]
324                 public void CompareLong () {
325                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 9999999, 100000000000L, 0xAA00000000, long.MaxValue, long.MinValue };
326
327                         for (int i = 0; i < values.Length; ++i) {
328                                 for (int j = 0; j < values.Length; ++j) {
329                                         var a = new BigInteger (values [i]);
330                                         var b = values [j];
331                                         var c = new BigInteger (b);
332                                         
333                                         Assert.AreEqual (a.CompareTo (c), a.CompareTo (b), "#a_" + i + "_" + j);
334
335                                         Assert.AreEqual (a > c, a > b, "#b_" + i + "_" + j);
336                                         Assert.AreEqual (a < c, a < b, "#c_" + i + "_" + j);
337                                         Assert.AreEqual (a <= c, a <= b, "#d_" + i + "_" + j);
338                                         Assert.AreEqual (a == c, a == b, "#e_" + i + "_" + j);
339                                         Assert.AreEqual (a != c, a != b, "#f_" + i + "_" + j);
340                                         Assert.AreEqual (a >= c, a >= b, "#g_" + i + "_" + j);
341
342                                         Assert.AreEqual (c > a, b > a, "#ib_" + i + "_" + j);
343                                         Assert.AreEqual (c < a, b < a, "#ic_" + i + "_" + j);
344                                         Assert.AreEqual (c <= a, b <= a, "#id_" + i + "_" + j);
345                                         Assert.AreEqual (c == a, b == a, "#ie_" + i + "_" + j);
346                                         Assert.AreEqual (c != a, b != a, "#if_" + i + "_" + j);
347                                         Assert.AreEqual (c >= a, b >= a, "#ig_" + i + "_" + j);
348                                 }
349                         }
350                 }
351
352                 [Test]
353                 public void TestEquals () {
354                                 var a = new BigInteger (10);
355                                 var b = new BigInteger (10);
356                                 var c = new BigInteger (-10);
357
358                                 Assert.AreEqual (a, b, "#1");
359                                 Assert.AreNotEqual (a, c, "#2");
360                                 Assert.AreNotEqual (a, 10, "#3");
361                 }
362
363                 [Test]
364                 public void ByteArrayCtor ()
365                 {
366                         try {
367                                 new BigInteger (null);
368                                 Assert.Fail ("#1");
369                         } catch (ArgumentNullException) {}
370
371                         Assert.AreEqual (0, (int)new BigInteger (new byte [0]), "#2");
372                 }
373
374                 [Test]
375                 public void IntCtorRoundTrip ()
376                 {
377                         int[] values = new int [] {
378                                 int.MinValue, -0x2F33BB, -0x1F33, -0x33, 0, 0x33,
379                                 0x80, 0x8190, 0xFF0011, 0x1234, 0x11BB99, 0x44BB22CC,
380                                 int.MaxValue };
381                         foreach (var val in values) {
382                                 var a = new BigInteger (val);
383                                 var b = new BigInteger (a.ToByteArray ());
384
385                                 Assert.AreEqual (val, (int)a, "#a_" + val);
386                                 Assert.AreEqual (val, (int)b, "#b_" + val);
387                         }
388                 }
389
390                 [Test]
391                 public void LongCtorRoundTrip ()
392                 {
393                         long[] values = new long [] {
394                                 0, long.MinValue, long.MaxValue, -1, 1L + int.MaxValue, -1L + int.MinValue, 0x1234, 0xFFFFFFFFL, 0x1FFFFFFFFL, -0xFFFFFFFFL, -0x1FFFFFFFFL,
395                                 0x100000000L, -0x100000000L, 0x100000001L, -0x100000001L };
396                         foreach (var val in values) {
397                                 var a = new BigInteger (val);
398                                 var b = new BigInteger (a.ToByteArray ());
399
400                                 Assert.AreEqual (val, (long)a, "#a_" + val);
401                                 Assert.AreEqual (val, (long)b, "#b_" + val);
402                         }
403                 }
404
405                 [Test]
406                 public void ByteArrayCtorRoundTrip ()
407                 {
408                         var arr = new byte [] { 1,2,3,4,5,6,7,8,9 };
409                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#1");
410
411                         arr = new byte [] { 1,2,3,4,5,6,7,8,0xFF, 0x0};
412                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#2");
413
414                         arr = new byte [] { 1,2,3,4,5,6,7,8,9, 0xF0 };
415                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#3");
416
417                         arr = new byte [] { 1};
418                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#4");
419
420                         arr = new byte [] { 1,2 };
421                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#5");
422
423                         arr = new byte [] { 1,2,3 };
424                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#6");
425
426                         arr = new byte [] { 1,2,3,4 };
427                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#7");
428
429                         arr = new byte [] { 1,2,3,4,5 };
430                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#8");
431
432                         arr = new byte [] { 1,2,3,4,5,6 };
433                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#9");
434
435                         arr = new byte [] { 1,2,3,4,5,6,7 };
436                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#10");
437
438                         arr = new byte [] { 1,2,3,4,5 };
439                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#11");
440
441                         arr = new byte [] { 0 };
442                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#12");
443
444                         arr = new byte [] { 0xFF, 00 };
445                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#13");
446
447                         arr = new byte [] { 1, 0, 0, 0, 0, 0, };
448                         Assert.AreEqual (new byte [1] {1}, new BigInteger (arr).ToByteArray (), "#14");
449                 }
450
451                 [Test]
452                 public void TestIntCtorProperties ()
453                 {
454                         BigInteger a = new BigInteger (10);
455                         Assert.IsTrue (a.IsEven, "#1");
456                         Assert.IsFalse (a.IsOne, "#2");
457                         Assert.IsFalse (a.IsPowerOfTwo, "#3");
458                         Assert.IsFalse (a.IsZero, "#4");
459                         Assert.AreEqual (1, a.Sign, "#5");
460
461                         Assert.IsFalse (new BigInteger (11).IsEven, "#6");
462                         Assert.IsTrue (new BigInteger (1).IsOne, "#7");
463                         Assert.IsTrue (new BigInteger (32).IsPowerOfTwo, "#8");
464                         Assert.IsTrue (new BigInteger (0).IsZero, "#9");
465                         Assert.AreEqual (0, new BigInteger (0).Sign, "#10");
466                         Assert.AreEqual (-1, new BigInteger (-99999).Sign, "#11");
467
468                         Assert.IsFalse (new BigInteger (0).IsPowerOfTwo, "#12");
469                         Assert.IsFalse (new BigInteger (-16).IsPowerOfTwo, "#13");
470                         Assert.IsTrue (new BigInteger (1).IsPowerOfTwo, "#14");
471                 }
472
473                 /*[Test]
474                 public void TestIntCtorToString ()
475                 {
476                         Assert.AreEqual ("5555", new BigInteger (5555).ToString (), "#1");
477                         Assert.AreEqual ("-99999", new BigInteger (-99999).ToString (), "#2");
478                 }*/
479
480                 [Test]
481                 public void TestToIntOperator ()
482                 {
483                         try {
484                                 int v = (int)new BigInteger (huge_a);
485                                 Assert.Fail ("#1");
486                         } catch (OverflowException) {}
487
488                         try {
489                                 int v = (int)new BigInteger (1L + int.MaxValue);
490                                 Assert.Fail ("#2");
491                         } catch (OverflowException) {}
492
493                         try {
494                                 int v = (int)new BigInteger (-1L + int.MinValue);
495                                 Assert.Fail ("#3");
496                         } catch (OverflowException) {}
497
498                         Assert.AreEqual (int.MaxValue, (int)new BigInteger (int.MaxValue), "#4");
499                         Assert.AreEqual (int.MinValue, (int)new BigInteger (int.MinValue), "#5");
500                 }
501
502
503                 [Test]
504                 public void TestToLongOperator ()
505                 {
506                         try {
507                                 long v = (long)new BigInteger (huge_a);
508                                 Assert.Fail ("#1");
509                         } catch (OverflowException) {}
510
511                         //long.MaxValue + 1
512                         try {
513                                 long v = (long)new BigInteger (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00 });
514                                 Assert.Fail ("#2");
515                         } catch (OverflowException) {}
516
517                         //TODO long.MinValue - 1
518                         try {
519                                 long v = (long)new BigInteger (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF });
520                                 Assert.Fail ("#3");
521                         } catch (OverflowException) {}
522
523                         Assert.AreEqual (long.MaxValue, (long)new BigInteger (long.MaxValue), "#4");
524                         Assert.AreEqual (long.MinValue, (long)new BigInteger (long.MinValue), "#5");
525                 }
526
527                 [Test]
528                 public void TestIntCtorToByteArray ()
529                 {
530                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-1).ToByteArray (), "#1");
531                         Assert.AreEqual (new byte[] { 0xD4, 0xFE }, new BigInteger (-300).ToByteArray (), "#2");
532                         Assert.AreEqual (new byte[] { 0x80, 0x00 }, new BigInteger (128).ToByteArray (), "#3");
533                         Assert.AreEqual (new byte[] { 0x00, 0x60 }, new BigInteger (0x6000).ToByteArray (), "#4");
534                         Assert.AreEqual (new byte[] { 0x00, 0x80, 0x00 }, new BigInteger (0x8000).ToByteArray (), "#5");
535                         Assert.AreEqual (new byte[] { 0xDD, 0xBC, 0x00, 0x7A }, new BigInteger (0x7A00BCDD).ToByteArray (), "#6");
536                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (int.MaxValue).ToByteArray (), "#7");
537                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue).ToByteArray (), "#8");
538                         Assert.AreEqual (new byte[] { 0x01, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue + 1).ToByteArray (), "#9");
539                         Assert.AreEqual (new byte[] { 0x7F }, new BigInteger (0x7F).ToByteArray (), "#10");
540                         Assert.AreEqual (new byte[] { 0x45, 0xCC, 0xD0 }, new BigInteger (-0x2F33BB).ToByteArray (), "#11");
541                         Assert.AreEqual (new byte[] { 0 }, new BigInteger (0).ToByteArray (), "#12");
542                 }
543
544                 [Test]
545                 public void TestLongCtorToByteArray ()
546                 {
547                         Assert.AreEqual (new byte[] { 0x01 }, new BigInteger (0x01L).ToByteArray (), "#1");
548                         Assert.AreEqual (new byte[] { 0x02, 0x01 }, new BigInteger (0x0102L).ToByteArray (), "#2");
549                         Assert.AreEqual (new byte[] { 0x03, 0x02, 0x01 }, new BigInteger (0x010203L).ToByteArray (), "#3");
550                         Assert.AreEqual (new byte[] { 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304L).ToByteArray (), "#4");
551                         Assert.AreEqual (new byte[] { 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405L).ToByteArray (), "#5");
552                         Assert.AreEqual (new byte[] { 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x010203040506L).ToByteArray (), "#6");
553                         Assert.AreEqual (new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304050607L).ToByteArray (), "#7");
554                         Assert.AreEqual (new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405060708L).ToByteArray (), "#8");
555
556                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-0x01L).ToByteArray (), "#1m");
557                         Assert.AreEqual (new byte[] { 0xFE, 0xFE}, new BigInteger (-0x0102L).ToByteArray (), "#2m");
558                         Assert.AreEqual (new byte[] { 0xFD, 0xFD, 0xFE }, new BigInteger (-0x010203L).ToByteArray (), "#3m");
559                         Assert.AreEqual (new byte[] { 0xFC, 0xFC, 0xFD, 0xFE}, new BigInteger (-0x01020304L).ToByteArray (), "#4m");
560                         Assert.AreEqual (new byte[] { 0xFB, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405L).ToByteArray (), "#5m");
561                         Assert.AreEqual (new byte[] { 0xFA, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x010203040506L).ToByteArray (), "#6m");
562                         Assert.AreEqual (new byte[] { 0xF9, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x01020304050607L).ToByteArray (), "#7m");
563                         Assert.AreEqual (new byte[] { 0xF8, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405060708L).ToByteArray (), "#8m");
564
565
566                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (long.MaxValue).ToByteArray (), "#9");
567                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, new BigInteger (long.MinValue).ToByteArray (), "#10");
568
569                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F, 0xFF }, new BigInteger (-2147483649L).ToByteArray (), "11");
570                 }
571
572                 [Test]
573                 public void IComparable () {
574                         var a = new BigInteger (99);
575                         Assert.AreEqual (-1, a.CompareTo (100), "#1");
576                         Assert.AreEqual (1, a.CompareTo (null), "#2");
577                 }
578
579                 [Test]
580                 public void ShortOperators () {
581                         Assert.AreEqual (22, (int)new BigInteger ((short)22), "#1");
582                         Assert.AreEqual (-22, (int)new BigInteger ((short)-22), "#2");
583
584                         try {
585                                 short x = (short)new BigInteger (10000000);
586                                 Assert.Fail ("#3");
587                         } catch (OverflowException) {}
588
589                         try {
590                                 short x = (short)new BigInteger (-10000000);
591                                 Assert.Fail ("#4");
592                         } catch (OverflowException) {}
593                 }
594         }
595 }