2002-03-09 Martin Baulig <martin@gnome.org>
[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() : base ("MonoTests.System.Int16Test testcase") {}\r
37         public Int16Test(string name) : base(name) {}
38
39         private CultureInfo old_culture;
40
41         protected override void SetUp() 
42         {
43                 old_culture = Thread.CurrentThread.CurrentCulture;
44
45                 // Set culture to en-US and don't let the user override.
46                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
47
48                 // We can't initialize this until we set the culture.
49                 Results1 [0] = "("+NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,768.00)";
50                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,767.00000";
51         }
52
53         protected override void TearDown()
54         {
55                 Thread.CurrentThread.CurrentCulture = old_culture;
56         }
57
58         public static ITest Suite {
59                 get { 
60                         return new TestSuite(typeof(Int16Test)); 
61                 }
62         }
63     
64         public void TestMinMax()
65         {
66                 
67                 AssertEquals(Int16.MinValue, MyInt16_2);
68                 AssertEquals(Int16.MaxValue, MyInt16_3);
69         }
70         
71         public void TestCompareTo()
72         {
73                 Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
74                 Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
75                 Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
76                 Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
77                 try {
78                         MyInt16_2.CompareTo(100);
79                         Fail("Should raise a System.ArgumentException");
80                 }
81                 catch (Exception e) {
82                         Assert(typeof(ArgumentException) == e.GetType());
83                 }
84         }
85
86         public void TestEquals()
87         {
88                 Assert(MyInt16_1.Equals(MyInt16_1));
89                 Assert(MyInt16_1.Equals((Int16)(-42)));
90                 Assert(MyInt16_1.Equals((SByte)(-42)) == false);
91                 Assert(MyInt16_1.Equals(MyInt16_2) == false);
92         }
93         
94         public void TestGetHashCode()
95         {
96                 try {
97                         MyInt16_1.GetHashCode();
98                         MyInt16_2.GetHashCode();
99                         MyInt16_3.GetHashCode();
100                 }
101                 catch {
102                         Fail("GetHashCode should not raise an exception here");
103                 }
104         }
105         
106         public void TestParse()
107         {
108                 //test Parse(string s)
109                 Assert(MyInt16_1 == Int16.Parse(MyString1));
110                 Assert(MyInt16_2 == Int16.Parse(MyString2));
111                 Assert(MyInt16_3 == Int16.Parse(MyString3));
112                 try {
113                         Int16.Parse(null);
114                         Fail("Should raise a System.ArgumentNullException");
115                 }
116                 catch (Exception e) {
117                         Assert(typeof(ArgumentNullException) == e.GetType());
118                 }
119                 try {
120                         Int16.Parse("not-a-number");
121                         Fail("Should raise a System.FormatException");
122                 }
123                 catch (Exception e) {
124                         Assert(typeof(FormatException) == e.GetType());
125                 }
126                 try {
127                         int OverInt = Int16.MaxValue + 1;
128                         Int16.Parse(OverInt.ToString());
129                         Fail("Should raise a System.OverflowException");
130                 }
131                 catch (Exception e) {
132                         Assert(typeof(OverflowException) == e.GetType());
133                 }
134                 //test Parse(string s, NumberStyles style)
135                 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
136                 try {
137                         Int16.Parse("$42", NumberStyles.Integer);
138                         Fail("Should raise a System.FormatException");
139                 }
140                 catch (Exception e) {
141                         Assert(typeof(FormatException) == e.GetType());
142                 }
143                 //test Parse(string s, IFormatProvider provider)
144                 Assert(-42 == Int16.Parse(" -42 ", Nfi));
145                 try {
146                         Int16.Parse("%42", Nfi);
147                         Fail("Should raise a System.FormatException");
148                 }
149                 catch (Exception e) {
150                         Assert(typeof(FormatException) == e.GetType());
151                 }
152                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
153                 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
154                 try {
155                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
156                         Fail("Should raise a System.FormatException");
157                 }
158                 catch (Exception e) {
159                         Assert(typeof(FormatException) == e.GetType());
160                 }
161         }
162         
163         public void TestToString()
164         {
165                 //test ToString()
166                 Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
167                 Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
168                 Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
169                 //test ToString(string format)
170                 /*
171                 TODO: These tests are culture sensitive.  Need to find a way to determine the culture
172                         of the system to decide the correct expected result.
173                 for (int i=0; i < Formats1.Length; i++) {
174                         Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
175                         Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
176                 }
177                 */
178                 //test ToString(string format, IFormatProvider provider);
179                 for (int i=0; i < Formats1.Length; i++) {
180                         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);
181                         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);
182                 }
183                 try {
184                         MyInt16_1.ToString("z");
185                         Fail("Should raise a System.FormatException");
186                 }
187                 catch (Exception e) {
188                         Assert(typeof(FormatException) == e.GetType());
189                 }
190         }
191 }
192
193 }