Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / corlib / Test / System / MathTest.cs
1 // MathTest.cs
2 //
3 // Jon Guymon (guymon@slackworks.com)
4 // Pedro Martínez Juliá (yoros@wanadoo.es)
5 //
6 // (C) 2002 Jon Guymon
7 // Copyright (C) 2003 Pedro Martínez Juliá <yoros@wanadoo.es>
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 // 
10
11 using System;
12 using NUnit.Framework;
13
14 namespace MonoTests.System
15 {
16         [TestFixture]
17         public class MathTest
18         {
19                 private static double double_epsilon =
20 #if TARGET_JVM
21                         1E-15;
22 #else
23                         double.Epsilon;
24 #endif
25
26                 static double x = 0.1234;
27                 static double y = 12.345;
28
29                 [Test]
30                 public void TestDecimalAbs ()
31                 {
32                         decimal a = -9.0M;
33
34                         Assert.IsTrue (9.0M == Math.Abs (a), "#1");
35                         Assert.IsTrue (Decimal.MaxValue == Math.Abs (Decimal.MaxValue), "#2");
36                         Assert.IsTrue (Decimal.MaxValue == Math.Abs (Decimal.MinValue), "#3");
37                         Assert.IsTrue (Decimal.Zero == Math.Abs (Decimal.Zero), "#4");
38                         Assert.IsTrue (Decimal.One == Math.Abs (Decimal.One), "#5");
39                         Assert.IsTrue (Decimal.One == Math.Abs (Decimal.MinusOne), "#6");
40                 }
41
42                 [Test]
43                 public void TestDoubleAbs ()
44                 {
45                         double a = -9.0D;
46
47                         Assert.IsTrue (9.0D == Math.Abs (a), "#1");
48                         Assert.IsTrue (0.0D == Math.Abs (0.0D), "#2");
49                         Assert.IsTrue (Double.MaxValue == Math.Abs (Double.MaxValue), "#3");
50                         Assert.IsTrue (Double.MaxValue == Math.Abs (Double.MinValue), "#4");
51                         Assert.IsTrue (Double.IsPositiveInfinity (Math.Abs (Double.PositiveInfinity)), "#5");
52                         Assert.IsTrue (Double.IsPositiveInfinity (Math.Abs (Double.NegativeInfinity)), "#6");
53                         Assert.IsTrue (Double.IsNaN (Math.Abs (Double.NaN)), "#7");
54                 }
55
56                 [Test]
57                 public void TestFloatAbs ()
58                 {
59                         float a = -9.0F;
60
61                         Assert.IsTrue (9.0F == Math.Abs (a), "#1");
62                         Assert.IsTrue (0.0F == Math.Abs (0.0F), "#2");
63                         Assert.IsTrue (Single.MaxValue == Math.Abs (Single.MaxValue), "#3");
64                         Assert.IsTrue (Single.MaxValue == Math.Abs (Single.MinValue), "#4");
65                         Assert.IsTrue (Single.PositiveInfinity == Math.Abs (Single.PositiveInfinity), "#5");
66                         Assert.IsTrue (Single.PositiveInfinity == Math.Abs (Single.NegativeInfinity), "#6");
67                         Assert.IsTrue (Single.IsNaN (Math.Abs (Single.NaN)), "#7");
68                 }
69
70                 [Test]
71                 public void TestLongAbs ()
72                 {
73                         long a = -9L;
74                         long b = Int64.MinValue;
75
76                         Assert.IsTrue (9L == Math.Abs (a), "#1");
77                         try {
78                                 Math.Abs (b);
79                                 Assert.Fail ("#2");
80                         } catch (Exception e) {
81                                 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
82                         }
83                         Assert.IsTrue (Int64.MaxValue == Math.Abs (Int64.MaxValue), "#4");
84                 }
85
86                 [Test]
87                 public void TestIntAbs ()
88                 {
89                         int a = -9;
90                         int b = Int32.MinValue;
91
92                         Assert.IsTrue (9 == Math.Abs (a), "#1");
93                         try {
94                                 Math.Abs (b);
95                                 Assert.Fail ("#2");
96                         } catch (Exception e) {
97                                 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
98                         }
99                         Assert.IsTrue (Int32.MaxValue == Math.Abs (Int32.MaxValue), "#4");
100                 }
101
102                 [Test]
103                 public void TestSbyteAbs ()
104                 {
105                         sbyte a = -9;
106                         sbyte b = SByte.MinValue;
107
108                         Assert.IsTrue (9 == Math.Abs (a), "#1");
109                         try {
110                                 Math.Abs (b);
111                                 Assert.Fail ("#2");
112                         } catch (Exception e) {
113                                 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
114                         }
115                         Assert.IsTrue (SByte.MaxValue == Math.Abs (SByte.MaxValue), "#4");
116                 }
117
118                 [Test]
119                 public void TestShortAbs ()
120                 {
121                         short a = -9;
122                         short b = Int16.MinValue;
123
124                         Assert.IsTrue (9 == Math.Abs (a), "#1");
125                         try {
126                                 Math.Abs (b);
127                                 Assert.Fail ("#2");
128                         } catch (Exception e) {
129                                 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
130                         }
131                         Assert.IsTrue (Int16.MaxValue == Math.Abs (Int16.MaxValue), "#4");
132                 }
133
134                 [Test]
135                 public void TestAcos ()
136                 {
137                         double a = Math.Acos (x);
138                         double b = 1.4470809809523457;
139
140                         bool regularTest = (Math.Abs (a - b) <= double_epsilon);
141                         if (!regularTest){
142                                 //
143                                 // On MacOS X libc acos (0.1234) returns
144                                 // 1.4470809809523455 (hex 0x3ff7273e62fda9ab) instead
145                                 // of 1.4470809809523457 (hex 0x3ff7273e62fda9ac)
146                                 //
147                                 // For now, let it go
148                                 //
149                                 if (a == 1.4470809809523455)
150                                         regularTest = true;
151                         }
152                         
153                         Assert.IsTrue (regularTest, a.ToString ("G99") + " != " + b.ToString ("G99"));
154                         
155                         Assert.IsTrue (double.IsNaN (Math.Acos (-1.01D)));
156                         Assert.IsTrue (double.IsNaN (Math.Acos (1.01D)));
157                         Assert.IsTrue (double.IsNaN (Math.Acos (Double.MinValue)));
158                         Assert.IsTrue (double.IsNaN (Math.Acos (Double.MaxValue)));
159                         Assert.IsTrue (double.IsNaN (Math.Acos (Double.NegativeInfinity)));
160                         Assert.IsTrue (double.IsNaN (Math.Acos (Double.PositiveInfinity)));
161                 }
162
163                 [Test]
164                 public void TestAsin ()
165                 {
166                         double a = Math.Asin (x);
167                         double b = 0.12371534584255098;
168
169                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
170                                 + " != " + b.ToString ("G99"));
171                         Assert.IsTrue (double.IsNaN (Math.Asin (-1.01D)));
172                         Assert.IsTrue (double.IsNaN (Math.Asin (1.01D)));
173                         Assert.IsTrue (double.IsNaN (Math.Asin (Double.MinValue)));
174                         Assert.IsTrue (double.IsNaN (Math.Asin (Double.MaxValue)));
175                         Assert.IsTrue (double.IsNaN (Math.Asin (Double.NegativeInfinity)));
176                         Assert.IsTrue (double.IsNaN (Math.Asin (Double.PositiveInfinity)));
177                 }
178
179                 [Test]
180                 public void TestAtan ()
181                 {
182                         double a = Math.Atan (x);
183                         double b = 0.12277930094473837;
184                         double c = 1.5707963267948966;
185                         double d = -1.5707963267948966;
186
187                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), "#1: " + a.ToString ("G99")
188                                 + " != " + b.ToString ("G99"));
189                         Assert.IsTrue (double.IsNaN (Math.Atan (double.NaN)), "should return NaN");
190                         Assert.IsTrue (Math.Abs ((double) Math.Atan (double.PositiveInfinity) - c) <= 0.0000000000000001,
191                                 "#2: " + Math.Atan (double.PositiveInfinity).ToString ("G99") + " != " + c.ToString ("G99"));
192                         Assert.IsTrue (Math.Abs ((double) Math.Atan (double.NegativeInfinity) - d) <= 0.0000000000000001,
193                                 "#3: " + Math.Atan (double.NegativeInfinity).ToString ("G99") + " != " + d.ToString ("G99"));
194                 }
195
196                 [Test]
197                 public void TestAtan2 ()
198                 {
199                         double a = Math.Atan2 (x, y);
200                         double b = 0.0099956168687207747;
201
202                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
203                                 + " != " + b.ToString ("G99"));
204                         Assert.IsTrue (double.IsNaN (Math.Acos (-2D)));
205                         Assert.IsTrue (double.IsNaN (Math.Acos (2D)));
206                 }
207
208                 // The following test is for methods that are in ECMA but they are
209                 // not implemented in MS.NET. I leave them commented.
210                 /*
211                 public void TestBigMul () {
212                         int a = int.MaxValue;
213                         int b = int.MaxValue;
214
215                         Assert(((long)a * (long)b) == Math.BigMul(a,b));
216                 }
217                 */
218
219                 [Test]
220                 public void TestCos ()
221                 {
222                         double a = Math.Cos (x);
223                         double b = 0.99239587670489104;
224
225                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
226                                 + " != " + b.ToString ("G99"));
227                         Assert.IsTrue (double.IsNaN (Math.Cos (Double.NaN)));
228                         Assert.IsTrue (double.IsNaN (Math.Cos (Double.NegativeInfinity)));
229                         Assert.IsTrue (double.IsNaN (Math.Cos (Double.PositiveInfinity)));
230                 }
231
232                 [Test]
233                 public void TestCosh ()
234                 {
235                         double a = Math.Cosh (x);
236                         double b = 1.0076234465130722;
237
238                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
239                                 + " != " + b.ToString ("G99"));
240                         Assert.IsTrue (Math.Cosh (double.NegativeInfinity) == double.PositiveInfinity);
241                         Assert.IsTrue (Math.Cosh (double.PositiveInfinity) == double.PositiveInfinity);
242                         Assert.IsTrue (double.IsNaN (Math.Cosh (double.NaN)));
243                 }
244
245                 // The following test is for methods that are in ECMA but they are
246                 // not implemented in MS.NET. I leave them commented.
247                 /*
248                 public void TestIntDivRem () {
249                         int a = 5;
250                         int b = 2;
251                         int div = 0, rem = 0;
252
253                         div = Math.DivRem (a, b, out rem);
254
255                         Assert.IsTrue (rem == 1);
256                         Assert.IsTrue (div == 2);
257                 }
258
259                 public void TestLongDivRem () {
260                         long a = 5;
261                         long b = 2;
262                         long div = 0, rem = 0;
263
264                         div = Math.DivRem (a, b, out rem);
265
266                         Assert.IsTrue (rem == 1);
267                         Assert.IsTrue (div == 2);
268                 }
269                 */
270
271                 [Test]
272                 public void TestSin ()
273                 {
274                         double a = Math.Sin (x);
275                         double b = 0.12308705821137626;
276
277                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
278                                 + " != " + b.ToString ("G99"));
279                         Assert.IsTrue (double.IsNaN (Math.Sin (Double.NaN)));
280                         Assert.IsTrue (double.IsNaN (Math.Sin (Double.NegativeInfinity)));
281                         Assert.IsTrue (double.IsNaN (Math.Sin (Double.PositiveInfinity)));
282                 }
283
284                 [Test]
285                 public void TestSinh ()
286                 {
287                         double a = Math.Sinh (x);
288                         double b = 0.12371341868561381;
289
290                         Assert.IsTrue (Math.Abs (a - b) <= 0.0000000000000001, a.ToString ("G99")
291                                 + " != " + b.ToString ("G99"));
292                         Assert.IsTrue (double.IsNaN (Math.Sinh (Double.NaN)));
293                         Assert.IsTrue (double.IsNegativeInfinity (Math.Sinh (Double.NegativeInfinity)));
294                         Assert.IsTrue (double.IsPositiveInfinity (Math.Sinh (Double.PositiveInfinity)));
295                 }
296
297                 [Test]
298                 public void TestTan ()
299                 {
300                         double a = Math.Tan (x);
301                         double b = 0.12403019913793806;
302
303                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
304                                 + " != " + b.ToString ("G99"));
305                         Assert.IsTrue (Double.IsNaN (Math.Tan (Double.NaN)));
306                         Assert.IsTrue (Double.IsNaN (Math.Tan (Double.PositiveInfinity)));
307                         Assert.IsTrue (Double.IsNaN (Math.Tan (Double.NegativeInfinity)));
308                 }
309
310                 [Test]
311                 public void TestTanh ()
312                 {
313                         double a = Math.Tanh (x);
314                         double b = 0.12277743150353424;
315
316                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
317                                 + " != " + b.ToString ("G99"));
318                         Assert.IsTrue (Double.IsNaN (Math.Tanh (Double.NaN)),
319                                 "Tanh(NaN) should be NaN");
320                         Assert.IsTrue (1 == Math.Tanh (Double.PositiveInfinity),
321                                 "Tanh(+Infinity) should be 1");
322                         Assert.IsTrue (-1 == Math.Tanh (Double.NegativeInfinity),
323                                 "Tanh(-Infinity) should be -1");
324                 }
325
326                 [Test]
327                 public void TestSqrt ()
328                 {
329                         double a = Math.Sqrt (x);
330                         double b = 0.35128336140500593;
331
332                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
333                                 + " != " + b.ToString ("G99"));
334                         Assert.IsTrue (Double.IsNaN (Math.Sqrt (Double.NaN)));
335                         Assert.IsTrue (Double.IsPositiveInfinity (Math.Sqrt (Double.PositiveInfinity)));
336                         Assert.IsTrue (Double.IsNaN (Math.Sqrt (Double.NegativeInfinity)));
337                 }
338
339                 [Test]
340                 public void TestExp ()
341                 {
342                         double a = Math.Exp (x);
343                         double b = 1.1313368651986859;
344
345                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
346                                 + " != " + b.ToString ("G99"));
347                         Assert.IsTrue (double.IsNaN (Math.Exp (double.NaN)));
348                         Assert.IsTrue (Math.Exp (double.NegativeInfinity) == 0);
349                         Assert.IsTrue (Math.Exp (double.PositiveInfinity) == double.PositiveInfinity);
350                 }
351
352                 [Test]
353                 public void TestCeiling ()
354                 {
355                         int iTest = 1;
356                         try {
357                                 double a = Math.Ceiling (1.5);
358                                 double b = 2;
359
360                                 iTest++;
361                                 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
362                                         + " != " + b.ToString ("G99"));
363                                 iTest++;
364                                 Assert.IsTrue (Math.Ceiling (double.NegativeInfinity) == double.NegativeInfinity);
365                                 iTest++;
366                                 Assert.IsTrue (Math.Ceiling (double.PositiveInfinity) == double.PositiveInfinity);
367                                 iTest++;
368                                 Assert.IsTrue (double.IsNaN (Math.Ceiling (double.NaN)));
369
370                                 iTest++;
371                                 Assert.IsTrue (Double.MaxValue == Math.Ceiling (Double.MaxValue));
372
373                                 iTest++;
374                                 Assert.IsTrue (Double.MinValue == Math.Ceiling (Double.MinValue));
375                         } catch (Exception e) {
376                                 Assert.Fail ("Unexpected Exception at iTest=" + iTest + ": " + e);
377                         }
378                 }
379
380 #if NET_2_0
381                 [Test]
382                 public void TestDecimalCeiling()
383                 {
384                         decimal a = Math.Ceiling(1.5M);
385                         decimal b = 2M;
386
387                         Assert.IsTrue (a == b, "#1");
388                         Assert.IsTrue (Decimal.MaxValue == Math.Ceiling(Decimal.MaxValue), "#2");
389                         Assert.IsTrue (Decimal.MinValue == Math.Ceiling(Decimal.MinValue), "#3");
390                 }
391 #endif
392
393                 [Test]
394                 public void TestFloor ()
395                 {
396                         double a = Math.Floor (1.5);
397                         double b = 1;
398
399                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
400                                 + " != " + b.ToString ("G99"));
401                         Assert.IsTrue (Math.Floor (double.NegativeInfinity) == double.NegativeInfinity);
402                         Assert.IsTrue (Math.Floor (double.PositiveInfinity) == double.PositiveInfinity);
403                         Assert.IsTrue (double.IsNaN (Math.Floor (double.NaN)));
404
405                         Assert.IsTrue (Double.MaxValue == Math.Floor (Double.MaxValue));
406
407                         Assert.IsTrue (Double.MinValue == Math.Floor (Double.MinValue));
408                 }
409
410                 [Test]
411                 public void TestIEEERemainder ()
412                 {
413                         double a = Math.IEEERemainder (y, x);
414                         double b = 0.0050000000000007816;
415
416                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
417                                 + " != " + b.ToString ("G99"));
418
419                         Assert.IsTrue (double.IsNaN (Math.IEEERemainder (y, 0)), "Positive 0");
420
421                         // http://www.obtuse.com/resources/negative_zero.html
422                         double n0 = BitConverter.Int64BitsToDouble (Int64.MinValue);
423                         Assert.IsTrue (double.IsNaN (Math.IEEERemainder (n0, 0)), "Negative 0");
424
425                         // the "zero" remainder of negative number is negative
426                         long result = BitConverter.DoubleToInt64Bits (Math.IEEERemainder (-1, 1));
427                         Assert.AreEqual (Int64.MinValue, result, "Negative Dividend");
428                 }
429
430                 [Test]
431                 public void TestLog ()
432                 {
433                         double a = Math.Log (y);
434                         double b = 2.513251122797143;
435
436                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
437                                 + " != " + b.ToString ("G99"));
438                         Assert.IsTrue (double.IsNaN (Math.Log (-1)));
439                         Assert.IsTrue (double.IsNaN (Math.Log (double.NaN)));
440
441                         // MS docs say this should be PositiveInfinity
442                         Assert.IsTrue (Math.Log (0) == double.NegativeInfinity);
443                         Assert.IsTrue (Math.Log (double.PositiveInfinity) == double.PositiveInfinity);
444                 }
445
446                 [Test]
447                 public void TestLog2 ()
448                 {
449                         double a = Math.Log (x, y);
450                         double b = -0.83251695325303621;
451
452                         Assert.IsTrue ((Math.Abs (a - b) <= 1e-14), a + " != " + b
453                                 + " because diff is " + Math.Abs (a - b));
454                         Assert.IsTrue (double.IsNaN (Math.Log (-1, y)));
455                         Assert.IsTrue (double.IsNaN (Math.Log (double.NaN, y)));
456                         Assert.IsTrue (double.IsNaN (Math.Log (x, double.NaN)));
457                         Assert.IsTrue (double.IsNaN (Math.Log (double.NegativeInfinity, y)));
458                         Assert.IsTrue (double.IsNaN (Math.Log (x, double.NegativeInfinity)));
459                         Assert.IsTrue (double.IsNaN (Math.Log (double.PositiveInfinity, double.PositiveInfinity)));
460                         Assert.IsTrue (double.IsNaN (Math.Log (2, 1)));
461
462                         // MS docs say this should be PositiveInfinity
463                         Assert.IsTrue (Math.Log (0, y) == double.NegativeInfinity);
464                         Assert.IsTrue (Math.Log (double.PositiveInfinity, y) == double.PositiveInfinity);
465                         Assert.IsTrue (Math.Log (x, double.PositiveInfinity) == 0);
466                 }
467
468                 [Test]
469                 public void TestLog10 ()
470                 {
471                         double a = Math.Log10 (x);
472                         double b = -0.90868484030277719;
473
474                         Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
475                                 + " != " + b.ToString ("G99"));
476                         Assert.IsTrue (double.IsNaN (Math.Log10 (-1)));
477                         Assert.IsTrue (double.IsNaN (Math.Log10 (double.NaN)));
478
479                         // MS docs say this should be PositiveInfinity
480                         Assert.IsTrue (Math.Log10 (0) == double.NegativeInfinity);
481                         Assert.IsTrue (Math.Log10 (double.PositiveInfinity) == double.PositiveInfinity);
482
483                 }
484
485                 [Test]
486                 public void TestPow ()
487                 {
488                         int iTest = 1;
489
490                         try {
491                                 double a = Math.Pow (y, x);
492                                 double b = 1.363609446060212;
493
494                                 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99") + " != " + b.ToString ("G99"));
495                                 iTest++;
496                                 Assert.IsTrue (double.IsNaN (Math.Pow (y, double.NaN)));
497                                 iTest++;
498                                 Assert.IsTrue (double.IsNaN (Math.Pow (double.NaN, x)));
499                                 iTest++;
500                                 Assert.IsTrue (double.IsNegativeInfinity (Math.Pow (double.NegativeInfinity, 1)),
501                                         "Math.Pow(double.NegativeInfinity, 1) should be NegativeInfinity");
502                                 iTest++;
503                                 Assert.IsTrue (double.IsPositiveInfinity (Math.Pow (double.NegativeInfinity, 2)),
504                                         "Math.Pow(double.NegativeInfinity, 2) should be PositiveInfinity");
505
506                                 // MS docs say this should be 0
507                                 iTest++;
508                                 Assert.IsTrue (double.IsNaN (Math.Pow (1, double.NegativeInfinity)));
509                                 iTest++;
510                                 Assert.AreEqual ((double) 0, Math.Pow (double.PositiveInfinity, double.NegativeInfinity),
511                                         "Math.Pow(double.PositiveInfinity, double.NegativeInfinity)");
512                                 iTest++;
513                                 Assert.IsTrue (double.IsPositiveInfinity (Math.Pow (double.PositiveInfinity, 1)),
514                                         "Math.Pow(double.PositiveInfinity, 1) should be PositiveInfinity");
515
516                                 // MS docs say this should be PositiveInfinity
517                                 iTest++;
518                                 Assert.IsTrue (double.IsNaN (Math.Pow (1, double.PositiveInfinity)),
519                                         "Math.Pow(1, double.PositiveInfinity) should be NaN");
520
521                                 iTest++;
522                                 Assert.IsTrue (Double.IsNaN (Math.Pow (1, Double.NaN)),
523                                         "Math.Pow(1, NaN) should be NaN");
524 #if !TARGET_JVM
525                                 iTest++;
526                                 Assert.IsTrue (Double.IsNaN (Math.Pow (Double.NaN, 0)),
527                                         "Math.Pow(NaN, 0) should be NaN");
528 #endif
529                                 iTest++;
530                                 Assert.IsTrue (1.0 == Math.Pow (-1, Double.MaxValue),
531                                         "Math.Pow(-1, MaxValue) should be 1.0");
532
533                                 iTest++;
534                                 Assert.IsTrue (1.0 == Math.Pow (-1, Double.MinValue),
535                                         "Math.Pow(-1, MinValue) should be 1.0");
536
537                                 iTest++;
538                                 Assert.IsTrue (Double.IsPositiveInfinity (Math.Pow (Double.MinValue,
539                                         Double.MaxValue)), "Math.Pow(MinValue, MaxValue) should be +Infinity");
540
541                                 iTest++;
542                                 Assert.IsTrue (0.0 == Math.Pow (Double.MinValue, Double.MinValue),
543                                         "Math.Pow(MinValue, MinValue) should be 0.0");
544
545                                 //
546                                 // The following bugs were present because we tried to outsmart the C Pow:
547                                 //
548                                 double infinity = Double.PositiveInfinity;
549                                 Assert.IsTrue (Math.Pow (0.5, infinity) == 0.0,
550                                         "Math.Pow(0.5, Infinity) == 0.0");
551                                 Assert.IsTrue (Math.Pow (0.5, -infinity) == infinity,
552                                         "Math.Pow(0.5, -Infinity) == Infinity");
553                                 Assert.IsTrue (Math.Pow (2, infinity) == infinity,
554                                         "Math.Pow(2, Infinity) == Infinity");
555                                 Assert.IsTrue (Math.Pow (2, -infinity) == 0.0,
556                                         "Math.Pow(2, -Infinity) == 0");
557                                 Assert.IsTrue (Math.Pow (infinity, 0) == 1.0,
558                                         "Math.Pow(Infinity, 0) == 1.0");
559                                 Assert.IsTrue (Math.Pow (-infinity, 0) == 1.0,
560                                         "Math.Pow(-Infinity, 0) == 1.0");
561                         } catch (Exception e) {
562                                 Assert.Fail ("Unexpected exception at iTest=" + iTest + ". e=" + e);
563                         }
564                 }
565
566                 [Test]
567                 public void TestByteMax ()
568                 {
569                         byte a = 1;
570                         byte b = 2;
571
572                         Assert.IsTrue (b == Math.Max (a, b), "#1");
573                         Assert.IsTrue (b == Math.Max (b, a), "#2");
574                 }
575
576                 [Test]
577                 public void TestDecimalMax ()
578                 {
579                         decimal a = 1.5M;
580                         decimal b = 2.5M;
581
582                         Assert.IsTrue (b == Math.Max (a, b), "#1");
583                         Assert.IsTrue (b == Math.Max (b, a), "#2");
584                 }
585
586                 [Test]
587                 public void TestDoubleMax ()
588                 {
589                         double a = 1.5D;
590                         double b = 2.5D;
591
592                         Assert.IsTrue (b == Math.Max (a, b), "#1");
593                         Assert.IsTrue (b == Math.Max (b, a), "#2");
594
595                         Assert.IsTrue (Double.IsNaN (Math.Max (Double.NaN, Double.NaN)), "#3");
596                         Assert.IsTrue (Double.IsNaN (Math.Max (Double.NaN, a)), "#4");
597                         Assert.IsTrue (Double.IsNaN (Math.Max (b, Double.NaN)), "#5");
598                 }
599
600                 [Test]
601                 public void TestFloatMax ()
602                 {
603                         float a = 1.5F;
604                         float b = 2.5F;
605
606                         Assert.IsTrue (b == Math.Max (a, b), "#1");
607                         Assert.IsTrue (b == Math.Max (b, a), "#2");
608                         Assert.IsTrue (Single.IsNaN (Math.Max (Single.NaN, Single.NaN)), "#3");
609                         Assert.IsTrue (Single.IsNaN (Math.Max (Single.NaN, a)), "#4");
610                         Assert.IsTrue (Single.IsNaN (Math.Max (b, Single.NaN)), "#5");
611                 }
612
613                 [Test]
614                 public void TestIntMax ()
615                 {
616                         int a = 1;
617                         int b = 2;
618                         int c = 100;
619                         int d = -2147483647;
620
621                         Assert.AreEqual (b, Math.Max (a, b), "#1");
622                         Assert.AreEqual (b, Math.Max (b, a), "#2");
623                         Assert.AreEqual (c, Math.Max (c, d), "#3");
624                         Assert.AreEqual (c, Math.Max (d, c), "#4");
625                 }
626
627                 [Test]
628                 public void TestLongMax ()
629                 {
630                         long a = 1L;
631                         long b = 2L;
632
633                         Assert.IsTrue (b == Math.Max (a, b), "#1");
634                         Assert.IsTrue (b == Math.Max (b, a), "#2");
635                 }
636
637                 [Test]
638                 public void TestSbyteMax ()
639                 {
640                         sbyte a = 1;
641                         sbyte b = 2;
642
643                         Assert.IsTrue (b == Math.Max (a, b), "#1");
644                         Assert.IsTrue (b == Math.Max (b, a), "#2");
645                 }
646
647                 [Test]
648                 public void TestShortMax ()
649                 {
650                         short a = 1;
651                         short b = 2;
652
653                         Assert.IsTrue (b == Math.Max (a, b), "#1");
654                         Assert.IsTrue (b == Math.Max (b, a), "#2");
655                 }
656
657                 [Test]
658                 public void TestUintMax ()
659                 {
660                         uint a = 1U;
661                         uint b = 2U;
662
663                         Assert.IsTrue (b == Math.Max (a, b), "#1");
664                         Assert.IsTrue (b == Math.Max (b, a), "#2");
665                 }
666
667                 [Test]
668                 public void TestUlongMax ()
669                 {
670                         ulong a = 1UL;
671                         ulong b = 2UL;
672
673                         Assert.IsTrue (b == Math.Max (a, b), "#1");
674                         Assert.IsTrue (b == Math.Max (b, a), "#2");
675                 }
676
677                 [Test]
678                 public void TestUshortMax ()
679                 {
680                         ushort a = 1;
681                         ushort b = 2;
682
683                         Assert.IsTrue (b == Math.Max (a, b), "#1");
684                         Assert.IsTrue (b == Math.Max (b, a), "#2");
685                 }
686
687                 [Test]
688                 public void TestByteMin ()
689                 {
690                         byte a = 1;
691                         byte b = 2;
692
693                         Assert.IsTrue (a == Math.Min (a, b), "#1");
694                         Assert.IsTrue (a == Math.Min (b, a), "#2");
695                 }
696
697                 [Test]
698                 public void TestDecimalMin ()
699                 {
700                         decimal a = 1.5M;
701                         decimal b = 2.5M;
702
703                         Assert.IsTrue (a == Math.Min (a, b), "#1");
704                         Assert.IsTrue (a == Math.Min (b, a), "#2");
705                 }
706
707                 [Test]
708                 public void TestDoubleMin ()
709                 {
710                         double a = 1.5D;
711                         double b = 2.5D;
712
713                         Assert.IsTrue (a == Math.Min (a, b), "#1");
714                         Assert.IsTrue (a == Math.Min (b, a), "#2");
715                         Assert.IsTrue (Double.IsNaN (Math.Min (Double.NaN, Double.NaN)), "#3");
716                         Assert.IsTrue (Double.IsNaN (Math.Min (Double.NaN, a)), "#4");
717                         Assert.IsTrue (Double.IsNaN (Math.Min (b, Double.NaN)), "#5");
718                 }
719
720                 [Test]
721                 public void TestFloatMin ()
722                 {
723                         float a = 1.5F;
724                         float b = 2.5F;
725
726                         Assert.IsTrue (a == Math.Min (a, b), "#1");
727                         Assert.IsTrue (a == Math.Min (b, a), "#2");
728                         Assert.IsTrue (Single.IsNaN (Math.Min (Single.NaN, Single.NaN)), "#3");
729                         Assert.IsTrue (Single.IsNaN (Math.Min (Single.NaN, a)), "#4");
730                         Assert.IsTrue (Single.IsNaN (Math.Min (b, Single.NaN)), "#5");
731                 }
732
733                 [Test]
734                 public void TestIntMin ()
735                 {
736                         int a = 1;
737                         int b = 2;
738
739                         Assert.IsTrue (a == Math.Min (a, b), "#1");
740                         Assert.IsTrue (a == Math.Min (b, a), "#2");
741                 }
742
743                 [Test]
744                 public void TestLongMin ()
745                 {
746                         long a = 1L;
747                         long b = 2L;
748
749                         Assert.IsTrue (a == Math.Min (a, b), "#1");
750                         Assert.IsTrue (a == Math.Min (b, a), "#2");
751                 }
752
753                 [Test]
754                 public void TestSbyteMin ()
755                 {
756                         sbyte a = 1;
757                         sbyte b = 2;
758
759                         Assert.IsTrue (a == Math.Min (a, b), "#1");
760                         Assert.IsTrue (a == Math.Min (b, a), "#2");
761                 }
762
763                 [Test]
764                 public void TestShortMin ()
765                 {
766                         short a = 1;
767                         short b = 2;
768
769                         Assert.IsTrue (a == Math.Min (a, b), "#1");
770                         Assert.IsTrue (a == Math.Min (b, a), "#2");
771                 }
772
773                 [Test]
774                 public void TestUintMin ()
775                 {
776                         uint a = 1U;
777                         uint b = 2U;
778
779                         Assert.IsTrue (a == Math.Min (a, b), "#1");
780                         Assert.IsTrue (a == Math.Min (b, a), "#2");
781                 }
782
783                 [Test]
784                 public void TestUlongMin ()
785                 {
786                         ulong a = 1UL;
787                         ulong b = 2UL;
788
789                         Assert.IsTrue (a == Math.Min (a, b), "#1");
790                         Assert.IsTrue (a == Math.Min (b, a), "#2");
791                 }
792
793                 [Test]
794                 public void TestUshortMin ()
795                 {
796                         ushort a = 1;
797                         ushort b = 2;
798
799                         Assert.IsTrue (a == Math.Min (a, b), "#1");
800                         Assert.IsTrue (a == Math.Min (b, a), "#2");
801                 }
802
803                 [Test]
804                 public void TestDecimalRound ()
805                 {
806                         decimal a = 1.5M;
807                         decimal b = 2.5M;
808
809                         Assert.IsTrue (Math.Round (a) == 2, "#1");
810                         Assert.IsTrue (Math.Round (b) == 2, "#2");
811                         Assert.IsTrue (Decimal.MaxValue == Math.Round (Decimal.MaxValue), "#3");
812                         Assert.IsTrue (Decimal.MinValue == Math.Round (Decimal.MinValue), "#4");
813                 }
814
815                 [Test]
816                 public void TestDecimalRound2 ()
817                 {
818                         decimal a = 3.45M;
819                         decimal b = 3.46M;
820
821                         Assert.AreEqual (3.4M, Math.Round (a, 1), "#1");
822                         Assert.AreEqual (3.5M, Math.Round (b, 1), "#2");
823                 }
824
825                 [Test]
826                 public void TestDoubleRound ()
827                 {
828                         double a = 1.5D;
829                         double b = 2.5D;
830
831                         Assert.AreEqual (2D, Math.Round (a), "#1");
832                         Assert.AreEqual (2D, Math.Round (b), "#2");
833                         Assert.IsTrue (Double.MaxValue == Math.Round (Double.MaxValue), "#3");
834                         Assert.IsTrue (Double.MinValue == Math.Round (Double.MinValue), "#4");
835                 }
836
837 #if NET_2_0
838                 [Test]
839                 public void TestDoubleTruncate ()
840                 {
841                         double a = 1.2D;
842                         double b = 2.8D;
843                         double c = 0D;
844
845                         Assert.AreEqual (1D, Math.Truncate (a), "#1");
846                         Assert.AreEqual (2D, Math.Truncate (b), "#2");
847
848                         Assert.AreEqual (-1D, Math.Truncate (a * -1D), "#3");
849                         Assert.AreEqual (-2D, Math.Truncate (b * -1D), "#4");
850
851                         Assert.AreEqual (0D, Math.Truncate (c), "#5");
852
853                         Assert.IsTrue (Double.MaxValue == Math.Truncate (Double.MaxValue), "#6");
854                         Assert.IsTrue (Double.MinValue == Math.Truncate (Double.MinValue), "#7");
855                 }
856
857                 [Test]
858                 public void TestDecimalTruncate ()
859                 {
860                         decimal a = 1.2M;
861                         decimal b = 2.8M;
862                         decimal c = 0M;
863
864                         Assert.AreEqual (1M, Math.Truncate (a), "#1");
865                         Assert.AreEqual (2M, Math.Truncate (b), "#2");
866
867                         Assert.AreEqual (-1M, Math.Truncate (a * -1M), "#3");
868                         Assert.AreEqual (-2M, Math.Truncate (b * -1M), "#4");
869
870                         Assert.AreEqual (0M, Math.Truncate (c), "#5");
871
872                         Assert.IsTrue (Decimal.MaxValue == Math.Truncate (Decimal.MaxValue), "#6");
873                         Assert.IsTrue (Decimal.MinValue == Math.Truncate (Decimal.MinValue), "#7");
874                 }
875 #endif
876
877                 [Test]
878                 public void TestDoubleRound2 ()
879                 {
880                         double a = 3.45D;
881                         double b = 3.46D;
882
883                         Assert.AreEqual (3.4D, Math.Round (a, 1), "#1");
884                         Assert.AreEqual (3.5D, Math.Round (b, 1), "#2");
885                         Assert.AreEqual (-0.1, Math.Round (-0.123456789, 1), "#3");
886                 }
887
888 #if NET_2_0
889                 [Test]
890                 public void TestDoubleRound3 ()
891                 {
892                         Assert.AreEqual (1D, Math.Round (1D, 0, MidpointRounding.ToEven), "#1");
893                         Assert.AreEqual (1D, Math.Round (1D, 0, MidpointRounding.AwayFromZero), "#2");
894
895                         Assert.AreEqual (-1D, Math.Round (-1D, 0, MidpointRounding.ToEven), "#3");
896                         Assert.AreEqual (-1D, Math.Round (-1D, 0, MidpointRounding.AwayFromZero), "#4");
897
898                         Assert.AreEqual (1D, Math.Round (1D, 1, MidpointRounding.ToEven), "#5");
899                         Assert.AreEqual (1D, Math.Round (1D, 1, MidpointRounding.AwayFromZero), "#6");
900
901                         Assert.AreEqual (-1D, Math.Round (-1D, 1, MidpointRounding.ToEven), "#7");
902                         Assert.AreEqual (-1D, Math.Round (-1D, 1, MidpointRounding.AwayFromZero), "#8");
903
904                         Assert.AreEqual (1D, Math.Round (1.2345D, 0, MidpointRounding.ToEven), "#9");
905                         Assert.AreEqual (1D, Math.Round (1.2345D, 0, MidpointRounding.AwayFromZero), "#A");
906
907                         Assert.AreEqual (-1D, Math.Round (-1.2345D, 0, MidpointRounding.ToEven), "#B");
908                         Assert.AreEqual (-1D, Math.Round (-1.2345D, 0, MidpointRounding.AwayFromZero), "#C");
909
910                         Assert.AreEqual (1.2D, Math.Round (1.2345D, 1, MidpointRounding.ToEven), "#D");
911                         Assert.AreEqual (1.2D, Math.Round (1.2345D, 1, MidpointRounding.AwayFromZero), "#E");
912
913                         Assert.AreEqual (-1.2D, Math.Round (-1.2345D, 1, MidpointRounding.ToEven), "#F");
914                         Assert.AreEqual (-1.2D, Math.Round (-1.2345D, 1, MidpointRounding.AwayFromZero), "#10");
915
916                         Assert.AreEqual (1.23D, Math.Round (1.2345D, 2, MidpointRounding.ToEven), "#11");
917                         Assert.AreEqual (1.23D, Math.Round (1.2345D, 2, MidpointRounding.AwayFromZero), "#12");
918
919                         Assert.AreEqual (-1.23D, Math.Round (-1.2345D, 2, MidpointRounding.ToEven), "#13");
920                         Assert.AreEqual (-1.23D, Math.Round (-1.2345D, 2, MidpointRounding.AwayFromZero), "#14");
921
922                         Assert.AreEqual (1.234D, Math.Round (1.2345D, 3, MidpointRounding.ToEven), "#15");
923                         Assert.AreEqual (1.235D, Math.Round (1.2345D, 3, MidpointRounding.AwayFromZero), "#16");
924
925                         Assert.AreEqual (-1.234D, Math.Round (-1.2345D, 3, MidpointRounding.ToEven), "#17");
926                         Assert.AreEqual (-1.235D, Math.Round (-1.2345D, 3, MidpointRounding.AwayFromZero), "#18");
927
928                         Assert.AreEqual (1.2345D, Math.Round (1.2345D, 4, MidpointRounding.ToEven), "#19");
929                         Assert.AreEqual (1.2345D, Math.Round (1.2345D, 4, MidpointRounding.AwayFromZero), "#1A");
930
931                         Assert.AreEqual (-1.2345D, Math.Round (-1.2345D, 4, MidpointRounding.ToEven), "#1B");
932                         Assert.AreEqual (-1.2345D, Math.Round (-1.2345D, 4, MidpointRounding.AwayFromZero), "#1C");
933
934                         Assert.AreEqual (2D, Math.Round (1.5432D, 0, MidpointRounding.ToEven), "#1D");
935                         Assert.AreEqual (2D, Math.Round (1.5432D, 0, MidpointRounding.AwayFromZero), "#1E");
936
937                         Assert.AreEqual (-2D, Math.Round (-1.5432D, 0, MidpointRounding.ToEven), "#1F");
938                         Assert.AreEqual (-2D, Math.Round (-1.5432D, 0, MidpointRounding.AwayFromZero), "#20");
939
940                         Assert.AreEqual (1.5D, Math.Round (1.5432D, 1, MidpointRounding.ToEven), "#21");
941                         Assert.AreEqual (1.5D, Math.Round (1.5432D, 1, MidpointRounding.AwayFromZero), "#22");
942
943                         Assert.AreEqual (-1.5D, Math.Round (-1.5432D, 1, MidpointRounding.ToEven), "#23");
944                         Assert.AreEqual (-1.5D, Math.Round (-1.5432D, 1, MidpointRounding.AwayFromZero), "#24");
945
946                         Assert.AreEqual (1.54D, Math.Round (1.5432D, 2, MidpointRounding.ToEven), "#25");
947                         Assert.AreEqual (1.54D, Math.Round (1.5432D, 2, MidpointRounding.AwayFromZero), "#26");
948
949                         Assert.AreEqual (-1.54D, Math.Round (-1.5432D, 2, MidpointRounding.ToEven), "#27");
950                         Assert.AreEqual (-1.54D, Math.Round (-1.5432D, 2, MidpointRounding.AwayFromZero), "#28");
951
952                         Assert.AreEqual (1.543D, Math.Round (1.5432D, 3, MidpointRounding.ToEven), "#29");
953                         Assert.AreEqual (1.543D, Math.Round (1.5432D, 3, MidpointRounding.AwayFromZero), "#2A");
954
955                         Assert.AreEqual (-1.543D, Math.Round (-1.5432D, 3, MidpointRounding.ToEven), "#2B");
956                         Assert.AreEqual (-1.543D, Math.Round (-1.5432D, 3, MidpointRounding.AwayFromZero), "#2C");
957
958                         Assert.AreEqual (1.5432D, Math.Round (1.5432D, 4, MidpointRounding.ToEven), "#2D");
959                         Assert.AreEqual (1.5432D, Math.Round (1.5432D, 4, MidpointRounding.AwayFromZero), "#2E");
960
961                         Assert.AreEqual (-1.5432D, Math.Round (-1.5432D, 4, MidpointRounding.ToEven), "#2F");
962                         Assert.AreEqual (-1.5432D, Math.Round (-1.5432D, 4, MidpointRounding.AwayFromZero), "#30");
963
964                         Assert.AreEqual (63988D, Math.Round (63987.83593942D, 0, MidpointRounding.ToEven), "#31");
965                         Assert.AreEqual (63988D, Math.Round (63987.83593942D, 0, MidpointRounding.AwayFromZero), "#32");
966
967                         Assert.AreEqual (-63988D, Math.Round (-63987.83593942D, 0, MidpointRounding.ToEven), "#33");
968                         Assert.AreEqual (-63988D, Math.Round (-63987.83593942D, 0, MidpointRounding.AwayFromZero), "#34");
969
970                         Assert.AreEqual (63987.83594D, Math.Round (63987.83593942D, 5, MidpointRounding.ToEven), "#35");
971                         Assert.AreEqual (63987.83594D, Math.Round (63987.83593942D, 5, MidpointRounding.AwayFromZero), "#36");
972
973                         Assert.AreEqual (-63987.83594D, Math.Round (-63987.83593942D, 5, MidpointRounding.ToEven), "#37");
974                         Assert.AreEqual (-63987.83594D, Math.Round (-63987.83593942D, 5, MidpointRounding.AwayFromZero), "#38");
975
976                         Assert.AreEqual (63987.83593942D, Math.Round (63987.83593942D, 8, MidpointRounding.ToEven), "#39");
977                         Assert.AreEqual (63987.83593942D, Math.Round (63987.83593942D, 8, MidpointRounding.AwayFromZero), "#3A");
978
979                         Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.ToEven), "#3B");
980                         Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.AwayFromZero), "#3C");
981
982                         Assert.AreEqual (1, Math.Round (0.5, 0, MidpointRounding.AwayFromZero));
983                 }
984 #endif
985                 
986                 [Test]
987                 public void TestDecimalSign ()
988                 {
989                         decimal a = -5M;
990                         decimal b = 5M;
991
992                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
993                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
994                         Assert.IsTrue (Math.Sign (0M) == 0, "#3");
995                 }
996
997                 [Test]
998                 public void TestDoubleSign ()
999                 {
1000                         double a = -5D;
1001                         double b = 5D;
1002
1003                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1004                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1005                         Assert.IsTrue (Math.Sign (0D) == 0, "#3");
1006                 }
1007
1008                 [Test]
1009                 public void TestFloatSign ()
1010                 {
1011                         float a = -5F;
1012                         float b = 5F;
1013
1014                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1015                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1016                         Assert.IsTrue (Math.Sign (0F) == 0, "#3");
1017                 }
1018
1019                 [Test]
1020                 public void TestIntSign ()
1021                 {
1022                         int a = -5;
1023                         int b = 5;
1024
1025                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1026                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1027                         Assert.IsTrue (Math.Sign (0) == 0, "#3");
1028                 }
1029
1030                 [Test]
1031                 public void TestLongSign ()
1032                 {
1033                         long a = -5L;
1034                         long b = 5L;
1035
1036                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1037                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1038                         Assert.IsTrue (Math.Sign (0L) == 0, "#3");
1039                 }
1040
1041                 [Test]
1042                 public void TestSbyteSign ()
1043                 {
1044                         sbyte a = -5;
1045                         sbyte b = 5;
1046
1047                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1048                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1049                         Assert.IsTrue (Math.Sign (0) == 0, "#3");
1050                 }
1051
1052                 [Test]
1053                 public void TestShortSign ()
1054                 {
1055                         short a = -5;
1056                         short b = 5;
1057
1058                         Assert.IsTrue (Math.Sign (a) == -1, "#1");
1059                         Assert.IsTrue (Math.Sign (b) == 1, "#2");
1060                         Assert.IsTrue (Math.Sign (0) == 0, "#3");
1061                 }
1062         }
1063 }