Copied remotely
[mono.git] / mcs / class / corlib / Test / System / UInt16Test.cs
1 // UInt16Test.cs - NUnit Test Cases for the System.UInt16 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.Threading;
11 using System.Globalization;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class UInt16Test : Assertion
18 {
19         private const UInt16 MyUInt16_1 = 42;
20         private const UInt16 MyUInt16_2 = 0;
21         private const UInt16 MyUInt16_3 = 65535;
22         private const string MyString1 = "42";
23         private const string MyString2 = "0";
24         private const string MyString3 = "65535";
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 = {null,
28                                      "0", "0.000000e+000", "0.00",
29                                      "0", "0.00", "0.00 %", "0"};
30         private string[] Results2 = {null,
31                                      "65535", "6.55350e+004", "65535.00000",
32                                      "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
33         private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
34                                         "0", "0.000000e+000", "0.00",
35                                         "0", "0.00", "0.00 %", "0"};
36         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"65,535.00000",
37                                         "65535", "6.55350e+004", "65535.00000",
38                                         "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
39
40         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
41         
42         private CultureInfo old_culture;
43
44         [TestFixtureSetUp]
45         public void SetUp () 
46         {
47                 old_culture = Thread.CurrentThread.CurrentCulture;
48
49                 // Set culture to en-US and don't let the user override.
50                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
51
52                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
53                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
54                 
55                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
56                 Results1 [3] = "0." + decimals;
57                 Results1 [5] = "0." + decimals;
58                 Results1 [6] = perPattern.Replace ("n","0.00");
59                 
60                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "65,535.00000";
61                 Results2 [6] = perPattern.Replace ("n","6,553,500.00000");
62         }
63
64         [TestFixtureTearDown]
65         public void TearDown ()
66         {
67                 Thread.CurrentThread.CurrentCulture = old_culture;
68         }
69
70         public void TestMinMax()
71         {
72                 
73                 AssertEquals(UInt16.MinValue, MyUInt16_2);
74                 AssertEquals(UInt16.MaxValue, MyUInt16_3);
75         }
76         
77         public void TestCompareTo()
78         {
79                 Assert(MyUInt16_3.CompareTo(MyUInt16_2) > 0);
80                 Assert(MyUInt16_2.CompareTo(MyUInt16_2) == 0);
81                 Assert(MyUInt16_1.CompareTo((UInt16)(42)) == 0);
82                 Assert(MyUInt16_2.CompareTo(MyUInt16_3) < 0);
83                 try {
84                         MyUInt16_2.CompareTo(100);
85                         Fail("Should raise a System.ArgumentException");
86                 }
87                 catch (Exception e) {
88                         Assert(typeof(ArgumentException) == e.GetType());
89                 }
90         }
91
92         public void TestEquals()
93         {
94                 Assert(MyUInt16_1.Equals(MyUInt16_1));
95                 Assert(MyUInt16_1.Equals((UInt16)(42)));
96                 Assert(MyUInt16_1.Equals((SByte)(42)) == false);
97                 Assert(MyUInt16_1.Equals(MyUInt16_2) == false);
98         }
99         
100         public void TestGetHashCode()
101         {
102                 try {
103                         MyUInt16_1.GetHashCode();
104                         MyUInt16_2.GetHashCode();
105                         MyUInt16_3.GetHashCode();
106                 }
107                 catch {
108                         Fail("GetHashCode should not raise an exception here");
109                 }
110         }
111         
112         public void TestParse()
113         {
114                 //test Parse(string s)
115                 Assert(MyUInt16_1 == UInt16.Parse(MyString1));
116                 Assert(MyUInt16_2 == UInt16.Parse(MyString2));
117                 Assert(MyUInt16_3 == UInt16.Parse(MyString3));
118                 try {
119                         UInt16.Parse(null);
120                         Fail("Should raise a System.ArgumentNullException");
121                 }
122                 catch (Exception e) {
123                         Assert(typeof(ArgumentNullException) == e.GetType());
124                 }
125                 try {
126                         UInt16.Parse("not-a-number");
127                         Fail("Should raise a System.FormatException");
128                 }
129                 catch (Exception e) {
130                         Assert(typeof(FormatException) == e.GetType());
131                 }
132                 try {
133                         int OverInt = UInt16.MaxValue + 1;
134                         UInt16.Parse(OverInt.ToString());
135                         Fail("Should raise a System.OverflowException");
136                 }
137                 catch (Exception e) {
138                         Assert(typeof(OverflowException) == e.GetType());
139                 }
140                 //test Parse(string s, NumberStyles style)
141                 Assert(42 == UInt16.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
142                 try {
143                         UInt16.Parse("$42", NumberStyles.Integer);
144                         Fail("Should raise a System.FormatException");
145                 }
146                 catch (Exception e) {
147                         Assert(typeof(FormatException) == e.GetType());
148                 }
149                 //test Parse(string s, IFormatProvider provider)
150                 Assert(42 == UInt16.Parse(" 42 ", Nfi));
151                 try {
152                         UInt16.Parse("%42", Nfi);
153                         Fail("Should raise a System.FormatException");
154                 }
155                 catch (Exception e) {
156                         Assert(typeof(FormatException) == e.GetType());
157                 }
158                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
159                 Assert(16 == UInt16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
160                 try {
161                         UInt16.Parse("$42", NumberStyles.Integer, Nfi);
162                         Fail("Should raise a System.FormatException");
163                 }
164                 catch (Exception e) {
165                         Assert(typeof(FormatException) == e.GetType());
166                 }
167         }
168         
169         public void TestToString()
170         {
171                 //test ToString()
172                 AssertEquals("A1", MyString1, MyUInt16_1.ToString());
173                 AssertEquals("A2", MyString2, MyUInt16_2.ToString());
174                 AssertEquals("A3", MyString3, MyUInt16_3.ToString());
175                 //test ToString(string format)
176                 for (int i=0; i < Formats1.Length; i++) {
177                         Console.WriteLine ("d:" + NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
178                         AssertEquals("A4:"+i.ToString(), Results1[i], MyUInt16_2.ToString(Formats1[i]));
179                         AssertEquals("A5:"+i.ToString(), Results2[i], MyUInt16_3.ToString(Formats2[i]));
180                 }
181                 //test ToString(string format, IFormatProvider provider);
182                 for (int i=0; i < Formats1.Length; i++) {
183                         AssertEquals("A6:"+i.ToString(), ResultsNfi1[i], MyUInt16_2.ToString(Formats1[i], Nfi));
184                         AssertEquals("A7:"+i.ToString(), ResultsNfi2[i], MyUInt16_3.ToString(Formats2[i], Nfi));
185                 }
186                 try {
187                         MyUInt16_1.ToString("z");
188                         Fail("Should raise a System.FormatException");
189                 }
190                 catch (Exception e) {
191                         Assert("A8", typeof(FormatException) == e.GetType());
192                 }
193         }
194
195         [Test]
196         public void ToString_Defaults () 
197         {
198                 UInt16 i = 254;
199                 // everything defaults to "G"
200                 string def = i.ToString ("G");
201                 AssertEquals ("ToString()", def, i.ToString ());
202                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
203                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
204                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
205                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
206                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
207
208                 AssertEquals ("ToString(G)", "254", def);
209         }
210 }
211
212 }