Implemented overloaded versions of Parse and TryParse functions for BigInteger.
[mono.git] / mcs / class / corlib / Test / System / DoubleTest.cs
1 // DoubleTest.cs - NUnit Test Cases for the System.Double class
2 //
3 // Bob Doan <bdoan@sicompos.com>
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
8
9 using System;
10 using System.Collections;
11 using System.Globalization;
12 using System.Threading;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System
17 {
18         [TestFixture]
19         public class DoubleTest
20         {
21                 private const Double d_zero = 0.0;
22                 private const Double d_neg = -1234.5678;
23                 private const Double d_pos = 1234.9999;
24                 private const Double d_pos2 = 1234.9999;
25                 private const Double d_nan = Double.NaN;
26                 private const Double d_pinf = Double.PositiveInfinity;
27                 private const Double d_ninf = Double.NegativeInfinity;
28                 private const String s = "What Ever";
29                 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
30
31                 private string [] string_values;
32                 private string [] string_values_fail = {
33                         "",     // empty
34                         "- 1.0", // Inner whitespace
35                         "3 5" // Inner whitespace 2
36                 };
37
38                 private double [] double_values = {
39                         1, .1, 1.1, -12, 44.444432, .000021121,
40                         .00001, .223, -221.3233,
41                         1.7976931348623157e308, 1.7976931348623157e308, -1.7976931348623157e308,
42                         4.9406564584124650e-324,
43                         6.28318530717958647692528676655900577,
44                         1e-05,
45                         0,
46                 };
47
48                 [SetUp]
49                 public void Setup ()
50                 {
51                         string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
52                         string_values = new string [15];
53                         string_values [0] = "1";
54                         string_values [1] = sep + "1";
55                         string_values [2] = "1" + sep + "1";
56                         string_values [3] = "-12";
57                         string_values [4] = "44" + sep + "444432";
58                         string_values [5] = sep + "000021121";
59                         string_values [6] = "   " + sep + "00001";
60                         string_values [7] = "  " + sep + "223    ";
61                         string_values [8] = "         -221" + sep + "3233";
62                         string_values [9] = " 1" + sep + "7976931348623157e308 ";
63                         string_values [10] = "+1" + sep + "7976931348623157E308";
64                         string_values [11] = "-1" + sep + "7976931348623157e308";
65                         string_values [12] = "4" + sep + "9406564584124650e-324";
66                         string_values [13] = "6" + sep + "28318530717958647692528676655900577";
67                         string_values [14] = "1e-05";
68                 }
69
70                 [Test]
71                 public void PublicFields ()
72                 {
73                         Assert.AreEqual (3.9406564584124654e-324, Double.Epsilon, "#1");
74                         Assert.AreEqual (1.7976931348623157e+308, Double.MaxValue, "#2");
75                         Assert.AreEqual (-1.7976931348623157e+308, Double.MinValue, "#3");
76                         Assert.AreEqual ((double) -1.0 / (double) (0.0), Double.NegativeInfinity, "#4");
77                         Assert.AreEqual ((double) 1.0 / (double) (0.0), Double.PositiveInfinity, "#5");
78                 }
79
80                 [Test]
81                 public void CompareTo ()
82                 {
83                         //If you do int foo =  d_ninf.CompareTo(d_pinf); Assertion.Assert(".." foo < 0, true) this works.... WHY???
84                         Assert.IsTrue (d_ninf.CompareTo (d_pinf) < 0, "#A1");
85                         Assert.IsTrue (d_neg.CompareTo (d_pos) < 0, "#A2");
86                         Assert.IsTrue (d_nan.CompareTo (d_neg) < 0, "#A3");
87
88                         Assert.AreEqual (0, d_pos.CompareTo (d_pos2), "#B1");
89                         Assert.AreEqual (0, d_pinf.CompareTo (d_pinf), "#B2");
90                         Assert.AreEqual (0, d_ninf.CompareTo (d_ninf), "#B3");
91                         Assert.AreEqual (0, d_nan.CompareTo (d_nan), "#B4");
92
93                         Assert.IsTrue (d_pos.CompareTo (d_neg) > 0, "#C1");
94                         Assert.IsTrue (d_pos.CompareTo (d_nan) > 0, "#C2");
95                         Assert.IsTrue (d_pos.CompareTo (null) > 0, "#C3");
96
97                         try {
98                                 d_pos.CompareTo (s);
99                                 Assert.Fail ("#D1");
100                         } catch (ArgumentException ex) {
101                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
102                         }
103                 }
104
105                 [Test]
106                 public void Equals ()
107                 {
108                         Assert.IsTrue (d_pos.Equals (d_pos2), "#1");
109                         Assert.IsFalse (d_pos.Equals (d_neg), "#2");
110                         Assert.IsFalse (d_pos.Equals (s), "#3");
111                 }
112
113                 [Test]
114                 public void TestTypeCode ()
115                 {
116                         Assert.AreEqual (TypeCode.Double, d_pos.GetTypeCode ());
117                 }
118
119                 [Test]
120                 public void IsInfinity ()
121                 {
122                         Assert.IsTrue (Double.IsInfinity (Double.PositiveInfinity), "#1");
123                         Assert.IsTrue (Double.IsInfinity (Double.NegativeInfinity), "#2");
124                         Assert.IsFalse (Double.IsInfinity (12), "#3");
125                 }
126
127                 [Test]
128                 public void IsNan ()
129                 {
130                         Assert.IsTrue (Double.IsNaN (Double.NaN), "#1");
131                         Assert.IsFalse (Double.IsNaN (12), "#2");
132                         Assert.IsFalse (Double.IsNaN (Double.PositiveInfinity), "#3");
133                 }
134
135                 [Test]
136                 public void IsNegativeInfinity ()
137                 {
138                         Assert.IsTrue (Double.IsNegativeInfinity (Double.NegativeInfinity), "#1");
139                         Assert.IsFalse (Double.IsNegativeInfinity (12), "#2");
140                 }
141
142                 [Test]
143                 public void IsPositiveInfinity ()
144                 {
145                         Assert.IsTrue (Double.IsPositiveInfinity (Double.PositiveInfinity), "#1");
146                         Assert.IsFalse (Double.IsPositiveInfinity (12), "#2");
147                 }
148
149                 [Test]
150                 public void Parse ()
151                 {
152                         int i = 0;
153                         try {
154                                 for (i = 0; i < string_values.Length; i++) {
155                                         Assert.AreEqual (double_values [i], Double.Parse (string_values [i]), "#A1");
156                                 }
157                         } catch (Exception e) {
158                                 Assert.Fail ("#A2: i=" + i + " failed with e = " + e.ToString ());
159                         }
160
161                         try {
162                                 Assert.AreEqual (10.1111, Double.Parse (" 10.1111 ", NumberStyles.Float, Nfi), "#B1");
163                         } catch (Exception e) {
164                                 Assert.Fail ("#B2: Parse Failed NumberStyles.Float with e = " + e.ToString ());
165                         }
166
167                         try {
168                                 Assert.AreEqual (1234.5678, Double.Parse ("1,234.5678", NumberStyles.Float | NumberStyles.AllowThousands, Nfi), "#C1");
169                         } catch (Exception e) {
170                                 Assert.Fail ("#C2: Parse Failed NumberStyles.AllowThousands with e = " + e.ToString ());
171                         }
172
173                         try {
174                                 Double.Parse (null);
175                                 Assert.Fail ("#D1");
176                         } catch (ArgumentNullException ex) {
177                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
178                                 Assert.IsNull (ex.InnerException, "#D3");
179                                 Assert.IsNotNull (ex.Message, "#D4");
180                                 Assert.IsNotNull (ex.ParamName, "#D5");
181                         }
182
183                         try {
184                                 Double.Parse ("save the elk");
185                                 Assert.Fail ("#E1");
186                         } catch (FormatException ex) {
187                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#E2");
188                                 Assert.IsNull (ex.InnerException, "#E3");
189                                 Assert.IsNotNull (ex.Message, "#E4");
190                         }
191
192                         string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
193                         double ovf_plus = 0;
194                         try {
195                                 ovf_plus = Double.Parse ("1" + sep + "79769313486232e308");
196                                 Assert.Fail ("#F1");
197                         } catch (OverflowException ex) {
198                                 Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#F2");
199                                 Assert.IsNull (ex.InnerException, "#F3");
200                                 Assert.IsNotNull (ex.Message, "#F4");
201                         }
202
203                         try {
204                                 Double.Parse ("-1" + sep + "79769313486232e308");
205                                 Assert.Fail ("#G1");
206                         } catch (OverflowException ex) {
207                                 Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#G2");
208                                 Assert.IsNull (ex.InnerException, "#G3");
209                                 Assert.IsNotNull (ex.Message, "#G4");
210                         }
211
212                         for (i = 0; i < string_values_fail.Length; ++i) {
213                                 try {
214                                         Double.Parse (string_values_fail [i]);
215                                         Assert.Fail ("#H1: " + string_values_fail [i]);
216                                 } catch (FormatException ex) {
217                                         Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
218                                         Assert.IsNull (ex.InnerException, "#H3");
219                                         Assert.IsNotNull (ex.Message, "#H4");
220                                 }
221                         }
222                 }
223
224                 [Test]
225                 public void ParseAllowWhitespaces ()
226                 {
227                         var nf = CultureInfo.CurrentCulture.NumberFormat;
228                         NumberStyles style = NumberStyles.Float;
229                         double.Parse (" 32 ");
230                         double.Parse (string.Format ("  {0}  ", nf.PositiveInfinitySymbol));
231                         double.Parse (string.Format ("  {0}  ", nf.NegativeInfinitySymbol));
232                         double.Parse (string.Format ("  {0}  ", nf.NaNSymbol));
233                 }
234
235                 [Test] // bug #81630
236                 public void Parse_Whitespace ()
237                 {
238                         try {
239                                 double.Parse (" ");
240                                 Assert.Fail ("#1");
241                         } catch (FormatException ex) {
242                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2");
243                                 Assert.IsNull (ex.InnerException, "#3");
244                                 Assert.IsNotNull (ex.Message, "#4");
245                         }
246                 }
247
248                 [Test] // //bug #81777
249                 public void Parse_TrailingGarbage ()
250                 {
251                         try {
252                                 double.Parse ("22 foo");
253                                 Assert.Fail ("#1");
254                         } catch (FormatException ex) {
255                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2");
256                                 Assert.IsNull (ex.InnerException, "#3");
257                                 Assert.IsNotNull (ex.Message, "#4");
258                         }
259                 }
260
261                 [Test]
262                 public void Parse_Infinity ()
263                 {
264                         double value;
265                         IFormatProvider german = new CultureInfo ("de-DE");
266                         var res = double.Parse ("+unendlich", NumberStyles.Float, german);
267                         Assert.AreEqual (double.PositiveInfinity, res);
268                 }
269
270                 [Test]
271                 public void TestToString ()
272                 {
273                         try {
274                                 double d = 3.1415;
275                                 d.ToString ("X");
276                                 Assert.Fail ("#A1");
277                         } catch (FormatException ex) {
278                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#A2");
279                                 Assert.IsNull (ex.InnerException, "#A3");
280                                 Assert.IsNotNull (ex.Message, "#A4");
281                         }
282
283                         try {
284                                 double d = 3.1415;
285                                 d.ToString ("D");
286                                 Assert.Fail ("#B1");
287                         } catch (FormatException ex) {
288                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
289                                 Assert.IsNull (ex.InnerException, "#B3");
290                                 Assert.IsNotNull (ex.Message, "#B4");
291                         }
292
293                         CustomFormatHelper ();
294                 }
295
296                 private class Element
297                 {
298                         public double value;
299                         public string format;
300                         public string result;
301
302                         public Element (double value, string format, string result)
303                         {
304                                 this.value = value;
305                                 this.format = format;
306                                 this.result = result;
307                         }
308                 }
309
310                 public void CustomFormatHelper ()
311                 {
312                         NumberFormatInfo nfi = new NumberFormatInfo ();
313
314                         nfi.NaNSymbol = "NaN";
315                         nfi.PositiveSign = "+";
316                         nfi.NegativeSign = "-";
317                         nfi.PerMilleSymbol = "x";
318                         nfi.PositiveInfinitySymbol = "Infinity";
319                         nfi.NegativeInfinitySymbol = "-Infinity";
320
321                         nfi.NumberDecimalDigits = 5;
322                         nfi.NumberDecimalSeparator = ".";
323                         nfi.NumberGroupSeparator = ",";
324                         nfi.NumberGroupSizes = new int [] { 3 };
325                         nfi.NumberNegativePattern = 2;
326
327                         nfi.CurrencyDecimalDigits = 2;
328                         nfi.CurrencyDecimalSeparator = ".";
329                         nfi.CurrencyGroupSeparator = ",";
330                         nfi.CurrencyGroupSizes = new int [] { 3 };
331                         nfi.CurrencyNegativePattern = 8;
332                         nfi.CurrencyPositivePattern = 3;
333                         nfi.CurrencySymbol = "$";
334
335                         nfi.PercentDecimalDigits = 5;
336                         nfi.PercentDecimalSeparator = ".";
337                         nfi.PercentGroupSeparator = ",";
338                         nfi.PercentGroupSizes = new int [] { 3 };
339                         nfi.PercentNegativePattern = 0;
340                         nfi.PercentPositivePattern = 0;
341                         nfi.PercentSymbol = "%";
342
343                         ArrayList list = new ArrayList ();
344                         list.Add (new Element (123d, "#####", "123"));
345                         list.Add (new Element (123d, "00000", "00123"));
346                         list.Add (new Element (123d, "(###) ### - ####", "()  - 123"));
347                         list.Add (new Element (123d, "#.##", "123"));
348                         list.Add (new Element (123d, "0.00", "123.00"));
349                         list.Add (new Element (123d, "00.00", "123.00"));
350                         list.Add (new Element (123d, "#,#", "123"));
351                         list.Add (new Element (123d, "#,,", ""));
352                         list.Add (new Element (123d, "#,,,", ""));
353                         list.Add (new Element (123d, "#,##0,,", "0"));
354                         list.Add (new Element (123d, "#0.##%", "12300%"));
355                         list.Add (new Element (123d, "0.###E+0", "1.23E+2"));
356                         list.Add (new Element (123d, "0.###E+000", "1.23E+002"));
357                         list.Add (new Element (123d, "0.###E-000", "1.23E002"));
358                         list.Add (new Element (123d, "[##-##-##]", "[-1-23]"));
359                         list.Add (new Element (123d, "##;(##)", "123"));
360                         list.Add (new Element (123d, "##;(##)", "123"));
361                         list.Add (new Element (1234567890d, "#####", "1234567890"));
362                         list.Add (new Element (1234567890d, "00000", "1234567890"));
363                         list.Add (new Element (1234567890d,
364                                                 "(###) ### - ####", "(123) 456 - 7890"));
365                         list.Add (new Element (1234567890d, "#.##", "1234567890"));
366                         list.Add (new Element (1234567890d, "0.00", "1234567890.00"));
367                         list.Add (new Element (1234567890d, "00.00", "1234567890.00"));
368                         list.Add (new Element (1234567890d, "#,#", "1,234,567,890"));
369                         list.Add (new Element (1234567890d, "#,,", "1235"));
370                         list.Add (new Element (1234567890d, "#,,,", "1"));
371                         list.Add (new Element (1234567890d, "#,##0,,", "1,235"));
372                         list.Add (new Element (1234567890d, "#0.##%", "123456789000%"));
373                         list.Add (new Element (1234567890d, "0.###E+0", "1.235E+9"));
374                         list.Add (new Element (1234567890d, "0.###E+000", "1.235E+009"));
375                         list.Add (new Element (1234567890d, "0.###E-000", "1.235E009"));
376                         list.Add (new Element (1234567890d, "[##-##-##]", "[123456-78-90]"));
377                         list.Add (new Element (1234567890d, "##;(##)", "1234567890"));
378                         list.Add (new Element (1234567890d, "##;(##)", "1234567890"));
379                         list.Add (new Element (1.2d, "#####", "1"));
380                         list.Add (new Element (1.2d, "00000", "00001"));
381                         list.Add (new Element (1.2d, "(###) ### - ####", "()  - 1"));
382                         list.Add (new Element (1.2d, "#.##", "1.2"));
383                         list.Add (new Element (1.2d, "0.00", "1.20"));
384                         list.Add (new Element (1.2d, "00.00", "01.20"));
385                         list.Add (new Element (1.2d, "#,#", "1"));
386                         list.Add (new Element (1.2d, "#,,", ""));
387                         list.Add (new Element (1.2d, "#,,,", ""));
388                         list.Add (new Element (1.2d, "#,##0,,", "0"));
389                         list.Add (new Element (1.2d, "#0.##%", "120%"));
390                         list.Add (new Element (1.2d, "0.###E+0", "1.2E+0"));
391                         list.Add (new Element (1.2d, "0.###E+000", "1.2E+000"));
392                         list.Add (new Element (1.2d, "0.###E-000", "1.2E000"));
393                         list.Add (new Element (1.2d, "[##-##-##]", "[--1]"));
394                         list.Add (new Element (1.2d, "##;(##)", "1"));
395                         list.Add (new Element (1.2d, "##;(##)", "1"));
396                         list.Add (new Element (0.086d, "#####", ""));
397                         list.Add (new Element (0.086d, "00000", "00000"));
398                         list.Add (new Element (0.086d, "(###) ### - ####", "()  - "));
399                         list.Add (new Element (0.086d, "#.##", ".09"));
400                         list.Add (new Element (0.086d, "#.#", ".1"));
401                         list.Add (new Element (0.086d, "0.00", "0.09"));
402                         list.Add (new Element (0.086d, "00.00", "00.09"));
403                         list.Add (new Element (0.086d, "#,#", ""));
404                         list.Add (new Element (0.086d, "#,,", ""));
405                         list.Add (new Element (0.086d, "#,,,", ""));
406                         list.Add (new Element (0.086d, "#,##0,,", "0"));
407                         list.Add (new Element (0.086d, "#0.##%", "8.6%"));
408                         list.Add (new Element (0.086d, "0.###E+0", "8.6E-2"));
409                         list.Add (new Element (0.086d, "0.###E+000", "8.6E-002"));
410                         list.Add (new Element (0.086d, "0.###E-000", "8.6E-002"));
411                         list.Add (new Element (0.086d, "[##-##-##]", "[--]"));
412                         list.Add (new Element (0.086d, "##;(##)", ""));
413                         list.Add (new Element (0.086d, "##;(##)", ""));
414                         list.Add (new Element (86000d, "#####", "86000"));
415                         list.Add (new Element (86000d, "00000", "86000"));
416                         list.Add (new Element (86000d, "(###) ### - ####", "() 8 - 6000"));
417                         list.Add (new Element (86000d, "#.##", "86000"));
418                         list.Add (new Element (86000d, "0.00", "86000.00"));
419                         list.Add (new Element (86000d, "00.00", "86000.00"));
420                         list.Add (new Element (86000d, "#,#", "86,000"));
421                         list.Add (new Element (86000d, "#,,", ""));
422                         list.Add (new Element (86000d, "#,,,", ""));
423                         list.Add (new Element (86000d, "#,##0,,", "0"));
424                         list.Add (new Element (86000d, "#0.##%", "8600000%"));
425                         list.Add (new Element (86000d, "0.###E+0", "8.6E+4"));
426                         list.Add (new Element (86000d, "0.###E+000", "8.6E+004"));
427                         list.Add (new Element (86000d, "0.###E-000", "8.6E004"));
428                         list.Add (new Element (86000d, "[##-##-##]", "[8-60-00]"));
429                         list.Add (new Element (86000d, "##;(##)", "86000"));
430                         list.Add (new Element (86000d, "##;(##)", "86000"));
431                         list.Add (new Element (123456d, "#####", "123456"));
432                         list.Add (new Element (123456d, "00000", "123456"));
433                         list.Add (new Element (123456d, "(###) ### - ####", "() 12 - 3456"));
434                         list.Add (new Element (123456d, "#.##", "123456"));
435                         list.Add (new Element (123456d, "0.00", "123456.00"));
436                         list.Add (new Element (123456d, "00.00", "123456.00"));
437                         list.Add (new Element (123456d, "#,#", "123,456"));
438                         list.Add (new Element (123456d, "#,,", ""));
439                         list.Add (new Element (123456d, "#,,,", ""));
440                         list.Add (new Element (123456d, "#,##0,,", "0"));
441                         list.Add (new Element (123456d, "#0.##%", "12345600%"));
442                         list.Add (new Element (123456d, "0.###E+0", "1.235E+5"));
443                         list.Add (new Element (123456d, "0.###E+000", "1.235E+005"));
444                         list.Add (new Element (123456d, "0.###E-000", "1.235E005"));
445                         list.Add (new Element (123456d, "[##-##-##]", "[12-34-56]"));
446                         list.Add (new Element (123456d, "##;(##)", "123456"));
447                         list.Add (new Element (123456d, "##;(##)", "123456"));
448                         list.Add (new Element (1234d, "#####", "1234"));
449                         list.Add (new Element (1234d, "00000", "01234"));
450                         list.Add (new Element (1234d, "(###) ### - ####", "()  - 1234"));
451                         list.Add (new Element (1234d, "#.##", "1234"));
452                         list.Add (new Element (1234d, "0.00", "1234.00"));
453                         list.Add (new Element (1234d, "00.00", "1234.00"));
454                         list.Add (new Element (1234d, "#,#", "1,234"));
455                         list.Add (new Element (1234d, "#,,", ""));
456                         list.Add (new Element (1234d, "#,,,", ""));
457                         list.Add (new Element (1234d, "#,##0,,", "0"));
458                         list.Add (new Element (1234d, "#0.##%", "123400%"));
459                         list.Add (new Element (1234d, "0.###E+0", "1.234E+3"));
460                         list.Add (new Element (1234d, "0.###E+000", "1.234E+003"));
461                         list.Add (new Element (1234d, "0.###E-000", "1.234E003"));
462                         list.Add (new Element (1234d, "[##-##-##]", "[-12-34]"));
463                         list.Add (new Element (1234d, "##;(##)", "1234"));
464                         list.Add (new Element (1234d, "##;(##)", "1234"));
465                         list.Add (new Element (-1234d, "#####", "-1234"));
466                         list.Add (new Element (-1234d, "00000", "-01234"));
467                         list.Add (new Element (-1234d, "(###) ### - ####", "-()  - 1234"));
468                         list.Add (new Element (-1234d, "#.##", "-1234"));
469                         list.Add (new Element (-1234d, "0.00", "-1234.00"));
470                         list.Add (new Element (-1234d, "00.00", "-1234.00"));
471                         list.Add (new Element (-1234d, "#,#", "-1,234"));
472                         list.Add (new Element (-1234d, "#,,", ""));
473                         list.Add (new Element (-1234d, "#,,,", ""));
474                         list.Add (new Element (-1234d, "#,##0,,", "0"));
475                         list.Add (new Element (-1234d, "#0.##%", "-123400%"));
476                         list.Add (new Element (-1234d, "0.###E+0", "-1.234E+3"));
477                         list.Add (new Element (-1234d, "0.###E+000", "-1.234E+003"));
478                         list.Add (new Element (-1234d, "0.###E-000", "-1.234E003"));
479                         list.Add (new Element (-1234d, "[##-##-##]", "-[-12-34]"));
480                         list.Add (new Element (-1234d, "##;(##)", "(1234)"));
481                         list.Add (new Element (-1234d, "##;(##)", "(1234)"));
482                         list.Add (new Element (12345678901234567890.123d,
483                                                 "#####", "12345678901234600000"));
484                         list.Add (new Element (12345678901234567890.123d,
485                                                 "00000", "12345678901234600000"));
486                         list.Add (new Element (12345678901234567890.123d,
487                                                 "(###) ### - ####", "(1234567890123) 460 - 0000"));
488                         list.Add (new Element (12345678901234567890.123d,
489                                                 "#.##", "12345678901234600000"));
490                         list.Add (new Element (12345678901234567890.123d,
491                                                 "0.00", "12345678901234600000.00"));
492                         list.Add (new Element (12345678901234567890.123d,
493                                                 "00.00", "12345678901234600000.00"));
494                         list.Add (new Element (12345678901234567890.123d,
495                                                 "#,#", "12,345,678,901,234,600,000"));
496                         list.Add (new Element (12345678901234567890.123d,
497                                                 "#,,", "12345678901235"));
498                         list.Add (new Element (12345678901234567890.123d,
499                                                 "#,,,", "12345678901"));
500                         list.Add (new Element (12345678901234567890.123d,
501                                                 "#,##0,,", "12,345,678,901,235"));
502                         list.Add (new Element (12345678901234567890.123d,
503                                                 "#0.##%", "1234567890123460000000%"));
504                         list.Add (new Element (12345678901234567890.123d,
505                                                 "0.###E+0", "1.235E+19"));
506                         list.Add (new Element (12345678901234567890.123d,
507                                                 "0.###E+000", "1.235E+019"));
508                         list.Add (new Element (12345678901234567890.123d,
509                                                 "0.###E-000", "1.235E019"));
510                         list.Add (new Element (12345678901234567890.123d,
511                                                 "[##-##-##]", "[1234567890123460-00-00]"));
512                         list.Add (new Element (12345678901234567890.123d,
513                                                 "##;(##)", "12345678901234600000"));
514                         list.Add (new Element (12345678901234567890.123d,
515                                                 "##;(##)", "12345678901234600000"));
516                         foreach (Element e in list) {
517                                 Assert.AreEqual (e.result, e.value.ToString (e.format, nfi),
518                                         "ToString Failed: '" + e.value + "' Should be \"" + e.result + "\" with \"" + e.format + "\"");
519                         }
520                 }
521
522                 [Test]
523                 public void ToString_Defaults ()
524                 {
525                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
526                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
527                         try {
528                                 Double i = 254.9d;
529                                 // everything defaults to "G"
530                                 string def = i.ToString ("G");
531                                 Assert.AreEqual (def, i.ToString (), "#1");
532                                 Assert.AreEqual (def, i.ToString ((IFormatProvider) null), "#2");
533                                 Assert.AreEqual (def, i.ToString ((string) null), "#3");
534                                 Assert.AreEqual (def, i.ToString (String.Empty), "#4");
535                                 Assert.AreEqual (def, i.ToString (null, null), "#5");
536                                 Assert.AreEqual (def, i.ToString (String.Empty, null), "#6");
537                                 Assert.AreEqual ("254,9", def, "#7");
538                         } finally {
539                                 // restore original culture
540                                 Thread.CurrentThread.CurrentCulture = originalCulture;
541                         }
542                 }
543
544                 [Test]
545                 public void TestRoundtrip () // bug #320433
546                 {
547                         Assert.AreEqual ("10.78", 10.78.ToString ("R", NumberFormatInfo.InvariantInfo));
548                 }
549
550                 [Test] // bug #72955
551                 public void LongLongValueRoundtrip ()
552                 {
553                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
554                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
555                         try {
556                                 double d = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222;
557                                 Assert.AreEqual ("1,97626258336499E-323", d.ToString ("R"));
558                         } finally {
559                                 // restore original culture
560                                 Thread.CurrentThread.CurrentCulture = originalCulture;
561                         }
562                 }
563
564                 [Test]
565                 [ExpectedException (typeof (ArgumentException))]
566                 public void HexNumber_WithHexToParse ()
567                 {
568                         // from bug #72221
569                         double d;
570                         Assert.IsTrue (Double.TryParse ("0dead", NumberStyles.HexNumber, null, out d), "#1");
571                         Assert.AreEqual (57842, d, "#2");
572                 }
573
574                 [Test]
575                 [ExpectedException (typeof (ArgumentException))]
576                 public void HexNumber_NoHexToParse ()
577                 {
578                         double d;
579                         Assert.IsTrue (Double.TryParse ("0", NumberStyles.HexNumber, null, out d), "#1");
580                         Assert.AreEqual (0, d, "#2");
581                 }
582
583                 [Test]
584                 public void TryParseBug78546 ()
585                 {
586                         double value;
587                         Assert.IsFalse (Double.TryParse ("error", NumberStyles.Integer,
588                                 null, out value));
589                 }
590
591                 [Test]
592                 public void TryParse_NonDigitStrings ()
593                 {
594                         double value;
595                         Assert.IsFalse (Double.TryParse ("string", NumberStyles.Any, null, out value), "#1");
596                         Assert.IsFalse (Double.TryParse ("with whitespace", NumberStyles.Any, null, out value), "#2");
597                         
598                         Assert.IsFalse (Double.TryParse ("string", out value), "#3");
599                         Assert.IsFalse (Double.TryParse ("with whitespace", out value), "#4");
600                 }
601
602                                         
603                 [Test] // bug #77721
604                 public void ParseCurrency ()
605                 {
606                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
607                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
608                         try {
609                                 NumberFormatInfo f = NumberFormatInfo.CurrentInfo;
610                                 double d = double.Parse ("$4.56", NumberStyles.Currency, f);
611                                 Assert.AreEqual (4.56, d);
612                         } finally {
613                                 Thread.CurrentThread.CurrentCulture = originalCulture;
614                         }
615                 }
616
617                 [Test]
618                 public void ParseEmptyNumberGroupSeparator ()
619                 {
620                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
621                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
622                         try {
623                                 var nf = new NumberFormatInfo ();
624                                 nf.NumberDecimalSeparator = ".";
625                                 nf.NumberGroupSeparator = "";
626                                 double d = double.Parse ("4.5", nf);
627                                 Assert.AreEqual (4.5, d);
628                         } finally {
629                                 Thread.CurrentThread.CurrentCulture = originalCulture;
630                         }
631                 }
632         }
633 }