github.com/go-spring/spring-base@v1.1.3/cast/int_test.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cast_test 18 19 import ( 20 "errors" 21 "strconv" 22 "testing" 23 24 "github.com/go-spring/spring-base/assert" 25 "github.com/go-spring/spring-base/cast" 26 ) 27 28 func BenchmarkToInt(b *testing.B) { 29 // string/strconv-8 81830738 13.7 ns/op 30 // string/go-spring-8 26871295 44.7 ns/op 31 b.Run("string", func(b *testing.B) { 32 v := "10" 33 b.Run("strconv", func(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 _, err := strconv.ParseInt(v, 0, 0) 36 if err != nil { 37 b.Fatal(err) 38 } 39 } 40 }) 41 b.Run("go-spring", func(b *testing.B) { 42 for i := 0; i < b.N; i++ { 43 _, err := cast.ToInt64E(v) 44 if err != nil { 45 b.Fatal(err) 46 } 47 } 48 }) 49 }) 50 } 51 52 func TestToInt(t *testing.T) { 53 54 assert.Equal(t, cast.ToInt(nil), int(0)) 55 56 assert.Equal(t, cast.ToInt(int(3)), int(3)) 57 assert.Equal(t, cast.ToInt(int8(3)), int(3)) 58 assert.Equal(t, cast.ToInt(int16(3)), int(3)) 59 assert.Equal(t, cast.ToInt(int32(3)), int(3)) 60 assert.Equal(t, cast.ToInt(int64(3)), int(3)) 61 assert.Equal(t, cast.ToInt(cast.IntPtr(3)), int(3)) 62 assert.Equal(t, cast.ToInt(cast.Int8Ptr(3)), int(3)) 63 assert.Equal(t, cast.ToInt(cast.Int16Ptr(3)), int(3)) 64 assert.Equal(t, cast.ToInt(cast.Int32Ptr(3)), int(3)) 65 assert.Equal(t, cast.ToInt(cast.Int64Ptr(3)), int(3)) 66 67 assert.Equal(t, cast.ToInt8(uint(3)), int8(3)) 68 assert.Equal(t, cast.ToInt8(uint8(3)), int8(3)) 69 assert.Equal(t, cast.ToInt8(uint16(3)), int8(3)) 70 assert.Equal(t, cast.ToInt8(uint32(3)), int8(3)) 71 assert.Equal(t, cast.ToInt8(uint64(3)), int8(3)) 72 assert.Equal(t, cast.ToInt8(cast.UintPtr(3)), int8(3)) 73 assert.Equal(t, cast.ToInt8(cast.Uint8Ptr(3)), int8(3)) 74 assert.Equal(t, cast.ToInt8(cast.Uint16Ptr(3)), int8(3)) 75 assert.Equal(t, cast.ToInt8(cast.Uint32Ptr(3)), int8(3)) 76 assert.Equal(t, cast.ToInt8(cast.Uint64Ptr(3)), int8(3)) 77 78 assert.Equal(t, cast.ToInt16(float32(3)), int16(3)) 79 assert.Equal(t, cast.ToInt16(float64(3)), int16(3)) 80 assert.Equal(t, cast.ToInt16(cast.Float32Ptr(3)), int16(3)) 81 assert.Equal(t, cast.ToInt16(cast.Float64Ptr(3)), int16(3)) 82 83 assert.Equal(t, cast.ToInt32("3"), int32(3)) 84 assert.Equal(t, cast.ToInt32(cast.StringPtr("3")), int32(3)) 85 86 assert.Equal(t, cast.ToInt64(true), int64(1)) 87 assert.Equal(t, cast.ToInt64(false), int64(0)) 88 assert.Equal(t, cast.ToInt64(cast.BoolPtr(true)), int64(1)) 89 assert.Equal(t, cast.ToInt64(cast.BoolPtr(false)), int64(0)) 90 91 _, err := cast.ToInt64E("abc") 92 assert.Error(t, err, "strconv.ParseInt: parsing \"abc\": invalid syntax") 93 94 _, err = cast.ToInt64E(errors.New("abc")) 95 assert.Error(t, err, "unable to cast type \\(\\*errors\\.errorString\\) to int64") 96 }