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