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.Globalization;
12 namespace MonoTests.System
15 public class Int16Test : TestCase
17 private const Int16 MyInt16_1 = -42;
18 private const Int16 MyInt16_2 = -32768;
19 private const Int16 MyInt16_3 = 32767;
20 private const string MyString1 = "-42";
21 private const string MyString2 = "-32768";
22 private const string MyString3 = "32767";
23 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
24 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
25 private string[] Results1 = {"($32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
26 "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
27 private string[] Results2 = {"$32,767.00000", "32767", "3.27670e+004", "32767.00000",
28 "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
29 private string[] ResultsNfi1 = {"($32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
30 "-32768", "(32,768.00)", "-3,276,800.00 %", "8000"};
31 private string[] ResultsNfi2 = {"$32,767.00000", "32767", "3.27670e+004", "32767.00000",
32 "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
33 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
35 public Int16Test(string name) : base(name) {}
37 protected override void SetUp()
41 public static ITest Suite {
43 return new TestSuite(typeof(Int16Test));
47 public void TestMinMax()
50 AssertEquals(Int16.MinValue, MyInt16_2);
51 AssertEquals(Int16.MaxValue, MyInt16_3);
54 public void TestCompareTo()
56 Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
57 Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
58 Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
59 Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
61 MyInt16_2.CompareTo(100);
62 Fail("Should raise a System.ArgumentException");
65 Assert(typeof(ArgumentException) == e.GetType());
69 public void TestEquals()
71 Assert(MyInt16_1.Equals(MyInt16_1));
72 Assert(MyInt16_1.Equals((Int16)(-42)));
73 Assert(MyInt16_1.Equals((SByte)(-42)) == false);
74 Assert(MyInt16_1.Equals(MyInt16_2) == false);
77 public void TestGetHashCode()
80 MyInt16_1.GetHashCode();
81 MyInt16_2.GetHashCode();
82 MyInt16_3.GetHashCode();
85 Fail("GetHashCode should not raise an exception here");
89 public void TestParse()
91 //test Parse(string s)
92 Assert(MyInt16_1 == Int16.Parse(MyString1));
93 Assert(MyInt16_2 == Int16.Parse(MyString2));
94 Assert(MyInt16_3 == Int16.Parse(MyString3));
97 Fail("Should raise a System.ArgumentNullException");
100 Assert(typeof(ArgumentNullException) == e.GetType());
103 Int16.Parse("not-a-number");
104 Fail("Should raise a System.FormatException");
106 catch (Exception e) {
107 Assert(typeof(FormatException) == e.GetType());
110 int OverInt = Int16.MaxValue + 1;
111 Int16.Parse(OverInt.ToString());
112 Fail("Should raise a System.OverflowException");
114 catch (Exception e) {
115 Assert(typeof(OverflowException) == e.GetType());
117 //test Parse(string s, NumberStyles style)
118 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
120 Int16.Parse("$42", NumberStyles.Integer);
121 Fail("Should raise a System.FormatException");
123 catch (Exception e) {
124 Assert(typeof(FormatException) == e.GetType());
126 //test Parse(string s, IFormatProvider provider)
127 Assert(-42 == Int16.Parse(" -42 ", Nfi));
129 Int16.Parse("%42", Nfi);
130 Fail("Should raise a System.FormatException");
132 catch (Exception e) {
133 Assert(typeof(FormatException) == e.GetType());
135 //test Parse(string s, NumberStyles style, IFormatProvider provider)
136 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
138 Int16.Parse("$42", NumberStyles.Integer, Nfi);
139 Fail("Should raise a System.FormatException");
141 catch (Exception e) {
142 Assert(typeof(FormatException) == e.GetType());
146 public void TestToString()
149 Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
150 Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
151 Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
152 //test ToString(string format)
153 for (int i=0; i < Formats1.Length; i++) {
154 Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
155 Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
157 //test ToString(string format, IFormatProvider provider);
158 for (int i=0; i < Formats1.Length; i++) {
159 Assert(String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0);
160 Assert(String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0);
163 MyInt16_1.ToString("z");
164 Fail("Should raise a System.FormatException");
166 catch (Exception e) {
167 Assert(typeof(FormatException) == e.GetType());