Bug 15574. XML deserialization recursion: add array type to known_types?
[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 
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         [SetUp]
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         [TearDown]
65         public void TearDown ()
66         {
67                 Thread.CurrentThread.CurrentCulture = old_culture;
68         }
69
70         [Test]
71         public void TestMinMax()
72         {
73                 
74                 Assert.AreEqual(UInt16.MinValue, MyUInt16_2);
75                 Assert.AreEqual(UInt16.MaxValue, MyUInt16_3);
76         }
77
78         [Test]
79         public void TestCompareTo()
80         {
81                 Assert.IsTrue(MyUInt16_3.CompareTo(MyUInt16_2) > 0);
82                 Assert.IsTrue(MyUInt16_2.CompareTo(MyUInt16_2) == 0);
83                 Assert.IsTrue(MyUInt16_1.CompareTo((UInt16)(42)) == 0);
84                 Assert.IsTrue(MyUInt16_2.CompareTo(MyUInt16_3) < 0);
85                 try {
86                         MyUInt16_2.CompareTo((object)100);
87                         Assert.Fail("Should raise a System.ArgumentException");
88                 }
89                 catch (Exception e) {
90                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
91                 }
92         }
93
94         [Test]
95         public void TestEquals()
96         {
97                 Assert.IsTrue(MyUInt16_1.Equals(MyUInt16_1));
98                 Assert.IsTrue(MyUInt16_1.Equals((object)(UInt16)(42)));
99                 Assert.IsTrue(MyUInt16_1.Equals((object)(SByte)(42)) == false);
100                 Assert.IsTrue(MyUInt16_1.Equals(MyUInt16_2) == false);
101         }
102
103         [Test]
104         public void TestGetHashCode()
105         {
106                 try {
107                         MyUInt16_1.GetHashCode();
108                         MyUInt16_2.GetHashCode();
109                         MyUInt16_3.GetHashCode();
110                 }
111                 catch {
112                         Assert.Fail("GetHashCode should not raise an exception here");
113                 }
114         }
115
116         [Test]
117         public void TestParse()
118         {
119                 //test Parse(string s)
120                 Assert.IsTrue(MyUInt16_1 == UInt16.Parse(MyString1));
121                 Assert.IsTrue(MyUInt16_2 == UInt16.Parse(MyString2));
122                 Assert.IsTrue(MyUInt16_3 == UInt16.Parse(MyString3));
123                 try {
124                         UInt16.Parse(null);
125                         Assert.Fail("Should raise a System.ArgumentNullException");
126                 }
127                 catch (Exception e) {
128                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
129                 }
130                 try {
131                         UInt16.Parse("not-a-number");
132                         Assert.Fail("Should raise a System.FormatException");
133                 }
134                 catch (Exception e) {
135                         Assert.IsTrue(typeof(FormatException) == e.GetType());
136                 }
137                 try {
138                         int OverInt = UInt16.MaxValue + 1;
139                         UInt16.Parse(OverInt.ToString());
140                         Assert.Fail("Should raise a System.OverflowException");
141                 }
142                 catch (Exception e) {
143                         Assert.IsTrue(typeof(OverflowException) == e.GetType());
144                 }
145                 //test Parse(string s, NumberStyles style)
146                 Assert.IsTrue(42 == UInt16.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
147                 try {
148                         UInt16.Parse("$42", NumberStyles.Integer);
149                         Assert.Fail("Should raise a System.FormatException");
150                 }
151                 catch (Exception e) {
152                         Assert.IsTrue(typeof(FormatException) == e.GetType());
153                 }
154                 //test Parse(string s, IFormatProvider provider)
155                 Assert.IsTrue(42 == UInt16.Parse(" 42 ", Nfi));
156                 try {
157                         UInt16.Parse("%42", Nfi);
158                         Assert.Fail("Should raise a System.FormatException");
159                 }
160                 catch (Exception e) {
161                         Assert.IsTrue(typeof(FormatException) == e.GetType());
162                 }
163                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
164                 Assert.IsTrue(16 == UInt16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
165                 try {
166                         UInt16.Parse("$42", NumberStyles.Integer, Nfi);
167                         Assert.Fail("Should raise a System.FormatException");
168                 }
169                 catch (Exception e) {
170                         Assert.IsTrue(typeof(FormatException) == e.GetType());
171                 }
172         }
173
174         [Test]
175         public void TestParseExponent ()
176         {
177                 Assert.AreEqual (2, UInt16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
178                 Assert.AreEqual (20, UInt16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
179                 Assert.AreEqual (200, UInt16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
180                 Assert.AreEqual (200, UInt16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
181                 Assert.AreEqual (2, UInt16.Parse ("2", NumberStyles.AllowExponent), "A#5");
182
183                 try {
184                         UInt16.Parse ("2E");
185                         Assert.Fail ("B#1");
186                 } catch (FormatException) {
187                 }
188
189                 try {
190                         UInt16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
191                         Assert.Fail ("B#2");
192                 } catch (FormatException) {
193                 }
194
195                 try {
196                         UInt16.Parse ("2E 2", NumberStyles.AllowExponent);
197                         Assert.Fail ("B#3");
198                 } catch (FormatException) {
199                 }
200
201                 try {
202                         UInt16.Parse ("2E2 ", NumberStyles.AllowExponent);
203                         Assert.Fail ("B#4");
204                 } catch (FormatException) {
205                 }
206
207                 try {
208                         UInt16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
209                         Assert.Fail ("B#5");
210                 } catch (OverflowException) {
211                 }
212
213                 try {
214                         long exponent = (long) Int32.MaxValue + 10;
215                         UInt16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
216                         Assert.Fail ("B#6");
217                 } catch (OverflowException) {
218                 }
219
220                 try {
221                         UInt16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
222                         Assert.Fail ("B#7");
223                 } catch (OverflowException) {
224                 }
225                 
226                 try {
227                         UInt16.Parse ("2 math e1", NumberStyles.AllowExponent);
228                         Assert.Fail ("B#8");
229                 } catch (FormatException) {
230                 }
231         }
232
233         [Test]
234         public void TestToString()
235         {
236                 //test ToString()
237                 Assert.AreEqual(MyString1, MyUInt16_1.ToString(), "A1");
238                 Assert.AreEqual(MyString2, MyUInt16_2.ToString(), "A2");
239                 Assert.AreEqual(MyString3, MyUInt16_3.ToString(), "A3");
240                 //test ToString(string format)
241                 for (int i=0; i < Formats1.Length; i++) {
242                         Console.WriteLine ("d:" + NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
243                         Assert.AreEqual(Results1[i], MyUInt16_2.ToString(Formats1[i]), "A4:"+i.ToString());
244                         Assert.AreEqual(Results2[i], MyUInt16_3.ToString(Formats2[i]), "A5:"+i.ToString());
245                 }
246                 //test ToString(string format, IFormatProvider provider);
247                 for (int i=0; i < Formats1.Length; i++) {
248                         Assert.AreEqual(ResultsNfi1[i], MyUInt16_2.ToString(Formats1[i], Nfi), "A6:"+i.ToString());
249                         Assert.AreEqual(ResultsNfi2[i], MyUInt16_3.ToString(Formats2[i], Nfi), "A7:"+i.ToString());
250                 }
251                 try {
252                         MyUInt16_1.ToString("z");
253                         Assert.Fail("Should raise a System.FormatException");
254                 }
255                 catch (Exception e) {
256                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "A8");
257                 }
258         }
259
260         [Test]
261         public void ToString_Defaults () 
262         {
263                 UInt16 i = 254;
264                 // everything defaults to "G"
265                 string def = i.ToString ("G");
266                 Assert.AreEqual (def, i.ToString (), "ToString()");
267                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
268                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
269                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
270                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
271                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
272
273                 Assert.AreEqual ("254", def, "ToString(G)");
274         }
275 }
276
277 }