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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
20 // Federico Di Gregorio <fog@initd.org>
21 // Miguel de Icaza <miguel@novell.com>
24 using System.Reflection;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_Power
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.Power (null, Expression.Constant (1.0));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.Power (Expression.Constant (1.0), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void ArgTypesDifferent ()
52 Expression.Power (Expression.Constant (1), Expression.Constant (2.0));
56 [ExpectedException (typeof (InvalidOperationException))]
57 public void ArgTypesInt ()
59 Expression.Power (Expression.Constant (1), Expression.Constant (2));
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void ArgTypesFloat ()
66 Expression.Power (Expression.Constant ((float)1), Expression.Constant ((float)2));
70 public void ArgTypesDouble ()
72 var p = Expression.Power (Expression.Constant (1.0), Expression.Constant (2.0));
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 ());
80 public void TestCompile ()
82 var a = Expression.Parameter (typeof (double), "a");
83 var b = Expression.Parameter (typeof (double), "b");
85 var power = Expression.Lambda<Func<double,double,double>> (
86 Expression.Power (a, b), a, b).Compile ();
88 Assert.AreEqual (1, power (1, 10));
89 Assert.AreEqual (16, power (2, 4));
93 public void NullablePower ()
95 var a = Expression.Parameter (typeof (double?), "a");
96 var b = Expression.Parameter (typeof (double?), "b");
98 var power = Expression.Lambda<Func<double?, double?, double?>> (
99 Expression.Power (a, b), a, b).Compile ();
101 Assert.AreEqual ((double?) 1, power (1, 10));
102 Assert.AreEqual ((double?) 16, power (2, 4));
103 Assert.AreEqual ((double?) null, power (1, null));
104 Assert.AreEqual ((double?) null, power (null, 1));
105 Assert.AreEqual ((double?) null, power (null, null));