/*
 * Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.operation.scalar.arithmetic;

import io.crate.analyze.symbol.Function;
import io.crate.analyze.symbol.Literal;
import io.crate.analyze.symbol.Reference;
import io.crate.analyze.symbol.Symbol;
import io.crate.metadata.FunctionIdent;
import io.crate.operation.Input;
import io.crate.operation.scalar.AbstractScalarFunctionsTest;
import io.crate.types.DataType;
import io.crate.types.DataTypes;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.Arrays;

import static io.crate.testing.TestingHelpers.createReference;
import static io.crate.testing.TestingHelpers.isLiteral;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

public class CeilFunctionTest extends AbstractScalarFunctionsTest {

    private CeilFunction getFunction(DataType type) {
        return (CeilFunction) functions.get(new FunctionIdent(CeilFunction.NAME, Arrays.asList(type)));
    }

    private Number evaluate(Number number, DataType type) {
        return getFunction(type).evaluate((Input) Literal.newLiteral(type, number));
    }

    private Symbol normalize(Number number, DataType type) {
        CeilFunction function = getFunction(type);
        return function.normalizeSymbol(new Function(function.info(),
                Arrays.<Symbol>asList(Literal.newLiteral(type, number))));
    }

    @Test
    public void testEvaluateOnDouble() throws Exception {
        assertThat((Long) evaluate(29.9, DataTypes.DOUBLE), is(30L));
        assertThat(evaluate(null, DataTypes.DOUBLE), nullValue());
    }

    @Test
    public void testEvaluateOnFloat() throws Exception {
        assertThat((Integer) evaluate(29.9f, DataTypes.FLOAT), is(30));
        assertThat(evaluate(null, DataTypes.FLOAT), nullValue());
    }

    @Test
    public void testEvaluateOnIntAndLong() throws Exception {
        assertThat((Integer) evaluate(20, DataTypes.INTEGER), is(20));
        assertThat(evaluate(null, DataTypes.INTEGER), nullValue());
        assertThat((Long) evaluate(20L, DataTypes.LONG), is(20L));
        assertThat(evaluate(null, DataTypes.LONG), nullValue());
    }

    @Test
    public void testNormalizeValueSymbol() throws Exception {
        assertThat(normalize(42.9, DataTypes.DOUBLE), isLiteral(43L));
        assertThat(normalize(42.9f, DataTypes.FLOAT), isLiteral(43));
        assertThat(normalize(null, DataTypes.FLOAT), isLiteral(null, DataTypes.INTEGER));
    }

    @Test
    public void testNormalizeReference() throws Exception {
        Reference height = createReference("height", DataTypes.DOUBLE);
        CeilFunction round = getFunction(DataTypes.DOUBLE);
        Function function = new Function(round.info(), Arrays.<Symbol>asList(height));
        Function normalized = (Function) round.normalizeSymbol(function);
        assertThat(normalized, Matchers.sameInstance(function));
    }
}
@@ -1067,243 +1067,8 @@
 c;%0A%0A
-import io.crate.analyze.symbol.Function;%0Aimport io.crate.analyze.symbol.Literal;%0Aimport io.crate.analyze.symbol.Reference;%0Aimport io.crate.analyze.symbol.Symbol;%0Aimport io.crate.metadata.FunctionIdent;%0Aimport io.crate.operation.Input;%0A
 impo
@@ -1159,70 +1159,8 @@
 Type
-;%0Aimport io.crate.types.DataTypes;%0Aimport org.hamcrest.Matcher
 s;%0Ai
@@ -1186,34 +1186,8 @@
 t;%0A%0A
-import java.util.Arrays;%0A%0A
 impo
@@ -1228,31 +1228,26 @@
 Helpers.
-createReference
+isFunction
 ;%0Aimport
@@ -1301,1644 +1301,765 @@
 al;%0A
-import static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.nullValue;%0A%0Apublic class CeilFunctionTest extends AbstractScalarFunctionsTest %7B%0A%0A    private CeilFunction getFunction(DataType type) %7B%0A        return (CeilFunction) functions.get(new FunctionIdent(CeilFunction.NAME, Arrays.asList(type)));%0A    %7D%0A%0A    private Number evaluate(Number number, DataType type) %7B%0A        return getFunction(type).evaluate((Input) Literal.newLiteral(type, number));%0A    %7D%0A%0A    private Symbol normalize(Number number, DataType type) %7B%0A        CeilFunction function = getFunction(type);%0A        return function.normalizeSymbol(new Function(function.info(),%0A                Arrays.%3CSymbol%3EasList(Literal.newLiteral(type, number))));%0A    %7D%0A%0A    @Test%0A    public void testEvaluateOnDouble() throws Exception %7B%0A        assertThat((Long) evaluate(29.9, DataTypes.DOUBLE), is(30L));%0A        assertThat(evaluate(null, DataTypes.DOUBLE), nullValue());%0A    %7D%0A%0A    @Test%0A    public void testEvaluateOnFloat() throws Exception %7B%0A        assertThat((Integer) evaluate(29.9f, DataTypes.FLOAT), is(30));%0A        assertThat(evaluate(null, DataTypes.FLOAT), nullValue());%0A    %7D%0A%0A    @Test%0A    public void testEvaluateOnIntAndLong() throws Exception %7B%0A        assertThat((Integer) evaluate(20, DataTypes.INTEGER), is(20));%0A        assertThat(evaluate(null, DataTypes.INTEGER), nullValue());%0A        assertThat((Long) evaluate(20L, DataTypes.LONG), is(20L));%0A        assertThat(evaluate(null, DataTypes.LONG), nullValue());%0A    %7D%0A%0A    @Test%0A    public void testNormalizeValueSymbol() throws Exception %7B%0A        assertThat(normalize(42.9, DataTypes.DOUBLE)
+%0Apublic class CeilFunctionTest extends AbstractScalarFunctionsTest %7B%0A%0A    @Test%0A    public void testEvaluateOnDouble() throws Exception %7B%0A        assertNormalize(%22ceil(29.9)%22, isLiteral(30L));%0A        assertNormalize(%22ceil(null)%22, isLiteral(null));%0A    %7D%0A%0A    @Test%0A    public void testEvaluateOnFloat() throws Exception %7B%0A        assertNormalize(%22ceil(cast(29.9 as float))%22, isLiteral(30));%0A        assertNormalize(%22ceil(cast(null as float))%22, isLiteral(null, DataTypes.INTEGER));%0A    %7D%0A%0A    @Test%0A    public void testEvaluateOnIntAndLong() throws Exception %7B%0A        assertNormalize(%22ceil(cast(20 as integer))%22, isLiteral(20));%0A        assertNormalize(%22ceil(cast(null as integer))%22, isLiteral(null, DataTypes.INTEGER));%0A%0A        assertNormalize(%22ceil(20)%22
 , is
@@ -2066,18 +2066,18 @@
 Literal(
-43
+20
 L));%0A   
@@ -2079,38 +2079,33 @@
 ;%0A        assert
-That(n
+N
 ormalize(42.9f, 
@@ -2101,100 +2101,34 @@
 ize(
-42.9f, DataTypes.FLOAT), isLiteral(43));%0A        assertThat(normalize(null, DataTypes.FLOAT)
+%22ceil(cast(null as long))%22
 , is
@@ -2143,39 +2143,36 @@
 null, DataTypes.
-INTEGER
+LONG
 ));%0A    %7D%0A%0A    @
@@ -2248,354 +2248,65 @@
     
-Reference height = createReference(%22height%22, DataTypes.DOUBLE);%0A        CeilFunction round = getFunction(DataTypes.DOUBLE);%0A        Function function = new Function(round.info(), Arrays.%3CSymbol%3EasList(height));%0A        Function normalized = (Function) round.normalizeSymbol(function);%0A        assertThat(normalized, Matchers.sameInstance(function
+assertNormalize(%22ceil(age)%22, isFunction(CeilFunction.NAME
 ));%0A

use assertNormalize in CeilFunctionTest