Merge pull request #1896 from meum/patch-1
[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                 try {
166                         Int16.Parse ("7345\0\0\0    \0");
167                         Assert.Fail ("#2");
168                 } catch (FormatException) {}
169
170                 try {           
171                         Int16.Parse ("7345\0\0\0    ");
172                         Assert.Fail ("#3");
173                 } catch (FormatException) {}
174
175                 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
176
177                 Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
178         }
179
180         [Test]
181         public void TestParseExponent ()
182         {
183                 Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
184                 Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
185                 Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
186                 Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
187                 Assert.AreEqual (2, Int16.Parse ("2", NumberStyles.AllowExponent), "A#5");
188
189                 try {
190                         Int16.Parse ("2E");
191                         Assert.Fail ("B#1");
192                 } catch (FormatException) {
193                 }
194
195                 try {
196                         Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
197                         Assert.Fail ("B#2");
198                 } catch (FormatException) {
199                 }
200
201                 try {
202                         Int16.Parse ("2E 2", NumberStyles.AllowExponent);
203                         Assert.Fail ("B#3");
204                 } catch (FormatException) {
205                 }
206
207                 try {
208                         Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
209                         Assert.Fail ("B#4");
210                 } catch (FormatException) {
211                 }
212
213                 try {
214                         Int16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
215                         Assert.Fail ("B#5");
216                 } catch (OverflowException) {
217                 }
218
219                 try {
220                         long exponent = (long) Int32.MaxValue + 10;
221                         Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
222                         Assert.Fail ("B#6");
223                 } catch (OverflowException) {
224                 }
225
226                 try {
227                         Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
228                         Assert.Fail ("B#7");
229                 } catch (OverflowException) {
230                 }
231                 
232                 try {
233                         Int16.Parse ("2 math e1", NumberStyles.AllowExponent);
234                         Assert.Fail ("B#8");
235                 } catch (FormatException) {
236                 }
237         }
238
239         [Test]
240         public void Parse_MinMax ()
241         {
242                 Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
243                 Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
244                 Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
245         }
246
247         [Test]  
248         public void TestToString()
249         {
250                 //test ToString()
251                 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
252                 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
253                 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
254                 //test ToString(string format)
255                 /*
256                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
257                         of the system to decide the correct expected result.
258                 for (int i=0; i < Formats1.Length; i++) {
259                         Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
260                         Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
261                 }
262                 */
263                 //test ToString(string format, IFormatProvider provider);
264                 for (int i=0; i < Formats1.Length; i++) {
265                         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));
266                         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));
267                 }
268                 try {
269                         MyInt16_1.ToString("z");
270                         Assert.Fail ("Should raise a System.FormatException");
271                 }
272                 catch (Exception e) {
273                         Assert.IsTrue(typeof(FormatException) == e.GetType());
274                 }
275         }
276
277         [Test]
278         public void ToString_Defaults () 
279         {
280                 Int16 i = 254;
281                 // everything defaults to "G"
282                 string def = i.ToString ("G");
283                 Assert.AreEqual (def, i.ToString (), "ToString()");
284                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
285                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
286                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
287                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
288                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
289
290                 Assert.AreEqual ("254", def, "ToString(G)");
291         }
292
293         [Test]
294         public void Bug3677 ()
295         {
296                 Assert.AreEqual (-7197, short.Parse("E3E3", NumberStyles.HexNumber), "HexNumber");
297         }
298 }
299
300 }