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.Threading;
11 using System.Globalization;
13 namespace MonoTests.System
17 public class UInt16Test
19 private const UInt16 MyUInt16_1 = 42;
20 private const UInt16 MyUInt16_2 = 0;
21 private const UInt16 MyUInt16_3 = 65535;
22 private const string MyString1 = "42";
23 private const string MyString2 = "0";
24 private const string MyString3 = "65535";
25 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
26 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
27 private string[] Results1 = {null,
28 "0", "0.000000e+000", "0.00",
29 "0", "0.00", "0.00 %", "0"};
30 private string[] Results2 = {null,
31 "65535", "6.55350e+004", "65535.00000",
32 "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
33 private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
34 "0", "0.000000e+000", "0.00",
35 "0", "0.00", "0.00 %", "0"};
36 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"65,535.00000",
37 "65535", "6.55350e+004", "65535.00000",
38 "65535", "65,535.00000", "6,553,500.00000 %", "0ffff"};
40 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
42 private CultureInfo old_culture;
47 old_culture = Thread.CurrentThread.CurrentCulture;
49 // Set culture to en-US and don't let the user override.
50 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
52 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
53 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
55 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
56 Results1 [3] = "0." + decimals;
57 Results1 [5] = "0." + decimals;
58 Results1 [6] = perPattern.Replace ("n","0.00");
60 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "65,535.00000";
61 Results2 [6] = perPattern.Replace ("n","6,553,500.00000");
65 public void TearDown ()
67 Thread.CurrentThread.CurrentCulture = old_culture;
71 public void TestMinMax()
74 Assert.AreEqual(UInt16.MinValue, MyUInt16_2);
75 Assert.AreEqual(UInt16.MaxValue, MyUInt16_3);
79 public void TestCompareTo()
81 Assert.AreEqual(65535, MyUInt16_3.CompareTo(MyUInt16_2), "#1");
82 Assert.AreEqual(0, MyUInt16_2.CompareTo(MyUInt16_2), "#2");
83 Assert.AreEqual(0, MyUInt16_1.CompareTo((UInt16)(42)), "#3");
84 Assert.AreEqual(-65535, MyUInt16_2.CompareTo(MyUInt16_3), "#4");
86 MyUInt16_2.CompareTo((object)100);
87 Assert.Fail("Should raise a System.ArgumentException");
88 } catch (ArgumentException e) {
93 public void TestEquals()
95 Assert.IsTrue(MyUInt16_1.Equals(MyUInt16_1));
96 Assert.IsTrue(MyUInt16_1.Equals((object)(UInt16)(42)));
97 Assert.IsTrue(MyUInt16_1.Equals((object)(SByte)(42)) == false);
98 Assert.IsTrue(MyUInt16_1.Equals(MyUInt16_2) == false);
102 public void TestGetHashCode()
105 MyUInt16_1.GetHashCode();
106 MyUInt16_2.GetHashCode();
107 MyUInt16_3.GetHashCode();
110 Assert.Fail("GetHashCode should not raise an exception here");
115 public void TestParse()
117 //test Parse(string s)
118 Assert.IsTrue(MyUInt16_1 == UInt16.Parse(MyString1));
119 Assert.IsTrue(MyUInt16_2 == UInt16.Parse(MyString2));
120 Assert.IsTrue(MyUInt16_3 == UInt16.Parse(MyString3));
123 Assert.Fail("Should raise a System.ArgumentNullException");
125 catch (Exception e) {
126 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
129 UInt16.Parse("not-a-number");
130 Assert.Fail("Should raise a System.FormatException");
132 catch (Exception e) {
133 Assert.IsTrue(typeof(FormatException) == e.GetType());
136 int OverInt = UInt16.MaxValue + 1;
137 UInt16.Parse(OverInt.ToString());
138 Assert.Fail("Should raise a System.OverflowException");
140 catch (Exception e) {
141 Assert.IsTrue(typeof(OverflowException) == e.GetType());
143 //test Parse(string s, NumberStyles style)
144 Assert.IsTrue(42 == UInt16.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
146 UInt16.Parse("$42", NumberStyles.Integer);
147 Assert.Fail("Should raise a System.FormatException");
149 catch (Exception e) {
150 Assert.IsTrue(typeof(FormatException) == e.GetType());
152 //test Parse(string s, IFormatProvider provider)
153 Assert.IsTrue(42 == UInt16.Parse(" 42 ", Nfi));
155 UInt16.Parse("%42", Nfi);
156 Assert.Fail("Should raise a System.FormatException");
158 catch (Exception e) {
159 Assert.IsTrue(typeof(FormatException) == e.GetType());
161 //test Parse(string s, NumberStyles style, IFormatProvider provider)
162 Assert.IsTrue(16 == UInt16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
164 UInt16.Parse("$42", NumberStyles.Integer, Nfi);
165 Assert.Fail("Should raise a System.FormatException");
167 catch (Exception e) {
168 Assert.IsTrue(typeof(FormatException) == e.GetType());
173 public void TestParseExponent ()
175 Assert.AreEqual (2, UInt16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
176 Assert.AreEqual (20, UInt16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
177 Assert.AreEqual (200, UInt16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
178 Assert.AreEqual (200, UInt16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
179 Assert.AreEqual (2, UInt16.Parse ("2", NumberStyles.AllowExponent), "A#5");
184 } catch (FormatException) {
188 UInt16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
190 } catch (FormatException) {
194 UInt16.Parse ("2E 2", NumberStyles.AllowExponent);
196 } catch (FormatException) {
200 UInt16.Parse ("2E2 ", NumberStyles.AllowExponent);
202 } catch (FormatException) {
206 UInt16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
208 } catch (OverflowException) {
212 long exponent = (long) Int32.MaxValue + 10;
213 UInt16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
215 } catch (OverflowException) {
219 UInt16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
221 } catch (OverflowException) {
225 UInt16.Parse ("2 math e1", NumberStyles.AllowExponent);
227 } catch (FormatException) {
232 public void TestToString()
235 Assert.AreEqual(MyString1, MyUInt16_1.ToString(), "A1");
236 Assert.AreEqual(MyString2, MyUInt16_2.ToString(), "A2");
237 Assert.AreEqual(MyString3, MyUInt16_3.ToString(), "A3");
238 //test ToString(string format)
239 for (int i=0; i < Formats1.Length; i++) {
240 Console.WriteLine ("d:" + NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
241 Assert.AreEqual(Results1[i], MyUInt16_2.ToString(Formats1[i]), "A4:"+i.ToString());
242 Assert.AreEqual(Results2[i], MyUInt16_3.ToString(Formats2[i]), "A5:"+i.ToString());
244 //test ToString(string format, IFormatProvider provider);
245 for (int i=0; i < Formats1.Length; i++) {
246 Assert.AreEqual(ResultsNfi1[i], MyUInt16_2.ToString(Formats1[i], Nfi), "A6:"+i.ToString());
247 Assert.AreEqual(ResultsNfi2[i], MyUInt16_3.ToString(Formats2[i], Nfi), "A7:"+i.ToString());
250 MyUInt16_1.ToString("z");
251 Assert.Fail("Should raise a System.FormatException");
253 catch (Exception e) {
254 Assert.IsTrue(typeof(FormatException) == e.GetType(), "A8");
259 public void ToString_Defaults ()
262 // everything defaults to "G"
263 string def = i.ToString ("G");
264 Assert.AreEqual (def, i.ToString (), "ToString()");
265 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
266 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
267 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
268 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
269 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
271 Assert.AreEqual ("254", def, "ToString(G)");