New test.
[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 // Copyright (C) 2004 Novell (http://www.novell.com)
7 // 
8
9 using NUnit.Framework;
10 using System;
11 using System.Globalization;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class SByteTest : Assertion
18 {
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;
40         
41         public void TestMinMax()
42         {
43                 AssertEquals(SByte.MinValue, MySByte2);
44                 AssertEquals(SByte.MaxValue, MySByte3);
45         }
46         
47         public void TestCompareTo()
48         {
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);
53                 try {
54                         MySByte2.CompareTo((object)(int)100);
55                         Fail("Should raise a System.ArgumentException");
56                 }
57                 catch (Exception e) {
58                         Assert(typeof(ArgumentException) == e.GetType());
59                 }
60         }
61
62         public void TestEquals()
63         {
64                 Assert(MySByte1.Equals(MySByte1));
65                 Assert(MySByte1.Equals((SByte)(-42)));
66                 Assert(MySByte1.Equals((Int16)(-42)) == false);
67                 Assert(MySByte1.Equals(MySByte2) == false);
68         }
69         
70         public void TestGetHashCode()
71         {
72                 try {
73                         MySByte1.GetHashCode();
74                         MySByte2.GetHashCode();
75                         MySByte3.GetHashCode();
76                 }
77                 catch {
78                         Fail("GetHashCode should not raise an exception here");
79                 }
80         }
81         
82         public void TestParse()
83         {
84                 //test Parse(string s)
85                 Assert(MySByte1 == SByte.Parse(MyString1));
86                 Assert(MySByte2 == SByte.Parse(MyString2));
87                 Assert(MySByte3 == SByte.Parse(MyString3));
88                 try {
89                         SByte.Parse(null);
90                         Fail("Should raise a System.ArgumentNullException");
91                 }
92                 catch (Exception e) {
93                         Assert(typeof(ArgumentNullException) == e.GetType());
94                 }
95                 try {
96                         SByte.Parse("not-a-number");
97                         Fail("Should raise a System.FormatException");
98                 }
99                 catch (Exception e) {
100                         Assert(typeof(FormatException) == e.GetType());
101                 }
102                 try {
103                         int OverInt = SByte.MaxValue + 1;
104                         SByte.Parse(OverInt.ToString());
105                         Fail("Should raise a System.OverflowException");
106                 }
107                 catch (Exception e) {
108                         Assert(typeof(OverflowException) == e.GetType());
109                 }
110                 //test Parse(string s, NumberStyles style)
111                 AssertEquals("A1", (sbyte)42, SByte.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
112                 try {
113                         SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
114                         Fail("Should raise a System.FormatException");
115                 }
116                 catch (Exception e) {
117                         Assert(typeof(FormatException) == e.GetType());
118                 }
119                 //test Parse(string s, IFormatProvider provider)
120                 Assert(-42 == SByte.Parse(" -42 ", Nfi));
121                 try {
122                         SByte.Parse("%42", Nfi);
123                         Fail("Should raise a System.FormatException");
124                 }
125                 catch (Exception e) {
126                         Assert(typeof(FormatException) == e.GetType());
127                 }
128                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
129                 Assert(16 == SByte.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
130                 try {
131                         SByte.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
132                         Fail("Should raise a System.FormatException");
133                 }
134                 catch (Exception e) {
135                         Assert(typeof(FormatException) == e.GetType());
136                 }
137         }
138
139         [Test]
140         public void Parse_MinMax () 
141         {
142                 AssertEquals ("MinValue", SByte.MinValue, SByte.Parse ("-128"));
143                 AssertEquals ("MaxValue", SByte.MaxValue, SByte.Parse ("127"));
144         }
145         
146         public void TestToString()
147         {
148                 //test ToString()
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)
153                 /*
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);
159                 }
160                 */
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);
165                 }
166                 try {
167                         MySByte1.ToString("z");
168                         Fail("Should raise a System.FormatException");
169                 }
170                 catch (Exception e) {
171                         Assert("typeof(FormatException) == e.GetType()", typeof(FormatException) == e.GetType());
172                 }
173         }
174
175         [Test]
176         public void ToString_Defaults () 
177         {
178                 SByte i = 100;
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));
187
188                 AssertEquals ("ToString(G)", "100", def);
189         }
190 }
191
192 }