1 // ByteTest.cs - NUnit Test Cases for the System.Byte 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 ByteTest : Assertion
19 private const Byte MyByte1 = 42;
20 private const Byte MyByte2 = 0;
21 private const Byte MyByte3 = 255;
22 private const string MyString1 = "42";
23 private const string MyString2 = "0";
24 private const string MyString3 = "255";
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[] Results1_Nfi = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
31 "0", "0.000000e+000", "0.00",
32 "0", "0.00", "0.00 %", "0"};
33 private string[] Results2 = { "",
34 "00255", "2.55000e+002", "255.00000",
35 "255", "255.00000", "25,500.00000 %", "000ff"};
36 private string[] Results2_Nfi = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"255.00000",
37 "00255", "2.55000e+002", "255.00000",
38 "255", "255.00000", "25,500.00000 %", "000ff"};
40 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
47 CultureInfo EnUs = new CultureInfo ("en-us", false);
48 EnUs.NumberFormat.NumberDecimalDigits = 2;
49 Thread.CurrentThread.CurrentCulture = EnUs;
51 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
52 string sep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
53 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
54 string csuffix = (cdd > 0 ? sep : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
55 switch (NumberFormatInfo.CurrentInfo.CurrencyPositivePattern) {
57 Results1[0] = csym + "0" + csuffix;
58 Results2[0] = csym + "255" + sep + "00000";
61 Results1[0] = "0" + csuffix + csym;
62 Results2[0] = "255" + sep + "00000" + csym;
65 Results1[0] = csym + " 0" + csuffix;
66 Results2[0] = csym + " 255" + sep + "00000";
69 Results1[0] = "0" + csuffix + " " + csym;
70 Results2[0] = "255" + sep + "00000 " + csym;
74 sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
75 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
76 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
78 Results1[2] = "0" + sep + "000000e+000";
79 Results1[3] = "0" + sep + decimals;
80 Results1[5] = "0" + sep + decimals;
81 Results1[6] = perPattern.Replace ("n","0" + sep + "00");
83 Results2[2] = "2" + sep + "55000e+002";
84 Results2[3] = "255" + sep + "00000";
85 Results2[3] = "255" + sep + "00000";
86 Results2[5] = "255" + sep + "00000";
87 string gsep = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
88 Results2[6] = perPattern.Replace ("n","25" + gsep + "500" + sep + "00000");
91 public void TestMinMax()
93 AssertEquals(Byte.MinValue, MyByte2);
94 AssertEquals(Byte.MaxValue, MyByte3);
97 public void TestCompareTo()
99 Assert(MyByte3.CompareTo(MyByte2) > 0);
100 Assert(MyByte2.CompareTo(MyByte2) == 0);
101 Assert(MyByte1.CompareTo((object)(Byte)42) == 0);
102 Assert(MyByte2.CompareTo(MyByte3) < 0);
104 MyByte2.CompareTo((object)100);
105 Fail("Should raise a System.ArgumentException");
107 catch (Exception e) {
108 Assert(typeof(ArgumentException) == e.GetType());
112 public void TestEquals()
114 Assert(MyByte1.Equals(MyByte1));
115 Assert(MyByte1.Equals((object)(Byte)42));
116 Assert(MyByte1.Equals((object)(Int16)42) == false);
117 Assert(MyByte1.Equals(MyByte2) == false);
120 public void TestGetHashCode()
123 MyByte1.GetHashCode();
124 MyByte2.GetHashCode();
125 MyByte3.GetHashCode();
128 Fail("GetHashCode should not raise an exception here");
132 public void TestParse()
134 //test Parse(string s)
135 Assert("MyByte1="+MyByte1+", MyString1="+MyString1+", Parse="+Byte.Parse(MyString1) , MyByte1 == Byte.Parse(MyString1));
136 Assert("MyByte2", MyByte2 == Byte.Parse(MyString2));
137 Assert("MyByte3", MyByte3 == Byte.Parse(MyString3));
141 Fail("Should raise a System.ArgumentNullException");
143 catch (Exception e) {
144 Assert("Should get ArgumentNullException", typeof(ArgumentNullException) == e.GetType());
147 Byte.Parse("not-a-number");
148 Fail("Should raise a System.FormatException");
150 catch (Exception e) {
151 Assert("not-a-number", typeof(FormatException) == e.GetType());
154 //test Parse(string s, NumberStyles style)
155 AssertEquals(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ",
156 (byte)42, Byte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ",
157 NumberStyles.Currency));
159 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
160 Fail("Should raise a System.FormatException");
162 catch (Exception e) {
163 Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer", typeof(FormatException) == e.GetType());
165 //test Parse(string s, IFormatProvider provider)
166 Assert(" 42 and Nfi", 42 == Byte.Parse(" 42 ", Nfi));
168 Byte.Parse("%42", Nfi);
169 Fail("Should raise a System.FormatException");
171 catch (Exception e) {
172 Assert("%42 and Nfi", typeof(FormatException) == e.GetType());
174 //test Parse(string s, NumberStyles style, IFormatProvider provider)
175 Assert("NumberStyles.HexNumber", 16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
177 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
178 Fail("Should raise a System.FormatException");
180 catch (Exception e) {
181 Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi", typeof(FormatException) == e.GetType());
186 [ExpectedException (typeof(OverflowException))]
187 public void ParseOverflow()
189 int OverInt = Byte.MaxValue + 1;
190 Byte.Parse(OverInt.ToString());
194 public void TestToString()
197 AssertEquals("Compare failed for MyString1 and MyByte1", MyString1, MyByte1.ToString());
198 AssertEquals("Compare failed for MyString2 and MyByte2", MyString2, MyByte2.ToString());
199 AssertEquals("Compare failed for MyString3 and MyByte3", MyString3, MyByte3.ToString());
200 //test ToString(string format)
201 for (int i=0; i < Formats1.Length; i++) {
202 AssertEquals("Compare failed for Formats1["+i.ToString()+"]", Results1[i], MyByte2.ToString(Formats1[i]));
203 AssertEquals("Compare failed for Formats2["+i.ToString()+"]", Results2[i], MyByte3.ToString(Formats2[i]));
205 //test ToString(string format, IFormatProvider provider);
206 for (int i=0; i < Formats1.Length; i++) {
207 AssertEquals("Compare failed for Formats1["+i.ToString()+"] with Nfi", Results1_Nfi[i], MyByte2.ToString(Formats1[i], Nfi));
208 AssertEquals("Compare failed for Formats2["+i.ToString()+"] with Nfi", Results2_Nfi[i], MyByte3.ToString(Formats2[i], Nfi));
211 MyByte1.ToString("z");
212 Fail("Should raise a System.FormatException");
214 catch (Exception e) {
215 AssertEquals("Exception is the wrong type", typeof(FormatException), e.GetType());
221 public void ToString_Defaults ()
224 // everything defaults to "G"
225 string def = i.ToString ("G");
226 AssertEquals ("ToString()", def, i.ToString ());
227 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
228 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
229 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
230 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
231 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
233 AssertEquals ("ToString(G)", "254", def);