Add [Category ("NotWorking")] to failing test.
[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.IsTrue(MyInt16_3.CompareTo(MyInt16_2) > 0);
75                 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_2) == 0);
76                 Assert.IsTrue(MyInt16_1.CompareTo((Int16)(-42)) == 0);
77                 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_3) < 0);
78                 try {
79                         MyInt16_2.CompareTo((object)100);
80                         Assert.Fail ("Should raise a System.ArgumentException");
81                 }
82                 catch (Exception e) {
83                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
84                 }
85         }
86
87         [Test]
88         public void TestEquals()
89         {
90                 Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
91                 Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
92                 Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
93                 Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
94         }
95
96         [Test]  
97         public void TestGetHashCode()
98         {
99                 try {
100                         MyInt16_1.GetHashCode();
101                         MyInt16_2.GetHashCode();
102                         MyInt16_3.GetHashCode();
103                 }
104                 catch {
105                         Assert.Fail ("GetHashCode should not raise an exception here");
106                 }
107         }
108
109         [Test]  
110         public void TestParse()
111         {
112                 //test Parse(string s)
113                 Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
114                 Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
115                 Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
116                 try {
117                         Int16.Parse(null);
118                         Assert.Fail ("Should raise a System.ArgumentNullException");
119                 }
120                 catch (Exception e) {
121                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
122                 }
123                 try {
124                         Int16.Parse("not-a-number");
125                         Assert.Fail ("Should raise a System.FormatException");
126                 }
127                 catch (Exception e) {
128                         Assert.IsTrue(typeof(FormatException) == e.GetType());
129                 }
130                 try {
131                         int OverInt = Int16.MaxValue + 1;
132                         Int16.Parse(OverInt.ToString());
133                         Assert.Fail ("Should raise a System.OverflowException");
134                 }
135                 catch (Exception e) {
136                         Assert.IsTrue(typeof(OverflowException) == e.GetType());
137                 }
138                 //test Parse(string s, NumberStyles style)
139                 Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
140                 try {
141                         Int16.Parse("$42", NumberStyles.Integer);
142                         Assert.Fail ("Should raise a System.FormatException");
143                 }
144                 catch (Exception e) {
145                         Assert.IsTrue(typeof(FormatException) == e.GetType());
146                 }
147                 //test Parse(string s, IFormatProvider provider)
148                 Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
149                 try {
150                         Int16.Parse("%42", Nfi);
151                         Assert.Fail ("Should raise a System.FormatException");
152                 }
153                 catch (Exception e) {
154                         Assert.IsTrue(typeof(FormatException) == e.GetType());
155                 }
156                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
157                 Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
158                 try {
159                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
160                         Assert.Fail ("Should raise a System.FormatException");
161                 }
162                 catch (Exception e) {
163                         Assert.IsTrue(typeof(FormatException) == e.GetType());
164                 }
165
166                 Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1");
167                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    \0"), "#2");
168                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    "), "#3");
169                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
170
171                 Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
172         }
173
174         [Test]
175         public void TestParseExponent ()
176         {
177                 Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
178                 Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
179                 Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
180                 Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
181                 Assert.AreEqual (2, Int16.Parse ("2", NumberStyles.AllowExponent), "A#5");
182
183                 try {
184                         Int16.Parse ("2E");
185                         Assert.Fail ("B#1");
186                 } catch (FormatException) {
187                 }
188
189                 try {
190                         Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
191                         Assert.Fail ("B#2");
192                 } catch (FormatException) {
193                 }
194
195                 try {
196                         Int16.Parse ("2E 2", NumberStyles.AllowExponent);
197                         Assert.Fail ("B#3");
198                 } catch (FormatException) {
199                 }
200
201                 try {
202                         Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
203                         Assert.Fail ("B#4");
204                 } catch (FormatException) {
205                 }
206
207                 try {
208                         Int16.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                         Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
216                         Assert.Fail ("B#6");
217                 } catch (OverflowException) {
218                 }
219
220                 try {
221                         Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
222                         Assert.Fail ("B#7");
223                 } catch (OverflowException) {
224                 }
225                 
226                 try {
227                         Int16.Parse ("2 math e1", NumberStyles.AllowExponent);
228                         Assert.Fail ("B#8");
229                 } catch (FormatException) {
230                 }
231         }
232
233         [Test]
234         public void Parse_MinMax ()
235         {
236                 Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
237                 Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
238                 Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
239         }
240
241         [Test]  
242         public void TestToString()
243         {
244                 //test ToString()
245                 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
246                 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
247                 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
248                 //test ToString(string format)
249                 /*
250                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
251                         of the system to decide the correct expected result.
252                 for (int i=0; i < Formats1.Length; i++) {
253                         Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
254                         Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
255                 }
256                 */
257                 //test ToString(string format, IFormatProvider provider);
258                 for (int i=0; i < Formats1.Length; i++) {
259                         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));
260                         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));
261                 }
262                 try {
263                         MyInt16_1.ToString("z");
264                         Assert.Fail ("Should raise a System.FormatException");
265                 }
266                 catch (Exception e) {
267                         Assert.IsTrue(typeof(FormatException) == e.GetType());
268                 }
269         }
270
271         [Test]
272         public void ToString_Defaults () 
273         {
274                 Int16 i = 254;
275                 // everything defaults to "G"
276                 string def = i.ToString ("G");
277                 Assert.AreEqual (def, i.ToString (), "ToString()");
278                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
279                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
280                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
281                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
282                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
283
284                 Assert.AreEqual ("254", def, "ToString(G)");
285         }
286
287         [Test]
288         public void Bug3677 ()
289         {
290                 Assert.AreEqual (-7197, short.Parse("E3E3", NumberStyles.HexNumber), "HexNumber");
291         }
292 }
293
294 }