DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlUnaryExpressionTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 
17 using Deveel.Data.Sql.Objects;
18 using Deveel.Data.Types;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Sql.Expressions {
23  [TestFixture]
24  public class SqlUnaryExpressionTests {
25  [TestCase(4637377, 4637377)]
26  [TestCase(-1929934, -1929934)]
27  public void NumericPlus(double a, double expected) {
28  var exp1 = SqlExpression.Constant(DataObject.Number(new SqlNumber(a)));
29  var plusExp = SqlExpression.UnaryPlus(exp1);
30 
31  SqlExpression resultExp = null;
32  Assert.DoesNotThrow(() => resultExp = plusExp.Evaluate());
33  Assert.IsNotNull(resultExp);
34  Assert.IsInstanceOf<SqlConstantExpression>(resultExp);
35 
36  var constExp = (SqlConstantExpression)resultExp;
37  Assert.IsNotNull(constExp.Value.Value);
38  Assert.IsInstanceOf<NumericType>(constExp.Value.Type);
39  Assert.IsInstanceOf<SqlNumber>(constExp.Value.Value);
40 
41  var actual = ((SqlNumber)constExp.Value.Value);
42  var expectedResult = new SqlNumber(expected);
43  Assert.AreEqual(expectedResult, actual);
44  }
45 
46  [TestCase(884920009.9948, -884920009.9948)]
47  [TestCase(-92338.122, 92338.122)]
48  public void NumericNegate(double a, double expected) {
49  var exp1 = SqlExpression.Constant(DataObject.Number(new SqlNumber(a)));
50  var negExp = SqlExpression.Negate(exp1);
51 
52  SqlExpression resultExp = null;
53  Assert.DoesNotThrow(() => resultExp = negExp.Evaluate());
54  Assert.IsNotNull(resultExp);
55  Assert.IsInstanceOf<SqlConstantExpression>(resultExp);
56 
57  var constExp = (SqlConstantExpression)resultExp;
58  Assert.IsNotNull(constExp.Value.Value);
59  Assert.IsInstanceOf<NumericType>(constExp.Value.Type);
60  Assert.IsInstanceOf<SqlNumber>(constExp.Value.Value);
61 
62  var actual = ((SqlNumber)constExp.Value.Value);
63  var expectedResult = new SqlNumber(expected);
64  Assert.AreEqual(expectedResult, actual);
65  }
66 
67  [TestCase(true, false)]
68  [TestCase(false, true)]
69  public void BooleanNegate(bool a, bool expected) {
71  var negExp = SqlExpression.Negate(exp1);
72 
73  SqlExpression resultExp = null;
74  Assert.DoesNotThrow(() => resultExp = negExp.Evaluate());
75  Assert.IsNotNull(resultExp);
76  Assert.IsInstanceOf<SqlConstantExpression>(resultExp);
77 
78  var constExp = (SqlConstantExpression)resultExp;
79  Assert.IsNotNull(constExp.Value.Value);
80  Assert.IsInstanceOf<BooleanType>(constExp.Value.Type);
81  Assert.IsInstanceOf<SqlBoolean>(constExp.Value.Value);
82 
83  var actual = ((SqlBoolean)constExp.Value.Value);
84  var expectedResult = new SqlBoolean(expected);
85  Assert.AreEqual(expectedResult, actual);
86  }
87  }
88 }
static DataObject Number(SqlNumber value)
Definition: DataObject.cs:552
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static SqlUnaryExpression UnaryPlus(SqlExpression operand)
An expression that holds a constant value.
Deveel.Data.Sql.Objects.SqlBoolean SqlBoolean
Definition: DataObject.cs:26
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlUnaryExpression Negate(SqlExpression operand)