2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 : Assertion
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 NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
41         
42         public ByteTest() {}
43
44         [SetUp]
45         public void SetUp() 
46         {
47                 CultureInfo EnUs = new CultureInfo ("en-us", false);
48                 EnUs.NumberFormat.NumberDecimalDigits = 2;
49                 Thread.CurrentThread.CurrentCulture = EnUs;
50
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) {
56                         case 0: // $n
57                                 Results1[0] = csym + "0" + csuffix;
58                                 Results2[0] = csym + "255" + sep + "00000";
59                                 break;
60                         case 1: // n$
61                                 Results1[0] = "0" + csuffix + csym;
62                                 Results2[0] = "255" + sep + "00000" + csym;
63                                 break;
64                         case 2: // $ n
65                                 Results1[0] = csym + " 0" + csuffix;
66                                 Results2[0] = csym + " 255" + sep + "00000";
67                                 break;
68                         case 3: // n $
69                                 Results1[0] = "0" + csuffix + " " + csym;
70                                 Results2[0] = "255" + sep + "00000 " + csym;
71                                 break;
72                 }
73                 
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];
77                 
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");
82                 
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");
89         }
90
91         public void TestMinMax()
92         {
93                 AssertEquals(Byte.MinValue, MyByte2);
94                 AssertEquals(Byte.MaxValue, MyByte3);
95         }
96         
97         public void TestCompareTo()
98         {
99                 Assert(MyByte3.CompareTo(MyByte2) > 0);
100                 Assert(MyByte2.CompareTo(MyByte2) == 0);
101                 Assert(MyByte1.CompareTo((Byte)42) == 0);
102                 Assert(MyByte2.CompareTo(MyByte3) < 0);
103                 try {
104                         MyByte2.CompareTo(100);
105                         Fail("Should raise a System.ArgumentException");
106                 }
107                 catch (Exception e) {
108                         Assert(typeof(ArgumentException) == e.GetType());
109                 }
110         }
111
112         public void TestEquals()
113         {
114                 Assert(MyByte1.Equals(MyByte1));
115                 Assert(MyByte1.Equals((Byte)42));
116                 Assert(MyByte1.Equals((Int16)42) == false);
117                 Assert(MyByte1.Equals(MyByte2) == false);
118         }
119         
120         public void TestGetHashCode()
121         {
122                 try {
123                         MyByte1.GetHashCode();
124                         MyByte2.GetHashCode();
125                         MyByte3.GetHashCode();
126                 }
127                 catch {
128                         Fail("GetHashCode should not raise an exception here");
129                 }
130         }
131         
132         public void TestParse()
133         {
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));
138
139                 try {
140                         Byte.Parse(null);
141                         Fail("Should raise a System.ArgumentNullException");
142                 }
143                 catch (Exception e) {
144                         Assert("Should get ArgumentNullException", typeof(ArgumentNullException) == e.GetType());
145                 }
146                 try {
147                         Byte.Parse("not-a-number");
148                         Fail("Should raise a System.FormatException");
149                 }
150                 catch (Exception e) {
151                         Assert("not-a-number", typeof(FormatException) == e.GetType());
152                 }
153
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));
158                 try {
159                         Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
160                         Fail("Should raise a System.FormatException");
161                 }
162                 catch (Exception e) {
163                         Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 and NumberStyles.Integer", typeof(FormatException) == e.GetType());
164                 }
165                 //test Parse(string s, IFormatProvider provider)
166                 Assert(" 42 and Nfi", 42 == Byte.Parse(" 42 ", Nfi));
167                 try {
168                         Byte.Parse("%42", Nfi);
169                         Fail("Should raise a System.FormatException");
170                 }
171                 catch (Exception e) {
172                         Assert("%42 and Nfi", typeof(FormatException) == e.GetType());
173                 }
174                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
175                 Assert("NumberStyles.HexNumber", 16 == Byte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
176                 try {
177                         Byte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
178                         Fail("Should raise a System.FormatException");
179                 }
180                 catch (Exception e) {
181                         Assert(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi", typeof(FormatException) == e.GetType());
182                 }
183         }
184
185         [Test]
186         [ExpectedException (typeof(OverflowException))]
187         public void ParseOverflow()
188         {
189                 int OverInt = Byte.MaxValue + 1;
190                 Byte.Parse(OverInt.ToString());
191         }
192
193
194         public void TestToString()
195         {
196                 //test ToString()
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]));
204                 }
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));
209                 }
210                 try {
211                         MyByte1.ToString("z");
212                         Fail("Should raise a System.FormatException");
213                 }
214                 catch (Exception e) {
215                         AssertEquals("Exception is the wrong type", typeof(FormatException), e.GetType());
216                 }
217                 
218         }
219
220         [Test]
221         public void ToString_Defaults () 
222         {
223                 byte i = 254;
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));
232
233                 AssertEquals ("ToString(G)", "254", def);
234         }
235 }
236
237 }