2002-06-11 Nick Drochak <ndrochak@gol.com>
[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 public class UInt64Test : TestCase
17 {
18         private const UInt64 MyUInt64_1 = 42;
19         private const UInt64 MyUInt64_2 = 0;
20         private const UInt64 MyUInt64_3 = 18446744073709551615;
21         private const string MyString1 = "42";
22         private const string MyString2 = "0";
23         private const string MyString3 = "18446744073709551615";
24         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
25         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
26         private string[] Results1 = {"",
27                                         "0", "0.000000e+000", "0.00",
28                                         "0", "0.00", "0.00 %", "0"};
29         private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
30                                         "0", "0.000000e+000", "0.00",
31                                         "0", "0.00", "0.00 %", "0"};
32         private string[] Results2 = {"",
33                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
34                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
35                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
36         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"18,446,744,073,709,551,615.00000",
37                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
38                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
39                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
40
41         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
42         
43         public UInt64Test() : base ("MonoTests.System.UInt64Test testcase") {}\r
44         public UInt64Test(string name) : base(name) {}
45
46         private CultureInfo old_culture;
47
48         protected override void SetUp() 
49         {
50                 old_culture = Thread.CurrentThread.CurrentCulture;
51
52                 // Set culture to en-US and don't let the user override.
53                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
54
55                 // We can't initialize this until we set the culture.
56                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"0.00";
57                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"18,446,744,073,709,551,615.00000";
58         }
59
60         protected override void TearDown()
61         {
62                 Thread.CurrentThread.CurrentCulture = old_culture;
63         }
64
65         public static ITest Suite {
66                 get { 
67                         return new TestSuite(typeof(UInt64Test)); 
68                 }
69         }
70     
71         public void TestMinMax()
72         {
73                 
74                 AssertEquals(UInt64.MinValue, MyUInt64_2);
75                 AssertEquals(UInt64.MaxValue, MyUInt64_3);
76         }
77         
78         public void TestCompareTo()
79         {
80                 Assert(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
81                 Assert(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
82                 Assert(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
83                 Assert(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
84                 try {
85                         MyUInt64_2.CompareTo((Int16)100);
86                         Fail("Should raise a System.ArgumentException");
87                 }
88                 catch (Exception e) {
89                         Assert(typeof(ArgumentException) == e.GetType());
90                 }
91         }
92
93         public void TestEquals()
94         {
95                 Assert(MyUInt64_1.Equals(MyUInt64_1));
96                 Assert(MyUInt64_1.Equals((UInt64)(42)));
97                 Assert(MyUInt64_1.Equals((SByte)(42)) == false);
98                 Assert(MyUInt64_1.Equals(MyUInt64_2) == false);
99         }
100         
101         public void TestGetHashCode()
102         {
103                 try {
104                         MyUInt64_1.GetHashCode();
105                         MyUInt64_2.GetHashCode();
106                         MyUInt64_3.GetHashCode();
107                 }
108                 catch {
109                         Fail("GetHashCode should not raise an exception here");
110                 }
111         }
112         
113         public void TestParse()
114         {
115                 //test Parse(string s)
116                 Assert(MyUInt64_1 == UInt64.Parse(MyString1));
117                 Assert(MyUInt64_2 == UInt64.Parse(MyString2));
118                 Assert(MyUInt64_3 == UInt64.Parse(MyString3));
119                 try {
120                         UInt64.Parse(null);
121                         Fail("Should raise a ArgumentNullException");
122                 }
123                 catch (Exception e) {
124                         Assert(typeof(ArgumentNullException) == e.GetType());
125                 }
126                 try {
127                         UInt64.Parse("not-a-number");
128                         Fail("Should raise a System.FormatException");
129                 }
130                 catch (Exception e) {
131                         Assert(typeof(FormatException) == e.GetType());
132                 }
133                 //test Parse(string s, NumberStyles style)
134                 try {
135                         double OverInt = (double)UInt64.MaxValue + 1;
136                         UInt64.Parse(OverInt.ToString(), NumberStyles.Float);
137                         Fail("Should raise a OverflowException");
138                 }
139                 catch (Exception e) {
140                         Assert(typeof(OverflowException) == e.GetType());
141                 }
142                 try {
143                         double OverInt = (double)UInt64.MaxValue + 1;
144                         UInt64.Parse(OverInt.ToString(), NumberStyles.Integer);
145                         Fail("Should raise a System.FormatException");
146                 }
147                 catch (Exception e) {
148                         Assert(typeof(FormatException) == e.GetType());
149                 }
150                 Assert(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
151                 try {
152                         UInt64.Parse("$42", NumberStyles.Integer);
153                         Fail("Should raise a FormatException");
154                 }
155                 catch (Exception e) {
156                         Assert(typeof(FormatException) == e.GetType());
157                 }
158                 //test Parse(string s, IFormatProvider provider)
159                 Assert(42 == UInt64.Parse(" 42 ", Nfi));
160                 try {
161                         UInt64.Parse("%42", Nfi);
162                         Fail("Should raise a System.FormatException");
163                 }
164                 catch (Exception e) {
165                         Assert(typeof(FormatException) == e.GetType());
166                 }
167                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
168                 Assert(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
169                 try {
170                         UInt64.Parse("$42", NumberStyles.Integer, Nfi);
171                         Fail("Should raise a System.FormatException");
172                 }
173                 catch (Exception e) {
174                         Assert(typeof(FormatException) == e.GetType());
175                 }
176         }
177         
178         public void TestToString()
179         {
180                 //test ToString()
181                 AssertEquals(MyString1, MyUInt64_1.ToString());
182                 AssertEquals(MyString2, MyUInt64_2.ToString());
183                 AssertEquals(MyString3, MyUInt64_3.ToString());
184                 //test ToString(string format)
185                 for (int i=0; i < Formats1.Length; i++) {
186                         AssertEquals(Results1[i], MyUInt64_2.ToString(Formats1[i]));
187                         AssertEquals(Results2[i], MyUInt64_3.ToString(Formats2[i]));
188                 }
189                 //test ToString(string format, IFormatProvider provider);
190                 for (int i=0; i < Formats1.Length; i++) {
191                         AssertEquals(ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi));
192                         AssertEquals(ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi));
193                 }
194                 try {
195                         MyUInt64_1.ToString("z");
196                         Fail("Should raise a System.FormatException");
197                 }
198                 catch (Exception e) {
199                         Assert(typeof(FormatException) == e.GetType());
200                 }
201         }
202 }
203
204 }