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;
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;
59 public void TestMinMax()
61 Assert.AreEqual(Int16.MinValue, MyInt16_2);
62 Assert.AreEqual(Int16.MaxValue, MyInt16_3);
66 public void TestCompareTo()
68 Assert.IsTrue(MyInt16_3.CompareTo(MyInt16_2) > 0);
69 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_2) == 0);
70 Assert.IsTrue(MyInt16_1.CompareTo((Int16)(-42)) == 0);
71 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_3) < 0);
73 MyInt16_2.CompareTo((object)100);
74 Assert.Fail ("Should raise a System.ArgumentException");
77 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
82 public void TestEquals()
84 Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
85 Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
86 Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
87 Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
91 public void TestGetHashCode()
94 MyInt16_1.GetHashCode();
95 MyInt16_2.GetHashCode();
96 MyInt16_3.GetHashCode();
99 Assert.Fail ("GetHashCode should not raise an exception here");
104 public void TestParse()
106 //test Parse(string s)
107 Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
108 Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
109 Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
112 Assert.Fail ("Should raise a System.ArgumentNullException");
114 catch (Exception e) {
115 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
118 Int16.Parse("not-a-number");
119 Assert.Fail ("Should raise a System.FormatException");
121 catch (Exception e) {
122 Assert.IsTrue(typeof(FormatException) == e.GetType());
125 int OverInt = Int16.MaxValue + 1;
126 Int16.Parse(OverInt.ToString());
127 Assert.Fail ("Should raise a System.OverflowException");
129 catch (Exception e) {
130 Assert.IsTrue(typeof(OverflowException) == e.GetType());
132 //test Parse(string s, NumberStyles style)
133 Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
135 Int16.Parse("$42", NumberStyles.Integer);
136 Assert.Fail ("Should raise a System.FormatException");
138 catch (Exception e) {
139 Assert.IsTrue(typeof(FormatException) == e.GetType());
141 //test Parse(string s, IFormatProvider provider)
142 Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
144 Int16.Parse("%42", Nfi);
145 Assert.Fail ("Should raise a System.FormatException");
147 catch (Exception e) {
148 Assert.IsTrue(typeof(FormatException) == e.GetType());
150 //test Parse(string s, NumberStyles style, IFormatProvider provider)
151 Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
153 Int16.Parse("$42", NumberStyles.Integer, Nfi);
154 Assert.Fail ("Should raise a System.FormatException");
156 catch (Exception e) {
157 Assert.IsTrue(typeof(FormatException) == e.GetType());
160 Assert.AreEqual (7345, Int64.Parse ("7345\0"), "#1");
161 Assert.AreEqual (7345, Int64.Parse ("7345\0\0\0 \0"), "#2");
162 Assert.AreEqual (7345, Int64.Parse ("7345\0\0\0 "), "#3");
163 Assert.AreEqual (7345, Int64.Parse ("7345\0\0\0"), "#4");
167 public void TestToString()
170 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
171 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
172 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
173 //test ToString(string format)
175 TODO: These tests are culture sensitive. Need to find a way to determine the culture
176 of the system to decide the correct expected result.
177 for (int i=0; i < Formats1.Length; i++) {
178 Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
179 Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
182 //test ToString(string format, IFormatProvider provider);
183 for (int i=0; i < Formats1.Length; i++) {
184 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));
185 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));
188 MyInt16_1.ToString("z");
189 Assert.Fail ("Should raise a System.FormatException");
191 catch (Exception e) {
192 Assert.IsTrue(typeof(FormatException) == e.GetType());
197 public void ToString_Defaults ()
200 // everything defaults to "G"
201 string def = i.ToString ("G");
202 Assert.AreEqual (def, i.ToString (), "ToString()");
203 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
204 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
205 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
206 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
207 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
209 Assert.AreEqual ("254", def, "ToString(G)");