1 // UInt32Test.cs - NUnit Test Cases for the System.UInt32 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 UInt32Test
: Assertion
19 private const UInt32 MyUInt32_1
= 42;
20 private const UInt32 MyUInt32_2
= 0;
21 private const UInt32 MyUInt32_3
= 4294967295;
22 private const string MyString1
= "42";
23 private const string MyString2
= "0";
24 private const string MyString3
= "4294967295";
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 "4294967295", "4.29497e+009", "4294967295.00000",
35 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
36 private string[] ResultsNfi2
= {NumberFormatInfo
.InvariantInfo
.CurrencySymbol
+"4,294,967,295.00000",
37 "4294967295", "4.29497e+009", "4294967295.00000",
38 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
39 private NumberFormatInfo Nfi
= NumberFormatInfo
.InvariantInfo
;
41 private CultureInfo old_culture
;
46 old_culture
= Thread
.CurrentThread
.CurrentCulture
;
48 // Set culture to en-US and don't let the user override.
49 Thread
.CurrentThread
.CurrentCulture
= new CultureInfo ("en-US", false);
51 // We can't initialize this until we set the culture.
53 string decimals
= new String ('0', NumberFormatInfo
.CurrentInfo
.NumberDecimalDigits
);
54 string perPattern
= new string[] {"n %","n%","%n"}
[NumberFormatInfo
.CurrentInfo
.PercentPositivePattern
];
56 Results1
[0] = NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+ "0.00";
57 Results1
[3] = "0." + decimals
;
58 Results1
[5] = "0." + decimals
;
59 Results1
[6] = perPattern
.Replace ("n","0.00");
61 Results2
[0] = NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+ "4,294,967,295.00000";
62 Results2
[6] = perPattern
.Replace ("n","429,496,729,500.00000");
66 public void TearDown ()
68 Thread
.CurrentThread
.CurrentCulture
= old_culture
;
71 public void TestMinMax()
74 AssertEquals(UInt32
.MinValue
, MyUInt32_2
);
75 AssertEquals(UInt32
.MaxValue
, MyUInt32_3
);
78 public void TestCompareTo()
80 Assert(MyUInt32_3
.CompareTo(MyUInt32_2
) > 0);
81 Assert(MyUInt32_2
.CompareTo(MyUInt32_2
) == 0);
82 Assert(MyUInt32_1
.CompareTo((UInt32
)(42)) == 0);
83 Assert(MyUInt32_2
.CompareTo(MyUInt32_3
) < 0);
84 Assert (1 == UInt32
.Parse ("1"));
85 Assert (1 == UInt32
.Parse (" 1"));
86 Assert (1 == UInt32
.Parse (" 1"));
87 Assert (1 == UInt32
.Parse ("1 "));
88 Assert (1 == UInt32
.Parse ("+1"));
91 UInt32
.Parse (" + 1 ");
92 Fail ("Should raise FormatException1");
93 } catch (Exception e
){
94 Assert (typeof (FormatException
) == e
.GetType ());
99 Fail ("Should raise FormatException");
100 } catch (Exception e
){
101 Assert (typeof (FormatException
) == e
.GetType ());
104 MyUInt32_2
.CompareTo((Int16
)100);
105 Fail("Should raise a System.ArgumentException");
107 catch (Exception e
) {
108 Assert(typeof(ArgumentException
) == e
.GetType());
112 public void TestEquals()
114 Assert(MyUInt32_1
.Equals(MyUInt32_1
));
115 Assert(MyUInt32_1
.Equals((UInt32
)(42)));
116 Assert(MyUInt32_1
.Equals((SByte
)(42)) == false);
117 Assert(MyUInt32_1
.Equals(MyUInt32_2
) == false);
120 public void TestGetHashCode()
123 MyUInt32_1
.GetHashCode();
124 MyUInt32_2
.GetHashCode();
125 MyUInt32_3
.GetHashCode();
128 Fail("GetHashCode should not raise an exception here");
132 public void TestParse()
134 //test Parse(string s)
135 Assert("Parse problem on \""+MyString1
+"\"", MyUInt32_1
== UInt32
.Parse(MyString1
));
136 Assert("Parse problem on \""+MyString2
+"\"", MyUInt32_2
== UInt32
.Parse(MyString2
));
137 Assert("Parse problem on \""+MyString3
+"\"", MyUInt32_3
== UInt32
.Parse(MyString3
));
140 Fail("Should raise a System.ArgumentNullException");
142 catch (Exception e
) {
143 Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException
) == e
.GetType());
146 UInt32
.Parse("not-a-number");
147 Fail("Should raise a System.FormatException");
149 catch (Exception e
) {
150 Assert("Did not get FormatException type", typeof(FormatException
) == e
.GetType());
153 // TODO: Use this after ToString() is completed. For now, hard code string that generates
155 //double OverInt = (double)UInt32.MaxValue + 1;
156 //UInt32.Parse(OverInt.ToString());
157 UInt32
.Parse("4294967296");
158 Fail("Should raise a System.OverflowException");
160 catch (Exception e
) {
161 Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e
.GetType()+"'", typeof(OverflowException
) == e
.GetType());
163 //test Parse(string s, NumberStyles style)
164 Assert(42 == UInt32
.Parse(" "+NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+"42 ", NumberStyles
.Currency
));
166 UInt32
.Parse("$42", NumberStyles
.Integer
);
167 Fail("Should raise a System.FormatException");
169 catch (Exception e
) {
170 Assert(typeof(FormatException
) == e
.GetType());
172 //test Parse(string s, IFormatProvider provider)
173 Assert(42 == UInt32
.Parse(" 42 ", Nfi
));
175 UInt32
.Parse("%42", Nfi
);
176 Fail("Should raise a System.FormatException");
178 catch (Exception e
) {
179 Assert(typeof(FormatException
) == e
.GetType());
181 //test Parse(string s, NumberStyles style, IFormatProvider provider)
182 Assert(16 == UInt32
.Parse(" 10 ", NumberStyles
.HexNumber
, Nfi
));
184 UInt32
.Parse("$42", NumberStyles
.Integer
, Nfi
);
185 Fail("Should raise a System.FormatException");
187 catch (Exception e
) {
188 Assert(typeof(FormatException
) == e
.GetType());
192 public void TestToString()
197 AssertEquals(MyString1
, MyUInt32_1
.ToString());
199 AssertEquals(MyString2
, MyUInt32_2
.ToString());
201 AssertEquals(MyString3
, MyUInt32_3
.ToString());
202 } catch (Exception e
) {
203 Fail("TestToString: Failed on TestNumber=" + TestNumber
204 + " with exception: " + e
.ToString());
207 //test ToString(string format)
208 for (int i
=0; i
< Formats1
.Length
; i
++) {
210 AssertEquals(Results1
[i
], MyUInt32_2
.ToString(Formats1
[i
]));
211 } catch (Exception e
) {
212 Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i
213 + ". e = " + e
.ToString());
217 AssertEquals(Results2
[i
], MyUInt32_3
.ToString(Formats2
[i
]));
218 } catch (Exception e
) {
219 Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
220 + ". e = " + e
.ToString());
223 //test ToString(string format, IFormatProvider provider);
224 for (int i
=0; i
< Formats1
.Length
; i
++) {
225 AssertEquals(ResultsNfi1
[i
], MyUInt32_2
.ToString(Formats1
[i
], Nfi
));
226 AssertEquals(ResultsNfi2
[i
], MyUInt32_3
.ToString(Formats2
[i
], Nfi
));
229 MyUInt32_1
.ToString("z");
230 Fail("Should raise a System.FormatException");
232 catch (Exception e
) {
233 Assert(typeof(FormatException
) == e
.GetType());
238 public void ToString_Defaults ()
241 // everything defaults to "G"
242 string def
= i
.ToString ("G");
243 AssertEquals ("ToString()", def
, i
.ToString ());
244 AssertEquals ("ToString((IFormatProvider)null)", def
, i
.ToString ((IFormatProvider
)null));
245 AssertEquals ("ToString((string)null)", def
, i
.ToString ((string)null));
246 AssertEquals ("ToString(empty)", def
, i
.ToString (String
.Empty
));
247 AssertEquals ("ToString(null,null)", def
, i
.ToString (null, null));
248 AssertEquals ("ToString(empty,null)", def
, i
.ToString (String
.Empty
, null));
250 AssertEquals ("ToString(G)", "254", def
);