зеркало из https://github.com/github/vitess-gh.git
Fixed code coverage in arithmetic.go
- Added more tests in arithmetic_test.go to fix code coverage - Added error checking into arithmetic.go - Implemented function floatMinusAny() for subtraction expression Signed-off-by: Rasika Kale <rasika@planetscale.com>
This commit is contained in:
Родитель
9f9c2e04b0
Коммит
eeebadb297
|
@ -47,8 +47,14 @@ func Add(v1, v2 Value) (Value, error) {
|
|||
}
|
||||
|
||||
lv1, err := newNumeric(v1)
|
||||
if err != nil {
|
||||
return NULL, err
|
||||
}
|
||||
|
||||
lv2, err := newNumeric(v2)
|
||||
if err != nil {
|
||||
return NULL, err
|
||||
}
|
||||
|
||||
lresult, err := addNumericWithError(lv1, lv2)
|
||||
if err != nil {
|
||||
|
@ -65,8 +71,14 @@ func Subtract(v1, v2 Value) (Value, error) {
|
|||
}
|
||||
|
||||
lv1, err := newNumeric(v1)
|
||||
if err != nil {
|
||||
return NULL, err
|
||||
}
|
||||
|
||||
lv2, err := newNumeric(v2)
|
||||
if err != nil {
|
||||
return NULL, err
|
||||
}
|
||||
|
||||
lresult, err := subtractNumericWithError(lv1, lv2)
|
||||
if err != nil {
|
||||
|
@ -258,7 +270,10 @@ func ToInt64(v Value) (int64, error) {
|
|||
|
||||
// ToFloat64 converts Value to float64.
|
||||
func ToFloat64(v Value) (float64, error) {
|
||||
num, _ := newNumeric(v)
|
||||
num, err := newNumeric(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
switch num.typ {
|
||||
case Int64:
|
||||
return float64(num.ival), nil
|
||||
|
@ -404,7 +419,7 @@ func subtractNumericWithError(v1, v2 numeric) (numeric, error) {
|
|||
return uintMinusUintWithError(v1.uval, v2.uval)
|
||||
}
|
||||
case Float64:
|
||||
return floatPlusAny(v1.fval, v2), nil
|
||||
return floatMinusAny(v1.fval, v2), nil
|
||||
}
|
||||
panic("unreachable")
|
||||
|
||||
|
@ -467,6 +482,10 @@ func uintPlusIntWithError(v1 uint64, v2 int64) (numeric, error) {
|
|||
return numeric{}, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "BIGINT value is out of range in %v + %v", v1, v2)
|
||||
}
|
||||
|
||||
if v1 >= math.MaxUint64 && v2 > 0 {
|
||||
return numeric{}, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "BIGINT UNSIGNED value is out of range in %v + %v", v1, v2)
|
||||
}
|
||||
|
||||
//convert to int -> uint is because for numeric operators (such as + or -)
|
||||
//where one of the operands is an unsigned integer, the result is unsigned by default.
|
||||
return uintPlusUintWithError(v1, uint64(v2))
|
||||
|
@ -515,6 +534,17 @@ func floatPlusAny(v1 float64, v2 numeric) numeric {
|
|||
return numeric{typ: Float64, fval: v1 + v2.fval}
|
||||
}
|
||||
|
||||
func floatMinusAny(v1 float64, v2 numeric) numeric {
|
||||
switch v2.typ {
|
||||
case Int64:
|
||||
v2.fval = float64(v2.ival)
|
||||
case Uint64:
|
||||
v2.fval = float64(v2.uval)
|
||||
}
|
||||
return numeric{typ: Float64, fval: v1 - v2.fval}
|
||||
|
||||
}
|
||||
|
||||
func castFromNumeric(v numeric, resultType querypb.Type) Value {
|
||||
switch {
|
||||
case IsSigned(resultType):
|
||||
|
|
|
@ -94,6 +94,31 @@ func TestSubtract(t *testing.T) {
|
|||
v1: NewUint64(1),
|
||||
v2: TestValue(VarChar, "c"),
|
||||
out: NewUint64(1),
|
||||
}, {
|
||||
|
||||
v1: TestValue(Uint64, "1.2"),
|
||||
v2: NewInt64(2),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "strconv.ParseUint: parsing \"1.2\": invalid syntax"),
|
||||
}, {
|
||||
|
||||
v1: NewUint64(2),
|
||||
v2: TestValue(Uint64, "1.2"),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "strconv.ParseUint: parsing \"1.2\": invalid syntax"),
|
||||
}, {
|
||||
// uint64 - uint64
|
||||
v1: NewUint64(8),
|
||||
v2: NewUint64(4),
|
||||
out: NewUint64(4),
|
||||
}, {
|
||||
// testing for float subtraction: float - int
|
||||
v1: NewFloat64(1.2),
|
||||
v2: NewInt64(2),
|
||||
out: NewFloat64(-0.8),
|
||||
}, {
|
||||
// testin for float subtraction: float - uint
|
||||
v1: NewFloat64(1.2),
|
||||
v2: NewUint64(2),
|
||||
out: NewFloat64(-0.8),
|
||||
}}
|
||||
|
||||
for _, tcase := range tcases {
|
||||
|
@ -199,6 +224,20 @@ func TestAdd(t *testing.T) {
|
|||
v1: NewUint64(1),
|
||||
v2: TestValue(VarChar, "1.2"),
|
||||
out: NewFloat64(2.2),
|
||||
}, {
|
||||
|
||||
v1: TestValue(Int64, "1.2"),
|
||||
v2: NewInt64(2),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "strconv.ParseInt: parsing \"1.2\": invalid syntax"),
|
||||
}, {
|
||||
v1: NewInt64(2),
|
||||
v2: TestValue(Int64, "1.2"),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "strconv.ParseInt: parsing \"1.2\": invalid syntax"),
|
||||
}, {
|
||||
// testing for uint64 overflow with max uint64 + int value
|
||||
v1: NewUint64(math.MaxUint64),
|
||||
v2: NewInt64(2),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "BIGINT UNSIGNED value is out of range in 18446744073709551615 + 2"),
|
||||
}}
|
||||
|
||||
for _, tcase := range tcases {
|
||||
|
@ -541,6 +580,9 @@ func TestToFloat64(t *testing.T) {
|
|||
}, {
|
||||
v: NewFloat64(1.2),
|
||||
out: 1.2,
|
||||
}, {
|
||||
v: TestValue(Int64, "1.2"),
|
||||
err: vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "strconv.ParseInt: parsing \"1.2\": invalid syntax"),
|
||||
}}
|
||||
for _, tcase := range tcases {
|
||||
got, err := ToFloat64(tcase.v)
|
||||
|
|
Загрузка…
Ссылка в новой задаче