New tests.
[mono.git] / mcs / class / corlib / Test / System / UInt64Test.cs
1 // UInt64Test.cs - NUnit Test Cases for the System.UInt64 struct
2 //
3 // Mario Martinez (mariom925@home.om)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
11 using System.Threading;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class UInt64Test 
18 {
19         private const UInt64 MyUInt64_1 = 42;
20         private const UInt64 MyUInt64_2 = 0;
21         private const UInt64 MyUInt64_3 = 18446744073709551615;
22         private const string MyString1 = "42";
23         private const string MyString2 = "0";
24         private const string MyString3 = "18446744073709551615";
25         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
26         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
27         private string[] Results1 = {"",
28                                         "0", "0.000000e+000", "0.00",
29                                         "0", "0.00", "0.00 %", "0"};
30         private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
31                                         "0", "0.000000e+000", "0.00",
32                                         "0", "0.00", "0.00 %", "0"};
33         private string[] Results2 = {"",
34                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
35                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
36                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
37         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"18,446,744,073,709,551,615.00000",
38                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
39                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
40                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
41
42         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
43         
44         private CultureInfo old_culture;
45
46         [TestFixtureSetUp]
47         public void SetUp() 
48         {
49                 old_culture = Thread.CurrentThread.CurrentCulture;
50
51                 // Set culture to en-US and don't let the user override.
52                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
53
54                 // We can't initialize this until we set the culture.
55                 
56                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
57                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
58                 
59                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
60                 Results1 [3] = "0." + decimals;
61                 Results1 [5] = "0." + decimals;
62                 Results1 [6] = perPattern.Replace ("n","0.00");
63                 
64                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "18,446,744,073,709,551,615.00000";
65                 Results2 [6] = perPattern.Replace ("n","1,844,674,407,370,955,161,500.00000");
66         }
67
68         [TestFixtureTearDown]
69         public void TearDown ()
70         {
71                 Thread.CurrentThread.CurrentCulture = old_culture;
72         }
73
74         public void TestMinMax()
75         {
76                 
77                 Assert.AreEqual(UInt64.MinValue, MyUInt64_2);
78                 Assert.AreEqual(UInt64.MaxValue, MyUInt64_3);
79         }
80         
81         public void TestCompareTo()
82         {
83                 Assert.IsTrue(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
84                 Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
85                 Assert.IsTrue(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
86                 Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
87                 try {
88                         MyUInt64_2.CompareTo((object)(Int16)100);
89                         Assert.Fail("Should raise a System.ArgumentException");
90                 }
91                 catch (Exception e) {
92                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
93                 }
94         }
95
96         public void TestEquals()
97         {
98                 Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_1));
99                 Assert.IsTrue(MyUInt64_1.Equals((object)(UInt64)(42)));
100                 Assert.IsTrue(MyUInt64_1.Equals((object)(SByte)(42)) == false);
101                 Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_2) == false);
102         }
103         
104         public void TestGetHashCode()
105         {
106                 try {
107                         MyUInt64_1.GetHashCode();
108                         MyUInt64_2.GetHashCode();
109                         MyUInt64_3.GetHashCode();
110                 }
111                 catch {
112                         Assert.Fail("GetHashCode should not raise an exception here");
113                 }
114         }
115         
116         public void TestParse()
117         {
118                 //test Parse(string s)
119                 Assert.IsTrue(MyUInt64_1 == UInt64.Parse(MyString1));
120                 Assert.IsTrue(MyUInt64_2 == UInt64.Parse(MyString2));
121                 Assert.IsTrue(MyUInt64_3 == UInt64.Parse(MyString3));
122                 try {
123                         UInt64.Parse(null);
124                         Assert.Fail("Should raise a ArgumentNullException");
125                 }
126                 catch (Exception e) {
127                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
128                 }
129                 try {
130                         UInt64.Parse("not-a-number");
131                         Assert.Fail("Should raise a System.FormatException");
132                 }
133                 catch (Exception e) {
134                         Assert.IsTrue(typeof(FormatException) == e.GetType());
135                 }
136                 //test Parse(string s, NumberStyles style)
137                 try {
138                         double OverInt = (double)UInt64.MaxValue + 1;
139                         UInt64.Parse(OverInt.ToString(), NumberStyles.Float);
140                         Assert.Fail("Should raise a OverflowException");
141                 }
142                 catch (Exception e) {
143                         Assert.IsTrue(typeof(OverflowException) == e.GetType());
144                 }
145                 try {
146                         double OverInt = (double)UInt64.MaxValue + 1;
147                         UInt64.Parse(OverInt.ToString(), NumberStyles.Integer);
148                         Assert.Fail("Should raise a System.FormatException");
149                 }
150                 catch (Exception e) {
151                         Assert.IsTrue(typeof(FormatException) == e.GetType());
152                 }
153                 Assert.IsTrue(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
154                 try {
155                         UInt64.Parse("$42", NumberStyles.Integer);
156                         Assert.Fail("Should raise a FormatException");
157                 }
158                 catch (Exception e) {
159                         Assert.IsTrue(typeof(FormatException) == e.GetType());
160                 }
161                 //test Parse(string s, IFormatProvider provider)
162                 Assert.IsTrue(42 == UInt64.Parse(" 42 ", Nfi));
163                 try {
164                         UInt64.Parse("%42", Nfi);
165                         Assert.Fail("Should raise a System.FormatException");
166                 }
167                 catch (Exception e) {
168                         Assert.IsTrue(typeof(FormatException) == e.GetType());
169                 }
170                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
171                 Assert.IsTrue(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
172                 try {
173                         UInt64.Parse("$42", NumberStyles.Integer, Nfi);
174                         Assert.Fail("Should raise a System.FormatException");
175                 }
176                 catch (Exception e) {
177                         Assert.IsTrue(typeof(FormatException) == e.GetType());
178                 }
179
180                 // Pass a DateTimeFormatInfo, it is unable to format
181                 // numbers, but we should not crash
182                 
183                 UInt64.Parse ("123", new DateTimeFormatInfo ());
184         }
185         
186         public void TestToString()
187         {
188                 //test ToString()
189                 Assert.AreEqual(MyString1, MyUInt64_1.ToString(), "A");
190                 Assert.AreEqual(MyString2, MyUInt64_2.ToString(), "B");
191                 Assert.AreEqual(MyString3, MyUInt64_3.ToString(), "C");
192                 //test ToString(string format)
193                 for (int i=0; i < Formats1.Length; i++) {
194                         Assert.AreEqual(Results1[i], MyUInt64_2.ToString(Formats1[i]), "D");
195                         Assert.AreEqual(Results2[i], MyUInt64_3.ToString(Formats2[i]), "E: format #" + i);
196                 }
197                 //test ToString(string format, IFormatProvider provider);
198                 for (int i=0; i < Formats1.Length; i++) {
199                         Assert.AreEqual(ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi), "F");
200                         Assert.AreEqual(ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi), "G");
201                 }
202                 try {
203                         MyUInt64_1.ToString("z");
204                         Assert.Fail("Should raise a System.FormatException");
205                 }
206                 catch (Exception e) {
207                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "H");
208                 }
209         }
210
211         [Test]
212         public void ToString_Defaults () 
213         {
214                 UInt64 i = 254;
215                 // everything defaults to "G"
216                 string def = i.ToString ("G");
217                 Assert.AreEqual (def, i.ToString (), "ToString()");
218                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
219                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
220                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
221                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
222                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
223
224                 Assert.AreEqual ("254", def, "ToString(G)");
225         }
226 }
227
228 }