e4b8bf15e179dbbfeb9f7421aa802a3b6c9e69f3
[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
26                 static byte[][] add_a = new byte[][] {
27                         new byte[] {1},
28                         new byte[] {0xFF},
29                         huge_a
30                 };
31
32                 static byte[][] add_b = new byte[][] {
33                         new byte[] {1},
34                         new byte[] {1},
35                         huge_b
36                 };
37
38                 static byte[][] add_c = new byte[][] {
39                         new byte[] {2},
40                         new byte[] {0},
41                         huge_add
42                 };
43
44                 [Test]
45                 public void TestAdd () {
46                         for (int i = 0; i < add_a.Length; ++i) {
47                                 var a = new BigInteger (add_a [i]);
48                                 var b = new BigInteger (add_b [i]);
49                                 var c = new BigInteger (add_c [i]);
50
51                                 Assert.AreEqual (c, a + b, "#" + i + "a");
52                                 Assert.AreEqual (c, b + a, "#" + i + "b");
53                                 Assert.AreEqual (c, BigInteger.Add (a, b), "#" + i + "c");
54                                 Assert.AreEqual (add_c [i], (a + b).ToByteArray (), "#" + i + "d");
55                         }
56                 }
57
58                 [Test]
59                 public void TestAdd2 () {
60                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
61                         for (int i = 0; i < values.Length; ++i) {
62                                 for (int j = 0; j < values.Length; ++j) {
63                                         var a = new BigInteger (values [i]);
64                                         var b = new BigInteger (values [j]);
65                                         var c = a + b;
66                                         Assert.AreEqual (values [i] + values [j], (long)c, "#_" + i + "_" + j);
67                                 }
68                         }
69                 }
70
71                 [Test]
72                 public void CompareOps () {
73                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L };
74                         for (int i = 0; i < values.Length; ++i) {
75                                 for (int j = 0; j < values.Length; ++j) {
76                                         var a = new BigInteger (values [i]);
77                                         var b = new BigInteger (values [j]);
78                                         
79                                         Assert.AreEqual (values [i].CompareTo (values [j]), a.CompareTo (b), "#a_" + i + "_" + j);
80                                         Assert.AreEqual (values [i].CompareTo (values [j]), BigInteger.Compare (a, b), "#b_" + i + "_" + j);
81
82                                         Assert.AreEqual (values [i] < values [j], a < b, "#c_" + i + "_" + j);
83                                         Assert.AreEqual (values [i] <= values [j], a <= b, "#d_" + i + "_" + j);
84                                         Assert.AreEqual (values [i] == values [j], a == b, "#e_" + i + "_" + j);
85                                         Assert.AreEqual (values [i] != values [j], a != b, "#f_" + i + "_" + j);
86                                         Assert.AreEqual (values [i] >= values [j], a >= b, "#g_" + i + "_" + j);
87                                         Assert.AreEqual (values [i] > values [j], a > b, "#h_" + i + "_" + j);
88                                 }
89                         }
90                 }
91
92                 [Test]
93                 public void CompareOps2 () {
94                         BigInteger a = new BigInteger (100000000000L);
95                         BigInteger b = new BigInteger (28282828282UL);
96
97                         Assert.IsTrue (a >= b, "#1");
98                         Assert.IsTrue (a >= b, "#2");
99                         Assert.IsFalse (a < b, "#3");
100                         Assert.IsFalse (a <= b, "#4");
101                         Assert.AreEqual (1, a.CompareTo (b), "#5");
102                 }
103
104                 [Test]
105                 public void CompareULong () {
106                         long[] values = new long [] { -100000000000L, -1000, -1, 0, 1, 1000, 100000000000L, 0xAA00000000L };
107                         ulong[] uvalues = new ulong [] {0, 1, 1000, 100000000000L, 999999, 28282828282, 0xAA00000000, ulong.MaxValue };
108                         for (int i = 0; i < values.Length; ++i) {
109                                 for (int j = 0; j < uvalues.Length; ++j) {
110                                         var a = new BigInteger (values [i]);
111                                         var b = uvalues [j];
112                                         var c = new BigInteger (b);
113                                         
114                                         Assert.AreEqual (a.CompareTo (c), a.CompareTo (b), "#a_" + i + "_" + j);
115
116                                         Assert.AreEqual (a > c, a > b, "#b_" + i + "_" + j);
117                                         Assert.AreEqual (a < c, a < b, "#c_" + i + "_" + j);
118                                         Assert.AreEqual (a <= c, a <= b, "#d_" + i + "_" + j);
119                                         Assert.AreEqual (a == c, a == b, "#e_" + i + "_" + j);
120                                         Assert.AreEqual (a != c, a != b, "#f_" + i + "_" + j);
121                                         Assert.AreEqual (a >= c, a >= b, "#g_" + i + "_" + j);
122                                 }
123                         }
124                 }
125
126                 [Test]
127                 public void TestEquals () {
128                                 var a = new BigInteger (10);
129                                 var b = new BigInteger (10);
130                                 var c = new BigInteger (-10);
131
132                                 Assert.AreEqual (a, b, "#1");
133                                 Assert.AreNotEqual (a, c, "#2");
134                                 Assert.AreNotEqual (a, 10, "#3");
135                 }
136
137                 [Test]
138                 public void ByteArrayCtor ()
139                 {
140                         try {
141                                 new BigInteger (null);
142                                 Assert.Fail ("#1");
143                         } catch (ArgumentNullException) {}
144
145                         Assert.AreEqual (0, (int)new BigInteger (new byte [0]), "#2");
146                 }
147
148                 [Test]
149                 public void IntCtorRoundTrip ()
150                 {
151                         int[] values = new int [] {
152                                 int.MinValue, -0x2F33BB, -0x1F33, -0x33, 0, 0x33,
153                                 0x80, 0x8190, 0xFF0011, 0x1234, 0x11BB99, 0x44BB22CC,
154                                 int.MaxValue };
155                         foreach (var val in values) {
156                                 var a = new BigInteger (val);
157                                 var b = new BigInteger (a.ToByteArray ());
158
159                                 Assert.AreEqual (val, (int)a, "#a_" + val);
160                                 Assert.AreEqual (val, (int)b, "#b_" + val);
161                         }
162                 }
163
164                 [Test]
165                 public void LongCtorRoundTrip ()
166                 {
167                         long[] values = new long [] {
168                                 0, long.MinValue, long.MaxValue, -1, 1L + int.MaxValue, -1L + int.MinValue, 0x1234, 0xFFFFFFFFL, 0x1FFFFFFFFL, -0xFFFFFFFFL, -0x1FFFFFFFFL,
169                                 0x100000000L, -0x100000000L, 0x100000001L, -0x100000001L };
170                         foreach (var val in values) {
171                                 var a = new BigInteger (val);
172                                 var b = new BigInteger (a.ToByteArray ());
173
174                                 Assert.AreEqual (val, (long)a, "#a_" + val);
175                                 Assert.AreEqual (val, (long)b, "#b_" + val);
176                         }
177                 }
178
179                 [Test]
180                 public void ByteArrayCtorRoundTrip ()
181                 {
182                         var arr = new byte [] { 1,2,3,4,5,6,7,8,9 };
183                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#1");
184
185                         arr = new byte [] { 1,2,3,4,5,6,7,8,0xFF, 0x0};
186                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#2");
187
188                         arr = new byte [] { 1,2,3,4,5,6,7,8,9, 0xF0 };
189                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#3");
190
191                         arr = new byte [] { 1};
192                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#4");
193
194                         arr = new byte [] { 1,2 };
195                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#5");
196
197                         arr = new byte [] { 1,2,3 };
198                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#6");
199
200                         arr = new byte [] { 1,2,3,4 };
201                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#7");
202
203                         arr = new byte [] { 1,2,3,4,5 };
204                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#8");
205
206                         arr = new byte [] { 1,2,3,4,5,6 };
207                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#9");
208
209                         arr = new byte [] { 1,2,3,4,5,6,7 };
210                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#10");
211
212                         arr = new byte [] { 1,2,3,4,5 };
213                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#11");
214
215                         arr = new byte [] { 0 };
216                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#12");
217
218                         arr = new byte [] { 0xFF, 00 };
219                         Assert.AreEqual (arr, new BigInteger (arr).ToByteArray (), "#13");
220
221                         arr = new byte [] { 1, 0, 0, 0, 0, 0, };
222                         Assert.AreEqual (new byte [1] {1}, new BigInteger (arr).ToByteArray (), "#14");
223                 }
224
225                 [Test]
226                 public void TestIntCtorProperties ()
227                 {
228                         BigInteger a = new BigInteger (10);
229                         Assert.IsTrue (a.IsEven, "#1");
230                         Assert.IsFalse (a.IsOne, "#2");
231                         Assert.IsFalse (a.IsPowerOfTwo, "#3");
232                         Assert.IsFalse (a.IsZero, "#4");
233                         Assert.AreEqual (1, a.Sign, "#5");
234
235                         Assert.IsFalse (new BigInteger (11).IsEven, "#6");
236                         Assert.IsTrue (new BigInteger (1).IsOne, "#7");
237                         Assert.IsTrue (new BigInteger (32).IsPowerOfTwo, "#8");
238                         Assert.IsTrue (new BigInteger (0).IsZero, "#9");
239                         Assert.AreEqual (0, new BigInteger (0).Sign, "#10");
240                         Assert.AreEqual (-1, new BigInteger (-99999).Sign, "#11");
241
242                         Assert.IsFalse (new BigInteger (0).IsPowerOfTwo, "#12");
243                         Assert.IsFalse (new BigInteger (-16).IsPowerOfTwo, "#13");
244                         Assert.IsTrue (new BigInteger (1).IsPowerOfTwo, "#14");
245                 }
246
247                 /*[Test]
248                 public void TestIntCtorToString ()
249                 {
250                         Assert.AreEqual ("5555", new BigInteger (5555).ToString (), "#1");
251                         Assert.AreEqual ("-99999", new BigInteger (-99999).ToString (), "#2");
252                 }*/
253
254                 [Test]
255                 public void TestToIntOperator ()
256                 {
257                         try {
258                                 int v = (int)new BigInteger (huge_a);
259                                 Assert.Fail ("#1");
260                         } catch (OverflowException) {}
261
262                         try {
263                                 int v = (int)new BigInteger (1L + int.MaxValue);
264                                 Assert.Fail ("#2");
265                         } catch (OverflowException) {}
266
267                         try {
268                                 int v = (int)new BigInteger (-1L + int.MinValue);
269                                 Assert.Fail ("#3");
270                         } catch (OverflowException) {}
271
272                         Assert.AreEqual (int.MaxValue, (int)new BigInteger (int.MaxValue), "#4");
273                         Assert.AreEqual (int.MinValue, (int)new BigInteger (int.MinValue), "#5");
274                 }
275
276
277                 [Test]
278                 public void TestToLongOperator ()
279                 {
280                         try {
281                                 long v = (long)new BigInteger (huge_a);
282                                 Assert.Fail ("#1");
283                         } catch (OverflowException) {}
284
285                         //long.MaxValue + 1
286                         try {
287                                 long v = (long)new BigInteger (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00 });
288                                 Assert.Fail ("#2");
289                         } catch (OverflowException) {}
290
291                         //TODO long.MinValue - 1
292                         try {
293                                 long v = (long)new BigInteger (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF });
294                                 Assert.Fail ("#3");
295                         } catch (OverflowException) {}
296
297                         Assert.AreEqual (long.MaxValue, (long)new BigInteger (long.MaxValue), "#4");
298                         Assert.AreEqual (long.MinValue, (long)new BigInteger (long.MinValue), "#5");
299                 }
300
301                 [Test]
302                 public void TestIntCtorToByteArray ()
303                 {
304                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-1).ToByteArray (), "#1");
305                         Assert.AreEqual (new byte[] { 0xD4, 0xFE }, new BigInteger (-300).ToByteArray (), "#2");
306                         Assert.AreEqual (new byte[] { 0x80, 0x00 }, new BigInteger (128).ToByteArray (), "#3");
307                         Assert.AreEqual (new byte[] { 0x00, 0x60 }, new BigInteger (0x6000).ToByteArray (), "#4");
308                         Assert.AreEqual (new byte[] { 0x00, 0x80, 0x00 }, new BigInteger (0x8000).ToByteArray (), "#5");
309                         Assert.AreEqual (new byte[] { 0xDD, 0xBC, 0x00, 0x7A }, new BigInteger (0x7A00BCDD).ToByteArray (), "#6");
310                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (int.MaxValue).ToByteArray (), "#7");
311                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue).ToByteArray (), "#8");
312                         Assert.AreEqual (new byte[] { 0x01, 0x00, 0x00, 0x80 }, new BigInteger (int.MinValue + 1).ToByteArray (), "#9");
313                         Assert.AreEqual (new byte[] { 0x7F }, new BigInteger (0x7F).ToByteArray (), "#10");
314                         Assert.AreEqual (new byte[] { 0x45, 0xCC, 0xD0 }, new BigInteger (-0x2F33BB).ToByteArray (), "#11");
315                         Assert.AreEqual (new byte[] { 0 }, new BigInteger (0).ToByteArray (), "#12");
316                 }
317
318                 [Test]
319                 public void TestLongCtorToByteArray ()
320                 {
321                         Assert.AreEqual (new byte[] { 0x01 }, new BigInteger (0x01L).ToByteArray (), "#1");
322                         Assert.AreEqual (new byte[] { 0x02, 0x01 }, new BigInteger (0x0102L).ToByteArray (), "#2");
323                         Assert.AreEqual (new byte[] { 0x03, 0x02, 0x01 }, new BigInteger (0x010203L).ToByteArray (), "#3");
324                         Assert.AreEqual (new byte[] { 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304L).ToByteArray (), "#4");
325                         Assert.AreEqual (new byte[] { 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405L).ToByteArray (), "#5");
326                         Assert.AreEqual (new byte[] { 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x010203040506L).ToByteArray (), "#6");
327                         Assert.AreEqual (new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x01020304050607L).ToByteArray (), "#7");
328                         Assert.AreEqual (new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x2, 0x01 }, new BigInteger (0x0102030405060708L).ToByteArray (), "#8");
329
330                         Assert.AreEqual (new byte[] { 0xFF }, new BigInteger (-0x01L).ToByteArray (), "#1m");
331                         Assert.AreEqual (new byte[] { 0xFE, 0xFE}, new BigInteger (-0x0102L).ToByteArray (), "#2m");
332                         Assert.AreEqual (new byte[] { 0xFD, 0xFD, 0xFE }, new BigInteger (-0x010203L).ToByteArray (), "#3m");
333                         Assert.AreEqual (new byte[] { 0xFC, 0xFC, 0xFD, 0xFE}, new BigInteger (-0x01020304L).ToByteArray (), "#4m");
334                         Assert.AreEqual (new byte[] { 0xFB, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405L).ToByteArray (), "#5m");
335                         Assert.AreEqual (new byte[] { 0xFA, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x010203040506L).ToByteArray (), "#6m");
336                         Assert.AreEqual (new byte[] { 0xF9, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x01020304050607L).ToByteArray (), "#7m");
337                         Assert.AreEqual (new byte[] { 0xF8, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE }, new BigInteger (-0x0102030405060708L).ToByteArray (), "#8m");
338
339
340                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F }, new BigInteger (long.MaxValue).ToByteArray (), "#9");
341                         Assert.AreEqual (new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, new BigInteger (long.MinValue).ToByteArray (), "#10");
342
343                         Assert.AreEqual (new byte[] { 0xFF, 0xFF, 0xFF, 0x7F, 0xFF }, new BigInteger (-2147483649L).ToByteArray (), "11");
344
345
346                 }
347
348         }
349 }