1 // UInt16Test.cs - NUnit Test Cases for the System.UInt16 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 UInt16Test : TestCase
17 private const UInt16 MyUInt16_1 = 42;
18 private const UInt16 MyUInt16_2 = 0;
19 private const UInt16 MyUInt16_3 = 65535;
20 private const string MyString1 = "42";
21 private const string MyString2 = "0";
22 private const string MyString3 = "65535";
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 = {NumberFormatInfo.CurrentInfo.CurrencySymbol+"0.00",
26 "0", "0.000000e+000", "0.00",
27 "0", "0.00", "0.00 %", "0"};
28 private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
29 "0", "0.000000e+000", "0.00",
30 "0", "0.00", "0.00 %", "0"};
31 private string[] Results2 = {NumberFormatInfo.CurrentInfo.CurrencySymbol+"65,535.00000",
32 "65535", "6.55350e+004", "65535.00000",
33 "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
34 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"65,535.00000",
35 "65535", "6.55350e+004", "65535.00000",
36 "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
38 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
40 public UInt16Test(string name) : base(name) {}
42 protected override void SetUp()
46 public static ITest Suite {
48 return new TestSuite(typeof(UInt16Test));
52 public void TestMinMax()
55 AssertEquals(UInt16.MinValue, MyUInt16_2);
56 AssertEquals(UInt16.MaxValue, MyUInt16_3);
59 public void TestCompareTo()
61 Assert(MyUInt16_3.CompareTo(MyUInt16_2) > 0);
62 Assert(MyUInt16_2.CompareTo(MyUInt16_2) == 0);
63 Assert(MyUInt16_1.CompareTo((UInt16)(42)) == 0);
64 Assert(MyUInt16_2.CompareTo(MyUInt16_3) < 0);
66 MyUInt16_2.CompareTo(100);
67 Fail("Should raise a System.ArgumentException");
70 Assert(typeof(ArgumentException) == e.GetType());
74 public void TestEquals()
76 Assert(MyUInt16_1.Equals(MyUInt16_1));
77 Assert(MyUInt16_1.Equals((UInt16)(42)));
78 Assert(MyUInt16_1.Equals((SByte)(42)) == false);
79 Assert(MyUInt16_1.Equals(MyUInt16_2) == false);
82 public void TestGetHashCode()
85 MyUInt16_1.GetHashCode();
86 MyUInt16_2.GetHashCode();
87 MyUInt16_3.GetHashCode();
90 Fail("GetHashCode should not raise an exception here");
94 public void TestParse()
96 //test Parse(string s)
97 Assert(MyUInt16_1 == UInt16.Parse(MyString1));
98 Assert(MyUInt16_2 == UInt16.Parse(MyString2));
99 Assert(MyUInt16_3 == UInt16.Parse(MyString3));
102 Fail("Should raise a System.ArgumentNullException");
104 catch (Exception e) {
105 Assert(typeof(ArgumentNullException) == e.GetType());
108 UInt16.Parse("not-a-number");
109 Fail("Should raise a System.FormatException");
111 catch (Exception e) {
112 Assert(typeof(FormatException) == e.GetType());
115 int OverInt = UInt16.MaxValue + 1;
116 UInt16.Parse(OverInt.ToString());
117 Fail("Should raise a System.OverflowException");
119 catch (Exception e) {
120 Assert(typeof(OverflowException) == e.GetType());
122 //test Parse(string s, NumberStyles style)
123 Assert(42 == UInt16.Parse(" $42 ", NumberStyles.Currency));
125 UInt16.Parse("$42", NumberStyles.Integer);
126 Fail("Should raise a System.FormatException");
128 catch (Exception e) {
129 Assert(typeof(FormatException) == e.GetType());
131 //test Parse(string s, IFormatProvider provider)
132 Assert(42 == UInt16.Parse(" 42 ", Nfi));
134 UInt16.Parse("%42", Nfi);
135 Fail("Should raise a System.FormatException");
137 catch (Exception e) {
138 Assert(typeof(FormatException) == e.GetType());
140 //test Parse(string s, NumberStyles style, IFormatProvider provider)
141 Assert(16 == UInt16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
143 UInt16.Parse("$42", NumberStyles.Integer, Nfi);
144 Fail("Should raise a System.FormatException");
146 catch (Exception e) {
147 Assert(typeof(FormatException) == e.GetType());
151 public void TestToString()
154 AssertEquals(MyString1, MyUInt16_1.ToString());
155 AssertEquals(MyString2, MyUInt16_2.ToString());
156 AssertEquals(MyString3, MyUInt16_3.ToString());
157 //test ToString(string format)
158 for (int i=0; i < Formats1.Length; i++) {
159 AssertEquals(Results1[i], MyUInt16_2.ToString(Formats1[i]));
160 AssertEquals(Results2[i], MyUInt16_3.ToString(Formats2[i]));
162 //test ToString(string format, IFormatProvider provider);
163 for (int i=0; i < Formats1.Length; i++) {
164 AssertEquals(ResultsNfi1[i], MyUInt16_2.ToString(Formats1[i], Nfi));
165 AssertEquals(ResultsNfi2[i], MyUInt16_3.ToString(Formats2[i], Nfi));
168 MyUInt16_1.ToString("z");
169 Fail("Should raise a System.FormatException");
171 catch (Exception e) {
172 Assert(typeof(FormatException) == e.GetType());