2009-06-30 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / class / corlib / Test / System / ByteTest.cs
blob21fbe38d21960820de49eb68f3d891c99207b870
1 // ByteTest.cs - NUnit Test Cases for the System.Byte struct
2 //
3 // Mario Martinez (mariom925@home.om)
4 //
5 // (C) Ximian, Inc. http://www.ximian.com
6 //
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
11 using System.Threading;
13 namespace MonoTests.System
16 [TestFixture]
17 public class ByteTest
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;
43 [SetUp]
44 public void SetUp ()
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) {
57 case 0: // $n
58 Results1[0] = csym + "0" + csuffix;
59 Results2[0] = csym + "255" + sep + "00000";
60 break;
61 case 1: // n$
62 Results1[0] = "0" + csuffix + csym;
63 Results2[0] = "255" + sep + "00000" + csym;
64 break;
65 case 2: // $ n
66 Results1[0] = csym + " 0" + csuffix;
67 Results2[0] = csym + " 255" + sep + "00000";
68 break;
69 case 3: // n $
70 Results1[0] = "0" + csuffix + " " + csym;
71 Results2[0] = "255" + sep + "00000 " + csym;
72 break;
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");
92 [TearDown]
93 public void TearDown ()
95 Thread.CurrentThread.CurrentCulture = old_culture;
98 public void TestMinMax()
100 Assert.AreEqual(Byte.MinValue, MyByte2);
101 Assert.AreEqual(Byte.MaxValue, MyByte3);
104 public void TestCompareTo()
106 Assert.IsTrue (MyByte3.CompareTo(MyByte2) > 0);
107 Assert.IsTrue (MyByte2.CompareTo(MyByte2) == 0);
108 Assert.IsTrue (MyByte1.CompareTo((object)(Byte)42) == 0);
109 Assert.IsTrue (MyByte2.CompareTo(MyByte3) < 0);
110 try {
111 MyByte2.CompareTo((object)100);
112 Assert.Fail ("Should raise a System.ArgumentException");
114 catch (Exception e) {
115 Assert.IsTrue (typeof(ArgumentException) == e.GetType());
119 public void TestEquals()
121 Assert.IsTrue (MyByte1.Equals(MyByte1));
122 Assert.IsTrue (MyByte1.Equals((object)(Byte)42));
123 Assert.IsTrue (MyByte1.Equals((object)(Int16)42) == false);
124 Assert.IsTrue (MyByte1.Equals(MyByte2) == false);
127 public void TestGetHashCode()
129 try {
130 MyByte1.GetHashCode();
131 MyByte2.GetHashCode();
132 MyByte3.GetHashCode();
134 catch {
135 Assert.Fail ("GetHashCode should not raise an exception here");
139 public void TestParse()
141 //test Parse(string s)
142 Assert.IsTrue (MyByte1 == Byte.Parse(MyString1), "MyByte1="+MyByte1+", MyString1="+MyString1+", Parse="+Byte.Parse(MyString1));
143 Assert.IsTrue(MyByte2 == Byte.Parse(MyString2), "MyByte2");
144 Assert.IsTrue(MyByte3 == Byte.Parse(MyString3), "MyByte3");
146 try {
147 Byte.Parse(null);
148 Assert.Fail ("Should raise a System.ArgumentNullException");
150 catch (Exception e) {
151 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Should get ArgumentNullException");
153 try {
154 Byte.Parse("not-a-number");
155 Assert.Fail ("Should raise a System.FormatException");
157 catch (Exception e) {
158 Assert.IsTrue(typeof(FormatException) == e.GetType(), "not-a-number");
161 //test Parse(string s, NumberStyles style)
162 Assert.AreEqual((byte)42, Byte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency),
163 " "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ");
164 try {
165 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
166 Assert.Fail ("Should raise a System.FormatException");
168 catch (Exception e) {
169 Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer");
171 //test Parse(string s, IFormatProvider provider)
172 Assert.IsTrue(42 == Byte.Parse(" 42 ", Nfi), " 42 and Nfi");
173 try {
174 Byte.Parse("%42", Nfi);
175 Assert.Fail ("Should raise a System.FormatException");
177 catch (Exception e) {
178 Assert.IsTrue(typeof(FormatException) == e.GetType(), "%42 and Nfi");
180 //test Parse(string s, NumberStyles style, IFormatProvider provider)
181 Assert.IsTrue(16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "NumberStyles.HexNumber");
182 try {
183 Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
184 Assert.Fail ("Should raise a System.FormatException");
186 catch (Exception e) {
187 Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi");
191 [Test]
192 [ExpectedException (typeof(OverflowException))]
193 public void ParseOverflow()
195 int OverInt = Byte.MaxValue + 1;
196 Byte.Parse(OverInt.ToString());
200 public void TestToString()
202 //test ToString()
203 Assert.AreEqual(MyString1, MyByte1.ToString(), "Compare failed for MyString1 and MyByte1");
204 Assert.AreEqual(MyString2, MyByte2.ToString(), "Compare failed for MyString2 and MyByte2");
205 Assert.AreEqual(MyString3, MyByte3.ToString(), "Compare failed for MyString3 and MyByte3");
206 //test ToString(string format)
207 for (int i=0; i < Formats1.Length; i++) {
208 Assert.AreEqual(Results1[i], MyByte2.ToString(Formats1[i]), "Compare failed for Formats1["+i.ToString()+"]");
209 Assert.AreEqual(Results2[i], MyByte3.ToString(Formats2[i]), "Compare failed for Formats2["+i.ToString()+"]");
211 //test ToString(string format, IFormatProvider provider);
212 for (int i=0; i < Formats1.Length; i++) {
213 Assert.AreEqual(Results1_Nfi[i], MyByte2.ToString(Formats1[i], Nfi), "Compare failed for Formats1["+i.ToString()+"] with Nfi");
214 Assert.AreEqual(Results2_Nfi[i], MyByte3.ToString(Formats2[i], Nfi), "Compare failed for Formats2["+i.ToString()+"] with Nfi");
216 try {
217 MyByte1.ToString("z");
218 Assert.Fail ("Should raise a System.FormatException");
220 catch (Exception e) {
221 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is the wrong type");
226 [Test]
227 public void ToString_Defaults ()
229 byte i = 254;
230 // everything defaults to "G"
231 string def = i.ToString ("G");
232 Assert.AreEqual (def, i.ToString (), "ToString()");
233 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
234 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
235 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
236 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
237 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
239 Assert.AreEqual ("254", def, "ToString(G)");