Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Power.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // Ad
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21 //              Miguel de Icaza <miguel@novell.com>
22
23 using System;
24 using System.Reflection;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_Power
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.Power (null, Expression.Constant (1.0));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.Power (Expression.Constant (1.0), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void ArgTypesDifferent ()
51                 {
52                         Expression.Power (Expression.Constant (1), Expression.Constant (2.0));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void ArgTypesInt ()
58                 {
59                         Expression.Power (Expression.Constant (1), Expression.Constant (2));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void ArgTypesFloat ()
65                 {
66                         Expression.Power (Expression.Constant ((float)1), Expression.Constant ((float)2));
67                 }
68
69                 [Test]
70                 public void ArgTypesDouble ()
71                 {
72                         var p = Expression.Power (Expression.Constant (1.0), Expression.Constant (2.0));
73
74                         Assert.AreEqual (ExpressionType.Power, p.NodeType, "Power#01");
75                         Assert.AreEqual (typeof (double), p.Type, "Add#02");
76                         Assert.AreEqual ("(1 ^ 2)", p.ToString ());
77                 }
78
79                 [Test]
80                 public void TestCompile ()
81                 {
82                         var a = Expression.Parameter (typeof (double), "a");
83                         var b = Expression.Parameter (typeof (double), "b");
84
85                         var power = Expression.Lambda<Func<double,double,double>> (
86                                 Expression.Power (a, b), a, b).Compile ();
87
88                         Assert.AreEqual (1, power (1, 10));
89                         Assert.AreEqual (16, power (2, 4));
90                 }
91
92                 [Test]
93                 [Category ("NotWorkingInterpreter")]
94                 public void NullablePower ()
95                 {
96                         var a = Expression.Parameter (typeof (double?), "a");
97                         var b = Expression.Parameter (typeof (double?), "b");
98
99                         var power = Expression.Lambda<Func<double?, double?, double?>> (
100                                 Expression.Power (a, b), a, b).Compile ();
101
102                         Assert.AreEqual ((double?) 1, power (1, 10));
103                         Assert.AreEqual ((double?) 16, power (2, 4));
104                         Assert.AreEqual ((double?) null, power (1, null));
105                         Assert.AreEqual ((double?) null, power (null, 1));
106                         Assert.AreEqual ((double?) null, power (null, null));
107                 }
108         }
109 }