1 // Int16Test.cs - NUnit Test Cases for the System.Int16 struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System
.Threading
;
11 using System
.Globalization
;
13 namespace MonoTests
.System
17 public class Int16Test
: Assertion
19 private const Int16 MyInt16_1
= -42;
20 private const Int16 MyInt16_2
= -32768;
21 private const Int16 MyInt16_3
= 32767;
22 private const string MyString1
= "-42";
23 private const string MyString2
= "-32768";
24 private const string MyString3
= "32767";
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
= {null, "-32768", "-3.276800e+004", "-32768.00",
28 "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
29 private string[] Results2
= {null, "32767", "3.27670e+004", "32767.00000",
30 "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
31 private string[] ResultsNfi1
= {"("+NumberFormatInfo
.InvariantInfo
.CurrencySymbol
+"32,768.00)", "-32768", "-3.276800e+004", "-32768.00",
32 "-32768", "-32,768.00", "-3,276,800.00 %", "8000"};
33 private string[] ResultsNfi2
= {NumberFormatInfo
.InvariantInfo
.CurrencySymbol
+"32,767.00000", "32767", "3.27670e+004", "32767.00000",
34 "32767", "32,767.00000", "3,276,700.00000 %", "07fff"};
35 private NumberFormatInfo Nfi
= NumberFormatInfo
.InvariantInfo
;
37 private CultureInfo old_culture
;
42 old_culture
= Thread
.CurrentThread
.CurrentCulture
;
44 // Set culture to en-US and don't let the user override.
45 Thread
.CurrentThread
.CurrentCulture
= new CultureInfo ("en-US", false);
47 // We can't initialize this until we set the culture.
48 Results1
[0] = "("+NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+"32,768.00)";
49 Results2
[0] = NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+"32,767.00000";
53 public void TearDown ()
55 Thread
.CurrentThread
.CurrentCulture
= old_culture
;
58 public void TestMinMax()
61 AssertEquals(Int16
.MinValue
, MyInt16_2
);
62 AssertEquals(Int16
.MaxValue
, MyInt16_3
);
65 public void TestCompareTo()
67 Assert(MyInt16_3
.CompareTo(MyInt16_2
) > 0);
68 Assert(MyInt16_2
.CompareTo(MyInt16_2
) == 0);
69 Assert(MyInt16_1
.CompareTo((Int16
)(-42)) == 0);
70 Assert(MyInt16_2
.CompareTo(MyInt16_3
) < 0);
72 MyInt16_2
.CompareTo(100);
73 Fail("Should raise a System.ArgumentException");
76 Assert(typeof(ArgumentException
) == e
.GetType());
80 public void TestEquals()
82 Assert(MyInt16_1
.Equals(MyInt16_1
));
83 Assert(MyInt16_1
.Equals((Int16
)(-42)));
84 Assert(MyInt16_1
.Equals((SByte
)(-42)) == false);
85 Assert(MyInt16_1
.Equals(MyInt16_2
) == false);
88 public void TestGetHashCode()
91 MyInt16_1
.GetHashCode();
92 MyInt16_2
.GetHashCode();
93 MyInt16_3
.GetHashCode();
96 Fail("GetHashCode should not raise an exception here");
100 public void TestParse()
102 //test Parse(string s)
103 Assert(MyInt16_1
== Int16
.Parse(MyString1
));
104 Assert(MyInt16_2
== Int16
.Parse(MyString2
));
105 Assert(MyInt16_3
== Int16
.Parse(MyString3
));
108 Fail("Should raise a System.ArgumentNullException");
110 catch (Exception e
) {
111 Assert(typeof(ArgumentNullException
) == e
.GetType());
114 Int16
.Parse("not-a-number");
115 Fail("Should raise a System.FormatException");
117 catch (Exception e
) {
118 Assert(typeof(FormatException
) == e
.GetType());
121 int OverInt
= Int16
.MaxValue
+ 1;
122 Int16
.Parse(OverInt
.ToString());
123 Fail("Should raise a System.OverflowException");
125 catch (Exception e
) {
126 Assert(typeof(OverflowException
) == e
.GetType());
128 //test Parse(string s, NumberStyles style)
129 Assert(42 == Int16
.Parse(" $42 ", NumberStyles
.Currency
));
131 Int16
.Parse("$42", NumberStyles
.Integer
);
132 Fail("Should raise a System.FormatException");
134 catch (Exception e
) {
135 Assert(typeof(FormatException
) == e
.GetType());
137 //test Parse(string s, IFormatProvider provider)
138 Assert(-42 == Int16
.Parse(" -42 ", Nfi
));
140 Int16
.Parse("%42", Nfi
);
141 Fail("Should raise a System.FormatException");
143 catch (Exception e
) {
144 Assert(typeof(FormatException
) == e
.GetType());
146 //test Parse(string s, NumberStyles style, IFormatProvider provider)
147 Assert(16 == Int16
.Parse(" 10 ", NumberStyles
.HexNumber
, Nfi
));
149 Int16
.Parse("$42", NumberStyles
.Integer
, Nfi
);
150 Fail("Should raise a System.FormatException");
152 catch (Exception e
) {
153 Assert(typeof(FormatException
) == e
.GetType());
157 public void TestToString()
160 Assert(String
.Compare(MyString1
, MyInt16_1
.ToString()) == 0);
161 Assert(String
.Compare(MyString2
, MyInt16_2
.ToString()) == 0);
162 Assert(String
.Compare(MyString3
, MyInt16_3
.ToString()) == 0);
163 //test ToString(string format)
165 TODO: These tests are culture sensitive. Need to find a way to determine the culture
166 of the system to decide the correct expected result.
167 for (int i=0; i < Formats1.Length; i++) {
168 Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
169 Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
172 //test ToString(string format, IFormatProvider provider);
173 for (int i
=0; i
< Formats1
.Length
; i
++) {
174 Assert("i="+i
+", ResultsNfi1[i]="+ResultsNfi1
[i
]+", MyInt16_2.ToString(Formats1[i]="+Formats1
[i
]+"): Expected "+ResultsNfi1
[i
]+" but got "+MyInt16_2
.ToString(Formats1
[i
], Nfi
), String
.Compare(ResultsNfi1
[i
], MyInt16_2
.ToString(Formats1
[i
], Nfi
)) == 0);
175 Assert("i="+i
+", ResultsNfi2[i]="+ResultsNfi2
[i
]+", MyInt16_3.ToString(Formats2[i]="+Formats2
[i
]+"): Expected "+ResultsNfi2
[i
]+" but got "+MyInt16_3
.ToString(Formats2
[i
], Nfi
), String
.Compare(ResultsNfi2
[i
], MyInt16_3
.ToString(Formats2
[i
], Nfi
)) == 0);
178 MyInt16_1
.ToString("z");
179 Fail("Should raise a System.FormatException");
181 catch (Exception e
) {
182 Assert(typeof(FormatException
) == e
.GetType());
187 public void ToString_Defaults ()
190 // everything defaults to "G"
191 string def
= i
.ToString ("G");
192 AssertEquals ("ToString()", def
, i
.ToString ());
193 AssertEquals ("ToString((IFormatProvider)null)", def
, i
.ToString ((IFormatProvider
)null));
194 AssertEquals ("ToString((string)null)", def
, i
.ToString ((string)null));
195 AssertEquals ("ToString(empty)", def
, i
.ToString (String
.Empty
));
196 AssertEquals ("ToString(null,null)", def
, i
.ToString (null, null));
197 AssertEquals ("ToString(empty,null)", def
, i
.ToString (String
.Empty
, null));
199 AssertEquals ("ToString(G)", "254", def
);