github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/evaluator/builtin_math_test.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package evaluator
    15  
    16  import (
    17  	. "github.com/insionng/yougam/libraries/pingcap/check"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testutil"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    21  )
    22  
    23  func (s *testEvaluatorSuite) TestAbs(c *C) {
    24  	defer testleak.AfterTest(c)()
    25  	tbl := []struct {
    26  		Arg interface{}
    27  		Ret interface{}
    28  	}{
    29  		{nil, nil},
    30  		{int64(1), int64(1)},
    31  		{uint64(1), uint64(1)},
    32  		{int64(-1), int64(1)},
    33  		{float64(3.14), float64(3.14)},
    34  		{float64(-3.14), float64(3.14)},
    35  	}
    36  
    37  	Dtbl := tblToDtbl(tbl)
    38  
    39  	for _, t := range Dtbl {
    40  		v, err := builtinAbs(t["Arg"], nil)
    41  		c.Assert(err, IsNil)
    42  		c.Assert(v, testutil.DatumEquals, t["Ret"][0])
    43  	}
    44  }
    45  
    46  func (s *testEvaluatorSuite) TestRand(c *C) {
    47  	defer testleak.AfterTest(c)()
    48  	v, err := builtinRand(make([]types.Datum, 0), nil)
    49  	c.Assert(err, IsNil)
    50  	c.Assert(v.GetFloat64(), Less, float64(1))
    51  	c.Assert(v.GetFloat64(), GreaterEqual, float64(0))
    52  }
    53  
    54  func (s *testEvaluatorSuite) TestPow(c *C) {
    55  	defer testleak.AfterTest(c)()
    56  	tbl := []struct {
    57  		Arg []interface{}
    58  		Ret float64
    59  	}{
    60  		{[]interface{}{1, 3}, 1},
    61  		{[]interface{}{2, 2}, 4},
    62  		{[]interface{}{4, 0.5}, 2},
    63  		{[]interface{}{4, -2}, 0.0625},
    64  	}
    65  
    66  	Dtbl := tblToDtbl(tbl)
    67  
    68  	for _, t := range Dtbl {
    69  		v, err := builtinPow(t["Arg"], nil)
    70  		c.Assert(err, IsNil)
    71  		c.Assert(v, testutil.DatumEquals, t["Ret"][0])
    72  	}
    73  
    74  	errTbl := []struct {
    75  		Arg []interface{}
    76  	}{
    77  		{[]interface{}{"test", "test"}},
    78  		{[]interface{}{nil, nil}},
    79  		{[]interface{}{1, "test"}},
    80  		{[]interface{}{1, nil}},
    81  	}
    82  
    83  	errDtbl := tblToDtbl(errTbl)
    84  	for _, t := range errDtbl {
    85  		_, err := builtinPow(t["Arg"], nil)
    86  		c.Assert(err, NotNil)
    87  	}
    88  }
    89  
    90  func (s *testEvaluatorSuite) TestRound(c *C) {
    91  	defer testleak.AfterTest(c)()
    92  	tbl := []struct {
    93  		Arg []interface{}
    94  		Ret float64
    95  	}{
    96  		{[]interface{}{-1.23}, -1},
    97  		{[]interface{}{-1.23, 0}, -1},
    98  		{[]interface{}{-1.58}, -2},
    99  		{[]interface{}{1.58}, 2},
   100  		{[]interface{}{1.298, 1}, 1.3},
   101  		{[]interface{}{1.298}, 1},
   102  		{[]interface{}{1.298, 0}, 1},
   103  		{[]interface{}{23.298, -1}, 20},
   104  	}
   105  
   106  	Dtbl := tblToDtbl(tbl)
   107  
   108  	for _, t := range Dtbl {
   109  		v, err := builtinRound(t["Arg"], nil)
   110  		c.Assert(err, IsNil)
   111  		c.Assert(v, testutil.DatumEquals, t["Ret"][0])
   112  	}
   113  }