1 // Int16Test.cs - NUnit Test Cases for the System.Int16 struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Threading;
11 using System.Globalization;
13 namespace MonoTests.System
16 public class Int16Test : TestCase
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;
36 public Int16Test() : base ("MonoTests.System.Int16Test testcase") {}
\r
37 public Int16Test(string name) : base(name) {}
39 private CultureInfo old_culture;
41 protected override void SetUp()
43 old_culture = Thread.CurrentThread.CurrentCulture;
45 // Set culture to en-US and don't let the user override.
46 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
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";
53 protected override void TearDown()
55 Thread.CurrentThread.CurrentCulture = old_culture;
58 public static ITest Suite {
60 return new TestSuite(typeof(Int16Test));
64 public void TestMinMax()
67 AssertEquals(Int16.MinValue, MyInt16_2);
68 AssertEquals(Int16.MaxValue, MyInt16_3);
71 public void TestCompareTo()
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);
78 MyInt16_2.CompareTo(100);
79 Fail("Should raise a System.ArgumentException");
82 Assert(typeof(ArgumentException) == e.GetType());
86 public void TestEquals()
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);
94 public void TestGetHashCode()
97 MyInt16_1.GetHashCode();
98 MyInt16_2.GetHashCode();
99 MyInt16_3.GetHashCode();
102 Fail("GetHashCode should not raise an exception here");
106 public void TestParse()
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));
114 Fail("Should raise a System.ArgumentNullException");
116 catch (Exception e) {
117 Assert(typeof(ArgumentNullException) == e.GetType());
120 Int16.Parse("not-a-number");
121 Fail("Should raise a System.FormatException");
123 catch (Exception e) {
124 Assert(typeof(FormatException) == e.GetType());
127 int OverInt = Int16.MaxValue + 1;
128 Int16.Parse(OverInt.ToString());
129 Fail("Should raise a System.OverflowException");
131 catch (Exception e) {
132 Assert(typeof(OverflowException) == e.GetType());
134 //test Parse(string s, NumberStyles style)
135 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
137 Int16.Parse("$42", NumberStyles.Integer);
138 Fail("Should raise a System.FormatException");
140 catch (Exception e) {
141 Assert(typeof(FormatException) == e.GetType());
143 //test Parse(string s, IFormatProvider provider)
144 Assert(-42 == Int16.Parse(" -42 ", Nfi));
146 Int16.Parse("%42", Nfi);
147 Fail("Should raise a System.FormatException");
149 catch (Exception e) {
150 Assert(typeof(FormatException) == e.GetType());
152 //test Parse(string s, NumberStyles style, IFormatProvider provider)
153 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
155 Int16.Parse("$42", NumberStyles.Integer, Nfi);
156 Fail("Should raise a System.FormatException");
158 catch (Exception e) {
159 Assert(typeof(FormatException) == e.GetType());
163 public void TestToString()
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)
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);
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);
184 MyInt16_1.ToString("z");
185 Fail("Should raise a System.FormatException");
187 catch (Exception e) {
188 Assert(typeof(FormatException) == e.GetType());