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]
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]
TearDown()93 	public void TearDown ()
94 	{
95 		Thread.CurrentThread.CurrentCulture = old_culture;
96 	}
97 
98 	[Test]
TestMinMax()99 	public void TestMinMax()
100 	{
101 		Assert.AreEqual(Byte.MinValue, MyByte2);
102 		Assert.AreEqual(Byte.MaxValue, MyByte3);
103 	}
104 
105 	[Test]
TestCompareTo()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]
TestEquals()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]
TestGetHashCode()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]
TestParse()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 (34, Byte.Parse ("34\0"), "#1");
194 		try {
195 			Byte.Parse ("34\0\0\0    \0");
196 			Assert.Fail ("#2");
197 		} catch (FormatException) {}
198 
199 		try {
200 			Byte.Parse ("34\0\0\0    ");
201 			Assert.Fail ("#3");
202 		} catch (FormatException) {}
203 
204 		Assert.AreEqual (34, Byte.Parse ("34\0\0\0"), "#4");
205 	}
206 
207 	[Test]
208 	[ExpectedException (typeof(OverflowException))]
ParseOverflow()209 	public void ParseOverflow()
210 	{
211 		int OverInt = Byte.MaxValue + 1;
212 		Byte.Parse(OverInt.ToString());
213 	}
214 
215 	[Test]
TestToString()216 	public void TestToString()
217 	{
218 		//test ToString()
219 		Assert.AreEqual(MyString1, MyByte1.ToString(), "Compare failed for MyString1 and MyByte1");
220 		Assert.AreEqual(MyString2, MyByte2.ToString(), "Compare failed for MyString2 and MyByte2");
221 		Assert.AreEqual(MyString3, MyByte3.ToString(), "Compare failed for MyString3 and MyByte3");
222 		//test ToString(string format)
223 		for (int i=0; i < Formats1.Length; i++) {
224 			Assert.AreEqual(Results1[i], MyByte2.ToString(Formats1[i]), "Compare failed for Formats1["+i.ToString()+"]");
225 			Assert.AreEqual(Results2[i], MyByte3.ToString(Formats2[i]), "Compare failed for Formats2["+i.ToString()+"]");
226 		}
227 		//test ToString(string format, IFormatProvider provider);
228 		for (int i=0; i < Formats1.Length; i++) {
229 			Assert.AreEqual(Results1_Nfi[i], MyByte2.ToString(Formats1[i], Nfi), "Compare failed for Formats1["+i.ToString()+"] with Nfi");
230 			Assert.AreEqual(Results2_Nfi[i], MyByte3.ToString(Formats2[i], Nfi), "Compare failed for Formats2["+i.ToString()+"] with Nfi");
231 		}
232 		try {
233 			MyByte1.ToString("z");
234 			Assert.Fail ("Should raise a System.FormatException");
235 		}
236 		catch (Exception e) {
237 			Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is the wrong type");
238 		}
239 
240 	}
241 
242 	[Test]
ToString_Defaults()243 	public void ToString_Defaults ()
244 	{
245 		byte i = 254;
246 		// everything defaults to "G"
247 		string def = i.ToString ("G");
248 		Assert.AreEqual (def, i.ToString (), "ToString()");
249 		Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
250 		Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
251 		Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
252 		Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
253 		Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
254 
255 		Assert.AreEqual ("254", def, "ToString(G)");
256 	}
257 }
258 
259 }
260