2002-06-07 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System / SByteTest.cs
1 // SByteTest.cs - NUnit Test Cases for the System.SByte 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
12 namespace MonoTests.System
13 {
14
15 public class SByteTest : TestCase
16 {
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;
38         
39         public SByteTest() : base ("MonoTests.System.SByteTest testcase") {}\r
40         public SByteTest(string name) : base(name) {}
41
42         protected override void SetUp() 
43         {
44         }
45
46         public static ITest Suite {
47                 get { 
48                         return new TestSuite(typeof(SByteTest)); 
49                 }
50         }
51     
52         public void TestMinMax()
53         {
54                 AssertEquals(SByte.MinValue, MySByte2);
55                 AssertEquals(SByte.MaxValue, MySByte3);
56         }
57         
58         public void TestCompareTo()
59         {
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);
64                 try {
65                         MySByte2.CompareTo(100);
66                         Fail("Should raise a System.ArgumentException");
67                 }
68                 catch (Exception e) {
69                         Assert(typeof(ArgumentException) == e.GetType());
70                 }
71         }
72
73         public void TestEquals()
74         {
75                 Assert(MySByte1.Equals(MySByte1));
76                 Assert(MySByte1.Equals((SByte)(-42)));
77                 Assert(MySByte1.Equals((Int16)(-42)) == false);
78                 Assert(MySByte1.Equals(MySByte2) == false);
79         }
80         
81         public void TestGetHashCode()
82         {
83                 try {
84                         MySByte1.GetHashCode();
85                         MySByte2.GetHashCode();
86                         MySByte3.GetHashCode();
87                 }
88                 catch {
89                         Fail("GetHashCode should not raise an exception here");
90                 }
91         }
92         
93         public void TestParse()
94         {
95                 //test Parse(string s)
96                 Assert(MySByte1 == SByte.Parse(MyString1));
97                 Assert(MySByte2 == SByte.Parse(MyString2));
98                 Assert(MySByte3 == SByte.Parse(MyString3));
99                 try {
100                         SByte.Parse(null);
101                         Fail("Should raise a System.ArgumentNullException");
102                 }
103                 catch (Exception e) {
104                         Assert(typeof(ArgumentNullException) == e.GetType());
105                 }
106                 try {
107                         SByte.Parse("not-a-number");
108                         Fail("Should raise a System.FormatException");
109                 }
110                 catch (Exception e) {
111                         Assert(typeof(FormatException) == e.GetType());
112                 }
113                 try {
114                         int OverInt = SByte.MaxValue + 1;
115                         SByte.Parse(OverInt.ToString());
116                         Fail("Should raise a System.OverflowException");
117                 }
118                 catch (Exception e) {
119                         Assert(typeof(OverflowException) == e.GetType());
120                 }
121                 //test Parse(string s, NumberStyles style)
122                 AssertEquals("A1", (sbyte)42, SByte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
123                 try {
124                         SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
125                         Fail("Should raise a System.FormatException");
126                 }
127                 catch (Exception e) {
128                         Assert(typeof(FormatException) == e.GetType());
129                 }
130                 //test Parse(string s, IFormatProvider provider)
131                 Assert(-42 == SByte.Parse(" -42 ", Nfi));
132                 try {
133                         SByte.Parse("%42", Nfi);
134                         Fail("Should raise a System.FormatException");
135                 }
136                 catch (Exception e) {
137                         Assert(typeof(FormatException) == e.GetType());
138                 }
139                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
140                 Assert(16 == SByte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
141                 try {
142                         SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
143                         Fail("Should raise a System.FormatException");
144                 }
145                 catch (Exception e) {
146                         Assert(typeof(FormatException) == e.GetType());
147                 }
148         }
149         
150         public void TestToString()
151         {
152                 //test ToString()
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)
157                 /*
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);
163                 }
164                 */
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);
169                 }
170                 try {
171                         MySByte1.ToString("z");
172                         Fail("Should raise a System.FormatException");
173                 }
174                 catch (Exception e) {
175                         Assert("typeof(FormatException) == e.GetType()", typeof(FormatException) == e.GetType());
176                 }
177         }
178 }
179
180 }