2003-11-14 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System / UInt16Test.cs
1 // UInt16Test.cs - NUnit Test Cases for the System.UInt16 struct
2 //
3 // Mario Martinez (mariom925@home.om)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.Threading;
11 using System.Globalization;
12
13 namespace MonoTests.System
14 {
15
16 public class UInt16Test : TestCase
17 {
18         private const UInt16 MyUInt16_1 = 42;
19         private const UInt16 MyUInt16_2 = 0;
20         private const UInt16 MyUInt16_3 = 65535;
21         private const string MyString1 = "42";
22         private const string MyString2 = "0";
23         private const string MyString3 = "65535";
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,
27                                      "0", "0.000000e+000", "0.00",
28                                      "0", "0.00", "0.00 %", "0"};
29         private string[] Results2 = {null,
30                                      "65535", "6.55350e+004", "65535.00000",
31                                      "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
32         private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
33                                         "0", "0.000000e+000", "0.00",
34                                         "0", "0.00", "0.00 %", "0"};
35         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"65,535.00000",
36                                         "65535", "6.55350e+004", "65535.00000",
37                                         "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
38
39         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
40         
41         public UInt16Test() {}
42
43         private CultureInfo old_culture;
44
45         protected override void SetUp() 
46         {
47                 old_culture = Thread.CurrentThread.CurrentCulture;
48
49                 // Set culture to en-US and don't let the user override.
50                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
51
52                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"0.00";
53                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"65,535.00000";
54         }
55
56         protected override void TearDown()
57         {
58                 Thread.CurrentThread.CurrentCulture = old_culture;
59         }
60
61         public void TestMinMax()
62         {
63                 
64                 AssertEquals(UInt16.MinValue, MyUInt16_2);
65                 AssertEquals(UInt16.MaxValue, MyUInt16_3);
66         }
67         
68         public void TestCompareTo()
69         {
70                 Assert(MyUInt16_3.CompareTo(MyUInt16_2) > 0);
71                 Assert(MyUInt16_2.CompareTo(MyUInt16_2) == 0);
72                 Assert(MyUInt16_1.CompareTo((UInt16)(42)) == 0);
73                 Assert(MyUInt16_2.CompareTo(MyUInt16_3) < 0);
74                 try {
75                         MyUInt16_2.CompareTo(100);
76                         Fail("Should raise a System.ArgumentException");
77                 }
78                 catch (Exception e) {
79                         Assert(typeof(ArgumentException) == e.GetType());
80                 }
81         }
82
83         public void TestEquals()
84         {
85                 Assert(MyUInt16_1.Equals(MyUInt16_1));
86                 Assert(MyUInt16_1.Equals((UInt16)(42)));
87                 Assert(MyUInt16_1.Equals((SByte)(42)) == false);
88                 Assert(MyUInt16_1.Equals(MyUInt16_2) == false);
89         }
90         
91         public void TestGetHashCode()
92         {
93                 try {
94                         MyUInt16_1.GetHashCode();
95                         MyUInt16_2.GetHashCode();
96                         MyUInt16_3.GetHashCode();
97                 }
98                 catch {
99                         Fail("GetHashCode should not raise an exception here");
100                 }
101         }
102         
103         public void TestParse()
104         {
105                 //test Parse(string s)
106                 Assert(MyUInt16_1 == UInt16.Parse(MyString1));
107                 Assert(MyUInt16_2 == UInt16.Parse(MyString2));
108                 Assert(MyUInt16_3 == UInt16.Parse(MyString3));
109                 try {
110                         UInt16.Parse(null);
111                         Fail("Should raise a System.ArgumentNullException");
112                 }
113                 catch (Exception e) {
114                         Assert(typeof(ArgumentNullException) == e.GetType());
115                 }
116                 try {
117                         UInt16.Parse("not-a-number");
118                         Fail("Should raise a System.FormatException");
119                 }
120                 catch (Exception e) {
121                         Assert(typeof(FormatException) == e.GetType());
122                 }
123                 try {
124                         int OverInt = UInt16.MaxValue + 1;
125                         UInt16.Parse(OverInt.ToString());
126                         Fail("Should raise a System.OverflowException");
127                 }
128                 catch (Exception e) {
129                         Assert(typeof(OverflowException) == e.GetType());
130                 }
131                 //test Parse(string s, NumberStyles style)
132                 Assert(42 == UInt16.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
133                 try {
134                         UInt16.Parse("$42", NumberStyles.Integer);
135                         Fail("Should raise a System.FormatException");
136                 }
137                 catch (Exception e) {
138                         Assert(typeof(FormatException) == e.GetType());
139                 }
140                 //test Parse(string s, IFormatProvider provider)
141                 Assert(42 == UInt16.Parse(" 42 ", Nfi));
142                 try {
143                         UInt16.Parse("%42", Nfi);
144                         Fail("Should raise a System.FormatException");
145                 }
146                 catch (Exception e) {
147                         Assert(typeof(FormatException) == e.GetType());
148                 }
149                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
150                 Assert(16 == UInt16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
151                 try {
152                         UInt16.Parse("$42", NumberStyles.Integer, Nfi);
153                         Fail("Should raise a System.FormatException");
154                 }
155                 catch (Exception e) {
156                         Assert(typeof(FormatException) == e.GetType());
157                 }
158         }
159         
160         public void TestToString()
161         {
162                 //test ToString()
163                 AssertEquals("A1", MyString1, MyUInt16_1.ToString());
164                 AssertEquals("A2", MyString2, MyUInt16_2.ToString());
165                 AssertEquals("A3", MyString3, MyUInt16_3.ToString());
166                 //test ToString(string format)
167                 for (int i=0; i < Formats1.Length; i++) {
168                         AssertEquals("A4:"+i.ToString(), Results1[i], MyUInt16_2.ToString(Formats1[i]));
169                         AssertEquals("A5:"+i.ToString(), Results2[i], MyUInt16_3.ToString(Formats2[i]));
170                 }
171                 //test ToString(string format, IFormatProvider provider);
172                 for (int i=0; i < Formats1.Length; i++) {
173                         AssertEquals("A6:"+i.ToString(), ResultsNfi1[i], MyUInt16_2.ToString(Formats1[i], Nfi));
174                         AssertEquals("A7:"+i.ToString(), ResultsNfi2[i], MyUInt16_3.ToString(Formats2[i], Nfi));
175                 }
176                 try {
177                         MyUInt16_1.ToString("z");
178                         Fail("Should raise a System.FormatException");
179                 }
180                 catch (Exception e) {
181                         Assert("A8", typeof(FormatException) == e.GetType());
182                 }
183         }
184 }
185
186
187 }