2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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 : Assertion
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 SetUp () 
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         [TestFixtureTearDown]
53         public void TearDown ()
54         {
55                 Thread.CurrentThread.CurrentCulture = old_culture;
56         }
57
58         public void TestMinMax()
59         {
60                 
61                 AssertEquals(Int16.MinValue, MyInt16_2);
62                 AssertEquals(Int16.MaxValue, MyInt16_3);
63         }
64         
65         public void TestCompareTo()
66         {
67                 Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
68                 Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
69                 Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
70                 Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
71                 try {
72                         MyInt16_2.CompareTo((object)100);
73                         Fail("Should raise a System.ArgumentException");
74                 }
75                 catch (Exception e) {
76                         Assert(typeof(ArgumentException) == e.GetType());
77                 }
78         }
79
80         public void TestEquals()
81         {
82                 Assert(MyInt16_1.Equals(MyInt16_1));
83                 Assert(MyInt16_1.Equals((object)(Int16)(-42)));
84                 Assert(MyInt16_1.Equals((object)(SByte)(-42)) == false);
85                 Assert(MyInt16_1.Equals(MyInt16_2) == false);
86         }
87         
88         public void TestGetHashCode()
89         {
90                 try {
91                         MyInt16_1.GetHashCode();
92                         MyInt16_2.GetHashCode();
93                         MyInt16_3.GetHashCode();
94                 }
95                 catch {
96                         Fail("GetHashCode should not raise an exception here");
97                 }
98         }
99         
100         public void TestParse()
101         {
102                 //test Parse(string s)
103                 Assert(MyInt16_1 == Int16.Parse(MyString1));
104                 Assert(MyInt16_2 == Int16.Parse(MyString2));
105                 Assert(MyInt16_3 == Int16.Parse(MyString3));
106                 try {
107                         Int16.Parse(null);
108                         Fail("Should raise a System.ArgumentNullException");
109                 }
110                 catch (Exception e) {
111                         Assert(typeof(ArgumentNullException) == e.GetType());
112                 }
113                 try {
114                         Int16.Parse("not-a-number");
115                         Fail("Should raise a System.FormatException");
116                 }
117                 catch (Exception e) {
118                         Assert(typeof(FormatException) == e.GetType());
119                 }
120                 try {
121                         int OverInt = Int16.MaxValue + 1;
122                         Int16.Parse(OverInt.ToString());
123                         Fail("Should raise a System.OverflowException");
124                 }
125                 catch (Exception e) {
126                         Assert(typeof(OverflowException) == e.GetType());
127                 }
128                 //test Parse(string s, NumberStyles style)
129                 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
130                 try {
131                         Int16.Parse("$42", NumberStyles.Integer);
132                         Fail("Should raise a System.FormatException");
133                 }
134                 catch (Exception e) {
135                         Assert(typeof(FormatException) == e.GetType());
136                 }
137                 //test Parse(string s, IFormatProvider provider)
138                 Assert(-42 == Int16.Parse(" -42 ", Nfi));
139                 try {
140                         Int16.Parse("%42", Nfi);
141                         Fail("Should raise a System.FormatException");
142                 }
143                 catch (Exception e) {
144                         Assert(typeof(FormatException) == e.GetType());
145                 }
146                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
147                 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
148                 try {
149                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
150                         Fail("Should raise a System.FormatException");
151                 }
152                 catch (Exception e) {
153                         Assert(typeof(FormatException) == e.GetType());
154                 }
155         }
156         
157         public void TestToString()
158         {
159                 //test ToString()
160                 Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
161                 Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
162                 Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
163                 //test ToString(string format)
164                 /*
165                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
166                         of the system to decide the correct expected result.
167                 for (int i=0; i < Formats1.Length; i++) {
168                         Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
169                         Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
170                 }
171                 */
172                 //test ToString(string format, IFormatProvider provider);
173                 for (int i=0; i < Formats1.Length; i++) {
174                         Assert("i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MyInt16_2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MyInt16_2.ToString(Formats1[i], Nfi), String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0);
175                         Assert("i="+i+", ResultsNfi2[i]="+ResultsNfi2[i]+", MyInt16_3.ToString(Formats2[i]="+Formats2[i]+"): Expected "+ResultsNfi2[i]+" but got "+MyInt16_3.ToString(Formats2[i], Nfi), String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0);
176                 }
177                 try {
178                         MyInt16_1.ToString("z");
179                         Fail("Should raise a System.FormatException");
180                 }
181                 catch (Exception e) {
182                         Assert(typeof(FormatException) == e.GetType());
183                 }
184         }
185
186         [Test]
187         public void ToString_Defaults () 
188         {
189                 Int16 i = 254;
190                 // everything defaults to "G"
191                 string def = i.ToString ("G");
192                 AssertEquals ("ToString()", def, i.ToString ());
193                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
194                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
195                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
196                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
197                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
198
199                 AssertEquals ("ToString(G)", "254", def);
200         }
201 }
202
203 }