2002-12-21 Nick Drochak <ndrochak@gol.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 public class Int16Test : TestCase
17 {
18         private const Int16 MyInt16_1 = -42;
19         private const Int16 MyInt16_2 = -32768;
20         private const Int16 MyInt16_3 = 32767;
21         private const string MyString1 = "-42";
22         private const string MyString2 = "-32768";
23         private const string MyString3 = "32767";
24         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
25         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
26         private string[] Results1 = {null, "-32768", "-3.276800e+004", "-32768.00",
27                                           "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
28         private string[] Results2 = {null, "32767", "3.27670e+004", "32767.00000",
29                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
30         private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
31                                           "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
32         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"32,767.00000", "32767", "3.27670e+004", "32767.00000",
33                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
34         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
35         
36         public Int16Test() {}
37
38         private CultureInfo old_culture;
39
40         protected override 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         protected override void TearDown()
53         {
54                 Thread.CurrentThread.CurrentCulture = old_culture;
55         }
56
57         public void TestMinMax()
58         {
59                 
60                 AssertEquals(Int16.MinValue, MyInt16_2);
61                 AssertEquals(Int16.MaxValue, MyInt16_3);
62         }
63         
64         public void TestCompareTo()
65         {
66                 Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
67                 Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
68                 Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
69                 Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
70                 try {
71                         MyInt16_2.CompareTo(100);
72                         Fail("Should raise a System.ArgumentException");
73                 }
74                 catch (Exception e) {
75                         Assert(typeof(ArgumentException) == e.GetType());
76                 }
77         }
78
79         public void TestEquals()
80         {
81                 Assert(MyInt16_1.Equals(MyInt16_1));
82                 Assert(MyInt16_1.Equals((Int16)(-42)));
83                 Assert(MyInt16_1.Equals((SByte)(-42)) == false);
84                 Assert(MyInt16_1.Equals(MyInt16_2) == false);
85         }
86         
87         public void TestGetHashCode()
88         {
89                 try {
90                         MyInt16_1.GetHashCode();
91                         MyInt16_2.GetHashCode();
92                         MyInt16_3.GetHashCode();
93                 }
94                 catch {
95                         Fail("GetHashCode should not raise an exception here");
96                 }
97         }
98         
99         public void TestParse()
100         {
101                 //test Parse(string s)
102                 Assert(MyInt16_1 == Int16.Parse(MyString1));
103                 Assert(MyInt16_2 == Int16.Parse(MyString2));
104                 Assert(MyInt16_3 == Int16.Parse(MyString3));
105                 try {
106                         Int16.Parse(null);
107                         Fail("Should raise a System.ArgumentNullException");
108                 }
109                 catch (Exception e) {
110                         Assert(typeof(ArgumentNullException) == e.GetType());
111                 }
112                 try {
113                         Int16.Parse("not-a-number");
114                         Fail("Should raise a System.FormatException");
115                 }
116                 catch (Exception e) {
117                         Assert(typeof(FormatException) == e.GetType());
118                 }
119                 try {
120                         int OverInt = Int16.MaxValue + 1;
121                         Int16.Parse(OverInt.ToString());
122                         Fail("Should raise a System.OverflowException");
123                 }
124                 catch (Exception e) {
125                         Assert(typeof(OverflowException) == e.GetType());
126                 }
127                 //test Parse(string s, NumberStyles style)
128                 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
129                 try {
130                         Int16.Parse("$42", NumberStyles.Integer);
131                         Fail("Should raise a System.FormatException");
132                 }
133                 catch (Exception e) {
134                         Assert(typeof(FormatException) == e.GetType());
135                 }
136                 //test Parse(string s, IFormatProvider provider)
137                 Assert(-42 == Int16.Parse(" -42 ", Nfi));
138                 try {
139                         Int16.Parse("%42", Nfi);
140                         Fail("Should raise a System.FormatException");
141                 }
142                 catch (Exception e) {
143                         Assert(typeof(FormatException) == e.GetType());
144                 }
145                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
146                 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
147                 try {
148                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
149                         Fail("Should raise a System.FormatException");
150                 }
151                 catch (Exception e) {
152                         Assert(typeof(FormatException) == e.GetType());
153                 }
154         }
155         
156         public void TestToString()
157         {
158                 //test ToString()
159                 Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
160                 Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
161                 Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
162                 //test ToString(string format)
163                 /*
164                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
165                         of the system to decide the correct expected result.
166                 for (int i=0; i < Formats1.Length; i++) {
167                         Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
168                         Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
169                 }
170                 */
171                 //test ToString(string format, IFormatProvider provider);
172                 for (int i=0; i < Formats1.Length; i++) {
173                         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);
174                         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);
175                 }
176                 try {
177                         MyInt16_1.ToString("z");
178                         Fail("Should raise a System.FormatException");
179                 }
180                 catch (Exception e) {
181                         Assert(typeof(FormatException) == e.GetType());
182                 }
183         }
184 }
185
186 }