Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System / Int16Test.cs
1 // Int16Test.cs - NUnit Test Cases for the System.Int16 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 Int16Test 
18 {
19         private const Int16 MyInt16_1 = -42;
20         private const Int16 MyInt16_2 = -32768;
21         private const Int16 MyInt16_3 = 32767;
22         private const string MyString1 = "-42";
23         private const string MyString2 = "-32768";
24         private const string MyString3 = "32767";
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, "-32768", "-3.276800e+004", "-32768.00",
28                                           "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
29         private string[] Results2 = {null, "32767", "3.27670e+004", "32767.00000",
30                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
31         private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
32                                           "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
33         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"32,767.00000", "32767", "3.27670e+004", "32767.00000",
34                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
35         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
36         
37         private CultureInfo old_culture;
38
39         [TestFixtureSetUp]
40         public void SetUpFixture () 
41         {
42                 old_culture = Thread.CurrentThread.CurrentCulture;
43
44                 // Set culture to en-US and don't let the user override.
45                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
46
47                 // We can't initialize this until we set the culture.
48                 Results1 [0] = "("+NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,768.00)";
49                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,767.00000";
50         }
51         
52         [SetUp]
53         public void Setup ()
54         {
55                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
56         }
57
58         [TestFixtureTearDown]
59         public void TearDown ()
60         {
61                 Thread.CurrentThread.CurrentCulture = old_culture;
62         }
63
64         [Test]
65         public void TestMinMax()
66         {
67                 Assert.AreEqual(Int16.MinValue, MyInt16_2);
68                 Assert.AreEqual(Int16.MaxValue, MyInt16_3);
69         }
70
71         [Test]  
72         public void TestCompareTo()
73         {
74                 Assert.AreEqual(65535, MyInt16_3.CompareTo(MyInt16_2), "#1");
75                 Assert.AreEqual(0, MyInt16_2.CompareTo(MyInt16_2), "#2");
76                 Assert.AreEqual(0, MyInt16_1.CompareTo((Int16)(-42)), "#3");
77                 Assert.AreEqual(-65535, MyInt16_2.CompareTo(MyInt16_3), "#4");
78                 try {
79                         MyInt16_2.CompareTo((object)100);
80                         Assert.Fail ("Should raise a System.ArgumentException");
81                 } catch (ArgumentException e) {
82                 }
83         }
84
85         [Test]
86         public void TestEquals()
87         {
88                 Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
89                 Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
90                 Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
91                 Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
92         }
93
94         [Test]  
95         public void TestGetHashCode()
96         {
97                 try {
98                         MyInt16_1.GetHashCode();
99                         MyInt16_2.GetHashCode();
100                         MyInt16_3.GetHashCode();
101                 }
102                 catch {
103                         Assert.Fail ("GetHashCode should not raise an exception here");
104                 }
105         }
106
107         [Test]  
108         public void TestParse()
109         {
110                 //test Parse(string s)
111                 Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
112                 Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
113                 Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
114                 try {
115                         Int16.Parse(null);
116                         Assert.Fail ("Should raise a System.ArgumentNullException");
117                 }
118                 catch (Exception e) {
119                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
120                 }
121                 try {
122                         Int16.Parse("not-a-number");
123                         Assert.Fail ("Should raise a System.FormatException");
124                 }
125                 catch (Exception e) {
126                         Assert.IsTrue(typeof(FormatException) == e.GetType());
127                 }
128                 try {
129                         int OverInt = Int16.MaxValue + 1;
130                         Int16.Parse(OverInt.ToString());
131                         Assert.Fail ("Should raise a System.OverflowException");
132                 }
133                 catch (Exception e) {
134                         Assert.IsTrue(typeof(OverflowException) == e.GetType());
135                 }
136                 //test Parse(string s, NumberStyles style)
137                 Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
138                 try {
139                         Int16.Parse("$42", NumberStyles.Integer);
140                         Assert.Fail ("Should raise a System.FormatException");
141                 }
142                 catch (Exception e) {
143                         Assert.IsTrue(typeof(FormatException) == e.GetType());
144                 }
145                 //test Parse(string s, IFormatProvider provider)
146                 Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
147                 try {
148                         Int16.Parse("%42", Nfi);
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, NumberStyles style, IFormatProvider provider)
155                 Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
156                 try {
157                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
158                         Assert.Fail ("Should raise a System.FormatException");
159                 }
160                 catch (Exception e) {
161                         Assert.IsTrue(typeof(FormatException) == e.GetType());
162                 }
163
164                 Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1");
165                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    \0"), "#2");
166                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    "), "#3");
167                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
168
169                 Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
170         }
171
172         [Test]
173         public void TestParseExponent ()
174         {
175                 Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
176                 Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
177                 Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
178                 Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
179                 Assert.AreEqual (2, Int16.Parse ("2", NumberStyles.AllowExponent), "A#5");
180
181                 try {
182                         Int16.Parse ("2E");
183                         Assert.Fail ("B#1");
184                 } catch (FormatException) {
185                 }
186
187                 try {
188                         Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
189                         Assert.Fail ("B#2");
190                 } catch (FormatException) {
191                 }
192
193                 try {
194                         Int16.Parse ("2E 2", NumberStyles.AllowExponent);
195                         Assert.Fail ("B#3");
196                 } catch (FormatException) {
197                 }
198
199                 try {
200                         Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
201                         Assert.Fail ("B#4");
202                 } catch (FormatException) {
203                 }
204
205                 try {
206                         Int16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
207                         Assert.Fail ("B#5");
208                 } catch (OverflowException) {
209                 }
210
211                 try {
212                         long exponent = (long) Int32.MaxValue + 10;
213                         Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
214                         Assert.Fail ("B#6");
215                 } catch (OverflowException) {
216                 }
217
218                 try {
219                         Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
220                         Assert.Fail ("B#7");
221                 } catch (OverflowException) {
222                 }
223                 
224                 try {
225                         Int16.Parse ("2 math e1", NumberStyles.AllowExponent);
226                         Assert.Fail ("B#8");
227                 } catch (FormatException) {
228                 }
229         }
230
231         [Test]
232         public void Parse_MinMax ()
233         {
234                 Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
235                 Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
236                 Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
237         }
238
239         [Test]  
240         public void TestToString()
241         {
242                 //test ToString()
243                 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
244                 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
245                 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
246                 //test ToString(string format)
247                 /*
248                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
249                         of the system to decide the correct expected result.
250                 for (int i=0; i < Formats1.Length; i++) {
251                         Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
252                         Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
253                 }
254                 */
255                 //test ToString(string format, IFormatProvider provider);
256                 for (int i=0; i < Formats1.Length; i++) {
257                         Assert.IsTrue(String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0, "i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MyInt16_2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MyInt16_2.ToString(Formats1[i], Nfi));
258                         Assert.IsTrue(String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0, "i="+i+", ResultsNfi2[i]="+ResultsNfi2[i]+", MyInt16_3.ToString(Formats2[i]="+Formats2[i]+"): Expected "+ResultsNfi2[i]+" but got "+MyInt16_3.ToString(Formats2[i], Nfi));
259                 }
260                 try {
261                         MyInt16_1.ToString("z");
262                         Assert.Fail ("Should raise a System.FormatException");
263                 }
264                 catch (Exception e) {
265                         Assert.IsTrue(typeof(FormatException) == e.GetType());
266                 }
267         }
268
269         [Test]
270         public void ToString_Defaults () 
271         {
272                 Int16 i = 254;
273                 // everything defaults to "G"
274                 string def = i.ToString ("G");
275                 Assert.AreEqual (def, i.ToString (), "ToString()");
276                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
277                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
278                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
279                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
280                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
281
282                 Assert.AreEqual ("254", def, "ToString(G)");
283         }
284
285         [Test]
286         public void Bug3677 ()
287         {
288                 Assert.AreEqual (-7197, short.Parse("E3E3", NumberStyles.HexNumber), "HexNumber");
289         }
290 }
291
292 }