* DirectoryTest.cs: Restore original CurrentCulture on teardown.
[mono.git] / mcs / class / corlib / Test / System / DecimalTest.cs
1 // DecimalTest.cs - NUnit Test Cases for the System.Decimal struct
2 //
3 // Authors:
4 //      Martin Weindel (martin.weindel@t-online.de)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) Martin Weindel, 2001
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 // 
10
11 using NUnit.Framework;
12 using System;
13
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Threading;
17
18 namespace MonoTests.System
19 {
20         internal struct ParseTest
21         {
22                 public ParseTest (String str, bool exceptionFlag)
23                 {
24                         this.str = str;
25                         this.exceptionFlag = exceptionFlag;
26                         this.style = NumberStyles.Number;
27                         this.d = 0;
28                 }
29
30                 public ParseTest (String str, Decimal d)
31                 {
32                         this.str = str;
33                         this.exceptionFlag = false;
34                         this.style = NumberStyles.Number;
35                         this.d = d;
36                 }
37
38                 public ParseTest (String str, Decimal d, NumberStyles style)
39                 {
40                         this.str = str;
41                         this.exceptionFlag = false;
42                         this.style = style;
43                         this.d = d;
44                 }
45
46                 public String str;
47                 public Decimal d;
48                 public NumberStyles style;
49                 public bool exceptionFlag;
50         }
51
52         internal struct ToStringTest
53         {
54                 public ToStringTest (String format, Decimal d, String str)
55                 {
56                         this.format = format;
57                         this.d = d;
58                         this.str = str;
59                 }
60
61                 public String format;
62                 public Decimal d;
63                 public String str;
64         }
65
66         [TestFixture]
67         public class DecimalTest : Assertion
68         {
69                 private const int negativeBitValue = unchecked ((int) 0x80000000);
70                 private const int negativeScale4Value = unchecked ((int) 0x80040000);
71                 private int [] parts0 = { 0, 0, 0, 0 }; //Positive Zero.
72                 private int [] parts1 = { 1, 0, 0, 0 };
73                 private int [] parts2 = { 0, 1, 0, 0 };
74                 private int [] parts3 = { 0, 0, 1, 0 };
75                 private int [] parts4 = { 0, 0, 0, negativeBitValue }; // Negative zero.
76                 private int [] parts5 = { 1, 1, 1, 0 };
77                 private int [] partsMaxValue = { -1, -1, -1, 0 };
78                 private int [] partsMinValue = { -1, -1, -1, negativeBitValue };
79                 private int [] parts6 = { 1234, 5678, 8888, negativeScale4Value };
80                 private NumberFormatInfo NfiUser;
81
82                 private CultureInfo old_culture;
83
84                 [TestFixtureSetUp]
85                 public void FixtureSetUp ()
86                 {
87                         old_culture = Thread.CurrentThread.CurrentCulture;
88
89                         // Set culture to en-US and don't let the user override.
90                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
91
92                         NfiUser = new NumberFormatInfo ();
93                         NfiUser.CurrencyDecimalDigits = 3;
94                         NfiUser.CurrencyDecimalSeparator = ",";
95                         NfiUser.CurrencyGroupSeparator = "_";
96                         NfiUser.CurrencyGroupSizes = new int [] { 2, 1, 0 };
97                         NfiUser.CurrencyNegativePattern = 10;
98                         NfiUser.CurrencyPositivePattern = 3;
99                         NfiUser.CurrencySymbol = "XYZ";
100                         NfiUser.NumberDecimalSeparator = "##";
101                         NfiUser.NumberDecimalDigits = 4;
102                         NfiUser.NumberGroupSeparator = "__";
103                         NfiUser.NumberGroupSizes = new int [] { 2, 1 };
104                         NfiUser.PercentDecimalDigits = 1;
105                         NfiUser.PercentDecimalSeparator = ";";
106                         NfiUser.PercentGroupSeparator = "~";
107                         NfiUser.PercentGroupSizes = new int [] { 1 };
108                         NfiUser.PercentNegativePattern = 2;
109                         NfiUser.PercentPositivePattern = 2;
110                         NfiUser.PercentSymbol = "%%%";
111                 }
112
113                 [TestFixtureTearDown]
114                 public void FixtureTearDown ()
115                 {
116                         Thread.CurrentThread.CurrentCulture = old_culture;
117                 }
118
119                 [Test]
120                 public void TestToString ()
121                 {
122                         ToStringTest [] tab = {
123                                 new ToStringTest ("F", 12.345678m, "12.35"),
124                                 new ToStringTest ("F3", 12.345678m, "12.346"),
125                                 new ToStringTest ("F0", 12.345678m, "12"),
126                                 new ToStringTest ("F7", 12.345678m, "12.3456780"),
127                                 new ToStringTest ("g", 12.345678m, "12.345678"),
128                                 new ToStringTest ("E", 12.345678m, "1.234568E+001"),
129                                 new ToStringTest ("E3", 12.345678m, "1.235E+001"),
130                                 new ToStringTest ("E0", 12.345678m, "1E+001"),
131                                 new ToStringTest ("e8", 12.345678m, "1.23456780e+001"),
132                                 new ToStringTest ("F", 0.0012m, "0.00"),
133                                 new ToStringTest ("F3", 0.0012m, "0.001"),
134                                 new ToStringTest ("F0", 0.0012m, "0"),
135                                 new ToStringTest ("F6", 0.0012m, "0.001200"),
136                                 new ToStringTest ("e", 0.0012m, "1.200000e-003"),
137                                 new ToStringTest ("E3", 0.0012m, "1.200E-003"),
138                                 new ToStringTest ("E0", 0.0012m, "1E-003"),
139                                 new ToStringTest ("E6", 0.0012m, "1.200000E-003"),
140                                 new ToStringTest ("F4", -0.001234m, "-0.0012"),
141                                 new ToStringTest ("E3", -0.001234m, "-1.234E-003"),
142 #if NET_1_0
143                                 new ToStringTest ("g", -0.000012m, "-1.2e-05"),
144 #else
145                                 new ToStringTest ("g", -0.000012m, "-0.000012"),
146 #endif
147                                 new ToStringTest ("g", -0.00012m, "-0.00012"),
148                                 new ToStringTest ("g4", -0.00012m, "-0.00012"),
149                                 new ToStringTest ("g7", -0.00012m, "-0.00012"),
150                                 new ToStringTest ("g", -0.0001234m, "-0.0001234"),
151                                 new ToStringTest ("g", -0.0012m, "-0.0012"),
152                                 new ToStringTest ("g", -0.001234m, "-0.001234"),
153                                 new ToStringTest ("g", -0.012m, "-0.012"),
154                                 new ToStringTest ("g4", -0.012m, "-0.012"),
155                                 new ToStringTest ("g", -0.12m, "-0.12"),
156                                 new ToStringTest ("g", -1.2m, "-1.2"),
157                                 new ToStringTest ("g4", -120m, "-120"),
158                                 new ToStringTest ("g", -12m, "-12"),
159                                 new ToStringTest ("g", -120m, "-120"),
160                                 new ToStringTest ("g", -1200m, "-1200"),
161                                 new ToStringTest ("g4", -1200m, "-1200"),
162                                 new ToStringTest ("g", -1234m, "-1234"),
163                                 new ToStringTest ("g", -12000m, "-12000"),
164                                 new ToStringTest ("g4", -12000m, "-1.2e+04"),
165                                 new ToStringTest ("g5", -12000m, "-12000"),
166                                 new ToStringTest ("g", -12345m, "-12345"),
167                                 new ToStringTest ("g", -120000m, "-120000"),
168                                 new ToStringTest ("g4", -120000m, "-1.2e+05"),
169                                 new ToStringTest ("g5", -120000m, "-1.2e+05"),
170                                 new ToStringTest ("g6", -120000m, "-120000"),
171                                 new ToStringTest ("g", -123456.1m, "-123456.1"),
172                                 new ToStringTest ("g5", -123456.1m, "-1.2346e+05"),
173                                 new ToStringTest ("g6", -123456.1m, "-123456"),
174                                 new ToStringTest ("g", -1200000m, "-1200000"),
175                                 new ToStringTest ("g", -123456.1m, "-123456.1"),
176                                 new ToStringTest ("g", -123456.1m, "-123456.1"),
177                                 new ToStringTest ("g", -1234567.1m, "-1234567.1"),
178                                 new ToStringTest ("g", -12000000m, "-12000000"),
179                                 new ToStringTest ("g", -12345678.1m, "-12345678.1"),
180                                 new ToStringTest ("g", -12000000000000000000m, "-12000000000000000000"),
181                                 new ToStringTest ("F", -123, "-123.00"),
182                                 new ToStringTest ("F3", -123, "-123.000"),
183                                 new ToStringTest ("F0", -123, "-123"),
184                                 new ToStringTest ("E3", -123, "-1.230E+002"),
185                                 new ToStringTest ("E0", -123, "-1E+002"),
186                                 new ToStringTest ("E", -123, "-1.230000E+002"),
187                                 new ToStringTest ("F3", Decimal.MinValue, "-79228162514264337593543950335.000"),
188                                 new ToStringTest ("F", Decimal.MinValue, "-79228162514264337593543950335.00"),
189                                 new ToStringTest ("F0", Decimal.MinValue, "-79228162514264337593543950335"),
190                                 new ToStringTest ("E", Decimal.MinValue, "-7.922816E+028"),
191                                 new ToStringTest ("E3", Decimal.MinValue, "-7.923E+028"),
192                                 new ToStringTest ("E28", Decimal.MinValue, "-7.9228162514264337593543950335E+028"),
193                                 new ToStringTest ("E30", Decimal.MinValue, "-7.922816251426433759354395033500E+028"),
194                                 new ToStringTest ("E0", Decimal.MinValue, "-8E+028"),
195                                 new ToStringTest ("N3", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335.000"),
196                                 new ToStringTest ("N0", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335"),
197                                 new ToStringTest ("N", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335.00"),
198                                 new ToStringTest ("n3", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335.000"),
199                                 new ToStringTest ("n0", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335"),
200                                 new ToStringTest ("n", Decimal.MinValue, "-79,228,162,514,264,337,593,543,950,335.00"),
201                                 new ToStringTest ("C", 123456.7890m, NumberFormatInfo.InvariantInfo.CurrencySymbol + "123,456.79"),
202                                 new ToStringTest ("C", -123456.7890m, "(" + NumberFormatInfo.InvariantInfo.CurrencySymbol + "123,456.79)"),
203                                 new ToStringTest ("C3", 1123456.7890m, NumberFormatInfo.InvariantInfo.CurrencySymbol + "1,123,456.789"),
204                                 new ToStringTest ("P", 123456.7891m, "12,345,678.91 %"),
205                                 new ToStringTest ("P", -123456.7892m, "-12,345,678.92 %"),
206                                 new ToStringTest ("P3", 1234.56789m, "123,456.789 %"),
207                         };
208
209                         NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
210
211                         for (int i = 0; i < tab.Length; i++) {
212                                 try {
213                                         string s = tab [i].d.ToString (tab [i].format, nfi);
214                                         AssertEquals ("A01 tab[" + i + "].format = '" + tab [i].format + "')", tab [i].str, s);
215                                 } catch (OverflowException) {
216                                         Fail (tab [i].d.ToString (tab [i].format, nfi) + " (format = '" + tab [i].format + "'): unexpected exception !");
217                                 } catch (NUnit.Framework.AssertionException e) {
218                                         throw e;
219                                 } catch (Exception e) {
220                                         Fail ("Unexpected Exception when i = " + i + ". e = " + e);
221                                 }
222                         }
223                 }
224
225                 [Test]
226                 public void TestCurrencyPattern ()
227                 {
228                         NumberFormatInfo nfi2 = (NumberFormatInfo) NfiUser.Clone ();
229                         Decimal d = -1234567.8976m;
230                         string [] ergCurrencyNegativePattern = new String [16] {
231                                 "(XYZ1234_5_67,898)", "-XYZ1234_5_67,898", "XYZ-1234_5_67,898", "XYZ1234_5_67,898-",
232                                 "(1234_5_67,898XYZ)", "-1234_5_67,898XYZ", "1234_5_67,898-XYZ", "1234_5_67,898XYZ-",
233                                 "-1234_5_67,898 XYZ", "-XYZ 1234_5_67,898", "1234_5_67,898 XYZ-", "XYZ 1234_5_67,898-",
234                                 "XYZ -1234_5_67,898", "1234_5_67,898- XYZ", "(XYZ 1234_5_67,898)", "(1234_5_67,898 XYZ)",
235                         };
236
237                         for (int i = 0; i < ergCurrencyNegativePattern.Length; i++) {
238                                 nfi2.CurrencyNegativePattern = i;
239                                 if (d.ToString ("C", nfi2) != ergCurrencyNegativePattern [i]) {
240                                         Fail ("CurrencyNegativePattern #" + i + " failed: " +
241                                                 d.ToString ("C", nfi2) + " != " + ergCurrencyNegativePattern [i]);
242                                 }
243                         }
244
245                         d = 1234567.8976m;
246                         string [] ergCurrencyPositivePattern = new String [4] {
247                                 "XYZ1234_5_67,898", "1234_5_67,898XYZ", "XYZ 1234_5_67,898", "1234_5_67,898 XYZ",
248                         };
249
250                         for (int i = 0; i < ergCurrencyPositivePattern.Length; i++) {
251                                 nfi2.CurrencyPositivePattern = i;
252                                 if (d.ToString ("C", nfi2) != ergCurrencyPositivePattern [i]) {
253                                         Fail ("CurrencyPositivePattern #" + i + " failed: " +
254                                                 d.ToString ("C", nfi2) + " != " + ergCurrencyPositivePattern [i]);
255                                 }
256                         }
257                 }
258
259                 [Test]
260                 public void TestNumberNegativePattern ()
261                 {
262                         NumberFormatInfo nfi2 = (NumberFormatInfo) NfiUser.Clone ();
263                         Decimal d = -1234.89765m;
264                         string [] ergNumberNegativePattern = new String [5] {
265                                 "(1__2__34##8977)", "-1__2__34##8977", "- 1__2__34##8977", "1__2__34##8977-", "1__2__34##8977 -",
266                         };
267
268                         for (int i = 0; i < ergNumberNegativePattern.Length; i++) {
269                                 nfi2.NumberNegativePattern = i;
270                                 AssertEquals ("NumberNegativePattern #" + i, ergNumberNegativePattern [i], d.ToString ("N", nfi2));
271                         }
272                 }
273
274                 [Test]
275                 public void TestPercentPattern ()
276                 {
277                         NumberFormatInfo nfi2 = (NumberFormatInfo) NfiUser.Clone ();
278                         Decimal d = -1234.8976m;
279                         string [] ergPercentNegativePattern = new String [3] {
280                                 "-1~2~3~4~8~9;8 %%%", "-1~2~3~4~8~9;8%%%", "-%%%1~2~3~4~8~9;8"
281                         };
282
283                         for (int i = 0; i < ergPercentNegativePattern.Length; i++) {
284                                 nfi2.PercentNegativePattern = i;
285                                 if (d.ToString ("P", nfi2) != ergPercentNegativePattern [i]) {
286                                         Fail ("PercentNegativePattern #" + i + " failed: " +
287                                                 d.ToString ("P", nfi2) + " != " + ergPercentNegativePattern [i]);
288                                 }
289                         }
290
291                         d = 1234.8976m;
292                         string [] ergPercentPositivePattern = new String [3] {
293                                 "1~2~3~4~8~9;8 %%%", "1~2~3~4~8~9;8%%%", "%%%1~2~3~4~8~9;8"
294                         };
295
296                         for (int i = 0; i < ergPercentPositivePattern.Length; i++) {
297                                 nfi2.PercentPositivePattern = i;
298                                 if (d.ToString ("P", nfi2) != ergPercentPositivePattern [i]) {
299                                         Fail ("PercentPositivePattern #" + i + " failed: " +
300                                                 d.ToString ("P", nfi2) + " != " + ergPercentPositivePattern [i]);
301                                 }
302                         }
303                 }
304
305                 ParseTest [] tab = {
306                                 new ParseTest("1.2345", 1.2345m),
307                                 new ParseTest("-9876543210", -9876543210m),
308                                 new ParseTest(NumberFormatInfo.InvariantInfo.CurrencySymbol 
309                                         + " (  79,228,162,514,264,337,593,543,950,335.000 ) ", 
310                                         Decimal.MinValue, NumberStyles.Currency),
311                                 new ParseTest("1.234567890e-10", (Decimal)1.234567890e-10, NumberStyles.Float),
312                                 new ParseTest("1.234567890e-24", 1.2346e-24m, NumberStyles.Float),
313                                 new ParseTest("  47896396.457983645462346E10  ", 478963964579836454.62346m, NumberStyles.Float),
314                                 new ParseTest("-7922816251426433759354395033.250000000000001", -7922816251426433759354395033.3m),
315                                 new ParseTest("-00000000000000795033.2500000000000000", -795033.25m),
316                                 new ParseTest("-000000000000001922816251426433759354395033.300000000000000", -1922816251426433759354395033.3m),
317                                 new ParseTest("-7922816251426433759354395033.150000000000", -7922816251426433759354395033.2m),
318                                 new ParseTest("-7922816251426433759354395033.2400000000000", -7922816251426433759354395033.2m),
319                                 new ParseTest("-7922816251426433759354395033.2600000000000", -7922816251426433759354395033.3m)
320                 };
321
322                 [Test]
323                 public void TestParse ()
324                 {
325
326                         Decimal d;
327                         for (int i = 0; i < tab.Length; i++) {
328                                 try {
329                                         d = Decimal.Parse (tab [i].str, tab [i].style, NumberFormatInfo.InvariantInfo);
330                                         if (tab [i].exceptionFlag) {
331                                                 Fail (tab [i].str + ": missing exception !");
332                                         } else if (d != tab [i].d) {
333                                                 Fail (tab [i].str + " != " + d);
334                                         }
335                                 } catch (OverflowException) {
336                                         if (!tab [i].exceptionFlag) {
337                                                 Fail (tab [i].str + ": unexpected exception !");
338                                         }
339                                 }
340                         }
341
342                         try {
343                                 d = Decimal.Parse (null);
344                                 Fail ("Expected ArgumentNullException");
345                         } catch (ArgumentNullException) {
346                                 //ok
347                         }
348
349                         try {
350                                 d = Decimal.Parse ("123nx");
351                                 Fail ("Expected FormatException");
352                         } catch (FormatException) {
353                                 //ok
354                         }
355
356                         try {
357                                 d = Decimal.Parse ("79228162514264337593543950336");
358                                 Fail ("Expected OverflowException" + d);
359                         } catch (OverflowException) {
360                                 //ok
361                         }
362                 }
363
364                 [Test]
365                 public void TestConstants ()
366                 {
367                         AssertEquals ("Zero", 0m, Decimal.Zero);
368                         AssertEquals ("One", 1m, Decimal.One);
369                         AssertEquals ("MinusOne", -1m, Decimal.MinusOne);
370                         AssertEquals ("MaxValue", 79228162514264337593543950335m, Decimal.MaxValue);
371                         AssertEquals ("MinValue", -79228162514264337593543950335m, Decimal.MinValue);
372                         Assert ("MinusOne 2", -1m == Decimal.MinusOne);
373                 }
374
375                 [Test]
376                 public void TestConstructInt32 ()
377                 {
378                         decimal [] dtab = { 0m, 1m, -1m, 123456m, -1234567m };
379                         int [] itab = { 0, 1, -1, 123456, -1234567 };
380
381                         Decimal d;
382
383                         for (int i = 0; i < dtab.GetLength (0); i++) {
384                                 d = new Decimal (itab [i]);
385                                 if ((decimal) d != dtab [i]) {
386                                         Fail ("Int32 -> Decimal: " + itab [i] + " != " + d);
387                                 } else {
388                                         int n = (int) d;
389                                         if (n != itab [i]) {
390                                                 Fail ("Decimal -> Int32: " + d + " != " + itab [i]);
391                                         }
392                                 }
393                         }
394
395                         d = new Decimal (Int32.MaxValue);
396                         Assert ((int) d == Int32.MaxValue);
397
398                         d = new Decimal (Int32.MinValue);
399                         Assert ((int) d == Int32.MinValue);
400                 }
401
402                 [Test]
403                 public void TestConstructUInt32 ()
404                 {
405                         decimal [] dtab = { 0m, 1m, 123456m, 123456789m };
406                         uint [] itab = { 0, 1, 123456, 123456789 };
407
408                         Decimal d;
409
410                         for (int i = 0; i < dtab.GetLength (0); i++) {
411                                 d = new Decimal (itab [i]);
412                                 if ((decimal) d != dtab [i]) {
413                                         Fail ("UInt32 -> Decimal: " + itab [i] + " != " + d);
414                                 } else {
415                                         uint n = (uint) d;
416                                         if (n != itab [i]) {
417                                                 Fail ("Decimal -> UInt32: " + d + " != " + itab [i]);
418                                         }
419                                 }
420                         }
421
422                         d = new Decimal (UInt32.MaxValue);
423                         Assert ((uint) d == UInt32.MaxValue);
424
425                         d = new Decimal (UInt32.MinValue);
426                         Assert ((uint) d == UInt32.MinValue);
427                 }
428
429                 [Test]
430                 public void TestConstructInt64 ()
431                 {
432                         decimal [] dtab = { 0m, 1m, -1m, 9876543m, -9876543210m, 12345678987654321m };
433                         long [] itab = { 0, 1, -1, 9876543, -9876543210L, 12345678987654321L };
434
435                         Decimal d;
436
437                         for (int i = 0; i < dtab.GetLength (0); i++) {
438                                 d = new Decimal (itab [i]);
439                                 if ((decimal) d != dtab [i]) {
440                                         Fail ("Int64 -> Decimal: " + itab [i] + " != " + d);
441                                 } else {
442                                         long n = (long) d;
443                                         if (n != itab [i]) {
444                                                 Fail ("Decimal -> Int64: " + d + " != " + itab [i]);
445                                         }
446                                 }
447                         }
448
449                         d = new Decimal (Int64.MaxValue);
450                         Assert ((long) d == Int64.MaxValue);
451
452                         d = new Decimal (Int64.MinValue);
453                         Assert ((long) d == Int64.MinValue);
454                 }
455
456                 [Test]
457                 public void TestConstructUInt64 ()
458                 {
459                         decimal [] dtab = { 0m, 1m, 987654321m, 123456789876543210m };
460                         ulong [] itab = { 0, 1, 987654321, 123456789876543210L };
461
462                         Decimal d;
463
464                         for (int i = 0; i < dtab.GetLength (0); i++) {
465                                 d = new Decimal (itab [i]);
466                                 if ((decimal) d != dtab [i]) {
467                                         Fail ("UInt64 -> Decimal: " + itab [i] + " != " + d);
468                                 } else {
469                                         ulong n = (ulong) d;
470                                         if (n != itab [i]) {
471                                                 Fail ("Decimal -> UInt64: " + d + " != " + itab [i]);
472                                         }
473                                 }
474                         }
475
476                         d = new Decimal (UInt64.MaxValue);
477                         Assert ((ulong) d == UInt64.MaxValue);
478
479                         d = new Decimal (UInt64.MinValue);
480                         Assert ((ulong) d == UInt64.MinValue);
481                 }
482
483                 [Test]
484                 public void TestConstructSingle ()
485                 {
486                         Decimal d;
487
488                         d = new Decimal (-1.2345678f);
489                         AssertEquals ("A#01", -1.234568m, (decimal) d);
490
491                         d = 3;
492                         AssertEquals ("A#02", 3.0f, (float) d);
493
494                         d = new Decimal (0.0f);
495                         AssertEquals ("A#03", 0m, (decimal) d);
496                         AssertEquals ("A#04", 0.0f, (float) d);
497
498                         d = new Decimal (1.0f);
499                         AssertEquals ("A#05", 1m, (decimal) d);
500                         AssertEquals ("A#06", 1.0f, (float) d);
501
502                         d = new Decimal (-1.2345678f);
503                         AssertEquals ("A#07", -1.234568m, (decimal) d);
504                         AssertEquals ("A#08", -1.234568f, (float) d);
505
506                         d = new Decimal (1.2345673f);
507                         AssertEquals ("A#09", 1.234567m, (decimal) d);
508
509                         d = new Decimal (1.2345673e7f);
510                         AssertEquals ("A#10", 12345670m, (decimal) d);
511
512                         d = new Decimal (1.2345673e-17f);
513                         AssertEquals ("A#11", 0.00000000000000001234567m, (decimal) d);
514                         AssertEquals ("A#12", 1.234567e-17f, (float) d);
515
516                         // test exceptions
517                         try {
518                                 d = new Decimal (Single.MaxValue);
519                                 Fail ();
520                         } catch (OverflowException) {
521                         }
522
523                         try {
524                                 d = new Decimal (Single.NaN);
525                                 Fail ();
526                         } catch (OverflowException) {
527                         }
528
529                         try {
530                                 d = new Decimal (Single.PositiveInfinity);
531                                 Fail ();
532                         } catch (OverflowException) {
533                         }
534                 }
535
536                 [Test]
537                 public void TestConstructSingleRounding_NowWorking ()
538                 {
539                         decimal d;
540
541                         d = new Decimal (1765.23454f);
542                         AssertEquals ("failed banker's rule rounding test 2", 1765.234m, d);
543
544                         d = new Decimal (0.00017652356f);
545                         AssertEquals ("06", 0.0001765236m, d);
546
547                         d = new Decimal (0.000176523554f);
548                         AssertEquals ("failed banker's rule rounding test 3", 0.0001765236m, d);
549
550                         d = new Decimal (0.00017652354f);
551                         AssertEquals ("08", 0.0001765235m, d);
552
553                         d = new Decimal (0.00017652346f);
554                         AssertEquals ("09", 0.0001765235m, d);
555
556                         d = new Decimal (0.000176523454f);
557                         AssertEquals ("failed banker's rule rounding test 4", 0.0001765234m, d);
558
559                         d = new Decimal (0.00017652344f);
560                         AssertEquals ("11", 0.0001765234m, d);
561                 }
562
563                 public void TestConstructSingleRounding ()
564                 {
565                         decimal d;
566
567                         d = new Decimal (1765.2356f);
568                         Assert ("01", d == 1765.236m);
569
570                         d = new Decimal (1765.23554f);
571                         Assert ("failed banker's rule rounding test 1", d == 1765.236m);
572
573                         d = new Decimal (1765.2354f);
574                         Assert ("03", d == 1765.235m);
575
576                         d = new Decimal (1765.2346f);
577                         Assert ("04", d == 1765.235m);
578
579                         d = new Decimal (1765.2344f);
580                         Assert ("05", d == 1765.234m);
581
582                         d = new Decimal (3.7652356e10f);
583                         Assert ("12", d == 37652360000m);
584
585                         d = new Decimal (3.7652356e20f);
586                         Assert ("13", d == 376523600000000000000m);
587
588                         d = new Decimal (3.76523554e20f);
589                         Assert ("failed banker's rule rounding test 5", d == 376523600000000000000m);
590
591                         d = new Decimal (3.7652352e20f);
592                         Assert ("15", d == 376523500000000000000m);
593
594                         d = new Decimal (3.7652348e20f);
595                         Assert ("16", d == 376523500000000000000m);
596
597                         d = new Decimal (3.76523454e20f);
598                         Assert ("failed banker's rule rounding test 6", d == 376523400000000000000m);
599
600                         d = new Decimal (3.7652342e20f);
601                         Assert ("18", d == 376523400000000000000m);
602                 }
603
604                 [Test]
605                 public void TestConstructDouble ()
606                 {
607                         Decimal d;
608
609                         d = new Decimal (0.0);
610                         Assert ((decimal) d == 0m);
611
612                         d = new Decimal (1.0);
613                         Assert ((decimal) d == 1m);
614                         Assert (1.0 == (double) d);
615
616                         d = new Decimal (-1.2345678901234);
617                         Assert ((decimal) d == -1.2345678901234m);
618                         Assert (-1.2345678901234 == (double) d);
619
620                         d = new Decimal (1.2345678901234);
621                         Assert ((decimal) d == 1.2345678901234m);
622
623                         d = new Decimal (1.2345678901234e8);
624                         Assert ((decimal) d == 123456789.01234m);
625                         Assert (1.2345678901234e8 == (double) d);
626
627                         d = new Decimal (1.2345678901234e16);
628                         Assert ((decimal) d == 12345678901234000m);
629                         Assert (1.2345678901234e16 == (double) d);
630
631                         d = new Decimal (1.2345678901234e24);
632                         Assert ((decimal) d == 1234567890123400000000000m);
633                         Assert (1.2345678901234e24 == (double) d);
634
635                         d = new Decimal (1.2345678901234e28);
636                         Assert ((decimal) d == 1.2345678901234e28m);
637                         Assert (1.2345678901234e28 == (double) d);
638
639                         d = new Decimal (7.2345678901234e28);
640                         Assert ((decimal) d == 7.2345678901234e28m);
641                         Assert (new Decimal ((double) d) == d);
642
643                         d = new Decimal (1.2345678901234e-8);
644                         Assert ((decimal) d == 1.2345678901234e-8m);
645
646                         d = new Decimal (1.2345678901234e-14);
647                         Assert ((decimal) d == 1.2345678901234e-14m);
648                         Assert (1.2345678901234e-14 == (double) d);
649
650                         d = new Decimal (1.2342278901234e-25);
651                         AssertEquals ("A10", d, 1.234e-25m);
652
653                         // test exceptions
654                         try {
655                                 d = new Decimal (8e28);
656                                 Fail ();
657                         } catch (OverflowException) {
658                         }
659
660                         try {
661                                 d = new Decimal (8e48);
662                                 Fail ();
663                         } catch (OverflowException) {
664                         }
665
666                         try {
667                                 d = new Decimal (Double.NaN);
668                                 Fail ();
669                         } catch (OverflowException) {
670                         }
671
672                         try {
673                                 d = new Decimal (Double.PositiveInfinity);
674                                 Fail ();
675                         } catch (OverflowException) {
676                         }
677                 }
678
679                 [Test]
680                 public void TestConstructDoubleRound ()
681                 {
682                         decimal d;
683                         int TestNum = 1;
684
685                         try {
686                                 d = new Decimal (1765.231234567857);
687                                 AssertEquals ("A01", 1765.23123456786m, d);
688
689                                 TestNum++;
690                                 d = new Decimal (1765.2312345678554);
691                                 AssertEquals ("A02, failed banker's rule rounding test 1", 1765.23123456786m, d);
692                                 AssertEquals ("A03", 1765.23123456786, (double) d);
693
694                                 TestNum++;
695                                 d = new Decimal (1765.231234567853);
696                                 Assert (d == 1765.23123456785m);
697
698                                 TestNum++;
699                                 d = new Decimal (1765.231234567847);
700                                 Assert (d == 1765.23123456785m);
701
702                                 TestNum++;
703                                 d = new Decimal (1765.231234567843);
704                                 Assert (d == 1765.23123456784m);
705
706                                 TestNum++;
707                                 d = new Decimal (1.765231234567857e-9);
708                                 Assert (d == 1.76523123456786e-9m);
709
710                                 TestNum++;
711                                 d = new Decimal (1.7652312345678554e-9);
712                                 Assert ("failed banker's rule rounding test 3", d == 1.76523123456786e-9m);
713
714                                 TestNum++;
715                                 d = new Decimal (1.765231234567853e-9);
716                                 Assert (d == 1.76523123456785e-9m);
717
718                                 TestNum++;
719                                 d = new Decimal (1.765231234567857e+24);
720                                 Assert (d == 1.76523123456786e+24m);
721
722                                 TestNum++;
723                                 d = new Decimal (1.7652312345678554e+24);
724                                 Assert ("failed banker's rule rounding test 4", d == 1.76523123456786e+24m);
725
726                                 TestNum++;
727                                 d = new Decimal (1.765231234567853e+24);
728                                 Assert (d == 1.76523123456785e+24m);
729
730                                 TestNum++;
731                                 d = new Decimal (1765.2312345678454);
732                                 Assert (d == 1765.23123456785m);
733                         } catch (Exception e) {
734                                 Fail ("At TestNum = " + TestNum + " unexpected exception. e = " + e);
735                         }
736                 }
737
738                 [Test]
739                 public void TestNegate ()
740                 {
741                         decimal d;
742
743                         d = new Decimal (12345678);
744                         Assert ((decimal) Decimal.Negate (d) == -12345678m);
745                 }
746
747                 [Test]
748                 public void TestPartConstruct ()
749                 {
750                         decimal d;
751
752                         d = new Decimal (parts0);
753                         Assert (d == 0);
754
755                         d = new Decimal (parts1);
756                         Assert (d == 1);
757
758                         d = new Decimal (parts2);
759                         Assert (d == 4294967296m);
760
761                         d = new Decimal (parts3);
762                         Assert (d == 18446744073709551616m);
763
764                         d = new Decimal (parts4);
765                         Assert (d == 0m);
766
767                         d = new Decimal (parts5);
768                         Assert (d == 18446744078004518913m);
769
770                         d = new Decimal (partsMaxValue);
771                         Assert (d == Decimal.MaxValue);
772
773                         d = new Decimal (partsMinValue);
774                         Assert (d == Decimal.MinValue);
775
776                         d = new Decimal (parts6);
777                         int [] erg = Decimal.GetBits (d);
778                         for (int i = 0; i < 4; i++) {
779                                 Assert (erg [i] == parts6 [i]);
780                         }
781                 }
782
783                 [Test]
784                 public void TestFloorTruncate ()
785                 {
786                         decimal [,] dtab = {
787                                 {0m, 0m, 0m}, {1m, 1m, 1m}, {-1m, -1m, -1m}, {1.1m, 1m, 1m}, 
788                                 {-1.000000000001m, -2m, -1m}, {12345.67890m,12345m,12345m},
789                                 {-123456789012345.67890m, -123456789012346m, -123456789012345m},
790                                 {Decimal.MaxValue, Decimal.MaxValue, Decimal.MaxValue},
791                                 {Decimal.MinValue, Decimal.MinValue, Decimal.MinValue},
792                                 {6.999999999m, 6m, 6m}, {-6.999999999m, -7m, -6m}, 
793                                 {0.00001m, 0m, 0m}, {-0.00001m, -1m, 0m}
794                         };
795
796                         decimal d;
797
798                         for (int i = 0; i < dtab.GetLength (0); i++) {
799                                 d = Decimal.Floor (dtab [i, 0]);
800                                 if (d != dtab [i, 1]) {
801                                         Fail ("Floor: Floor(" + dtab [i, 0] + ") != " + d);
802                                 }
803                                 d = Decimal.Truncate (dtab [i, 0]);
804                                 if (d != dtab [i, 2]) {
805                                         Fail ("Truncate: Truncate(" + dtab [i, 0] + ") != " + d);
806                                 }
807                         }
808                 }
809
810                 [Test]
811                 public void Truncate ()
812                 {
813                         decimal dd = 249.9m;
814                         decimal dt = Decimal.Truncate (dd);
815                         AssertEquals ("Original", 249.9m, dd);
816                         AssertEquals ("Truncate", 249m, dt);
817                         AssertEquals ("Cast-Byte", 249, (byte) dd);
818                         AssertEquals ("Cast-Char", 249, (char) dd);
819                         AssertEquals ("Cast-Int16", 249, (short) dd);
820                         AssertEquals ("Cast-UInt16", 249, (ushort) dd);
821                         AssertEquals ("Cast-Int32", 249, (int) dd);
822                         AssertEquals ("Cast-UInt32", 249, (uint) dd);
823                         AssertEquals ("Cast-Int64", 249, (long) dd);
824                         AssertEquals ("Cast-UInt64", 249, (ulong) dd);
825                 }
826
827                 [Test]
828                 public void TestRound ()
829                 {
830                         decimal [,] dtab = { 
831                                 {1m, 0, 1m}, {1.234567890m, 1, 1.2m}, 
832                                 {1.234567890m, 2, 1.23m}, {1.23450000001m, 3, 1.235m}, 
833                                 {1.2355m, 3, 1.236m}, 
834                                 {1.234567890m, 4, 1.2346m}, {1.23567890m, 2, 1.24m}, 
835                                 {47893764694.4578563236436621m, 7, 47893764694.4578563m},
836                                 {-47893764694.4578563236436621m, 9, -47893764694.457856324m},
837                                 {-47893764694.4578m, 5, -47893764694.4578m}
838                         };
839
840                         decimal d;
841
842                         for (int i = 0; i < dtab.GetLength (0); i++) {
843                                 d = Decimal.Round (dtab [i, 0], (int) dtab [i, 1]);
844                                 if (d != dtab [i, 2]) {
845                                         Fail ("Round: Round(" + dtab [i, 0] + "," + (int) dtab [i, 1] + ") != " + d);
846                                 }
847                         }
848                 }
849
850                 [Test]
851                 public void TestRoundFailures ()
852                 {
853                         decimal [,] dtab = {
854                                 {1.2345m, 3, 1.234m} 
855                         };
856
857                         decimal d;
858
859                         for (int i = 0; i < dtab.GetLength (0); i++) {
860                                 d = Decimal.Round (dtab [i, 0], (int) dtab [i, 1]);
861                                 if (d != dtab [i, 2]) {
862                                         Fail ("FailRound: Round(" + dtab [i, 0] + "," + (int) dtab [i, 1] + ") != " + d);
863                                 }
864                         }
865                 }
866
867                 [Test]
868                 public void ParseInt64 ()
869                 {
870                         long max = Int64.MaxValue;
871                         Decimal dmax = Decimal.Parse (max.ToString ());
872                         AssertEquals ("Int64.MaxValue", Int64.MaxValue, Decimal.ToInt64 (dmax));
873
874                         long min = Int64.MinValue;
875                         Decimal dmin = Decimal.Parse (min.ToString ());
876                         AssertEquals ("Int64.MinValue", Int64.MinValue, Decimal.ToInt64 (dmin));
877
878                         dmax += 1.1m;
879                         dmax = Decimal.Parse (dmax.ToString ());
880                         AssertEquals ("Int64.MaxValue+1.1", Int64.MaxValue, Decimal.ToInt64 (dmax - 1.1m));
881
882                         dmin -= 1.1m;
883                         dmin = Decimal.Parse (dmin.ToString ());
884                         AssertEquals ("Int64.MinValue-1.1", Int64.MinValue, Decimal.ToInt64 (dmin + 1.1m));
885                 }
886
887                 [Test]
888                 public void ToByte ()
889                 {
890                         Decimal d = 254.9m;
891                         AssertEquals ("Decimal.ToByte", 254, Decimal.ToByte (d));
892                         AssertEquals ("Convert.ToByte", 255, Convert.ToByte (d));
893                         AssertEquals ("IConvertible.ToByte", 255, (d as IConvertible).ToByte (null));
894                 }
895
896                 [Test]
897                 public void ToSByte ()
898                 {
899                         Decimal d = 126.9m;
900                         AssertEquals ("Decimal.ToSByte", 126, Decimal.ToSByte (d));
901                         AssertEquals ("Convert.ToSByte", 127, Convert.ToSByte (d));
902                         AssertEquals ("IConvertible.ToSByte", 127, (d as IConvertible).ToSByte (null));
903                         d = -d;
904                         AssertEquals ("-Decimal.ToSByte", -126, Decimal.ToSByte (d));
905                         AssertEquals ("-Convert.ToSByte", -127, Convert.ToSByte (d));
906                         AssertEquals ("-IConvertible.ToSByte", -127, (d as IConvertible).ToSByte (null));
907                 }
908
909                 [Test]
910                 public void ToInt16 ()
911                 {
912                         Decimal d = 254.9m;
913                         AssertEquals ("Decimal.ToInt16", 254, Decimal.ToInt16 (d));
914                         AssertEquals ("Convert.ToInt16", 255, Convert.ToInt16 (d));
915                         AssertEquals ("IConvertible.ToInt16", 255, (d as IConvertible).ToInt16 (null));
916                         d = -d;
917                         AssertEquals ("-Decimal.ToInt16", -254, Decimal.ToInt16 (d));
918                         AssertEquals ("-Convert.ToInt16", -255, Convert.ToInt16 (d));
919                         AssertEquals ("-IConvertible.ToInt16", -255, (d as IConvertible).ToInt16 (null));
920                 }
921
922                 [Test]
923                 public void ToUInt16 ()
924                 {
925                         Decimal d = 254.9m;
926                         AssertEquals ("Decimal.ToUInt16", 254, Decimal.ToUInt16 (d));
927                         AssertEquals ("Convert.ToUInt16", 255, Convert.ToUInt16 (d));
928                         AssertEquals ("IConvertible.ToUInt16", 255, (d as IConvertible).ToUInt16 (null));
929                 }
930
931                 [Test]
932                 public void ToInt32 ()
933                 {
934                         Decimal d = 254.9m;
935                         AssertEquals ("Decimal.ToInt32", 254, Decimal.ToInt32 (d));
936                         AssertEquals ("Convert.ToInt32", 255, Convert.ToInt32 (d));
937                         AssertEquals ("IConvertible.ToInt32", 255, (d as IConvertible).ToInt32 (null));
938                         d = -d;
939                         AssertEquals ("-Decimal.ToInt32", -254, Decimal.ToInt32 (d));
940                         AssertEquals ("-Convert.ToInt32", -255, Convert.ToInt32 (d));
941                         AssertEquals ("-IConvertible.ToInt32", -255, (d as IConvertible).ToInt32 (null));
942                 }
943
944                 [Test]
945                 public void ToUInt32 ()
946                 {
947                         Decimal d = 254.9m;
948                         AssertEquals ("Decimal.ToUInt32", 254, Decimal.ToUInt32 (d));
949                         AssertEquals ("Convert.ToUInt32", 255, Convert.ToUInt32 (d));
950                         AssertEquals ("IConvertible.ToUInt32", 255, (d as IConvertible).ToUInt32 (null));
951                 }
952
953                 [Test]
954                 public void ToInt64 ()
955                 {
956                         Decimal d = 254.9m;
957                         AssertEquals ("Decimal.ToInt64", 254, Decimal.ToInt64 (d));
958                         AssertEquals ("Convert.ToInt64", 255, Convert.ToInt64 (d));
959                         AssertEquals ("IConvertible.ToInt64", 255, (d as IConvertible).ToInt64 (null));
960                         d = -d;
961                         AssertEquals ("-Decimal.ToInt64", -254, Decimal.ToInt64 (d));
962                         AssertEquals ("-Convert.ToInt64", -255, Convert.ToInt64 (d));
963                         AssertEquals ("-IConvertible.ToInt64", -255, (d as IConvertible).ToInt64 (null));
964                 }
965
966                 [Test]
967                 [ExpectedException (typeof (OverflowException))]
968                 public void ToInt64_TooBig ()
969                 {
970                         Decimal d = (Decimal) Int64.MaxValue;
971                         d += 1.1m;
972                         long value = Decimal.ToInt64 (d);
973                 }
974
975                 [Test]
976                 [ExpectedException (typeof (OverflowException))]
977                 public void ToInt64_TooSmall ()
978                 {
979                         Decimal d = (Decimal) Int64.MinValue;
980                         d -= 1.1m;
981                         long value = Decimal.ToInt64 (d);
982                 }
983
984                 [Test]
985                 public void ToUInt64 ()
986                 {
987                         Decimal d = 254.9m;
988                         AssertEquals ("Decimal.ToUInt64", 254, Decimal.ToUInt64 (d));
989                         AssertEquals ("Convert.ToUInt64", 255, Convert.ToUInt64 (d));
990                         AssertEquals ("IConvertible.ToUInt64", 255, (d as IConvertible).ToUInt64 (null));
991                 }
992
993                 [Test]
994                 public void ToSingle ()
995                 {
996                         Decimal d = 254.9m;
997                         AssertEquals ("Decimal.ToSingle", 254.9f, Decimal.ToSingle (d));
998                         AssertEquals ("Convert.ToSingle", 254.9f, Convert.ToSingle (d));
999                         AssertEquals ("IConvertible.ToSingle", 254.9f, (d as IConvertible).ToSingle (null));
1000                         d = -d;
1001                         AssertEquals ("-Decimal.ToSingle", -254.9f, Decimal.ToSingle (d));
1002                         AssertEquals ("-Convert.ToSingle", -254.9f, Convert.ToSingle (d));
1003                         AssertEquals ("-IConvertible.ToSingle", -254.9f, (d as IConvertible).ToSingle (null));
1004                 }
1005
1006                 [Test]
1007                 public void ToDouble ()
1008                 {
1009                         Decimal d = 254.9m;
1010                         AssertEquals ("Decimal.ToDouble", 254.9d, Decimal.ToDouble (d));
1011                         AssertEquals ("Convert.ToDouble", 254.9d, Convert.ToDouble (d));
1012                         AssertEquals ("IConvertible.ToDouble", 254.9d, (d as IConvertible).ToDouble (null));
1013                         d = -d;
1014                         AssertEquals ("-Decimal.ToDouble", -254.9d, Decimal.ToDouble (d));
1015                         AssertEquals ("-Convert.ToDouble", -254.9d, Convert.ToDouble (d));
1016                         AssertEquals ("-IConvertible.ToDouble", -254.9d, (d as IConvertible).ToDouble (null));
1017                 }
1018
1019                 [Test]
1020                 public void ToString_Defaults ()
1021                 {
1022                         Decimal d = 254.9m;
1023                         // everything defaults to "G"
1024                         string def = d.ToString ("G");
1025                         AssertEquals ("ToString()", def, d.ToString ());
1026                         AssertEquals ("ToString((IFormatProvider)null)", def, d.ToString ((IFormatProvider) null));
1027                         AssertEquals ("ToString((string)null)", def, d.ToString ((string) null));
1028                         AssertEquals ("ToString(empty)", def, d.ToString (String.Empty));
1029                         AssertEquals ("ToString(null,null)", def, d.ToString (null, null));
1030                         AssertEquals ("ToString(empty,null)", def, d.ToString (String.Empty, null));
1031
1032                         AssertEquals ("ToString()", "254.9", def);
1033                 }
1034
1035                 [Test]
1036                 public void CastTruncRounding ()
1037                 {
1038                         // casting truncs decimal value (not normal nor banker's rounding)
1039                         AssertEquals ("254.9==254", 254, (long) (254.9m));
1040                         AssertEquals ("-254.9=-254", -254, (long) (-254.9m));
1041                         AssertEquals ("255.9==256", 255, (long) (255.9m));
1042                         AssertEquals ("-255.9=-256", -255, (long) (-255.9m));
1043                 }
1044
1045                 [Test]
1046                 public void ParseFractions ()
1047                 {
1048                         decimal d1 = Decimal.Parse ("0.523456789012345467890123456789", CultureInfo.InvariantCulture);
1049                         AssertEquals ("f1", 0.5234567890123454678901234568m, d1);
1050                         decimal d2 = Decimal.Parse ("0.49214206543486529434634231456", CultureInfo.InvariantCulture);
1051                         AssertEquals ("f2", 0.4921420654348652943463423146m, d2);
1052                 }
1053
1054                 [Test]
1055                 [ExpectedException (typeof (OverflowException))]
1056                 public void Parse_Int64_Overflow ()
1057                 {
1058                         // Int64.MaxValue + 1 + small fraction to allow 30 digits
1059                         // 123456789012345678901234567890
1060                         decimal d = Decimal.Parse ("9223372036854775808.0000000009", CultureInfo.InvariantCulture);
1061                         long l = (long) d;
1062                 }
1063
1064 #if NET_2_0
1065                 [Test]
1066                 public void TryParse ()
1067                 {
1068                         Decimal r;
1069                 
1070                         // These should return false
1071                         AssertEquals (false, Decimal.TryParse ("79228162514264337593543950336", out r));
1072                         AssertEquals (false, Decimal.TryParse ("123nx", NumberStyles.Number, CultureInfo.InvariantCulture, out r));
1073                         AssertEquals (false, Decimal.TryParse (null, NumberStyles.Number, CultureInfo.InvariantCulture, out r));
1074
1075                         // These should pass
1076                         for (int i = 0; i < tab.Length; i++) {
1077                                 AssertEquals (!tab [i].exceptionFlag,
1078                                         Decimal.TryParse (tab [i].str, tab [i].style,
1079                                         NumberFormatInfo.InvariantInfo, out r));
1080                         }
1081                 }
1082 #endif
1083
1084                 [Test]
1085                 [ExpectedException (typeof (DivideByZeroException))]
1086                 public void Remainder_ByZero ()
1087                 {
1088                         Decimal.Remainder (254.9m, 0m);
1089                 }
1090
1091                 [Test]
1092                 public void Remainder ()
1093                 {
1094                         decimal p1 = 254.9m;
1095                         decimal p2 = 12.1m;
1096                         decimal n1 = -254.9m;
1097                         decimal n2 = -12.1m;
1098
1099                         AssertEquals ("254.9 % 12.1", 0.8m, Decimal.Remainder (p1, p2));
1100                         AssertEquals ("-254.9 % 12.1", -0.8m, Decimal.Remainder (n1, p2));
1101                         AssertEquals ("254.9 % -12.1", 0.8m, Decimal.Remainder (p1, n2));
1102                         AssertEquals ("-254.9 % -12.1", -0.8m, Decimal.Remainder (n1, n2));
1103
1104                         AssertEquals ("12.1 % 254.9", 12.1m, Decimal.Remainder (p2, p1));
1105                         AssertEquals ("-12.1 % 254.9", -12.1m, Decimal.Remainder (n2, p1));
1106                         AssertEquals ("12.1 % -254.9", 12.1m, Decimal.Remainder (p2, n1));
1107                         AssertEquals ("-12.1 % -254.9", -12.1m, Decimal.Remainder (n2, n1));
1108 #if NET_2_0
1109                         AssertEquals ("12.1 % 12.1", 0.0m, Decimal.Remainder (p1, p1));
1110                         AssertEquals ("-12.1 % 12.1", 0.0m, Decimal.Remainder (n1, p1));
1111                         AssertEquals ("12.1 % -12.1", 0.0m, Decimal.Remainder (p1, n1));
1112                         AssertEquals ("-12.1 % -12.1", 0.0m, Decimal.Remainder (n1, n1));
1113 #else
1114                         AssertEquals ("12.1 % 12.1", 0, Decimal.Remainder (p1, p1));
1115                         AssertEquals ("-12.1 % 12.1", 0, Decimal.Remainder (n1, p1));
1116                         AssertEquals ("12.1 % -12.1", 0, Decimal.Remainder (p1, n1));
1117                         AssertEquals ("-12.1 % -12.1", 0, Decimal.Remainder (n1, n1));
1118 #endif
1119                 }
1120
1121                 [Test]
1122                 [ExpectedException (typeof (DivideByZeroException))]
1123                 public void Divide_ByZero ()
1124                 {
1125                         Decimal.Divide (254.9m, 0m);
1126                 }
1127
1128                 [Test]
1129                 public void Divide ()
1130                 {
1131                         decimal p1 = 254.9m;
1132                         decimal p2 = 12.1m;
1133                         decimal n1 = -254.9m;
1134                         decimal n2 = -12.1m;
1135
1136                         decimal c1 = 21.066115702479338842975206612m;
1137                         decimal c2 = 0.0474695959199686151431934092m;
1138
1139                         AssertEquals ("254.9 / 12.1", c1, Decimal.Divide (p1, p2));
1140                         AssertEquals ("-254.9 / 12.1", -c1, Decimal.Divide (n1, p2));
1141                         AssertEquals ("254.9 / -12.1", -c1, Decimal.Divide (p1, n2));
1142                         AssertEquals ("-254.9 / -12.1", c1, Decimal.Divide (n1, n2));
1143
1144                         AssertEquals ("12.1 / 254.9", c2, Decimal.Divide (p2, p1));
1145                         AssertEquals ("-12.1 / 254.9", -c2, Decimal.Divide (n2, p1));
1146                         AssertEquals ("12.1 / -254.9", -c2, Decimal.Divide (p2, n1));
1147                         AssertEquals ("-12.1 / -254.9", c2, Decimal.Divide (n2, n1));
1148
1149                         AssertEquals ("12.1 / 12.1", 1, Decimal.Divide (p1, p1));
1150                         AssertEquals ("-12.1 / 12.1", -1, Decimal.Divide (n1, p1));
1151                         AssertEquals ("12.1 / -12.1", -1, Decimal.Divide (p1, n1));
1152                         AssertEquals ("-12.1 / -12.1", 1, Decimal.Divide (n1, n1));
1153                 }
1154
1155                 [Test]
1156                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1157                 public void Round_InvalidDecimals_Negative ()
1158                 {
1159                         Decimal.Round (254.9m, -1);
1160                 }
1161
1162                 [Test]
1163                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1164                 public void Round_InvalidDecimals_TooHigh ()
1165                 {
1166                         Decimal.Round (254.9m, 29);
1167                 }
1168
1169                 [Test]
1170                 public void Round_OddValue ()
1171                 {
1172                         decimal five = 5.5555555555555555555555555555m;
1173                         AssertEquals ("5,5_,00", 6, Decimal.Round (five, 0));
1174                         AssertEquals ("5,5_,01", 5.6m, Decimal.Round (five, 1));
1175                         AssertEquals ("5,5_,02", 5.56m, Decimal.Round (five, 2));
1176                         AssertEquals ("5,5_,03", 5.556m, Decimal.Round (five, 3));
1177                         AssertEquals ("5,5_,04", 5.5556m, Decimal.Round (five, 4));
1178                         AssertEquals ("5,5_,05", 5.55556m, Decimal.Round (five, 5));
1179                         AssertEquals ("5,5_,06", 5.555556m, Decimal.Round (five, 6));
1180                         AssertEquals ("5,5_,07", 5.5555556m, Decimal.Round (five, 7));
1181                         AssertEquals ("5,5_,08", 5.55555556m, Decimal.Round (five, 8));
1182                         AssertEquals ("5,5_,09", 5.555555556m, Decimal.Round (five, 9));
1183                         AssertEquals ("5,5_,10", 5.5555555556m, Decimal.Round (five, 10));
1184                         AssertEquals ("5,5_,11", 5.55555555556m, Decimal.Round (five, 11));
1185                         AssertEquals ("5,5_,12", 5.555555555556m, Decimal.Round (five, 12));
1186                         AssertEquals ("5,5_,13", 5.5555555555556m, Decimal.Round (five, 13));
1187                         AssertEquals ("5,5_,14", 5.55555555555556m, Decimal.Round (five, 14));
1188                         AssertEquals ("5,5_,15", 5.555555555555556m, Decimal.Round (five, 15));
1189                         AssertEquals ("5,5_,16", 5.5555555555555556m, Decimal.Round (five, 16));
1190                         AssertEquals ("5,5_,17", 5.55555555555555556m, Decimal.Round (five, 17));
1191                         AssertEquals ("5,5_,18", 5.555555555555555556m, Decimal.Round (five, 18));
1192                         AssertEquals ("5,5_,19", 5.5555555555555555556m, Decimal.Round (five, 19));
1193                         AssertEquals ("5,5_,20", 5.55555555555555555556m, Decimal.Round (five, 20));
1194                         AssertEquals ("5,5_,21", 5.555555555555555555556m, Decimal.Round (five, 21));
1195                         AssertEquals ("5,5_,22", 5.5555555555555555555556m, Decimal.Round (five, 22));
1196                         AssertEquals ("5,5_,23", 5.55555555555555555555556m, Decimal.Round (five, 23));
1197                         AssertEquals ("5,5_,24", 5.555555555555555555555556m, Decimal.Round (five, 24));
1198                         AssertEquals ("5,5_,25", 5.5555555555555555555555556m, Decimal.Round (five, 25));
1199                         AssertEquals ("5,5_,26", 5.55555555555555555555555556m, Decimal.Round (five, 26));
1200                         AssertEquals ("5,5_,27", 5.555555555555555555555555556m, Decimal.Round (five, 27));
1201                         AssertEquals ("5.5_,28", 5.5555555555555555555555555555m, Decimal.Round (five, 28));
1202                 }
1203
1204                 [Test]
1205                 public void Round_EvenValue ()
1206                 {
1207                         AssertEquals ("2,2_5,00", 2, Decimal.Round (2.5m, 0));
1208                         AssertEquals ("2,2_5,01", 2.2m, Decimal.Round (2.25m, 1));
1209                         AssertEquals ("2,2_5,02", 2.22m, Decimal.Round (2.225m, 2));
1210                         AssertEquals ("2,2_5,03", 2.222m, Decimal.Round (2.2225m, 3));
1211                         AssertEquals ("2,2_5,04", 2.2222m, Decimal.Round (2.22225m, 4));
1212                         AssertEquals ("2,2_5,05", 2.22222m, Decimal.Round (2.222225m, 5));
1213                         AssertEquals ("2,2_5,06", 2.222222m, Decimal.Round (2.2222225m, 6));
1214                         AssertEquals ("2,2_5,07", 2.2222222m, Decimal.Round (2.22222225m, 7));
1215                         AssertEquals ("2,2_5,08", 2.22222222m, Decimal.Round (2.222222225m, 8));
1216                         AssertEquals ("2,2_5,09", 2.222222222m, Decimal.Round (2.2222222225m, 9));
1217                         AssertEquals ("2,2_5,10", 2.2222222222m, Decimal.Round (2.22222222225m, 10));
1218                         AssertEquals ("2,2_5,11", 2.22222222222m, Decimal.Round (2.222222222225m, 11));
1219                         AssertEquals ("2,2_5,12", 2.222222222222m, Decimal.Round (2.2222222222225m, 12));
1220                         AssertEquals ("2,2_5,13", 2.2222222222222m, Decimal.Round (2.22222222222225m, 13));
1221                         AssertEquals ("2,2_5,14", 2.22222222222222m, Decimal.Round (2.222222222222225m, 14));
1222                         AssertEquals ("2,2_5,15", 2.222222222222222m, Decimal.Round (2.2222222222222225m, 15));
1223                         AssertEquals ("2,2_5,16", 2.2222222222222222m, Decimal.Round (2.22222222222222225m, 16));
1224                         AssertEquals ("2,2_5,17", 2.22222222222222222m, Decimal.Round (2.222222222222222225m, 17));
1225                         AssertEquals ("2,2_5,18", 2.222222222222222222m, Decimal.Round (2.2222222222222222225m, 18));
1226                         AssertEquals ("2,2_5,19", 2.2222222222222222222m, Decimal.Round (2.22222222222222222225m, 19));
1227                         AssertEquals ("2,2_5,20", 2.22222222222222222222m, Decimal.Round (2.222222222222222222225m, 20));
1228                         AssertEquals ("2,2_5,21", 2.222222222222222222222m, Decimal.Round (2.2222222222222222222225m, 21));
1229                         AssertEquals ("2,2_5,22", 2.2222222222222222222222m, Decimal.Round (2.22222222222222222222225m, 22));
1230                         AssertEquals ("2,2_5,23", 2.22222222222222222222222m, Decimal.Round (2.222222222222222222222225m, 23));
1231                         AssertEquals ("2,2_5,24", 2.222222222222222222222222m, Decimal.Round (2.2222222222222222222222225m, 24));
1232                         AssertEquals ("2,2_5,25", 2.2222222222222222222222222m, Decimal.Round (2.22222222222222222222222225m, 25));
1233                         AssertEquals ("2,2_5,26", 2.22222222222222222222222222m, Decimal.Round (2.222222222222222222222222225m, 26));
1234                         AssertEquals ("2,2_5,27", 2.222222222222222222222222222m, Decimal.Round (2.2222222222222222222222222225m, 27));
1235                         AssertEquals ("2,2_5,28", 2.2222222222222222222222222222m, Decimal.Round (2.22222222222222222222222222225m, 28));
1236                 }
1237
1238                 [Test]
1239                 public void Round_OddValue_Negative ()
1240                 {
1241                         decimal five = -5.5555555555555555555555555555m;
1242                         AssertEquals ("-5,5_,00", -6, Decimal.Round (five, 0));
1243                         AssertEquals ("-5,5_,01", -5.6m, Decimal.Round (five, 1));
1244                         AssertEquals ("-5,5_,02", -5.56m, Decimal.Round (five, 2));
1245                         AssertEquals ("-5,5_,03", -5.556m, Decimal.Round (five, 3));
1246                         AssertEquals ("-5,5_,04", -5.5556m, Decimal.Round (five, 4));
1247                         AssertEquals ("-5,5_,05", -5.55556m, Decimal.Round (five, 5));
1248                         AssertEquals ("-5,5_,06", -5.555556m, Decimal.Round (five, 6));
1249                         AssertEquals ("-5,5_,07", -5.5555556m, Decimal.Round (five, 7));
1250                         AssertEquals ("-5,5_,08", -5.55555556m, Decimal.Round (five, 8));
1251                         AssertEquals ("-5,5_,09", -5.555555556m, Decimal.Round (five, 9));
1252                         AssertEquals ("-5,5_,10", -5.5555555556m, Decimal.Round (five, 10));
1253                         AssertEquals ("-5,5_,11", -5.55555555556m, Decimal.Round (five, 11));
1254                         AssertEquals ("-5,5_,12", -5.555555555556m, Decimal.Round (five, 12));
1255                         AssertEquals ("-5,5_,13", -5.5555555555556m, Decimal.Round (five, 13));
1256                         AssertEquals ("-5,5_,14", -5.55555555555556m, Decimal.Round (five, 14));
1257                         AssertEquals ("-5,5_,15", -5.555555555555556m, Decimal.Round (five, 15));
1258                         AssertEquals ("-5,5_,16", -5.5555555555555556m, Decimal.Round (five, 16));
1259                         AssertEquals ("-5,5_,17", -5.55555555555555556m, Decimal.Round (five, 17));
1260                         AssertEquals ("-5,5_,18", -5.555555555555555556m, Decimal.Round (five, 18));
1261                         AssertEquals ("-5,5_,19", -5.5555555555555555556m, Decimal.Round (five, 19));
1262                         AssertEquals ("-5,5_,20", -5.55555555555555555556m, Decimal.Round (five, 20));
1263                         AssertEquals ("-5,5_,21", -5.555555555555555555556m, Decimal.Round (five, 21));
1264                         AssertEquals ("-5,5_,22", -5.5555555555555555555556m, Decimal.Round (five, 22));
1265                         AssertEquals ("-5,5_,23", -5.55555555555555555555556m, Decimal.Round (five, 23));
1266                         AssertEquals ("-5,5_,24", -5.555555555555555555555556m, Decimal.Round (five, 24));
1267                         AssertEquals ("-5,5_,25", -5.5555555555555555555555556m, Decimal.Round (five, 25));
1268                         AssertEquals ("-5,5_,26", -5.55555555555555555555555556m, Decimal.Round (five, 26));
1269                         AssertEquals ("-5,5_,27", -5.555555555555555555555555556m, Decimal.Round (five, 27));
1270                         AssertEquals ("-5.5_,28", -5.5555555555555555555555555555m, Decimal.Round (five, 28));
1271                 }
1272
1273                 [Test]
1274                 public void Round_EvenValue_Negative ()
1275                 {
1276                         AssertEquals ("-2,2_5,00", -2, Decimal.Round (-2.5m, 0));
1277                         AssertEquals ("-2,2_5,01", -2.2m, Decimal.Round (-2.25m, 1));
1278                         AssertEquals ("-2,2_5,02", -2.22m, Decimal.Round (-2.225m, 2));
1279                         AssertEquals ("-2,2_5,03", -2.222m, Decimal.Round (-2.2225m, 3));
1280                         AssertEquals ("-2,2_5,04", -2.2222m, Decimal.Round (-2.22225m, 4));
1281                         AssertEquals ("-2,2_5,05", -2.22222m, Decimal.Round (-2.222225m, 5));
1282                         AssertEquals ("-2,2_5,06", -2.222222m, Decimal.Round (-2.2222225m, 6));
1283                         AssertEquals ("-2,2_5,07", -2.2222222m, Decimal.Round (-2.22222225m, 7));
1284                         AssertEquals ("-2,2_5,08", -2.22222222m, Decimal.Round (-2.222222225m, 8));
1285                         AssertEquals ("-2,2_5,09", -2.222222222m, Decimal.Round (-2.2222222225m, 9));
1286                         AssertEquals ("-2,2_5,10", -2.2222222222m, Decimal.Round (-2.22222222225m, 10));
1287                         AssertEquals ("-2,2_5,11", -2.22222222222m, Decimal.Round (-2.222222222225m, 11));
1288                         AssertEquals ("-2,2_5,12", -2.222222222222m, Decimal.Round (-2.2222222222225m, 12));
1289                         AssertEquals ("-2,2_5,13", -2.2222222222222m, Decimal.Round (-2.22222222222225m, 13));
1290                         AssertEquals ("-2,2_5,14", -2.22222222222222m, Decimal.Round (-2.222222222222225m, 14));
1291                         AssertEquals ("-2,2_5,15", -2.222222222222222m, Decimal.Round (-2.2222222222222225m, 15));
1292                         AssertEquals ("-2,2_5,16", -2.2222222222222222m, Decimal.Round (-2.22222222222222225m, 16));
1293                         AssertEquals ("-2,2_5,17", -2.22222222222222222m, Decimal.Round (-2.222222222222222225m, 17));
1294                         AssertEquals ("-2,2_5,18", -2.222222222222222222m, Decimal.Round (-2.2222222222222222225m, 18));
1295                         AssertEquals ("-2,2_5,19", -2.2222222222222222222m, Decimal.Round (-2.22222222222222222225m, 19));
1296                         AssertEquals ("-2,2_5,20", -2.22222222222222222222m, Decimal.Round (-2.222222222222222222225m, 20));
1297                         AssertEquals ("-2,2_5,21", -2.222222222222222222222m, Decimal.Round (-2.2222222222222222222225m, 21));
1298                         AssertEquals ("-2,2_5,22", -2.2222222222222222222222m, Decimal.Round (-2.22222222222222222222225m, 22));
1299                         AssertEquals ("-2,2_5,23", -2.22222222222222222222222m, Decimal.Round (-2.222222222222222222222225m, 23));
1300                         AssertEquals ("-2,2_5,24", -2.222222222222222222222222m, Decimal.Round (-2.2222222222222222222222225m, 24));
1301                         AssertEquals ("-2,2_5,25", -2.2222222222222222222222222m, Decimal.Round (-2.22222222222222222222222225m, 25));
1302                         AssertEquals ("-2,2_5,26", -2.22222222222222222222222222m, Decimal.Round (-2.222222222222222222222222225m, 26));
1303                         AssertEquals ("-2,2_5,27", -2.222222222222222222222222222m, Decimal.Round (-2.2222222222222222222222222225m, 27));
1304                         AssertEquals ("-2,2_5,28", -2.2222222222222222222222222222m, Decimal.Round (-2.22222222222222222222222222225m, 28));
1305                 }
1306
1307                 [Test] // bug #59425
1308                 public void ParseAndKeepPrecision ()
1309                 {
1310                         string value = "5";
1311                         AssertEquals (value, value, Decimal.Parse (value).ToString ());
1312                         value += '.';
1313                         for (int i = 0; i < 28; i++) {
1314                                 value += "0";
1315                                 AssertEquals (i.ToString (), value, Decimal.Parse (value).ToString ());
1316                         }
1317
1318                         value = "-5";
1319                         AssertEquals (value, value, Decimal.Parse (value).ToString ());
1320                         value += '.';
1321                         for (int i = 0; i < 28; i++) {
1322                                 value += "0";
1323                                 AssertEquals ("-" + i.ToString (), value, Decimal.Parse (value).ToString ());
1324                         }
1325                 }
1326
1327                 [Test]
1328                 public void ToString_G ()
1329                 {
1330                         AssertEquals ("00", "1.0", (1.0m).ToString ());
1331                         AssertEquals ("01", "0.1", (0.1m).ToString ());
1332                         AssertEquals ("02", "0.01", (0.01m).ToString ());
1333                         AssertEquals ("03", "0.001", (0.001m).ToString ());
1334                         AssertEquals ("04", "0.0001", (0.0001m).ToString ());
1335                         AssertEquals ("05", "0.00001", (0.00001m).ToString ());
1336                         AssertEquals ("06", "0.000001", (0.000001m).ToString ());
1337                         AssertEquals ("07", "0.0000001", (0.0000001m).ToString ());
1338                         AssertEquals ("08", "0.00000001", (0.00000001m).ToString ());
1339                         AssertEquals ("09", "0.000000001", (0.000000001m).ToString ());
1340                         AssertEquals ("10", "0.0000000001", (0.0000000001m).ToString ());
1341                         AssertEquals ("11", "0.00000000001", (0.00000000001m).ToString ());
1342                         AssertEquals ("12", "0.000000000001", (0.000000000001m).ToString ());
1343                         AssertEquals ("13", "0.0000000000001", (0.0000000000001m).ToString ());
1344                         AssertEquals ("14", "0.00000000000001", (0.00000000000001m).ToString ());
1345                         AssertEquals ("15", "0.000000000000001", (0.000000000000001m).ToString ());
1346                         AssertEquals ("16", "0.0000000000000001", (0.0000000000000001m).ToString ());
1347                         AssertEquals ("17", "0.00000000000000001", (0.00000000000000001m).ToString ());
1348                         AssertEquals ("18", "0.000000000000000001", (0.000000000000000001m).ToString ());
1349                         AssertEquals ("19", "0.0000000000000000001", (0.0000000000000000001m).ToString ());
1350                         AssertEquals ("20", "0.00000000000000000001", (0.00000000000000000001m).ToString ());
1351                         AssertEquals ("21", "0.000000000000000000001", (0.000000000000000000001m).ToString ());
1352                         AssertEquals ("22", "0.0000000000000000000001", (0.0000000000000000000001m).ToString ());
1353                         AssertEquals ("23", "0.00000000000000000000001", (0.00000000000000000000001m).ToString ());
1354                         AssertEquals ("24", "0.000000000000000000000001", (0.000000000000000000000001m).ToString ());
1355                         AssertEquals ("25", "0.0000000000000000000000001", (0.0000000000000000000000001m).ToString ());
1356                         AssertEquals ("26", "0.00000000000000000000000001", (0.00000000000000000000000001m).ToString ());
1357                         AssertEquals ("27", "0.000000000000000000000000001", (0.000000000000000000000000001m).ToString ());
1358                         AssertEquals ("28", "0.0000000000000000000000000001", (0.0000000000000000000000000001m).ToString ());
1359                 }
1360         }
1361 }