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
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;
40 public void SetUpFixture ()
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";
55 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
59 public void TearDown ()
61 Thread.CurrentThread.CurrentCulture = old_culture;
65 public void TestMinMax()
67 Assert.AreEqual(Int16.MinValue, MyInt16_2);
68 Assert.AreEqual(Int16.MaxValue, MyInt16_3);
72 public void TestCompareTo()
74 Assert.AreEqual(65535, MyInt16_3.CompareTo(MyInt16_2), "#1");
75 Assert.AreEqual(0, MyInt16_2.CompareTo(MyInt16_2), "#2");
76 Assert.AreEqual(0, MyInt16_1.CompareTo((Int16)(-42)), "#3");
77 Assert.AreEqual(-65535, MyInt16_2.CompareTo(MyInt16_3), "#4");
79 MyInt16_2.CompareTo((object)100);
80 Assert.Fail ("Should raise a System.ArgumentException");
81 } catch (ArgumentException e) {
86 public void TestEquals()
88 Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
89 Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
90 Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
91 Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
95 public void TestGetHashCode()
98 MyInt16_1.GetHashCode();
99 MyInt16_2.GetHashCode();
100 MyInt16_3.GetHashCode();
103 Assert.Fail ("GetHashCode should not raise an exception here");
108 public void TestParse()
110 //test Parse(string s)
111 Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
112 Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
113 Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
116 Assert.Fail ("Should raise a System.ArgumentNullException");
118 catch (Exception e) {
119 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
122 Int16.Parse("not-a-number");
123 Assert.Fail ("Should raise a System.FormatException");
125 catch (Exception e) {
126 Assert.IsTrue(typeof(FormatException) == e.GetType());
129 int OverInt = Int16.MaxValue + 1;
130 Int16.Parse(OverInt.ToString());
131 Assert.Fail ("Should raise a System.OverflowException");
133 catch (Exception e) {
134 Assert.IsTrue(typeof(OverflowException) == e.GetType());
136 //test Parse(string s, NumberStyles style)
137 Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
139 Int16.Parse("$42", NumberStyles.Integer);
140 Assert.Fail ("Should raise a System.FormatException");
142 catch (Exception e) {
143 Assert.IsTrue(typeof(FormatException) == e.GetType());
145 //test Parse(string s, IFormatProvider provider)
146 Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
148 Int16.Parse("%42", Nfi);
149 Assert.Fail ("Should raise a System.FormatException");
151 catch (Exception e) {
152 Assert.IsTrue(typeof(FormatException) == e.GetType());
154 //test Parse(string s, NumberStyles style, IFormatProvider provider)
155 Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
157 Int16.Parse("$42", NumberStyles.Integer, Nfi);
158 Assert.Fail ("Should raise a System.FormatException");
160 catch (Exception e) {
161 Assert.IsTrue(typeof(FormatException) == e.GetType());
164 Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1");
165 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 \0"), "#2");
166 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 "), "#3");
167 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
169 Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
173 public void TestParseExponent ()
175 Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
176 Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
177 Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
178 Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
179 Assert.AreEqual (2, Int16.Parse ("2", NumberStyles.AllowExponent), "A#5");
184 } catch (FormatException) {
188 Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
190 } catch (FormatException) {
194 Int16.Parse ("2E 2", NumberStyles.AllowExponent);
196 } catch (FormatException) {
200 Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
202 } catch (FormatException) {
206 Int16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
208 } catch (OverflowException) {
212 long exponent = (long) Int32.MaxValue + 10;
213 Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
215 } catch (OverflowException) {
219 Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
221 } catch (OverflowException) {
225 Int16.Parse ("2 math e1", NumberStyles.AllowExponent);
227 } catch (FormatException) {
232 public void Parse_MinMax ()
234 Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
235 Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
236 Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
240 public void TestToString()
243 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
244 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
245 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
246 //test ToString(string format)
248 TODO: These tests are culture sensitive. Need to find a way to determine the culture
249 of the system to decide the correct expected result.
250 for (int i=0; i < Formats1.Length; i++) {
251 Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
252 Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
255 //test ToString(string format, IFormatProvider provider);
256 for (int i=0; i < Formats1.Length; i++) {
257 Assert.IsTrue(String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0, "i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MyInt16_2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MyInt16_2.ToString(Formats1[i], Nfi));
258 Assert.IsTrue(String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0, "i="+i+", ResultsNfi2[i]="+ResultsNfi2[i]+", MyInt16_3.ToString(Formats2[i]="+Formats2[i]+"): Expected "+ResultsNfi2[i]+" but got "+MyInt16_3.ToString(Formats2[i], Nfi));
261 MyInt16_1.ToString("z");
262 Assert.Fail ("Should raise a System.FormatException");
264 catch (Exception e) {
265 Assert.IsTrue(typeof(FormatException) == e.GetType());
270 public void ToString_Defaults ()
273 // everything defaults to "G"
274 string def = i.ToString ("G");
275 Assert.AreEqual (def, i.ToString (), "ToString()");
276 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
277 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
278 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
279 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
280 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
282 Assert.AreEqual ("254", def, "ToString(G)");
286 public void Bug3677 ()
288 Assert.AreEqual (-7197, short.Parse("E3E3", NumberStyles.HexNumber), "HexNumber");