1 // Int64Test.cs - NUnit Test Cases for the System.Int64 struct
3 // Author: Martin Weindel (martin.weindel@t-online.de)
5 // (C) Martin Weindel, 2001
7 // tests ToString and Parse function with the culture independent
8 // NumberFormatInfo.InvariantInfo
10 using NUnit.Framework;
12 using System.Globalization;
13 using System.Threading;
16 /// Tests for System.Int64
18 namespace MonoTests.System
21 public class Int64Test : TestCase
23 private const Int64 MyInt64_1 = -42;
24 private const Int64 MyInt64_2 = -9223372036854775808;
25 private const Int64 MyInt64_3 = 9223372036854775807;
26 private const string MyString1 = "-42";
27 private const string MyString2 = "-9223372036854775808";
28 private const string MyString3 = "9223372036854775807";
29 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
30 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
31 private string[] Results1 = {"", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
32 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
33 private string[] Results2 = {"", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
34 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
35 private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,808.00)", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
36 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
37 private string[] ResultsNfi2 = {""+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,807.00000", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
38 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
41 = { 0, Int64.MaxValue, Int64.MinValue,
42 1L, 12L, 123L, 1234L, -123L,
43 1234567890123456L, 6543210987654321L };
45 private const long val1 = -1234567L;
46 private const long val2 = 1234567L;
47 private const string sval1Test1 = " -1,234,567 ";
48 private const string sval1Test2 = " -1234567 ";
49 //private const string sval1Test3 = " -12345,,,,67 "; // interesting: this case works on SDK Beta2, but the specification says nothing about this case
50 private const string sval1Test4 = " -12345 67 ";
51 private string sval1Test5 = " -"+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00 ";
52 private string sval1Test6 = "("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00)";
53 private const string sval1Test7 = "-1,234,567.00";
54 private const string sval1UserCur1 = "1234/5/67:000 XYZ-";
55 private const string sval2UserCur1 = "1234/5/67:000 XYZ";
56 private const string sval1UserPercent1 = "-%%%1~2~3~4~5~6~7~0~0;0";
57 private const string sval2UserPercent1 = "%%%1~2~3~4~5~6~7~0~0;0";
58 private const NumberStyles style1 = NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign
59 | NumberStyles.AllowTrailingWhite | NumberStyles.AllowThousands;
60 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
61 private NumberFormatInfo NfiUser;
65 private CultureInfo old_culture;
67 protected override void SetUp()
69 old_culture = Thread.CurrentThread.CurrentCulture;
71 // Set culture to en-US and don't let the user override.
72 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
74 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
75 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
76 string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
78 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
79 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
81 Results1[0] = "(" + csym + "9,223,372,036,854,775,808" + csuffix + ")";
82 Results1[3] = "-9223372036854775808." + decimals;
83 Results1[5] = "-9,223,372,036,854,775,808." + decimals;
84 Results1[6] = perPattern.Replace ("n","-922,337,203,685,477,580,800.00");
86 Results2[0] = csym + "9,223,372,036,854,775,807.00000";
87 Results2[6] = perPattern.Replace ("n","922,337,203,685,477,580,700.00000");
89 NfiUser = new NumberFormatInfo();
90 NfiUser.CurrencyDecimalDigits = 3;
91 NfiUser.CurrencyDecimalSeparator = ":";
92 NfiUser.CurrencyGroupSeparator = "/";
93 NfiUser.CurrencyGroupSizes = new int[] { 2,1,0 };
94 NfiUser.CurrencyNegativePattern = 10; // n $-
95 NfiUser.CurrencyPositivePattern = 3; // n $
96 NfiUser.CurrencySymbol = "XYZ";
97 NfiUser.PercentDecimalDigits = 1;
98 NfiUser.PercentDecimalSeparator = ";";
99 NfiUser.PercentGroupSeparator = "~";
100 NfiUser.PercentGroupSizes = new int[] {1};
101 NfiUser.PercentNegativePattern = 2;
102 NfiUser.PercentPositivePattern = 2;
103 NfiUser.PercentSymbol = "%%%";
106 protected override void TearDown()
108 Thread.CurrentThread.CurrentCulture = old_culture;
111 public void TestMinMax()
114 AssertEquals(Int64.MinValue, MyInt64_2);
115 AssertEquals(Int64.MaxValue, MyInt64_3);
118 public void TestCompareTo()
120 Assert(MyInt64_3.CompareTo(MyInt64_2) > 0);
121 Assert(MyInt64_2.CompareTo(MyInt64_2) == 0);
122 Assert(MyInt64_1.CompareTo((Int64)(-42)) == 0);
123 Assert(MyInt64_2.CompareTo(MyInt64_3) < 0);
125 MyInt64_2.CompareTo((Int16)100);
126 Fail("Should raise a System.ArgumentException");
128 catch (Exception e) {
129 Assert(typeof(ArgumentException) == e.GetType());
133 public void TestEquals()
135 Assert(MyInt64_1.Equals(MyInt64_1));
136 Assert(MyInt64_1.Equals((Int64)(-42)));
137 Assert(MyInt64_1.Equals((SByte)(-42)) == false);
138 Assert(MyInt64_1.Equals(MyInt64_2) == false);
141 public void TestGetHashCode()
144 MyInt64_1.GetHashCode();
145 MyInt64_2.GetHashCode();
146 MyInt64_3.GetHashCode();
149 Fail("GetHashCode should not raise an exception here");
153 public void TestRoundTripGeneral()
155 foreach(long lv in vals)
157 string s = lv.ToString(Nfi);
158 long lv2 = Int64.Parse(s);
160 long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
165 public void TestRoundTripHex()
167 foreach(long lv in vals)
169 string s = lv.ToString("x", Nfi);
170 long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
175 public void TestParseNull()
180 Fail("Should raise System.ArgumentNullException");
182 catch (ArgumentNullException)
188 public void TestParse()
192 lv = Int64.Parse(sval1Test1, style1, Nfi);
193 AssertEquals("Long value should be equal for Test1", val1, lv);
197 lv = Int64.Parse(sval1Test1, Nfi);
198 Fail("Should raise FormatException 1");
200 catch (FormatException)
205 lv = Int64.Parse(sval1Test2, style1, Nfi);
206 AssertEquals("Value should be the same for Test2 with style1", val1, lv);
207 lv = Int64.Parse(sval1Test2, Nfi);
208 AssertEquals("Value should be the same for Test2 without style1", val1, lv);
212 lv = Int64.Parse(sval1Test4, style1, Nfi);
213 Fail("Should raise FormatException 3");
215 catch (FormatException)
220 lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
221 AssertEquals("Value should be the same for Test5 and currency style", val1, lv);
223 //test Parse(string s)
224 Assert(MyInt64_1 == Int64.Parse(MyString1));
225 Assert(MyInt64_2 == Int64.Parse(MyString2));
226 Assert(MyInt64_3 == Int64.Parse(MyString3));
229 Fail("Should raise a System.ArgumentNullException");
231 catch (Exception e) {
232 Assert(typeof(ArgumentNullException) == e.GetType());
235 Int64.Parse("not-a-number");
236 Fail("Should raise a System.FormatException");
238 catch (Exception e) {
239 Assert(typeof(FormatException) == e.GetType());
241 //test Parse(string s, NumberStyles style)
243 double OverInt = (double)Int64.MaxValue + 1;
244 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
245 Fail("Should raise a System.OverflowException");
247 catch (Exception e) {
248 Assert(typeof(OverflowException) == e.GetType());
251 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
252 Fail("Should raise a System.OverflowException");
254 catch (Exception e) {
255 Assert(typeof(OverflowException) == e.GetType());
258 double OverInt = (double)Int64.MaxValue + 1;
259 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
260 Fail("Should raise a System.FormatException");
262 catch (Exception e) {
263 Assert(typeof(FormatException) == e.GetType());
265 AssertEquals("A1", (long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
267 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
268 Fail("Should raise a System.FormatException");
270 catch (Exception e) {
271 Assert(typeof(FormatException) == e.GetType());
273 //test Parse(string s, IFormatProvider provider)
274 Assert(-42 == Int64.Parse(" -42 ", Nfi));
276 Int64.Parse("%42", Nfi);
277 Fail("Should raise a System.FormatException");
279 catch (Exception e) {
280 Assert(typeof(FormatException) == e.GetType());
282 //test Parse(string s, NumberStyles style, IFormatProvider provider)
283 Assert(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
285 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
286 Fail("Should raise a System.FormatException");
288 catch (Exception e) {
289 Assert(typeof(FormatException) == e.GetType());
293 public void TestToString()
297 s = val1.ToString("c", Nfi);
298 Assert("val1 does not become sval1Test6", s.Equals(sval1Test6));
300 s = val1.ToString("n", Nfi);
301 AssertEquals("val1 does not become sval1Test7", sval1Test7, s);
304 AssertEquals("MyInt64_1.ToString()", MyString1, MyInt64_1.ToString());
305 AssertEquals("MyInt64_2.ToString()", MyString2, MyInt64_2.ToString());
306 AssertEquals("MyInt64_3.ToString()", MyString3, MyInt64_3.ToString());
307 //test ToString(string format)
308 for (int i=0; i < Formats1.Length; i++) {
309 AssertEquals("MyInt64_2.ToString(Formats1["+i+"])", Results1[i], MyInt64_2.ToString(Formats1[i]));
310 AssertEquals("MyInt64_3.ToString(Formats2["+i+"])", Results2[i], MyInt64_3.ToString(Formats2[i]));
312 //test ToString(string format, IFormatProvider provider);
313 for (int i=0; i < Formats1.Length; i++) {
314 AssertEquals("MyInt64_2.ToString(Formats1["+i+"], Nfi)", ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi));
315 AssertEquals("MyInt64_3.ToString(Formats2["+i+"], Nfi)", ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi));
318 MyInt64_1.ToString("z");
319 Fail("Should raise a System.FormatException");
321 catch (Exception e) {
322 AssertEquals("Exception is wrong type", typeof(FormatException), e.GetType());
326 public void TestUserCurrency()
332 s = val1.ToString("c", NfiUser);
334 AssertEquals("Currency value type 1 is not what we want to try to parse", sval1UserCur1, s);
336 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
339 } catch (Exception e) {
340 Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
345 s = val2.ToString("c", NfiUser);
347 AssertEquals("Currency value type 2 is not what we want to try to parse", sval2UserCur1, s);
349 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
352 } catch (Exception e) {
353 Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
357 public void TestUserPercent()
361 s = val1.ToString("p", NfiUser);
362 Assert(s.Equals(sval1UserPercent1));
364 s = val2.ToString("p", NfiUser);
365 Assert(s.Equals(sval2UserPercent1));