Adding tests for the basic integer structs
[mono.git] / mcs / class / corlib / Test / System / Int16Test.cs
1 // Int16Test.cs - NUnit Test Cases for the System.Int16 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 public class Int16Test : TestCase
13 {
14         private const Int16 MyInt16_1 = -42;
15         private const Int16 MyInt16_2 = -32768;
16         private const Int16 MyInt16_3 = 32767;
17         private const string MyString1 = "-42";
18         private const string MyString2 = "-32768";
19         private const string MyString3 = "32767";
20         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
21         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
22         private string[] Results1 = {"($32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
23                                           "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
24         private string[] Results2 = {"$32,767.00000", "32767", "3.27670e+004", "32767.00000",
25                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
26         private string[] ResultsNfi1 = {"($32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
27                                           "-32768", "(32,768.00)", "-3,276,800.00 %", "8000"};
28         private string[] ResultsNfi2 = {"$32,767.00000", "32767", "3.27670e+004", "32767.00000",
29                                           "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
30         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
31         
32         public Int16Test(string name) : base(name) {}
33
34         protected override void SetUp() 
35         {
36         }
37
38         public static ITest Suite {
39                 get { 
40                         return new TestSuite(typeof(Int16Test)); 
41                 }
42         }
43     
44         public void TestMinMax()
45         {
46                 
47                 AssertEquals(Int16.MinValue, MyInt16_2);
48                 AssertEquals(Int16.MaxValue, MyInt16_3);
49         }
50         
51         public void TestCompareTo()
52         {
53                 Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
54                 Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
55                 Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
56                 Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
57                 try {
58                         MyInt16_2.CompareTo(100);
59                         Fail("Should raise a System.ArgumentException");
60                 }
61                 catch (Exception e) {
62                         Assert(typeof(System.ArgumentException) == e.GetType());
63                 }
64         }
65
66         public void TestEquals()
67         {
68                 Assert(MyInt16_1.Equals(MyInt16_1));
69                 Assert(MyInt16_1.Equals((Int16)(-42)));
70                 Assert(MyInt16_1.Equals((SByte)(-42)) == false);
71                 Assert(MyInt16_1.Equals(MyInt16_2) == false);
72         }
73         
74         public void TestGetHashCode()
75         {
76                 try {
77                         MyInt16_1.GetHashCode();
78                         MyInt16_2.GetHashCode();
79                         MyInt16_3.GetHashCode();
80                 }
81                 catch {
82                         Fail("GetHashCode should not raise an exception here");
83                 }
84         }
85         
86         public void TestParse()
87         {
88                 //test Parse(string s)
89                 Assert(MyInt16_1 == Int16.Parse(MyString1));
90                 Assert(MyInt16_2 == Int16.Parse(MyString2));
91                 Assert(MyInt16_3 == Int16.Parse(MyString3));
92                 try {
93                         Int16.Parse(null);
94                         Fail("Should raise a System.ArgumentNullException");
95                 }
96                 catch (Exception e) {
97                         Assert(typeof(System.ArgumentNullException) == e.GetType());
98                 }
99                 try {
100                         Int16.Parse("not-a-number");
101                         Fail("Should raise a System.FormatException");
102                 }
103                 catch (Exception e) {
104                         Assert(typeof(System.FormatException) == e.GetType());
105                 }
106                 try {
107                         int OverInt = Int16.MaxValue + 1;
108                         Int16.Parse(OverInt.ToString());
109                         Fail("Should raise a System.OverflowException");
110                 }
111                 catch (Exception e) {
112                         Assert(typeof(System.OverflowException) == e.GetType());
113                 }
114                 //test Parse(string s, NumberStyles style)
115                 Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
116                 try {
117                         Int16.Parse("$42", NumberStyles.Integer);
118                         Fail("Should raise a System.FormatException");
119                 }
120                 catch (Exception e) {
121                         Assert(typeof(System.FormatException) == e.GetType());
122                 }
123                 //test Parse(string s, IFormatProvider provider)
124                 Assert(-42 == Int16.Parse(" -42 ", Nfi));
125                 try {
126                         Int16.Parse("%42", Nfi);
127                         Fail("Should raise a System.FormatException");
128                 }
129                 catch (Exception e) {
130                         Assert(typeof(System.FormatException) == e.GetType());
131                 }
132                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
133                 Assert(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
134                 try {
135                         Int16.Parse("$42", NumberStyles.Integer, Nfi);
136                         Fail("Should raise a System.FormatException");
137                 }
138                 catch (Exception e) {
139                         Assert(typeof(System.FormatException) == e.GetType());
140                 }
141         }
142         
143         public void TestToString()
144         {
145                 //test ToString()
146                 Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
147                 Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
148                 Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
149                 //test ToString(string format)
150                 for (int i=0; i < Formats1.Length; i++) {
151                         Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
152                         Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
153                 }
154                 //test ToString(string format, IFormatProvider provider);
155                 for (int i=0; i < Formats1.Length; i++) {
156                         Assert(String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0);
157                         Assert(String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0);
158                 }
159                 try {
160                         MyInt16_1.ToString("z");
161                         Fail("Should raise a System.FormatException");
162                 }
163                 catch (Exception e) {
164                         Assert(typeof(System.FormatException) == e.GetType());
165                 }
166         }
167 }
168