2002-04-30 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System / UInt32Test.cs
1 // UInt32Test.cs - NUnit Test Cases for the System.UInt32 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 UInt32Test : TestCase
16 {
17         private const UInt32 MyUInt32_1 = 42;
18         private const UInt32 MyUInt32_2 = 0;
19         private const UInt32 MyUInt32_3 = 4294967295;
20         private const string MyString1 = "42";
21         private const string MyString2 = "0";
22         private const string MyString3 = "4294967295";
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.InvariantInfo.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.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
32                                         "4294967295", "4.29497e+009", "4294967295.00000",
33                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
34         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
35                                         "4294967295", "4.29497e+009", "4294967295.00000",
36                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
37         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
38         
39         public UInt32Test() : base ("MonoTests.System.UInt32Test testcase") {}\r
40         public UInt32Test(string name) : base(name) {}
41
42         protected override void SetUp() 
43         {
44         }
45
46         public static ITest Suite {
47                 get { 
48                         return new TestSuite(typeof(UInt32Test)); 
49                 }
50         }
51     
52         public void TestMinMax()
53         {
54                 
55                 AssertEquals(UInt32.MinValue, MyUInt32_2);
56                 AssertEquals(UInt32.MaxValue, MyUInt32_3);
57         }
58         
59         public void TestCompareTo()
60         {
61                 Assert(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
62                 Assert(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
63                 Assert(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
64                 Assert(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
65                 Assert (1 == UInt32.Parse ("1"));
66                 Assert (1 == UInt32.Parse (" 1"));
67                 Assert (1 == UInt32.Parse ("     1"));
68                 Assert (1 == UInt32.Parse ("1    "));
69                 Assert (1 == UInt32.Parse ("+1"));
70
71                 try {
72                         UInt32.Parse (" + 1 ");
73                         Fail ("Should raise FormatException1");
74                 } catch (Exception e){
75                         Assert (typeof (FormatException) == e.GetType ());
76                 }
77
78                 try {
79                         UInt32.Parse (" + ");
80                         Fail ("Should raise FormatException");
81                 } catch (Exception e){
82                         Assert (typeof (FormatException) == e.GetType ());
83                 }
84                 try {
85                         MyUInt32_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(MyUInt32_1.Equals(MyUInt32_1));
96                 Assert(MyUInt32_1.Equals((UInt32)(42)));
97                 Assert(MyUInt32_1.Equals((SByte)(42)) == false);
98                 Assert(MyUInt32_1.Equals(MyUInt32_2) == false);
99         }
100         
101         public void TestGetHashCode()
102         {
103                 try {
104                         MyUInt32_1.GetHashCode();
105                         MyUInt32_2.GetHashCode();
106                         MyUInt32_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("Parse problem on \""+MyString1+"\"", MyUInt32_1 == UInt32.Parse(MyString1));
117                 Assert("Parse problem on \""+MyString2+"\"", MyUInt32_2 == UInt32.Parse(MyString2));
118                 Assert("Parse problem on \""+MyString3+"\"", MyUInt32_3 == UInt32.Parse(MyString3));
119                 try {
120                         UInt32.Parse(null);
121                         Fail("Should raise a System.ArgumentNullException");
122                 }
123                 catch (Exception e) {
124                         Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException) == e.GetType());
125                 }
126                 try {
127                         UInt32.Parse("not-a-number");
128                         Fail("Should raise a System.FormatException");
129                 }
130                 catch (Exception e) {
131                         Assert("Did not get FormatException type", typeof(FormatException) == e.GetType());
132                 }
133                 try {
134                         // TODO: Use this after ToString() is completed. For now, hard code string that generates
135                         // exception.
136                         //double OverInt = (double)UInt32.MaxValue + 1;
137                         //UInt32.Parse(OverInt.ToString());
138                         UInt32.Parse("4294967296");
139                         Fail("Should raise a System.OverflowException");
140                 }
141                 catch (Exception e) {
142                         Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'", typeof(OverflowException) == e.GetType());
143                 }
144                 //test Parse(string s, NumberStyles style)
145                 Assert(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
146                 try {
147                         UInt32.Parse("$42", NumberStyles.Integer);
148                         Fail("Should raise a System.FormatException");
149                 }
150                 catch (Exception e) {
151                         Assert(typeof(FormatException) == e.GetType());
152                 }
153                 //test Parse(string s, IFormatProvider provider)
154                 Assert(42 == UInt32.Parse(" 42 ", Nfi));
155                 try {
156                         UInt32.Parse("%42", Nfi);
157                         Fail("Should raise a System.FormatException");
158                 }
159                 catch (Exception e) {
160                         Assert(typeof(FormatException) == e.GetType());
161                 }
162                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
163                 Assert(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
164                 try {
165                         UInt32.Parse("$42", NumberStyles.Integer, Nfi);
166                         Fail("Should raise a System.FormatException");
167                 }
168                 catch (Exception e) {
169                         Assert(typeof(FormatException) == e.GetType());
170                 }
171         }
172         
173         public void TestToString()
174         {
175                 int TestNumber = 1;
176                 try {
177                         //test ToString()
178                         AssertEquals(MyString1, MyUInt32_1.ToString());
179                         TestNumber++;
180                         AssertEquals(MyString2, MyUInt32_2.ToString());
181                         TestNumber++;
182                         AssertEquals(MyString3, MyUInt32_3.ToString());
183                 } catch (Exception e) {
184                         Fail("TestToString: Failed on TestNumber=" + TestNumber 
185                                 + " with exception: " + e.ToString());
186                 }
187
188                 //test ToString(string format)
189                 for (int i=0; i < Formats1.Length; i++) {
190                         try {
191                                 AssertEquals(Results1[i], MyUInt32_2.ToString(Formats1[i]));
192                         } catch (Exception e) {
193                                 Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
194                                         + ". e = " + e.ToString());
195                         }
196
197                         try {
198                                 AssertEquals(Results2[i], MyUInt32_3.ToString(Formats2[i]));
199                         } catch (Exception e) {
200                                 Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
201                                         + ". e = " + e.ToString());
202                         }
203                 }
204                 //test ToString(string format, IFormatProvider provider);
205                 for (int i=0; i < Formats1.Length; i++) {
206                         AssertEquals(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
207                         AssertEquals(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
208                 }
209                 try {
210                         MyUInt32_1.ToString("z");
211                         Fail("Should raise a System.FormatException");
212                 }
213                 catch (Exception e) {
214                         Assert(typeof(FormatException) == e.GetType());
215                 }
216         }
217 }
218
219
220 }