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
10 using System.Globalization;
12 namespace MonoTests.System
15 public class SByteTest : TestCase
17 private const SByte MySByte1 = -42;
18 private const SByte MySByte2 = -128;
19 private const SByte MySByte3 = 127;
20 private const string MyString1 = "-42";
21 private const string MyString2 = "-128";
22 private const string MyString3 = "127";
23 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
24 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
25 private string[] Results1 = {"("+NumberFormatInfo.CurrentInfo.CurrencySymbol+"128.00)",
26 "-128", "-1.280000e+002", "-128.00",
27 "-128", "-128.00", "-12,800.00 %", "80"};
28 private string[] Results2 = {NumberFormatInfo.CurrentInfo.CurrencySymbol+"127.00000",
29 "00127", "1.27000e+002", "127.00000",
30 "127", "127.00000", "12,700.00000 %", "0007f"};
31 private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"128.00)",
32 "-128", "-1.280000e+002", "-128.00",
33 "-128", "-128.00", "-12,800.00 %", "80"};
34 private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"127.00000",
35 "00127", "1.27000e+002", "127.00000",
36 "127", "127.00000", "12,700.00000 %", "0007f"};
37 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
39 public SByteTest() : base ("MonoTests.System.SByteTest testcase") {}
\r
40 public SByteTest(string name) : base(name) {}
42 protected override void SetUp()
46 public static ITest Suite {
48 return new TestSuite(typeof(SByteTest));
52 public void TestMinMax()
54 AssertEquals(SByte.MinValue, MySByte2);
55 AssertEquals(SByte.MaxValue, MySByte3);
58 public void TestCompareTo()
60 Assert(MySByte3.CompareTo(MySByte2) > 0);
61 Assert(MySByte2.CompareTo(MySByte2) == 0);
62 Assert(MySByte1.CompareTo((SByte)(-42)) == 0);
63 Assert(MySByte2.CompareTo(MySByte3) < 0);
65 MySByte2.CompareTo(100);
66 Fail("Should raise a System.ArgumentException");
69 Assert(typeof(ArgumentException) == e.GetType());
73 public void TestEquals()
75 Assert(MySByte1.Equals(MySByte1));
76 Assert(MySByte1.Equals((SByte)(-42)));
77 Assert(MySByte1.Equals((Int16)(-42)) == false);
78 Assert(MySByte1.Equals(MySByte2) == false);
81 public void TestGetHashCode()
84 MySByte1.GetHashCode();
85 MySByte2.GetHashCode();
86 MySByte3.GetHashCode();
89 Fail("GetHashCode should not raise an exception here");
93 public void TestParse()
95 //test Parse(string s)
96 Assert(MySByte1 == SByte.Parse(MyString1));
97 Assert(MySByte2 == SByte.Parse(MyString2));
98 Assert(MySByte3 == SByte.Parse(MyString3));
101 Fail("Should raise a System.ArgumentNullException");
103 catch (Exception e) {
104 Assert(typeof(ArgumentNullException) == e.GetType());
107 SByte.Parse("not-a-number");
108 Fail("Should raise a System.FormatException");
110 catch (Exception e) {
111 Assert(typeof(FormatException) == e.GetType());
114 int OverInt = SByte.MaxValue + 1;
115 SByte.Parse(OverInt.ToString());
116 Fail("Should raise a System.OverflowException");
118 catch (Exception e) {
119 Assert(typeof(OverflowException) == e.GetType());
121 //test Parse(string s, NumberStyles style)
122 AssertEquals("A1", (sbyte)42, SByte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
124 SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
125 Fail("Should raise a System.FormatException");
127 catch (Exception e) {
128 Assert(typeof(FormatException) == e.GetType());
130 //test Parse(string s, IFormatProvider provider)
131 Assert(-42 == SByte.Parse(" -42 ", Nfi));
133 SByte.Parse("%42", Nfi);
134 Fail("Should raise a System.FormatException");
136 catch (Exception e) {
137 Assert(typeof(FormatException) == e.GetType());
139 //test Parse(string s, NumberStyles style, IFormatProvider provider)
140 Assert(16 == SByte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
142 SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
143 Fail("Should raise a System.FormatException");
145 catch (Exception e) {
146 Assert(typeof(FormatException) == e.GetType());
150 public void TestToString()
153 Assert("MyString1, MySByte1.ToString()", String.Compare(MyString1, MySByte1.ToString()) == 0);
154 Assert("MyString2, MySByte2.ToString()", String.Compare(MyString2, MySByte2.ToString()) == 0);
155 Assert("MyString3, MySByte3.ToString()", String.Compare(MyString3, MySByte3.ToString()) == 0);
156 //test ToString(string format)
158 TODO: These tests depend on the culture of the system running the test.
159 So, this needs to be tested in a different way.
160 for (int i=0; i < Formats1.Length; i++) {
161 Assert("i="+i+", Results1[i]="+Results1[i]+", MySByte2.ToString(Formats1[i])="+MySByte2.ToString(Formats1[i]), String.Compare(Results1[i], MySByte2.ToString(Formats1[i])) == 0);
162 Assert("Results2[i], MySByte3.ToString(Formats2[i])", String.Compare(Results2[i], MySByte3.ToString(Formats2[i])) == 0);
165 //test ToString(string format, IFormatProvider provider);
166 for (int i=0; i < Formats1.Length; i++) {
167 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);
168 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);
171 MySByte1.ToString("z");
172 Fail("Should raise a System.FormatException");
174 catch (Exception e) {
175 Assert("typeof(FormatException) == e.GetType()", typeof(FormatException) == e.GetType());