* roottypes.cs: Rename from tree.cs.
[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 using System.Threading;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class UInt32Test : Assertion
18 {
19         private const UInt32 MyUInt32_1 = 42;
20         private const UInt32 MyUInt32_2 = 0;
21         private const UInt32 MyUInt32_3 = 4294967295;
22         private const string MyString1 = "42";
23         private const string MyString2 = "0";
24         private const string MyString3 = "4294967295";
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                                         "4294967295", "4.29497e+009", "4294967295.00000",
35                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
36         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
37                                         "4294967295", "4.29497e+009", "4294967295.00000",
38                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
39         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
40         
41         private CultureInfo old_culture;
42
43         [TestFixtureSetUp]
44         public void SetUp () 
45         {
46                 old_culture = Thread.CurrentThread.CurrentCulture;
47
48                 // Set culture to en-US and don't let the user override.
49                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
50
51                 // We can't initialize this until we set the culture.
52                 
53                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
54                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
55                 
56                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
57                 Results1 [3] = "0." + decimals;
58                 Results1 [5] = "0." + decimals;
59                 Results1 [6] = perPattern.Replace ("n","0.00");
60                 
61                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "4,294,967,295.00000";
62                 Results2 [6] = perPattern.Replace ("n","429,496,729,500.00000");
63         }
64
65         [TestFixtureTearDown]
66         public void TearDown ()
67         {
68                 Thread.CurrentThread.CurrentCulture = old_culture;
69         }
70
71         public void TestMinMax()
72         {
73                 
74                 AssertEquals(UInt32.MinValue, MyUInt32_2);
75                 AssertEquals(UInt32.MaxValue, MyUInt32_3);
76         }
77         
78         public void TestCompareTo()
79         {
80                 Assert(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
81                 Assert(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
82                 Assert(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
83                 Assert(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
84                 Assert (1 == UInt32.Parse ("1"));
85                 Assert (1 == UInt32.Parse (" 1"));
86                 Assert (1 == UInt32.Parse ("     1"));
87                 Assert (1 == UInt32.Parse ("1    "));
88                 Assert (1 == UInt32.Parse ("+1"));
89
90                 try {
91                         UInt32.Parse (" + 1 ");
92                         Fail ("Should raise FormatException1");
93                 } catch (Exception e){
94                         Assert (typeof (FormatException) == e.GetType ());
95                 }
96
97                 try {
98                         UInt32.Parse (" + ");
99                         Fail ("Should raise FormatException");
100                 } catch (Exception e){
101                         Assert (typeof (FormatException) == e.GetType ());
102                 }
103                 try {
104                         MyUInt32_2.CompareTo((object)(Int16)100);
105                         Fail("Should raise a System.ArgumentException");
106                 }
107                 catch (Exception e) {
108                         Assert(typeof(ArgumentException) == e.GetType());
109                 }
110         }
111
112         public void TestEquals()
113         {
114                 Assert(MyUInt32_1.Equals(MyUInt32_1));
115                 Assert(MyUInt32_1.Equals((object)(UInt32)(42)));
116                 Assert(MyUInt32_1.Equals((object)(SByte)(42)) == false);
117                 Assert(MyUInt32_1.Equals(MyUInt32_2) == false);
118         }
119         
120         public void TestGetHashCode()
121         {
122                 try {
123                         MyUInt32_1.GetHashCode();
124                         MyUInt32_2.GetHashCode();
125                         MyUInt32_3.GetHashCode();
126                 }
127                 catch {
128                         Fail("GetHashCode should not raise an exception here");
129                 }
130         }
131         
132         public void TestParse()
133         {
134                 //test Parse(string s)
135                 Assert("Parse problem on \""+MyString1+"\"", MyUInt32_1 == UInt32.Parse(MyString1));
136                 Assert("Parse problem on \""+MyString2+"\"", MyUInt32_2 == UInt32.Parse(MyString2));
137                 Assert("Parse problem on \""+MyString3+"\"", MyUInt32_3 == UInt32.Parse(MyString3));
138                 try {
139                         UInt32.Parse(null);
140                         Fail("Should raise a System.ArgumentNullException");
141                 }
142                 catch (Exception e) {
143                         Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException) == e.GetType());
144                 }
145                 try {
146                         UInt32.Parse("not-a-number");
147                         Fail("Should raise a System.FormatException");
148                 }
149                 catch (Exception e) {
150                         Assert("Did not get FormatException type", typeof(FormatException) == e.GetType());
151                 }
152                 try {
153                         // TODO: Use this after ToString() is completed. For now, hard code string that generates
154                         // exception.
155                         //double OverInt = (double)UInt32.MaxValue + 1;
156                         //UInt32.Parse(OverInt.ToString());
157                         UInt32.Parse("4294967296");
158                         Fail("Should raise a System.OverflowException");
159                 }
160                 catch (Exception e) {
161                         Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'", typeof(OverflowException) == e.GetType());
162                 }
163                 //test Parse(string s, NumberStyles style)
164                 Assert(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
165                 try {
166                         UInt32.Parse("$42", NumberStyles.Integer);
167                         Fail("Should raise a System.FormatException");
168                 }
169                 catch (Exception e) {
170                         Assert(typeof(FormatException) == e.GetType());
171                 }
172                 //test Parse(string s, IFormatProvider provider)
173                 Assert(42 == UInt32.Parse(" 42 ", Nfi));
174                 try {
175                         UInt32.Parse("%42", Nfi);
176                         Fail("Should raise a System.FormatException");
177                 }
178                 catch (Exception e) {
179                         Assert(typeof(FormatException) == e.GetType());
180                 }
181                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
182                 Assert(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
183                 try {
184                         UInt32.Parse("$42", NumberStyles.Integer, Nfi);
185                         Fail("Should raise a System.FormatException");
186                 }
187                 catch (Exception e) {
188                         Assert(typeof(FormatException) == e.GetType());
189                 }
190         }
191         
192         public void TestToString()
193         {
194                 int TestNumber = 1;
195                 try {
196                         //test ToString()
197                         AssertEquals(MyString1, MyUInt32_1.ToString());
198                         TestNumber++;
199                         AssertEquals(MyString2, MyUInt32_2.ToString());
200                         TestNumber++;
201                         AssertEquals(MyString3, MyUInt32_3.ToString());
202                 } catch (Exception e) {
203                         Fail("TestToString: Failed on TestNumber=" + TestNumber 
204                                 + " with exception: " + e.ToString());
205                 }
206
207                 //test ToString(string format)
208                 for (int i=0; i < Formats1.Length; i++) {
209                         try {
210                                 AssertEquals(Results1[i], MyUInt32_2.ToString(Formats1[i]));
211                         } catch (Exception e) {
212                                 Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
213                                         + ". e = " + e.ToString());
214                         }
215
216                         try {
217                                 AssertEquals(Results2[i], MyUInt32_3.ToString(Formats2[i]));
218                         } catch (Exception e) {
219                                 Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
220                                         + ". e = " + e.ToString());
221                         }
222                 }
223                 //test ToString(string format, IFormatProvider provider);
224                 for (int i=0; i < Formats1.Length; i++) {
225                         AssertEquals(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
226                         AssertEquals(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
227                 }
228                 try {
229                         MyUInt32_1.ToString("z");
230                         Fail("Should raise a System.FormatException");
231                 }
232                 catch (Exception e) {
233                         Assert(typeof(FormatException) == e.GetType());
234                 }
235         }
236
237         [Test]
238         public void ToString_Defaults () 
239         {
240                 UInt32 i = 254;
241                 // everything defaults to "G"
242                 string def = i.ToString ("G");
243                 AssertEquals ("ToString()", def, i.ToString ());
244                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
245                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
246                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
247                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
248                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
249
250                 AssertEquals ("ToString(G)", "254", def);
251         }
252 }
253
254
255 }