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
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 CultureInfo old_culture;
41 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
46 old_culture = Thread.CurrentThread.CurrentCulture;
48 CultureInfo EnUs = new CultureInfo ("en-us", false);
49 EnUs.NumberFormat.NumberDecimalDigits = 2;
50 Thread.CurrentThread.CurrentCulture = EnUs;
52 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
53 string sep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
54 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
55 string csuffix = (cdd > 0 ? sep : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
56 switch (NumberFormatInfo.CurrentInfo.CurrencyPositivePattern) {
58 Results1[0] = csym + "0" + csuffix;
59 Results2[0] = csym + "255" + sep + "00000";
62 Results1[0] = "0" + csuffix + csym;
63 Results2[0] = "255" + sep + "00000" + csym;
66 Results1[0] = csym + " 0" + csuffix;
67 Results2[0] = csym + " 255" + sep + "00000";
70 Results1[0] = "0" + csuffix + " " + csym;
71 Results2[0] = "255" + sep + "00000 " + csym;
75 sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
76 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
77 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
79 Results1[2] = "0" + sep + "000000e+000";
80 Results1[3] = "0" + sep + decimals;
81 Results1[5] = "0" + sep + decimals;
82 Results1[6] = perPattern.Replace ("n","0" + sep + "00");
84 Results2[2] = "2" + sep + "55000e+002";
85 Results2[3] = "255" + sep + "00000";
86 Results2[3] = "255" + sep + "00000";
87 Results2[5] = "255" + sep + "00000";
88 string gsep = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
89 Results2[6] = perPattern.Replace ("n","25" + gsep + "500" + sep + "00000");
93 public void TearDown ()
95 Thread.CurrentThread.CurrentCulture = old_culture;
99 public void TestMinMax()
101 Assert.AreEqual(Byte.MinValue, MyByte2);
102 Assert.AreEqual(Byte.MaxValue, MyByte3);
106 public void TestCompareTo()
108 Assert.AreEqual (255, MyByte3.CompareTo(MyByte2), "#1");
109 Assert.AreEqual (0, MyByte2.CompareTo(MyByte2), "#2");
110 Assert.AreEqual (0, MyByte1.CompareTo((object)(Byte)42), "#3");
111 Assert.AreEqual (-255, MyByte2.CompareTo(MyByte3), "#4");
113 MyByte2.CompareTo((object)100);
114 Assert.Fail ("Should raise a System.ArgumentException");
115 } catch (ArgumentException e) {
120 public void TestEquals()
122 Assert.IsTrue (MyByte1.Equals(MyByte1));
123 Assert.IsTrue (MyByte1.Equals((object)(Byte)42));
124 Assert.IsTrue (MyByte1.Equals((object)(Int16)42) == false);
125 Assert.IsTrue (MyByte1.Equals(MyByte2) == false);
129 public void TestGetHashCode()
132 MyByte1.GetHashCode();
133 MyByte2.GetHashCode();
134 MyByte3.GetHashCode();
137 Assert.Fail ("GetHashCode should not raise an exception here");
142 public void TestParse()
144 //test Parse(string s)
145 Assert.IsTrue (MyByte1 == Byte.Parse(MyString1), "MyByte1="+MyByte1+", MyString1="+MyString1+", Parse="+Byte.Parse(MyString1));
146 Assert.IsTrue(MyByte2 == Byte.Parse(MyString2), "MyByte2");
147 Assert.IsTrue(MyByte3 == Byte.Parse(MyString3), "MyByte3");
151 Assert.Fail ("Should raise a System.ArgumentNullException");
153 catch (Exception e) {
154 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Should get ArgumentNullException");
157 Byte.Parse("not-a-number");
158 Assert.Fail ("Should raise a System.FormatException");
160 catch (Exception e) {
161 Assert.IsTrue(typeof(FormatException) == e.GetType(), "not-a-number");
164 //test Parse(string s, NumberStyles style)
165 Assert.AreEqual((byte)42, Byte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency),
166 " "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ");
168 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
169 Assert.Fail ("Should raise a System.FormatException");
171 catch (Exception e) {
172 Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer");
174 //test Parse(string s, IFormatProvider provider)
175 Assert.IsTrue(42 == Byte.Parse(" 42 ", Nfi), " 42 and Nfi");
177 Byte.Parse("%42", Nfi);
178 Assert.Fail ("Should raise a System.FormatException");
180 catch (Exception e) {
181 Assert.IsTrue(typeof(FormatException) == e.GetType(), "%42 and Nfi");
183 //test Parse(string s, NumberStyles style, IFormatProvider provider)
184 Assert.IsTrue(16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "NumberStyles.HexNumber");
186 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
187 Assert.Fail ("Should raise a System.FormatException");
189 catch (Exception e) {
190 Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi");
193 Assert.AreEqual (734, Int64.Parse ("734\0"), "#1");
194 Assert.AreEqual (734, Int64.Parse ("734\0\0\0 \0"), "#2");
195 Assert.AreEqual (734, Int64.Parse ("734\0\0\0 "), "#3");
196 Assert.AreEqual (734, Int64.Parse ("734\0\0\0"), "#4");
200 [ExpectedException (typeof(OverflowException))]
201 public void ParseOverflow()
203 int OverInt = Byte.MaxValue + 1;
204 Byte.Parse(OverInt.ToString());
208 public void TestToString()
211 Assert.AreEqual(MyString1, MyByte1.ToString(), "Compare failed for MyString1 and MyByte1");
212 Assert.AreEqual(MyString2, MyByte2.ToString(), "Compare failed for MyString2 and MyByte2");
213 Assert.AreEqual(MyString3, MyByte3.ToString(), "Compare failed for MyString3 and MyByte3");
214 //test ToString(string format)
215 for (int i=0; i < Formats1.Length; i++) {
216 Assert.AreEqual(Results1[i], MyByte2.ToString(Formats1[i]), "Compare failed for Formats1["+i.ToString()+"]");
217 Assert.AreEqual(Results2[i], MyByte3.ToString(Formats2[i]), "Compare failed for Formats2["+i.ToString()+"]");
219 //test ToString(string format, IFormatProvider provider);
220 for (int i=0; i < Formats1.Length; i++) {
221 Assert.AreEqual(Results1_Nfi[i], MyByte2.ToString(Formats1[i], Nfi), "Compare failed for Formats1["+i.ToString()+"] with Nfi");
222 Assert.AreEqual(Results2_Nfi[i], MyByte3.ToString(Formats2[i], Nfi), "Compare failed for Formats2["+i.ToString()+"] with Nfi");
225 MyByte1.ToString("z");
226 Assert.Fail ("Should raise a System.FormatException");
228 catch (Exception e) {
229 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is the wrong type");
235 public void ToString_Defaults ()
238 // everything defaults to "G"
239 string def = i.ToString ("G");
240 Assert.AreEqual (def, i.ToString (), "ToString()");
241 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
242 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
243 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
244 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
245 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
247 Assert.AreEqual ("254", def, "ToString(G)");