1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hanchuanchuan-goInception

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
builtin_arithmetic_test.go 14 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hanchuanchuan Отправлено 6 лет назад 27f3c5a
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
// Copyright 2017 PingCAP, Inc.
//
// Licensed 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"time"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/types"
"github.com/hanchuanchuan/goInception/util/chunk"
"github.com/hanchuanchuan/goInception/util/testleak"
"github.com/hanchuanchuan/goInception/util/testutil"
. "github.com/pingcap/check"
)
func (s *testEvaluatorSuite) TestSetFlenDecimal4RealOrDecimal(c *C) {
defer testleak.AfterTest(c)()
ret := &types.FieldType{}
a := &types.FieldType{
Decimal: 1,
Flen: 3,
}
b := &types.FieldType{
Decimal: 0,
Flen: 2,
}
setFlenDecimal4RealOrDecimal(ret, a, b, true)
c.Assert(ret.Decimal, Equals, 1)
c.Assert(ret.Flen, Equals, 6)
b.Flen = 65
setFlenDecimal4RealOrDecimal(ret, a, b, true)
c.Assert(ret.Decimal, Equals, 1)
c.Assert(ret.Flen, Equals, mysql.MaxRealWidth)
setFlenDecimal4RealOrDecimal(ret, a, b, false)
c.Assert(ret.Decimal, Equals, 1)
c.Assert(ret.Flen, Equals, mysql.MaxDecimalWidth)
b.Flen = types.UnspecifiedLength
setFlenDecimal4RealOrDecimal(ret, a, b, true)
c.Assert(ret.Decimal, Equals, 1)
c.Assert(ret.Flen, Equals, types.UnspecifiedLength)
b.Decimal = types.UnspecifiedLength
setFlenDecimal4RealOrDecimal(ret, a, b, true)
c.Assert(ret.Decimal, Equals, types.UnspecifiedLength)
c.Assert(ret.Flen, Equals, types.UnspecifiedLength)
}
func (s *testEvaluatorSuite) TestSetFlenDecimal4Int(c *C) {
defer testleak.AfterTest(c)()
ret := &types.FieldType{}
a := &types.FieldType{
Decimal: 1,
Flen: 3,
}
b := &types.FieldType{
Decimal: 0,
Flen: 2,
}
setFlenDecimal4Int(ret, a, b)
c.Assert(ret.Decimal, Equals, 0)
c.Assert(ret.Flen, Equals, mysql.MaxIntWidth)
b.Flen = mysql.MaxIntWidth + 1
setFlenDecimal4Int(ret, a, b)
c.Assert(ret.Decimal, Equals, 0)
c.Assert(ret.Flen, Equals, mysql.MaxIntWidth)
b.Flen = types.UnspecifiedLength
setFlenDecimal4Int(ret, a, b)
c.Assert(ret.Decimal, Equals, 0)
c.Assert(ret.Flen, Equals, mysql.MaxIntWidth)
}
func (s *testEvaluatorSuite) TestArithmeticPlus(c *C) {
defer testleak.AfterTest(c)()
// case: 1
args := []interface{}{int64(12), int64(1)}
bf, err := funcs[ast.Plus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
intSig, ok := bf.(*builtinArithmeticPlusIntSig)
c.Assert(ok, IsTrue)
c.Assert(intSig, NotNil)
intResult, isNull, err := intSig.evalInt(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsFalse)
c.Assert(intResult, Equals, int64(13))
// case 2
args = []interface{}{float64(1.01001), float64(-0.01)}
bf, err = funcs[ast.Plus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok := bf.(*builtinArithmeticPlusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err := realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsFalse)
c.Assert(realResult, Equals, float64(1.00001))
// case 3
args = []interface{}{nil, float64(-0.11101)}
bf, err = funcs[ast.Plus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok = bf.(*builtinArithmeticPlusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err = realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
c.Assert(realResult, Equals, float64(0))
// case 4
args = []interface{}{nil, nil}
bf, err = funcs[ast.Plus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok = bf.(*builtinArithmeticPlusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err = realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
c.Assert(realResult, Equals, float64(0))
// case 5
hexStr, err := types.ParseHexStr("0x20000000000000")
c.Assert(err, IsNil)
args = []interface{}{hexStr, int64(1)}
bf, err = funcs[ast.Plus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
intSig, ok = bf.(*builtinArithmeticPlusIntSig)
c.Assert(ok, IsTrue)
c.Assert(intSig, NotNil)
intResult, _, err = intSig.evalInt(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(intResult, Equals, int64(9007199254740993))
}
func (s *testEvaluatorSuite) TestArithmeticMinus(c *C) {
defer testleak.AfterTest(c)()
// case: 1
args := []interface{}{int64(12), int64(1)}
bf, err := funcs[ast.Minus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
intSig, ok := bf.(*builtinArithmeticMinusIntSig)
c.Assert(ok, IsTrue)
c.Assert(intSig, NotNil)
intResult, isNull, err := intSig.evalInt(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsFalse)
c.Assert(intResult, Equals, int64(11))
// case 2
args = []interface{}{float64(1.01001), float64(-0.01)}
bf, err = funcs[ast.Minus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok := bf.(*builtinArithmeticMinusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err := realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsFalse)
c.Assert(realResult, Equals, float64(1.02001))
// case 3
args = []interface{}{nil, float64(-0.11101)}
bf, err = funcs[ast.Minus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok = bf.(*builtinArithmeticMinusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err = realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
c.Assert(realResult, Equals, float64(0))
// case 4
args = []interface{}{float64(1.01), nil}
bf, err = funcs[ast.Minus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok = bf.(*builtinArithmeticMinusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err = realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
c.Assert(realResult, Equals, float64(0))
// case 5
args = []interface{}{nil, nil}
bf, err = funcs[ast.Minus].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(args...)))
c.Assert(err, IsNil)
c.Assert(bf, NotNil)
realSig, ok = bf.(*builtinArithmeticMinusRealSig)
c.Assert(ok, IsTrue)
c.Assert(realSig, NotNil)
realResult, isNull, err = realSig.evalReal(chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
c.Assert(realResult, Equals, float64(0))
}
func (s *testEvaluatorSuite) TestArithmeticMultiply(c *C) {
defer testleak.AfterTest(c)()
testCases := []struct {
args []interface{}
expect interface{}
err error
}{
{
args: []interface{}{int64(11), int64(11)},
expect: int64(121),
},
{
args: []interface{}{uint64(11), uint64(11)},
expect: int64(121),
},
{
args: []interface{}{float64(11), float64(11)},
expect: float64(121),
},
{
args: []interface{}{nil, float64(-0.11101)},
expect: nil,
},
{
args: []interface{}{float64(1.01), nil},
expect: nil,
},
{
args: []interface{}{nil, nil},
expect: nil,
},
}
for _, tc := range testCases {
sig, err := funcs[ast.Mul].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(tc.args...)))
c.Assert(err, IsNil)
c.Assert(sig, NotNil)
val, err := evalBuiltinFunc(sig, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDatum(tc.expect))
}
}
func (s *testEvaluatorSuite) TestArithmeticDivide(c *C) {
defer testleak.AfterTest(c)()
testCases := []struct {
args []interface{}
expect interface{}
}{
{
args: []interface{}{float64(11.1111111), float64(11.1)},
expect: float64(1.001001),
},
{
args: []interface{}{float64(11.1111111), float64(0)},
expect: nil,
},
{
args: []interface{}{int64(11), int64(11)},
expect: float64(1),
},
{
args: []interface{}{int64(11), int64(2)},
expect: float64(5.5),
},
{
args: []interface{}{int64(11), int64(0)},
expect: nil,
},
{
args: []interface{}{uint64(11), uint64(11)},
expect: float64(1),
},
{
args: []interface{}{uint64(11), uint64(2)},
expect: float64(5.5),
},
{
args: []interface{}{uint64(11), uint64(0)},
expect: nil,
},
{
args: []interface{}{nil, float64(-0.11101)},
expect: nil,
},
{
args: []interface{}{float64(1.01), nil},
expect: nil,
},
{
args: []interface{}{nil, nil},
expect: nil,
},
}
for _, tc := range testCases {
sig, err := funcs[ast.Div].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(tc.args...)))
c.Assert(err, IsNil)
c.Assert(sig, NotNil)
val, err := evalBuiltinFunc(sig, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDatum(tc.expect))
}
}
func (s *testEvaluatorSuite) TestArithmeticIntDivide(c *C) {
defer testleak.AfterTest(c)()
testCases := []struct {
args []interface{}
expect []interface{}
}{
{
args: []interface{}{int64(13), int64(11)},
expect: []interface{}{int64(1), nil},
},
{
args: []interface{}{int64(-13), int64(11)},
expect: []interface{}{int64(-1), nil},
},
{
args: []interface{}{int64(13), int64(-11)},
expect: []interface{}{int64(-1), nil},
},
{
args: []interface{}{int64(-13), int64(-11)},
expect: []interface{}{int64(1), nil},
},
{
args: []interface{}{int64(33), int64(11)},
expect: []interface{}{int64(3), nil},
},
{
args: []interface{}{int64(-33), int64(11)},
expect: []interface{}{int64(-3), nil},
},
{
args: []interface{}{int64(33), int64(-11)},
expect: []interface{}{int64(-3), nil},
},
{
args: []interface{}{int64(-33), int64(-11)},
expect: []interface{}{int64(3), nil},
},
{
args: []interface{}{int64(11), int64(0)},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{int64(-11), int64(0)},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{float64(11.01), float64(1.1)},
expect: []interface{}{int64(10), nil},
},
{
args: []interface{}{float64(-11.01), float64(1.1)},
expect: []interface{}{int64(-10), nil},
},
{
args: []interface{}{float64(11.01), float64(-1.1)},
expect: []interface{}{int64(-10), nil},
},
{
args: []interface{}{float64(-11.01), float64(-1.1)},
expect: []interface{}{int64(10), nil},
},
{
args: []interface{}{nil, float64(-0.11101)},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{float64(1.01), nil},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{nil, int64(-1001)},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{int64(101), nil},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{nil, nil},
expect: []interface{}{nil, nil},
},
{
args: []interface{}{float64(123456789100000.0), float64(-0.00001)},
expect: []interface{}{nil, "*BIGINT value is out of range in '\\(123456789100000 DIV -0.00001\\)'"},
},
{
args: []interface{}{int64(-9223372036854775808), float64(-1)},
expect: []interface{}{nil, "*BIGINT value is out of range in '\\(-9223372036854775808 DIV -1\\)'"},
},
}
for _, tc := range testCases {
sig, err := funcs[ast.IntDiv].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(tc.args...)))
c.Assert(err, IsNil)
c.Assert(sig, NotNil)
val, err := evalBuiltinFunc(sig, chunk.Row{})
if tc.expect[1] == nil {
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDatum(tc.expect[0]))
} else {
c.Assert(err, ErrorMatches, tc.expect[1])
}
}
}
func (s *testEvaluatorSuite) TestArithmeticMod(c *C) {
defer testleak.AfterTest(c)()
testCases := []struct {
args []interface{}
expect interface{}
}{
{
args: []interface{}{int64(13), int64(11)},
expect: int64(2),
},
{
args: []interface{}{int64(-13), int64(11)},
expect: int64(-2),
},
{
args: []interface{}{int64(13), int64(-11)},
expect: int64(2),
},
{
args: []interface{}{int64(-13), int64(-11)},
expect: int64(-2),
},
{
args: []interface{}{int64(33), int64(11)},
expect: int64(0),
},
{
args: []interface{}{int64(-33), int64(11)},
expect: int64(0),
},
{
args: []interface{}{int64(33), int64(-11)},
expect: int64(0),
},
{
args: []interface{}{int64(-33), int64(-11)},
expect: int64(0),
},
{
args: []interface{}{int64(11), int64(0)},
expect: nil,
},
{
args: []interface{}{int64(-11), int64(0)},
expect: nil,
},
{
args: []interface{}{int64(1), float64(1.1)},
expect: float64(1),
},
{
args: []interface{}{int64(-1), float64(1.1)},
expect: float64(-1),
},
{
args: []interface{}{int64(1), float64(-1.1)},
expect: float64(1),
},
{
args: []interface{}{int64(-1), float64(-1.1)},
expect: float64(-1),
},
{
args: []interface{}{nil, float64(-0.11101)},
expect: nil,
},
{
args: []interface{}{float64(1.01), nil},
expect: nil,
},
{
args: []interface{}{nil, int64(-1001)},
expect: nil,
},
{
args: []interface{}{int64(101), nil},
expect: nil,
},
{
args: []interface{}{nil, nil},
expect: nil,
},
{
args: []interface{}{"1231", 12},
expect: 7,
},
{
args: []interface{}{"1231", "12"},
expect: float64(7),
},
{
args: []interface{}{types.Duration{Duration: 45296 * time.Second}, 122},
expect: 114,
},
{
args: []interface{}{types.Set{Value: 7, Name: "abc"}, "12"},
expect: float64(7),
},
}
for _, tc := range testCases {
sig, err := funcs[ast.Mod].getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(tc.args...)))
c.Assert(err, IsNil)
c.Assert(sig, NotNil)
val, err := evalBuiltinFunc(sig, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDatum(tc.expect))
}
}

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/hanchuanchuan-goInception.git
git@gitlife.ru:oschina-mirror/hanchuanchuan-goInception.git
oschina-mirror
hanchuanchuan-goInception
hanchuanchuan-goInception
v1.2.4