github.com/go-spring/spring-base@v1.1.3/cast/bool_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 BenchmarkToBool(b *testing.B) { 29 // string/strconv-8 957624752 1.27 ns/op 30 // string/go-spring-8 41272039 28.3 ns/op 31 b.Run("string", func(b *testing.B) { 32 v := "true" 33 b.Run("strconv", func(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 _, err := strconv.ParseBool(v) 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.ToBoolE(v) 44 if err != nil { 45 b.Fatal(err) 46 } 47 } 48 }) 49 }) 50 } 51 52 func TestToBool(t *testing.T) { 53 54 assert.Equal(t, cast.ToBool(nil), false) 55 56 assert.Equal(t, cast.ToBool(int(3)), true) 57 assert.Equal(t, cast.ToBool(int8(3)), true) 58 assert.Equal(t, cast.ToBool(int16(3)), true) 59 assert.Equal(t, cast.ToBool(int32(3)), true) 60 assert.Equal(t, cast.ToBool(int64(3)), true) 61 assert.Equal(t, cast.ToBool(cast.IntPtr(3)), true) 62 assert.Equal(t, cast.ToBool(cast.Int8Ptr(3)), true) 63 assert.Equal(t, cast.ToBool(cast.Int16Ptr(3)), true) 64 assert.Equal(t, cast.ToBool(cast.Int32Ptr(3)), true) 65 assert.Equal(t, cast.ToBool(cast.Int64Ptr(3)), true) 66 67 assert.Equal(t, cast.ToBool(uint(3)), true) 68 assert.Equal(t, cast.ToBool(uint8(3)), true) 69 assert.Equal(t, cast.ToBool(uint16(3)), true) 70 assert.Equal(t, cast.ToBool(uint32(3)), true) 71 assert.Equal(t, cast.ToBool(uint64(3)), true) 72 assert.Equal(t, cast.ToBool(cast.UintPtr(3)), true) 73 assert.Equal(t, cast.ToBool(cast.Uint8Ptr(3)), true) 74 assert.Equal(t, cast.ToBool(cast.Uint16Ptr(3)), true) 75 assert.Equal(t, cast.ToBool(cast.Uint32Ptr(3)), true) 76 assert.Equal(t, cast.ToBool(cast.Uint64Ptr(3)), true) 77 78 assert.Equal(t, cast.ToBool(float32(3)), true) 79 assert.Equal(t, cast.ToBool(float64(3)), true) 80 assert.Equal(t, cast.ToBool(cast.Float32Ptr(3)), true) 81 assert.Equal(t, cast.ToBool(cast.Float64Ptr(3)), true) 82 83 assert.Equal(t, cast.ToBool("3"), false) 84 assert.Equal(t, cast.ToBool(cast.StringPtr("3")), false) 85 assert.Equal(t, cast.ToBool("true"), true) 86 assert.Equal(t, cast.ToBool(cast.StringPtr("true")), true) 87 assert.Equal(t, cast.ToBool("false"), false) 88 assert.Equal(t, cast.ToBool(cast.StringPtr("false")), false) 89 90 assert.Equal(t, cast.ToBool(true), true) 91 assert.Equal(t, cast.ToBool(false), false) 92 assert.Equal(t, cast.ToBool(cast.BoolPtr(true)), true) 93 assert.Equal(t, cast.ToBool(cast.BoolPtr(false)), false) 94 95 _, err := cast.ToBoolE("abc") 96 assert.Error(t, err, "strconv.ParseBool: parsing \"abc\": invalid syntax") 97 98 _, err = cast.ToBoolE(errors.New("abc")) 99 assert.Error(t, err, "unable to cast type \\(\\*errors\\.errorString\\) to bool") 100 }