2009-06-30 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / class / corlib / Test / System / Int16Test.cs
blob676a7cd05b3d53b8e53638d969a3b0d9070c4a2d
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 //
8 using NUnit.Framework;
9 using System;
10 using System.Threading;
11 using System.Globalization;
13 namespace MonoTests.System
16 [TestFixture]
17 public class Int16Test
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;
37 private CultureInfo old_culture;
39 [TestFixtureSetUp]
40 public void SetUp ()
42 old_culture = Thread.CurrentThread.CurrentCulture;
44 // Set culture to en-US and don't let the user override.
45 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
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";
52 [TestFixtureTearDown]
53 public void TearDown ()
55 Thread.CurrentThread.CurrentCulture = old_culture;
58 [Test]
59 public void TestMinMax()
61 Assert.AreEqual(Int16.MinValue, MyInt16_2);
62 Assert.AreEqual(Int16.MaxValue, MyInt16_3);
65 [Test]
66 public void TestCompareTo()
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");
76 catch (Exception e) {
77 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
81 [Test]
82 public void TestEquals()
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);
90 [Test]
91 public void TestGetHashCode()
93 try {
94 MyInt16_1.GetHashCode();
95 MyInt16_2.GetHashCode();
96 MyInt16_3.GetHashCode();
98 catch {
99 Assert.Fail ("GetHashCode should not raise an exception here");
103 [Test]
104 public void TestParse()
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");
114 catch (Exception e) {
115 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
117 try {
118 Int16.Parse("not-a-number");
119 Assert.Fail ("Should raise a System.FormatException");
121 catch (Exception e) {
122 Assert.IsTrue(typeof(FormatException) == e.GetType());
124 try {
125 int OverInt = Int16.MaxValue + 1;
126 Int16.Parse(OverInt.ToString());
127 Assert.Fail ("Should raise a System.OverflowException");
129 catch (Exception e) {
130 Assert.IsTrue(typeof(OverflowException) == e.GetType());
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");
138 catch (Exception e) {
139 Assert.IsTrue(typeof(FormatException) == e.GetType());
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");
147 catch (Exception e) {
148 Assert.IsTrue(typeof(FormatException) == e.GetType());
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");
156 catch (Exception e) {
157 Assert.IsTrue(typeof(FormatException) == e.GetType());
161 [Test]
162 public void TestToString()
164 //test ToString()
165 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
166 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
167 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
168 //test ToString(string format)
170 TODO: These tests are culture sensitive. Need to find a way to determine the culture
171 of the system to decide the correct expected result.
172 for (int i=0; i < Formats1.Length; i++) {
173 Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
174 Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
177 //test ToString(string format, IFormatProvider provider);
178 for (int i=0; i < Formats1.Length; i++) {
179 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));
180 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));
182 try {
183 MyInt16_1.ToString("z");
184 Assert.Fail ("Should raise a System.FormatException");
186 catch (Exception e) {
187 Assert.IsTrue(typeof(FormatException) == e.GetType());
191 [Test]
192 public void ToString_Defaults ()
194 Int16 i = 254;
195 // everything defaults to "G"
196 string def = i.ToString ("G");
197 Assert.AreEqual (def, i.ToString (), "ToString()");
198 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
199 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
200 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
201 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
202 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
204 Assert.AreEqual ("254", def, "ToString(G)");