Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System / ByteTest.cs
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 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
11 using System.Threading;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class ByteTest 
18 {
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"};
39
40         private CultureInfo old_culture;
41         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
42
43         [SetUp]
44         public void SetUp ()
45         {
46                 old_culture = Thread.CurrentThread.CurrentCulture;
47
48                 CultureInfo EnUs = new CultureInfo ("en-us", false);
49                 EnUs.NumberFormat.NumberDecimalDigits = 2;
50                 Thread.CurrentThread.CurrentCulture = EnUs;
51
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;
73                 }
74                 
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];
78                 
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");
83                 
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");
90         }
91
92         [TearDown]
93         public void TearDown ()
94         {
95                 Thread.CurrentThread.CurrentCulture = old_culture;
96         }
97
98         [Test]
99         public void TestMinMax()
100         {
101                 Assert.AreEqual(Byte.MinValue, MyByte2);
102                 Assert.AreEqual(Byte.MaxValue, MyByte3);
103         }
104
105         [Test]  
106         public void TestCompareTo()
107         {
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");
112                 try {
113                         MyByte2.CompareTo((object)100);
114                         Assert.Fail ("Should raise a System.ArgumentException");
115                 } catch (ArgumentException e) {
116                 }
117         }
118
119         [Test]
120         public void TestEquals()
121         {
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);
126         }
127         
128         [Test]
129         public void TestGetHashCode()
130         {
131                 try {
132                         MyByte1.GetHashCode();
133                         MyByte2.GetHashCode();
134                         MyByte3.GetHashCode();
135                 }
136                 catch {
137                         Assert.Fail ("GetHashCode should not raise an exception here");
138                 }
139         }
140         
141         [Test]
142         public void TestParse()
143         {
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");
148
149                 try {
150                         Byte.Parse(null);
151                         Assert.Fail ("Should raise a System.ArgumentNullException");
152                 }
153                 catch (Exception e) {
154                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Should get ArgumentNullException");
155                 }
156                 try {
157                         Byte.Parse("not-a-number");
158                         Assert.Fail ("Should raise a System.FormatException");
159                 }
160                 catch (Exception e) {
161                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "not-a-number");
162                 }
163
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 ");
167                 try {
168                         Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
169                         Assert.Fail ("Should raise a System.FormatException");
170                 }
171                 catch (Exception e) {
172                         Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer");
173                 }
174                 //test Parse(string s, IFormatProvider provider)
175                 Assert.IsTrue(42 == Byte.Parse(" 42 ", Nfi), " 42 and Nfi");
176                 try {
177                         Byte.Parse("%42", Nfi);
178                         Assert.Fail ("Should raise a System.FormatException");
179                 }
180                 catch (Exception e) {
181                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "%42 and Nfi");
182                 }
183                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
184                 Assert.IsTrue(16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "NumberStyles.HexNumber");
185                 try {
186                         Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
187                         Assert.Fail ("Should raise a System.FormatException");
188                 }
189                 catch (Exception e) {
190                         Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi");
191                 }
192
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");
197         }
198
199         [Test]
200         [ExpectedException (typeof(OverflowException))]
201         public void ParseOverflow()
202         {
203                 int OverInt = Byte.MaxValue + 1;
204                 Byte.Parse(OverInt.ToString());
205         }
206
207         [Test]
208         public void TestToString()
209         {
210                 //test ToString()
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()+"]");
218                 }
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");
223                 }
224                 try {
225                         MyByte1.ToString("z");
226                         Assert.Fail ("Should raise a System.FormatException");
227                 }
228                 catch (Exception e) {
229                         Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is the wrong type");
230                 }
231                 
232         }
233
234         [Test]
235         public void ToString_Defaults () 
236         {
237                 byte i = 254;
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)");
246
247                 Assert.AreEqual ("254", def, "ToString(G)");
248         }
249 }
250
251 }