1 // UInt64Test.cs - NUnit Test Cases for the System.UInt64 struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Globalization;
11 using System.Threading;
13 namespace MonoTests.System
17 public class UInt64Test : Assertion
19 private const UInt64 MyUInt64_1 = 42;
20 private const UInt64 MyUInt64_2 = 0;
21 private const UInt64 MyUInt64_3 = 18446744073709551615;
22 private const string MyString1 = "42";
23 private const string MyString2 = "0";
24 private const string MyString3 = "18446744073709551615";
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 = {"",
28 "0", "0.000000e+000", "0.00",
29 "0", "0.00", "0.00 %", "0"};
30 private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
31 "0", "0.000000e+000", "0.00",
32 "0", "0.00", "0.00 %", "0"};
33 private string[] Results2 = {"",
34 "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
35 "1.8447e+19", "18,446,744,073,709,551,615.00000",
36 "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
37 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"18,446,744,073,709,551,615.00000",
38 "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
39 "1.8447e+19", "18,446,744,073,709,551,615.00000",
40 "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
42 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
44 private CultureInfo old_culture;
49 old_culture = Thread.CurrentThread.CurrentCulture;
51 // Set culture to en-US and don't let the user override.
52 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
54 // We can't initialize this until we set the culture.
56 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
57 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
59 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
60 Results1 [3] = "0." + decimals;
61 Results1 [5] = "0." + decimals;
62 Results1 [6] = perPattern.Replace ("n","0.00");
64 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "18,446,744,073,709,551,615.00000";
65 Results2 [6] = perPattern.Replace ("n","1,844,674,407,370,955,161,500.00000");
69 public void TearDown ()
71 Thread.CurrentThread.CurrentCulture = old_culture;
74 public void TestMinMax()
77 AssertEquals(UInt64.MinValue, MyUInt64_2);
78 AssertEquals(UInt64.MaxValue, MyUInt64_3);
81 public void TestCompareTo()
83 Assert(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
84 Assert(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
85 Assert(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
86 Assert(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
88 MyUInt64_2.CompareTo((object)(Int16)100);
89 Fail("Should raise a System.ArgumentException");
92 Assert(typeof(ArgumentException) == e.GetType());
96 public void TestEquals()
98 Assert(MyUInt64_1.Equals(MyUInt64_1));
99 Assert(MyUInt64_1.Equals((object)(UInt64)(42)));
100 Assert(MyUInt64_1.Equals((object)(SByte)(42)) == false);
101 Assert(MyUInt64_1.Equals(MyUInt64_2) == false);
104 public void TestGetHashCode()
107 MyUInt64_1.GetHashCode();
108 MyUInt64_2.GetHashCode();
109 MyUInt64_3.GetHashCode();
112 Fail("GetHashCode should not raise an exception here");
116 public void TestParse()
118 //test Parse(string s)
119 Assert(MyUInt64_1 == UInt64.Parse(MyString1));
120 Assert(MyUInt64_2 == UInt64.Parse(MyString2));
121 Assert(MyUInt64_3 == UInt64.Parse(MyString3));
124 Fail("Should raise a ArgumentNullException");
126 catch (Exception e) {
127 Assert(typeof(ArgumentNullException) == e.GetType());
130 UInt64.Parse("not-a-number");
131 Fail("Should raise a System.FormatException");
133 catch (Exception e) {
134 Assert(typeof(FormatException) == e.GetType());
136 //test Parse(string s, NumberStyles style)
138 double OverInt = (double)UInt64.MaxValue + 1;
139 UInt64.Parse(OverInt.ToString(), NumberStyles.Float);
140 Fail("Should raise a OverflowException");
142 catch (Exception e) {
143 Assert(typeof(OverflowException) == e.GetType());
146 double OverInt = (double)UInt64.MaxValue + 1;
147 UInt64.Parse(OverInt.ToString(), NumberStyles.Integer);
148 Fail("Should raise a System.FormatException");
150 catch (Exception e) {
151 Assert(typeof(FormatException) == e.GetType());
153 Assert(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
155 UInt64.Parse("$42", NumberStyles.Integer);
156 Fail("Should raise a FormatException");
158 catch (Exception e) {
159 Assert(typeof(FormatException) == e.GetType());
161 //test Parse(string s, IFormatProvider provider)
162 Assert(42 == UInt64.Parse(" 42 ", Nfi));
164 UInt64.Parse("%42", Nfi);
165 Fail("Should raise a System.FormatException");
167 catch (Exception e) {
168 Assert(typeof(FormatException) == e.GetType());
170 //test Parse(string s, NumberStyles style, IFormatProvider provider)
171 Assert(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
173 UInt64.Parse("$42", NumberStyles.Integer, Nfi);
174 Fail("Should raise a System.FormatException");
176 catch (Exception e) {
177 Assert(typeof(FormatException) == e.GetType());
181 public void TestToString()
184 AssertEquals("A", MyString1, MyUInt64_1.ToString());
185 AssertEquals("B", MyString2, MyUInt64_2.ToString());
186 AssertEquals("C", MyString3, MyUInt64_3.ToString());
187 //test ToString(string format)
188 for (int i=0; i < Formats1.Length; i++) {
189 AssertEquals("D", Results1[i], MyUInt64_2.ToString(Formats1[i]));
190 AssertEquals("E: format #" + i, Results2[i], MyUInt64_3.ToString(Formats2[i]));
192 //test ToString(string format, IFormatProvider provider);
193 for (int i=0; i < Formats1.Length; i++) {
194 AssertEquals("F", ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi));
195 AssertEquals("G", ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi));
198 MyUInt64_1.ToString("z");
199 Fail("Should raise a System.FormatException");
201 catch (Exception e) {
202 Assert("H", typeof(FormatException) == e.GetType());
207 public void ToString_Defaults ()
210 // everything defaults to "G"
211 string def = i.ToString ("G");
212 AssertEquals ("ToString()", def, i.ToString ());
213 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
214 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
215 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
216 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
217 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
219 AssertEquals ("ToString(G)", "254", def);