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;
12 namespace MonoTests.System
15 public class UInt32Test : TestCase
17 private const UInt32 MyUInt32_1 = 42;
18 private const UInt32 MyUInt32_2 = 0;
19 private const UInt32 MyUInt32_3 = 4294967295;
20 private const string MyString1 = "42";
21 private const string MyString2 = "0";
22 private const string MyString3 = "4294967295";
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.InvariantInfo.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.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
32 "4294967295", "4.29497e+009", "4294967295.00000",
33 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
34 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
35 "4294967295", "4.29497e+009", "4294967295.00000",
36 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
37 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
39 public UInt32Test() : base ("MonoTests.System.UInt32Test testcase") {}
\r
40 public UInt32Test(string name) : base(name) {}
42 protected override void SetUp()
46 public static ITest Suite {
48 return new TestSuite(typeof(UInt32Test));
52 public void TestMinMax()
55 AssertEquals(UInt32.MinValue, MyUInt32_2);
56 AssertEquals(UInt32.MaxValue, MyUInt32_3);
59 public void TestCompareTo()
61 Assert(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
62 Assert(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
63 Assert(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
64 Assert(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
65 Assert (1 == UInt32.Parse ("1"));
66 Assert (1 == UInt32.Parse (" 1"));
67 Assert (1 == UInt32.Parse (" 1"));
68 Assert (1 == UInt32.Parse ("1 "));
69 Assert (1 == UInt32.Parse ("+1"));
72 UInt32.Parse (" + 1 ");
73 Fail ("Should raise FormatException1");
74 } catch (Exception e){
75 Assert (typeof (FormatException) == e.GetType ());
80 Fail ("Should raise FormatException");
81 } catch (Exception e){
82 Assert (typeof (FormatException) == e.GetType ());
85 MyUInt32_2.CompareTo((Int16)100);
86 Fail("Should raise a System.ArgumentException");
89 Assert(typeof(ArgumentException) == e.GetType());
93 public void TestEquals()
95 Assert(MyUInt32_1.Equals(MyUInt32_1));
96 Assert(MyUInt32_1.Equals((UInt32)(42)));
97 Assert(MyUInt32_1.Equals((SByte)(42)) == false);
98 Assert(MyUInt32_1.Equals(MyUInt32_2) == false);
101 public void TestGetHashCode()
104 MyUInt32_1.GetHashCode();
105 MyUInt32_2.GetHashCode();
106 MyUInt32_3.GetHashCode();
109 Fail("GetHashCode should not raise an exception here");
113 public void TestParse()
115 //test Parse(string s)
116 Assert("Parse problem on \""+MyString1+"\"", MyUInt32_1 == UInt32.Parse(MyString1));
117 Assert("Parse problem on \""+MyString2+"\"", MyUInt32_2 == UInt32.Parse(MyString2));
118 Assert("Parse problem on \""+MyString3+"\"", MyUInt32_3 == UInt32.Parse(MyString3));
121 Fail("Should raise a System.ArgumentNullException");
123 catch (Exception e) {
124 Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException) == e.GetType());
127 UInt32.Parse("not-a-number");
128 Fail("Should raise a System.FormatException");
130 catch (Exception e) {
131 Assert("Did not get FormatException type", typeof(FormatException) == e.GetType());
134 // TODO: Use this after ToString() is completed. For now, hard code string that generates
136 //double OverInt = (double)UInt32.MaxValue + 1;
137 //UInt32.Parse(OverInt.ToString());
138 UInt32.Parse("4294967296");
139 Fail("Should raise a System.OverflowException");
141 catch (Exception e) {
142 Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'", typeof(OverflowException) == e.GetType());
144 //test Parse(string s, NumberStyles style)
145 Assert(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
147 UInt32.Parse("$42", NumberStyles.Integer);
148 Fail("Should raise a System.FormatException");
150 catch (Exception e) {
151 Assert(typeof(FormatException) == e.GetType());
153 //test Parse(string s, IFormatProvider provider)
154 Assert(42 == UInt32.Parse(" 42 ", Nfi));
156 UInt32.Parse("%42", Nfi);
157 Fail("Should raise a System.FormatException");
159 catch (Exception e) {
160 Assert(typeof(FormatException) == e.GetType());
162 //test Parse(string s, NumberStyles style, IFormatProvider provider)
163 Assert(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
165 UInt32.Parse("$42", NumberStyles.Integer, Nfi);
166 Fail("Should raise a System.FormatException");
168 catch (Exception e) {
169 Assert(typeof(FormatException) == e.GetType());
173 public void TestToString()
178 AssertEquals(MyString1, MyUInt32_1.ToString());
180 AssertEquals(MyString2, MyUInt32_2.ToString());
182 AssertEquals(MyString3, MyUInt32_3.ToString());
183 } catch (Exception e) {
184 Fail("TestToString: Failed on TestNumber=" + TestNumber
185 + " with exception: " + e.ToString());
188 //test ToString(string format)
189 for (int i=0; i < Formats1.Length; i++) {
191 AssertEquals(Results1[i], MyUInt32_2.ToString(Formats1[i]));
192 } catch (Exception e) {
193 Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i
194 + ". e = " + e.ToString());
198 AssertEquals(Results2[i], MyUInt32_3.ToString(Formats2[i]));
199 } catch (Exception e) {
200 Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
201 + ". e = " + e.ToString());
204 //test ToString(string format, IFormatProvider provider);
205 for (int i=0; i < Formats1.Length; i++) {
206 AssertEquals(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
207 AssertEquals(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
210 MyUInt32_1.ToString("z");
211 Fail("Should raise a System.FormatException");
213 catch (Exception e) {
214 Assert(typeof(FormatException) == e.GetType());