Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix err check #23008

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,10 @@ func (s *testTypeConvertSuite) TestConvert(c *C) {
signedAccept(c, mysql.TypeNewDecimal, "-123.456", "-123.456")
signedAccept(c, mysql.TypeNewDecimal, NewDecFromInt(12300000), "12300000")
dec := NewDecFromInt(-123)
dec.Shift(-5)
dec.Round(dec, 5, ModeHalfEven)
err := dec.Shift(-5)
c.Assert(err, IsNil)
err = dec.Round(dec, 5, ModeHalfEven)
c.Assert(err, IsNil)
signedAccept(c, mysql.TypeNewDecimal, dec, "-0.00123")
}

Expand Down
5 changes: 4 additions & 1 deletion types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,10 @@ func BenchmarkCompareDatum(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j, v := range vals {
v.CompareDatum(sc, &vals1[j])
_, err := v.CompareDatum(sc, &vals1[j])
if err != nil {
b.Fatal(err)
}
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions types/mydecimal_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,31 @@ func BenchmarkRound(b *testing.B) {
}

for i := 0; i < len(tests); i++ {
tests[i].inputDec.FromString([]byte(tests[i].input))
err := tests[i].inputDec.FromString([]byte(tests[i].input))
if err != nil {
b.Fatal(err)
}
}

b.StartTimer()
for n := 0; n < b.N; n++ {
for i := 0; i < len(tests); i++ {
tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeHalfEven)
err := tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeHalfEven)
if err != nil {
b.Fatal(err)
}
}
for i := 0; i < len(tests); i++ {
tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeTruncate)
err := tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeTruncate)
if err != nil {
b.Fatal(err)
}
}
for i := 0; i < len(tests); i++ {
tests[i].inputDec.Round(&roundTo, tests[i].scale, modeCeiling)
err := tests[i].inputDec.Round(&roundTo, tests[i].scale, modeCeiling)
if err != nil {
b.Fatal(err)
}
}
}
}
75 changes: 48 additions & 27 deletions types/mydecimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (s *testMyDecimalSuite) TestToInt(c *C) {
}
for _, tt := range tests {
var dec MyDecimal
dec.FromString([]byte(tt.input))
err := dec.FromString([]byte(tt.input))
c.Assert(err, IsNil)
result, ec := dec.ToInt()
c.Check(ec, Equals, tt.err)
c.Check(result, Equals, tt.output)
Expand All @@ -106,7 +107,8 @@ func (s *testMyDecimalSuite) TestToUint(c *C) {
}
for _, tt := range tests {
var dec MyDecimal
dec.FromString([]byte(tt.input))
err := dec.FromString([]byte(tt.input))
c.Assert(err, IsNil)
result, ec := dec.ToUint()
c.Check(ec, Equals, tt.err)
c.Check(result, Equals, tt.output)
Expand Down Expand Up @@ -144,7 +146,8 @@ func (s *testMyDecimalSuite) TestToFloat(c *C) {
}
for _, ca := range tests {
var dec MyDecimal
dec.FromString([]byte(ca.s))
err := dec.FromString([]byte(ca.s))
c.Assert(err, IsNil)
f, err := dec.ToFloat64()
c.Check(err, IsNil)
c.Check(f, Equals, ca.f)
Expand Down Expand Up @@ -402,9 +405,10 @@ func (s *testMyDecimalSuite) TestRoundWithHalfEven(c *C) {

for _, ca := range tests {
var dec MyDecimal
dec.FromString([]byte(ca.input))
err := dec.FromString([]byte(ca.input))
c.Assert(err, IsNil)
var rounded MyDecimal
err := dec.Round(&rounded, ca.scale, ModeHalfEven)
err = dec.Round(&rounded, ca.scale, ModeHalfEven)
c.Check(err, Equals, ca.err)
result := rounded.ToString()
c.Check(string(result), Equals, ca.output)
Expand Down Expand Up @@ -436,9 +440,10 @@ func (s *testMyDecimalSuite) TestRoundWithTruncate(c *C) {
}
for _, ca := range tests {
var dec MyDecimal
dec.FromString([]byte(ca.input))
err := dec.FromString([]byte(ca.input))
c.Assert(err, IsNil)
var rounded MyDecimal
err := dec.Round(&rounded, ca.scale, ModeTruncate)
err = dec.Round(&rounded, ca.scale, ModeTruncate)
c.Check(err, Equals, ca.err)
result := rounded.ToString()
c.Check(string(result), Equals, ca.output)
Expand Down Expand Up @@ -471,9 +476,10 @@ func (s *testMyDecimalSuite) TestRoundWithCeil(c *C) {
}
for _, ca := range tests {
var dec MyDecimal
dec.FromString([]byte(ca.input))
err := dec.FromString([]byte(ca.input))
c.Assert(err, IsNil)
var rounded MyDecimal
err := dec.Round(&rounded, ca.scale, modeCeiling)
err = dec.Round(&rounded, ca.scale, modeCeiling)
c.Check(err, Equals, ca.err)
result := rounded.ToString()
c.Check(string(result), Equals, ca.output)
Expand Down Expand Up @@ -544,7 +550,8 @@ func (s *testMyDecimalSuite) TestToString(c *C) {
}
for _, ca := range tests {
var dec MyDecimal
dec.FromString([]byte(ca.input))
err := dec.FromString([]byte(ca.input))
c.Assert(err, IsNil)
result := dec.ToString()
c.Check(string(result), Equals, ca.output)
}
Expand Down Expand Up @@ -641,8 +648,10 @@ func (s *testMyDecimalSuite) TestCompare(c *C) {
}
for _, tt := range tests {
var a, b MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
c.Assert(a.Compare(&b), Equals, tt.cmp)
}
}
Expand Down Expand Up @@ -758,9 +767,11 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
}
for _, tt := range tests {
var a, b, sum MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := DecimalSub(&a, &b, &sum)
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
err = DecimalSub(&a, &b, &sum)
c.Assert(err, Equals, tt.err)
result := sum.ToString()
c.Assert(string(result), Equals, tt.result)
Expand Down Expand Up @@ -790,9 +801,11 @@ func (s *testMyDecimalSuite) TestMul(c *C) {
}
for _, tt := range tests {
var a, b, product MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := DecimalMul(&a, &b, &product)
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
err = DecimalMul(&a, &b, &product)
c.Check(err, Equals, tt.err)
result := product.String()
c.Assert(result, Equals, tt.result)
Expand Down Expand Up @@ -825,9 +838,11 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := DecimalDiv(&a, &b, &to, 5)
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
err = DecimalDiv(&a, &b, &to, 5)
c.Check(err, Equals, tt.err)
if tt.err == ErrDivByZero {
continue
Expand All @@ -848,8 +863,10 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
ec := DecimalMod(&a, &b, &to)
c.Check(ec, Equals, tt.err)
if tt.err == ErrDivByZero {
Expand All @@ -869,8 +886,10 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
ec := DecimalDiv(&a, &b, &to, DivFracIncr)
c.Check(ec, Equals, tt.err)
if tt.err == ErrDivByZero {
Expand All @@ -887,8 +906,10 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := a.FromString([]byte(tt.a))
c.Assert(err, IsNil)
err = b.FromString([]byte(tt.b))
c.Assert(err, IsNil)
ec := DecimalMod(&a, &b, &to)
c.Check(ec, Equals, tt.err)
if tt.err == ErrDivByZero {
Expand Down
18 changes: 14 additions & 4 deletions types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,8 @@ func (s *testTimeSuite) TestConvertTimeZone(c *C) {

for _, test := range tests {
t := types.NewTime(test.input, 0, 0)
t.ConvertTimeZone(test.from, test.to)
err := t.ConvertTimeZone(test.from, test.to)
c.Assert(err, IsNil)
c.Assert(t.Compare(types.NewTime(test.expect, 0, 0)), Equals, 0)
}
}
Expand Down Expand Up @@ -2023,7 +2024,10 @@ func (s *testTimeSuite) TestParseWithTimezone(c *C) {
func BenchmarkFormat(b *testing.B) {
t1 := types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 0)
for i := 0; i < b.N; i++ {
t1.DateFormat("%Y-%m-%d %H:%i:%s")
_, err := t1.DateFormat("%Y-%m-%d %H:%i:%s")
if err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -2034,7 +2038,10 @@ func BenchmarkTimeAdd(b *testing.B) {
arg1, _ := types.ParseTime(sc, "2017-01-18", mysql.TypeDatetime, types.MaxFsp)
arg2, _ := types.ParseDuration(sc, "12:30:59", types.MaxFsp)
for i := 0; i < b.N; i++ {
arg1.Add(sc, arg2)
_, err := arg1.Add(sc, arg2)
if err != nil {
b.Fatal(err)
}
}
}

Expand Down Expand Up @@ -2093,7 +2100,10 @@ func BenchmarkParseDateFormat(b *testing.B) {
func benchmarkDatetimeFormat(b *testing.B, name string, sc *stmtctx.StatementContext, str string) {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
types.ParseDatetime(sc, str)
_, err := types.ParseDatetime(sc, str)
if err != nil {
b.Fatal(err)
}
}
})
}
Expand Down