1 // SByteTest.cs - NUnit Test Cases for the System.SByte struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
6 // Copyright (C) 2004 Novell (http://www.novell.com)
11 using System.Globalization;
13 namespace MonoTests.System
17 public class SByteTest : Assertion
19 private const SByte MySByte1 = -42;
20 private const SByte MySByte2 = -128;
21 private const SByte MySByte3 = 127;
22 private const string MyString1 = "-42";
23 private const string MyString2 = "-128";
24 private const string MyString3 = "127";
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 = {"("+NumberFormatInfo.CurrentInfo.CurrencySymbol+"128.00)",
28 "-128", "-1.280000e+002", "-128.00",
29 "-128", "-128.00", "-12,800.00 %", "80"};
30 private string[] Results2 = {NumberFormatInfo.CurrentInfo.CurrencySymbol+"127.00000",
31 "00127", "1.27000e+002", "127.00000",
32 "127", "127.00000", "12,700.00000 %", "0007f"};
33 private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"128.00)",
34 "-128", "-1.280000e+002", "-128.00",
35 "-128", "-128.00", "-12,800.00 %", "80"};
36 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"127.00000",
37 "00127", "1.27000e+002", "127.00000",
38 "127", "127.00000", "12,700.00000 %", "0007f"};
39 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
41 public void TestMinMax()
43 AssertEquals(SByte.MinValue, MySByte2);
44 AssertEquals(SByte.MaxValue, MySByte3);
47 public void TestCompareTo()
49 Assert(MySByte3.CompareTo(MySByte2) > 0);
50 Assert(MySByte2.CompareTo(MySByte2) == 0);
51 Assert(MySByte1.CompareTo((SByte)(-42)) == 0);
52 Assert(MySByte2.CompareTo(MySByte3) < 0);
54 MySByte2.CompareTo((object)(int)100);
55 Fail("Should raise a System.ArgumentException");
58 Assert(typeof(ArgumentException) == e.GetType());
62 public void TestEquals()
64 Assert(MySByte1.Equals(MySByte1));
65 Assert(MySByte1.Equals((SByte)(-42)));
66 Assert(MySByte1.Equals((Int16)(-42)) == false);
67 Assert(MySByte1.Equals(MySByte2) == false);
70 public void TestGetHashCode()
73 MySByte1.GetHashCode();
74 MySByte2.GetHashCode();
75 MySByte3.GetHashCode();
78 Fail("GetHashCode should not raise an exception here");
82 public void TestParse()
84 //test Parse(string s)
85 Assert(MySByte1 == SByte.Parse(MyString1));
86 Assert(MySByte2 == SByte.Parse(MyString2));
87 Assert(MySByte3 == SByte.Parse(MyString3));
90 Fail("Should raise a System.ArgumentNullException");
93 Assert(typeof(ArgumentNullException) == e.GetType());
96 SByte.Parse("not-a-number");
97 Fail("Should raise a System.FormatException");
100 Assert(typeof(FormatException) == e.GetType());
103 int OverInt = SByte.MaxValue + 1;
104 SByte.Parse(OverInt.ToString());
105 Fail("Should raise a System.OverflowException");
107 catch (Exception e) {
108 Assert(typeof(OverflowException) == e.GetType());
110 //test Parse(string s, NumberStyles style)
111 AssertEquals("A1", (sbyte)42, SByte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
113 SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
114 Fail("Should raise a System.FormatException");
116 catch (Exception e) {
117 Assert(typeof(FormatException) == e.GetType());
119 //test Parse(string s, IFormatProvider provider)
120 Assert(-42 == SByte.Parse(" -42 ", Nfi));
122 SByte.Parse("%42", Nfi);
123 Fail("Should raise a System.FormatException");
125 catch (Exception e) {
126 Assert(typeof(FormatException) == e.GetType());
128 //test Parse(string s, NumberStyles style, IFormatProvider provider)
129 Assert(16 == SByte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
131 SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
132 Fail("Should raise a System.FormatException");
134 catch (Exception e) {
135 Assert(typeof(FormatException) == e.GetType());
140 public void Parse_MinMax ()
142 AssertEquals ("MinValue", SByte.MinValue, SByte.Parse ("-128"));
143 AssertEquals ("MaxValue", SByte.MaxValue, SByte.Parse ("127"));
146 public void TestToString()
149 Assert("MyString1, MySByte1.ToString()", String.Compare(MyString1, MySByte1.ToString()) == 0);
150 Assert("MyString2, MySByte2.ToString()", String.Compare(MyString2, MySByte2.ToString()) == 0);
151 Assert("MyString3, MySByte3.ToString()", String.Compare(MyString3, MySByte3.ToString()) == 0);
152 //test ToString(string format)
154 TODO: These tests depend on the culture of the system running the test.
155 So, this needs to be tested in a different way.
156 for (int i=0; i < Formats1.Length; i++) {
157 Assert("i="+i+", Results1[i]="+Results1[i]+", MySByte2.ToString(Formats1[i])="+MySByte2.ToString(Formats1[i]), String.Compare(Results1[i], MySByte2.ToString(Formats1[i])) == 0);
158 Assert("Results2[i], MySByte3.ToString(Formats2[i])", String.Compare(Results2[i], MySByte3.ToString(Formats2[i])) == 0);
161 //test ToString(string format, IFormatProvider provider);
162 for (int i=0; i < Formats1.Length; i++) {
163 Assert("i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MySByte2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MySByte2.ToString(Formats1[i], Nfi), String.Compare(ResultsNfi1[i], MySByte2.ToString(Formats1[i], Nfi)) == 0);
164 Assert("ResultsNfi2[i], MySByte3.ToString(Formats2[i], Nfi):"+ResultsNfi2[i]+"<==>"+MySByte3.ToString(Formats2[i], Nfi), String.Compare(ResultsNfi2[i], MySByte3.ToString(Formats2[i], Nfi)) == 0);
167 MySByte1.ToString("z");
168 Fail("Should raise a System.FormatException");
170 catch (Exception e) {
171 Assert("typeof(FormatException) == e.GetType()", typeof(FormatException) == e.GetType());
176 public void ToString_Defaults ()
179 // everything defaults to "G"
180 string def = i.ToString ("G");
181 AssertEquals ("ToString()", def, i.ToString ());
182 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
183 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
184 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
185 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
186 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
188 AssertEquals ("ToString(G)", "100", def);