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