2005-01-03 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / Mono.Security / Test / Mono.Math / BigIntegerTest.cs
1 //
2 // BigIntegerTest.cs - NUnit Test Cases for BigInteger
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using myalias = System;
31 using Mono.Math;
32
33 namespace MonoTests.Mono.Math {
34
35         [TestFixture]
36         public class BigIntegerTest {
37
38                 [Test]
39                 public void DefaultBitCount () 
40                 {
41                         BigInteger bi = new BigInteger ();
42                         Assert.AreEqual (0, bi.BitCount (), "default BitCount");
43                         // note: not bit are set so BitCount is zero
44                 }
45
46                 [Test]
47                 public void DefaultRandom () 
48                 {
49                         // based on bugzilla entry #68452
50                         BigInteger bi = new BigInteger ();
51                         Assert.AreEqual (0, bi.BitCount (), "before randomize");
52                         bi.Randomize ();
53                         // Randomize returns a random number of BitCount length
54                         // so in this case it will ALWAYS return 0
55                         Assert.AreEqual (0, bi.BitCount (), "after randomize");
56                         Assert.AreEqual (new BigInteger (0), bi, "Zero");
57                 }
58
59                 [Test]\r
60                 public void ModPow_0_Even ()\r
61                 {
62                         BigInteger x = new BigInteger (1);
63                         BigInteger y = new BigInteger (0);
64                         BigInteger z = x.ModPow (y, 1024);
65                         Assert.AreEqual ("1", z.ToString (), "1 pow 0 == 1");
66                 }\r
67 \r
68                 [Test]\r
69                 public void ModPow_Big_Even ()\r
70                 {\r
71                         // http://gcc.gnu.org/ml/java/2001-01/msg00150.html\r
72                         BigInteger x = BigInteger.Parse ("222556259477882361118129720038750144464896096345697329917462180806109470940281821580712930114298080816996240075704780895407778416354633927929850543336844729388676722554712356733107888579404671103423966348754128720372408391573576775380281687780687492527566938517625657849775850241884119610654472761291507970934");\r
73                         BigInteger y = BigInteger.Parse ("110319153937683287453746757581772092163629769182044007837690319614087550020383807943886070460712008994638849038231331120616035703719955147238394349941968802357224177878230564379014395900786093465543114548034361805469457605783731382574787980771957640613447628351175959168798011343064123908688343944150028709336");\r
74                         BigInteger z = BigInteger.Parse ("211455809992703561445401788842734346323873054957006050135582190157359001703882707072169880651159563587522668850959539052488297197610540840476872693108381476249027986010074543599432542677282684917897250864056294311624311681558854158430574409491081490219256907243905496547813878640883064959346343865887971384185");\r
75                         BigInteger a = z.ModPow (x, y);\r
76                         Assert.AreEqual ("89040229313686098274750802637193802904787850353791629688385431482589769348345172944539658366893587456857347312314974124445695423885005533414559099801699612294235861570065774222911180890417009385455826560773741520297884850460324781620974467560905975577765401911117379967692495136423710471201230243826129276993", a.ToString ());\r
77                 }\r
78 \r
79                 [Test]\r
80                 public void ModPow_2 ()\r
81                 {\r
82                         // #70169\r
83                         BigInteger b = new BigInteger (10);\r
84                         BigInteger m = new BigInteger (32);\r
85                         // after 40 we start loosing double precision and result will differ\r
86                         for (int i=1; i < 40; i++) {\r
87                                 BigInteger e = new BigInteger (i);\r
88                                 BigInteger r = e.ModPow (b, m);\r
89                                 long expected = (long) myalias.Math.Pow (i, 10) % 32;\r
90                                 Assert.AreEqual (expected.ToString (), r.ToString (), i.ToString ());\r
91                         }\r
92                 }\r
93         }\r
94 }