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.IsTrue(MyInt16_3.CompareTo(MyInt16_2) > 0);
75 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_2) == 0);
76 Assert.IsTrue(MyInt16_1.CompareTo((Int16)(-42)) == 0);
77 Assert.IsTrue(MyInt16_2.CompareTo(MyInt16_3) < 0);
79 MyInt16_2.CompareTo((object)100);
80 Assert.Fail ("Should raise a System.ArgumentException");
83 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
88 public void TestEquals()
90 Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
91 Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
92 Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
93 Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
97 public void TestGetHashCode()
100 MyInt16_1.GetHashCode();
101 MyInt16_2.GetHashCode();
102 MyInt16_3.GetHashCode();
105 Assert.Fail ("GetHashCode should not raise an exception here");
110 public void TestParse()
112 //test Parse(string s)
113 Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
114 Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
115 Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
118 Assert.Fail ("Should raise a System.ArgumentNullException");
120 catch (Exception e) {
121 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
124 Int16.Parse("not-a-number");
125 Assert.Fail ("Should raise a System.FormatException");
127 catch (Exception e) {
128 Assert.IsTrue(typeof(FormatException) == e.GetType());
131 int OverInt = Int16.MaxValue + 1;
132 Int16.Parse(OverInt.ToString());
133 Assert.Fail ("Should raise a System.OverflowException");
135 catch (Exception e) {
136 Assert.IsTrue(typeof(OverflowException) == e.GetType());
138 //test Parse(string s, NumberStyles style)
139 Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
141 Int16.Parse("$42", NumberStyles.Integer);
142 Assert.Fail ("Should raise a System.FormatException");
144 catch (Exception e) {
145 Assert.IsTrue(typeof(FormatException) == e.GetType());
147 //test Parse(string s, IFormatProvider provider)
148 Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
150 Int16.Parse("%42", Nfi);
151 Assert.Fail ("Should raise a System.FormatException");
153 catch (Exception e) {
154 Assert.IsTrue(typeof(FormatException) == e.GetType());
156 //test Parse(string s, NumberStyles style, IFormatProvider provider)
157 Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
159 Int16.Parse("$42", NumberStyles.Integer, Nfi);
160 Assert.Fail ("Should raise a System.FormatException");
162 catch (Exception e) {
163 Assert.IsTrue(typeof(FormatException) == e.GetType());
166 Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1");
167 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 \0"), "#2");
168 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 "), "#3");
169 Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
171 Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
175 public void TestParseExponent ()
177 Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
178 Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
179 Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
180 Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
185 } catch (FormatException) {
189 Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
191 } catch (FormatException) {
195 Int16.Parse ("2E 2", NumberStyles.AllowExponent);
197 } catch (FormatException) {
201 Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
203 } catch (FormatException) {
207 Int16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
209 } catch (OverflowException) {
213 long exponent = (long) Int32.MaxValue + 10;
214 Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
216 } catch (OverflowException) {
220 Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
222 } catch (OverflowException) {
227 public void Parse_MinMax ()
229 Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
230 Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
231 Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
235 public void TestToString()
238 Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
239 Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
240 Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
241 //test ToString(string format)
243 TODO: These tests are culture sensitive. Need to find a way to determine the culture
244 of the system to decide the correct expected result.
245 for (int i=0; i < Formats1.Length; i++) {
246 Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
247 Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
250 //test ToString(string format, IFormatProvider provider);
251 for (int i=0; i < Formats1.Length; i++) {
252 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));
253 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));
256 MyInt16_1.ToString("z");
257 Assert.Fail ("Should raise a System.FormatException");
259 catch (Exception e) {
260 Assert.IsTrue(typeof(FormatException) == e.GetType());
265 public void ToString_Defaults ()
268 // everything defaults to "G"
269 string def = i.ToString ("G");
270 Assert.AreEqual (def, i.ToString (), "ToString()");
271 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
272 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
273 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
274 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
275 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
277 Assert.AreEqual ("254", def, "ToString(G)");