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
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 Assert.AreEqual(UInt32.MinValue, MyUInt32_2);
75 Assert.AreEqual(UInt32.MaxValue, MyUInt32_3);
78 public void TestCompareTo()
80 Assert.IsTrue(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
81 Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
82 Assert.IsTrue(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
83 Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
84 Assert.IsTrue (1 == UInt32.Parse ("1"));
85 Assert.IsTrue (1 == UInt32.Parse (" 1"));
86 Assert.IsTrue (1 == UInt32.Parse (" 1"));
87 Assert.IsTrue (1 == UInt32.Parse ("1 "));
88 Assert.IsTrue (1 == UInt32.Parse ("+1"));
91 UInt32.Parse (" + 1 ");
92 Assert.Fail ("Should raise FormatException1");
93 } catch (Exception e){
94 Assert.IsTrue (typeof (FormatException) == e.GetType ());
99 Assert.Fail ("Should raise FormatException");
100 } catch (Exception e){
101 Assert.IsTrue (typeof (FormatException) == e.GetType ());
104 MyUInt32_2.CompareTo((object)(Int16)100);
105 Assert.Fail("Should raise a System.ArgumentException");
107 catch (Exception e) {
108 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
112 public void TestEquals()
114 Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_1));
115 Assert.IsTrue(MyUInt32_1.Equals((object)(UInt32)(42)));
116 Assert.IsTrue(MyUInt32_1.Equals((object)(SByte)(42)) == false);
117 Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_2) == false);
120 public void TestGetHashCode()
123 MyUInt32_1.GetHashCode();
124 MyUInt32_2.GetHashCode();
125 MyUInt32_3.GetHashCode();
128 Assert.Fail("GetHashCode should not raise an exception here");
132 public void TestParse()
134 //test Parse(string s)
135 Assert.IsTrue(MyUInt32_1 == UInt32.Parse(MyString1), "Parse problem on \""+MyString1+"\"");
136 Assert.IsTrue(MyUInt32_2 == UInt32.Parse(MyString2), "Parse problem on \""+MyString2+"\"");
137 Assert.IsTrue(MyUInt32_3 == UInt32.Parse(MyString3), "Parse problem on \""+MyString3+"\"");
140 Assert.Fail("Should raise a System.ArgumentNullException");
142 catch (Exception e) {
143 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Did not get ArgumentNullException type");
146 UInt32.Parse("not-a-number");
147 Assert.Fail("Should raise a System.FormatException");
149 catch (Exception e) {
150 Assert.IsTrue(typeof(FormatException) == e.GetType(), "Did not get FormatException type");
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 Assert.Fail("Should raise a System.OverflowException");
160 catch (Exception e) {
161 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'");
163 //test Parse(string s, NumberStyles style)
164 Assert.IsTrue(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
166 UInt32.Parse("$42", NumberStyles.Integer);
167 Assert.Fail("Should raise a System.FormatException");
169 catch (Exception e) {
170 Assert.IsTrue(typeof(FormatException) == e.GetType());
172 //test Parse(string s, IFormatProvider provider)
173 Assert.IsTrue(42 == UInt32.Parse(" 42 ", Nfi));
175 UInt32.Parse("%42", Nfi);
176 Assert.Fail("Should raise a System.FormatException");
178 catch (Exception e) {
179 Assert.IsTrue(typeof(FormatException) == e.GetType());
181 //test Parse(string s, NumberStyles style, IFormatProvider provider)
182 Assert.IsTrue(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
184 UInt32.Parse("$42", NumberStyles.Integer, Nfi);
185 Assert.Fail("Should raise a System.FormatException");
187 catch (Exception e) {
188 Assert.IsTrue(typeof(FormatException) == e.GetType());
190 // Pass a DateTimeFormatInfo, it is unable to format
191 // numbers, but we should not crash
193 UInt32.Parse ("123", new DateTimeFormatInfo ());
196 public void TestToString()
201 Assert.AreEqual(MyString1, MyUInt32_1.ToString());
203 Assert.AreEqual(MyString2, MyUInt32_2.ToString());
205 Assert.AreEqual(MyString3, MyUInt32_3.ToString());
206 } catch (Exception e) {
207 Assert.Fail("TestToString: Assert.Failed on TestNumber=" + TestNumber
208 + " with exception: " + e.ToString());
211 //test ToString(string format)
212 for (int i=0; i < Formats1.Length; i++) {
214 Assert.AreEqual(Results1[i], MyUInt32_2.ToString(Formats1[i]));
215 } catch (Exception e) {
216 Assert.Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i
217 + ". e = " + e.ToString());
221 Assert.AreEqual(Results2[i], MyUInt32_3.ToString(Formats2[i]));
222 } catch (Exception e) {
223 Assert.Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
224 + ". e = " + e.ToString());
227 //test ToString(string format, IFormatProvider provider);
228 for (int i=0; i < Formats1.Length; i++) {
229 Assert.AreEqual(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
230 Assert.AreEqual(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
233 MyUInt32_1.ToString("z");
234 Assert.Fail("Should raise a System.FormatException");
236 catch (Exception e) {
237 Assert.IsTrue(typeof(FormatException) == e.GetType());
242 public void ToString_Defaults ()
245 // everything defaults to "G"
246 string def = i.ToString ("G");
247 Assert.AreEqual (def, i.ToString (), "ToString()");
248 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
249 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
250 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
251 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
252 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
254 Assert.AreEqual ("254", def, "ToString(G)");