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
22 public class Int64Test
24 private const Int64 MyInt64_1 = -42;
25 private const Int64 MyInt64_2 = -9223372036854775808;
26 private const Int64 MyInt64_3 = 9223372036854775807;
27 private const string MyString1 = "-42";
28 private const string MyString2 = "-9223372036854775808";
29 private const string MyString3 = "9223372036854775807";
30 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
31 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
32 private string[] Results1 = {"", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
33 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
34 private string[] Results2 = {"", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
35 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
36 private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,808.00)", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
37 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
38 private string[] ResultsNfi2 = {""+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,807.00000", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
39 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
42 = { 0, Int64.MaxValue, Int64.MinValue,
43 1L, 12L, 123L, 1234L, -123L,
44 1234567890123456L, 6543210987654321L };
46 private const long val1 = -1234567L;
47 private const long val2 = 1234567L;
48 private const string sval1Test1 = " -1,234,567 ";
49 private const string sval1Test2 = " -1234567 ";
50 //private const string sval1Test3 = " -12345,,,,67 "; // interesting: this case works on SDK Beta2, but the specification says nothing about this case
51 private const string sval1Test4 = " -12345 67 ";
52 private string sval1Test5 = " -"+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00 ";
53 private string sval1Test6 = "("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00)";
54 private const string sval1Test7 = "-1,234,567.00";
55 private const string sval1UserCur1 = "1234/5/67:000 XYZ-";
56 private const string sval2UserCur1 = "1234/5/67:000 XYZ";
57 private const string sval1UserPercent1 = "-%%%1~2~3~4~5~6~7~0~0;0";
58 private const string sval2UserPercent1 = "%%%1~2~3~4~5~6~7~0~0;0";
59 private const NumberStyles style1 = NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign
60 | NumberStyles.AllowTrailingWhite | NumberStyles.AllowThousands;
61 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
62 private NumberFormatInfo NfiUser;
64 private CultureInfo old_culture;
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 = "%%%";
107 public void TearDown ()
109 Thread.CurrentThread.CurrentCulture = old_culture;
113 public void TestMinMax()
116 Assert.AreEqual(Int64.MinValue, MyInt64_2);
117 Assert.AreEqual(Int64.MaxValue, MyInt64_3);
121 public void TestCompareTo()
123 Assert.IsTrue(MyInt64_3.CompareTo(MyInt64_2) > 0);
124 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_2) == 0);
125 Assert.IsTrue(MyInt64_1.CompareTo((object)(Int64)(-42)) == 0);
126 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_3) < 0);
128 MyInt64_2.CompareTo((object)(Int16)100);
129 Assert.Fail("Should raise a System.ArgumentException");
131 catch (Exception e) {
132 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
137 public void TestEquals()
139 Assert.IsTrue(MyInt64_1.Equals(MyInt64_1));
140 Assert.IsTrue(MyInt64_1.Equals((object)(Int64)(-42)));
141 Assert.IsTrue(MyInt64_1.Equals((object)(SByte)(-42)) == false);
142 Assert.IsTrue(MyInt64_1.Equals(MyInt64_2) == false);
146 public void TestGetHashCode()
149 MyInt64_1.GetHashCode();
150 MyInt64_2.GetHashCode();
151 MyInt64_3.GetHashCode();
154 Assert.Fail("GetHashCode should not raise an exception here");
159 public void TestRoundTripGeneral()
161 foreach(long lv in vals)
163 string s = lv.ToString(Nfi);
164 long lv2 = Int64.Parse(s);
165 Assert.IsTrue(lv == lv2);
166 long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
167 Assert.IsTrue(lv == lv3);
172 public void TestRoundTripHex()
174 foreach(long lv in vals)
176 string s = lv.ToString("x", Nfi);
177 long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
178 Assert.IsTrue(lv == lv2);
183 public void TestParseNull()
188 Assert.Fail("Should raise System.ArgumentNullException");
190 catch (ArgumentNullException)
197 public void TestParse()
201 lv = Int64.Parse(sval1Test1, style1, Nfi);
202 Assert.AreEqual(val1, lv, "Long value should be equal for Test1");
206 lv = Int64.Parse(sval1Test1, Nfi);
207 Assert.Fail("Should raise FormatException 1");
209 catch (FormatException)
214 lv = Int64.Parse(sval1Test2, style1, Nfi);
215 Assert.AreEqual(val1, lv, "Value should be the same for Test2 with style1");
216 lv = Int64.Parse(sval1Test2, Nfi);
217 Assert.AreEqual(val1, lv, "Value should be the same for Test2 without style1");
221 lv = Int64.Parse(sval1Test4, style1, Nfi);
222 Assert.Fail("Should raise FormatException 3");
224 catch (FormatException)
229 lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
230 Assert.AreEqual(val1, lv, "Value should be the same for Test5 and currency style");
232 //test Parse(string s)
233 Assert.IsTrue(MyInt64_1 == Int64.Parse(MyString1));
234 Assert.IsTrue(MyInt64_2 == Int64.Parse(MyString2));
235 Assert.IsTrue(MyInt64_3 == Int64.Parse(MyString3));
238 Assert.Fail("#1:Should raise a System.ArgumentNullException");
240 catch (Exception e) {
241 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "#2");
244 Int64.Parse("not-a-number");
245 Assert.Fail("#3:Should raise a System.FormatException");
247 catch (Exception e) {
248 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#4");
250 //test Parse(string s, NumberStyles style)
252 double OverInt = (double)Int64.MaxValue + 1;
253 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
254 Assert.Fail("#5:Should raise a System.OverflowException");
256 catch (Exception e) {
257 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#6");
260 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
261 Assert.Fail("#7:Should raise a System.OverflowException");
263 catch (Exception e) {
264 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#8");
267 double OverInt = (double)Int64.MaxValue + 1;
268 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
269 Assert.Fail("#9:Should raise a System.FormatException");
271 catch (Exception e) {
272 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#10");
274 Assert.AreEqual((long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency), "A1");
276 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
277 Assert.Fail("#11:Should raise a System.FormatException");
279 catch (Exception e) {
280 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#12");
282 //test Parse(string s, IFormatProvider provider)
283 Assert.IsTrue(-42 == Int64.Parse(" -42 ", Nfi), "A2");
285 Int64.Parse("%42", Nfi);
286 Assert.Fail("#13:Should raise a System.FormatException");
288 catch (Exception e) {
289 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#14");
291 //test Parse(string s, NumberStyles style, IFormatProvider provider)
292 Assert.IsTrue(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "A3");
294 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
295 Assert.Fail("#15:Should raise a System.FormatException");
297 catch (Exception e) {
298 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#16");
301 long.Parse ("9223372036854775808");
302 Assert.Fail ("#17:should raise an OverflowException");
303 } catch (Exception e) {
304 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#18");
307 long.Parse ("9223372036854775808", CultureInfo.InvariantCulture);
308 Assert.Fail ("#19:should raise an OverflowException");
309 } catch (Exception e) {
310 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#20");
315 public void TestToString()
319 s = val1.ToString("c", Nfi);
320 Assert.IsTrue(s.Equals(sval1Test6), "val1 does not become sval1Test6");
322 s = val1.ToString("n", Nfi);
323 Assert.AreEqual(sval1Test7, s, "val1 does not become sval1Test7");
326 Assert.AreEqual(MyString1, MyInt64_1.ToString(), "MyInt64_1.ToString()");
327 Assert.AreEqual(MyString2, MyInt64_2.ToString(), "MyInt64_2.ToString()");
328 Assert.AreEqual(MyString3, MyInt64_3.ToString(), "MyInt64_3.ToString()");
329 //test ToString(string format)
330 for (int i=0; i < Formats1.Length; i++) {
331 Assert.AreEqual(Results1[i], MyInt64_2.ToString(Formats1[i]), "MyInt64_2.ToString(Formats1["+i+"])");
332 Assert.AreEqual(Results2[i], MyInt64_3.ToString(Formats2[i]), "MyInt64_3.ToString(Formats2["+i+"])");
334 //test ToString(string format, IFormatProvider provider);
335 for (int i=0; i < Formats1.Length; i++) {
336 Assert.AreEqual(ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi), "MyInt64_2.ToString(Formats1["+i+"], Nfi)");
337 Assert.AreEqual(ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi), "MyInt64_3.ToString(Formats2["+i+"], Nfi)");
340 MyInt64_1.ToString("z");
341 Assert.Fail("Should raise a System.FormatException");
343 catch (Exception e) {
344 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is wrong type");
349 public void TestUserCurrency()
355 s = val1.ToString("c", NfiUser);
357 Assert.AreEqual(sval1UserCur1, s, "Currency value type 1 is not what we want to try to parse");
359 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
361 Assert.IsTrue(v == val1);
362 } catch (Exception e) {
363 Assert.Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
368 s = val2.ToString("c", NfiUser);
370 Assert.AreEqual(sval2UserCur1, s, "Currency value type 2 is not what we want to try to parse");
372 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
374 Assert.IsTrue(v == val2);
375 } catch (Exception e) {
376 Assert.Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
381 public void TestUserPercent()
385 s = val1.ToString("p", NfiUser);
386 Assert.IsTrue(s.Equals(sval1UserPercent1));
388 s = val2.ToString("p", NfiUser);
389 Assert.IsTrue(s.Equals(sval2UserPercent1));
393 public void Parse_MaxValue ()
395 Assert.AreEqual (Int64.MaxValue, Int64.Parse ("9223372036854775807"), "9223372036854775807");
399 public void Parse_MinValue ()
401 Assert.AreEqual (Int64.MinValue, Int64.Parse ("-9223372036854775808"), "-9223372036854775808,10");
405 [ExpectedException (typeof (OverflowException))]
406 public void Parse_OverByOneMaxValue ()
408 Int64.Parse ("9223372036854775808");
412 [ExpectedException (typeof (OverflowException))]
413 public void Parse_WayOverMaxValue ()
415 Int64.Parse ("1" + Int64.MaxValue.ToString ());
419 [ExpectedException (typeof (OverflowException))]
420 public void Parse_OverByOneMinValue ()
422 Int64.Parse ("-9223372036854775809");
426 [ExpectedException (typeof (OverflowException))]
427 public void Parse_WayOverMinValue ()
429 Int64.Parse (Int64.MinValue.ToString () + "1");
433 public void ToString_Defaults ()
436 // everything defaults to "G"
437 string def = i.ToString ("G");
438 Assert.AreEqual (def, i.ToString (), "ToString()");
439 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
440 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
441 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
442 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
443 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
445 Assert.AreEqual ("254", def, "ToString(G)");