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