improve gconv

This commit is contained in:
John 2019-07-20 16:41:17 +08:00
parent 0a0530af0a
commit 29020b0ac0
13 changed files with 722 additions and 258 deletions

View File

@ -44,8 +44,10 @@ func Assert(value, expect interface{}) {
}
return
}
if fmt.Sprintf("%v", value) != fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, value, expect))
strValue := gconv.String(value)
strExpect := gconv.String(expect)
if strValue != strExpect {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, strValue, strExpect))
}
}
@ -62,14 +64,16 @@ func AssertEQ(value, expect interface{}) {
}
return
}
if fmt.Sprintf("%v", value) != fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, value, expect))
strValue := gconv.String(value)
strExpect := gconv.String(expect)
if strValue != strExpect {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, strValue, strExpect))
}
// Type assert.
t1 := reflect.TypeOf(value)
t2 := reflect.TypeOf(expect)
if t1 != t2 {
panic(fmt.Sprintf(`[ASSERT] EXPECT TYPE %v[%v] == %v[%v]`, value, t1, expect, t2))
panic(fmt.Sprintf(`[ASSERT] EXPECT TYPE %v[%v] == %v[%v]`, strValue, t1, strExpect, t2))
}
}
@ -85,8 +89,10 @@ func AssertNE(value, expect interface{}) {
}
return
}
if fmt.Sprintf("%v", value) == fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v != %v`, value, expect))
strValue := gconv.String(value)
strExpect := gconv.String(expect)
if strValue == strExpect {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v != %v`, strValue, strExpect))
}
}

View File

@ -16,5 +16,7 @@ func TestCase(t *testing.T) {
gtest.Case(t, func() {
gtest.Assert(1, 1)
gtest.AssertNE(1, 0)
gtest.AssertEQ(float32(123.456), float32(123.456))
gtest.AssertEQ(float64(123.456), float64(123.456))
})
}

View File

@ -11,7 +11,6 @@ import (
"encoding/json"
"reflect"
"strconv"
"strings"
"github.com/gogf/gf/g/encoding/gbinary"
)
@ -207,34 +206,34 @@ func Bool(i interface{}) bool {
if i == nil {
return false
}
if v, ok := i.(bool); ok {
return v
}
if s, ok := i.(string); ok {
if _, ok := emptyStringMap[s]; ok {
switch value := i.(type) {
case bool:
return value
case string:
if _, ok := emptyStringMap[value]; ok {
return false
}
return true
}
rv := reflect.ValueOf(i)
switch rv.Kind() {
case reflect.Ptr:
return !rv.IsNil()
case reflect.Map:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
return rv.Len() != 0
case reflect.Struct:
return true
default:
s := String(i)
if _, ok := emptyStringMap[s]; ok {
return false
rv := reflect.ValueOf(i)
switch rv.Kind() {
case reflect.Ptr:
return !rv.IsNil()
case reflect.Map:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
return rv.Len() != 0
case reflect.Struct:
return true
default:
s := String(i)
if _, ok := emptyStringMap[s]; ok {
return false
}
return true
}
return true
}
}
@ -317,6 +316,8 @@ func Int64(i interface{}) int64 {
return 1
}
return 0
case []byte:
return gbinary.DecodeToInt64(value)
default:
s := String(value)
// Hexadecimal
@ -419,6 +420,8 @@ func Uint64(i interface{}) uint64 {
return 1
}
return 0
case []byte:
return gbinary.DecodeToUint64(value)
default:
s := String(value)
// Hexadecimal
@ -447,11 +450,17 @@ func Float32(i interface{}) float32 {
if i == nil {
return 0
}
if v, ok := i.(float32); ok {
return v
switch value := i.(type) {
case float32:
return value
case float64:
return float32(value)
case []byte:
return gbinary.DecodeToFloat32(value)
default:
v, _ := strconv.ParseFloat(String(i), 64)
return float32(v)
}
v, _ := strconv.ParseFloat(strings.TrimSpace(String(i)), 64)
return float32(v)
}
// Float64 converts <i> to float64.
@ -459,9 +468,15 @@ func Float64(i interface{}) float64 {
if i == nil {
return 0
}
if v, ok := i.(float64); ok {
switch value := i.(type) {
case float32:
return float64(value)
case float64:
return value
case []byte:
return gbinary.DecodeToFloat64(value)
default:
v, _ := strconv.ParseFloat(String(i), 64)
return v
}
v, _ := strconv.ParseFloat(strings.TrimSpace(String(i)), 64)
return v
}

View File

@ -63,61 +63,65 @@ func Ints(i interface{}) []int {
return r
} else {
array := make([]int, 0)
switch i.(type) {
switch value := i.(type) {
case []string:
for _, v := range i.([]string) {
for _, v := range value {
array = append(array, Int(v))
}
case []int8:
for _, v := range i.([]int8) {
for _, v := range value {
array = append(array, Int(v))
}
case []int16:
for _, v := range i.([]int16) {
for _, v := range value {
array = append(array, Int(v))
}
case []int32:
for _, v := range i.([]int32) {
for _, v := range value {
array = append(array, Int(v))
}
case []int64:
for _, v := range i.([]int64) {
for _, v := range value {
array = append(array, Int(v))
}
case []uint:
for _, v := range i.([]uint) {
for _, v := range value {
array = append(array, Int(v))
}
case []uint8:
for _, v := range i.([]uint8) {
for _, v := range value {
array = append(array, Int(v))
}
case []uint16:
for _, v := range i.([]uint16) {
for _, v := range value {
array = append(array, Int(v))
}
case []uint32:
for _, v := range i.([]uint32) {
for _, v := range value {
array = append(array, Int(v))
}
case []uint64:
for _, v := range i.([]uint64) {
for _, v := range value {
array = append(array, Int(v))
}
case []bool:
for _, v := range i.([]bool) {
for _, v := range value {
array = append(array, Int(v))
}
case []float32:
for _, v := range i.([]float32) {
for _, v := range value {
array = append(array, Int(v))
}
case []float64:
for _, v := range i.([]float64) {
for _, v := range value {
array = append(array, Int(v))
}
case []interface{}:
for _, v := range i.([]interface{}) {
for _, v := range value {
array = append(array, Int(v))
}
case [][]byte:
for _, v := range value {
array = append(array, Int(v))
}
default:
@ -136,61 +140,65 @@ func Strings(i interface{}) []string {
return r
} else {
array := make([]string, 0)
switch i.(type) {
switch value := i.(type) {
case []int:
for _, v := range i.([]int) {
for _, v := range value {
array = append(array, String(v))
}
case []int8:
for _, v := range i.([]int8) {
for _, v := range value {
array = append(array, String(v))
}
case []int16:
for _, v := range i.([]int16) {
for _, v := range value {
array = append(array, String(v))
}
case []int32:
for _, v := range i.([]int32) {
for _, v := range value {
array = append(array, String(v))
}
case []int64:
for _, v := range i.([]int64) {
for _, v := range value {
array = append(array, String(v))
}
case []uint:
for _, v := range i.([]uint) {
for _, v := range value {
array = append(array, String(v))
}
case []uint8:
for _, v := range i.([]uint8) {
for _, v := range value {
array = append(array, String(v))
}
case []uint16:
for _, v := range i.([]uint16) {
for _, v := range value {
array = append(array, String(v))
}
case []uint32:
for _, v := range i.([]uint32) {
for _, v := range value {
array = append(array, String(v))
}
case []uint64:
for _, v := range i.([]uint64) {
for _, v := range value {
array = append(array, String(v))
}
case []bool:
for _, v := range i.([]bool) {
for _, v := range value {
array = append(array, String(v))
}
case []float32:
for _, v := range i.([]float32) {
for _, v := range value {
array = append(array, String(v))
}
case []float64:
for _, v := range i.([]float64) {
for _, v := range value {
array = append(array, String(v))
}
case []interface{}:
for _, v := range i.([]interface{}) {
for _, v := range value {
array = append(array, String(v))
}
case [][]byte:
for _, v := range value {
array = append(array, String(v))
}
default:
@ -209,61 +217,61 @@ func Floats(i interface{}) []float64 {
return r
} else {
array := make([]float64, 0)
switch i.(type) {
switch value := i.(type) {
case []string:
for _, v := range i.([]string) {
for _, v := range value {
array = append(array, Float64(v))
}
case []int:
for _, v := range i.([]int) {
for _, v := range value {
array = append(array, Float64(v))
}
case []int8:
for _, v := range i.([]int8) {
for _, v := range value {
array = append(array, Float64(v))
}
case []int16:
for _, v := range i.([]int16) {
for _, v := range value {
array = append(array, Float64(v))
}
case []int32:
for _, v := range i.([]int32) {
for _, v := range value {
array = append(array, Float64(v))
}
case []int64:
for _, v := range i.([]int64) {
for _, v := range value {
array = append(array, Float64(v))
}
case []uint:
for _, v := range i.([]uint) {
for _, v := range value {
array = append(array, Float64(v))
}
case []uint8:
for _, v := range i.([]uint8) {
for _, v := range value {
array = append(array, Float64(v))
}
case []uint16:
for _, v := range i.([]uint16) {
for _, v := range value {
array = append(array, Float64(v))
}
case []uint32:
for _, v := range i.([]uint32) {
for _, v := range value {
array = append(array, Float64(v))
}
case []uint64:
for _, v := range i.([]uint64) {
for _, v := range value {
array = append(array, Float64(v))
}
case []bool:
for _, v := range i.([]bool) {
for _, v := range value {
array = append(array, Float64(v))
}
case []float32:
for _, v := range i.([]float32) {
for _, v := range value {
array = append(array, Float64(v))
}
case []interface{}:
for _, v := range i.([]interface{}) {
for _, v := range value {
array = append(array, Float64(v))
}
default:
@ -282,61 +290,61 @@ func Interfaces(i interface{}) []interface{} {
return r
} else {
array := make([]interface{}, 0)
switch i.(type) {
switch value := i.(type) {
case []string:
for _, v := range i.([]string) {
for _, v := range value {
array = append(array, v)
}
case []int:
for _, v := range i.([]int) {
for _, v := range value {
array = append(array, v)
}
case []int8:
for _, v := range i.([]int8) {
for _, v := range value {
array = append(array, v)
}
case []int16:
for _, v := range i.([]int16) {
for _, v := range value {
array = append(array, v)
}
case []int32:
for _, v := range i.([]int32) {
for _, v := range value {
array = append(array, v)
}
case []int64:
for _, v := range i.([]int64) {
for _, v := range value {
array = append(array, v)
}
case []uint:
for _, v := range i.([]uint) {
for _, v := range value {
array = append(array, v)
}
case []uint8:
for _, v := range i.([]uint8) {
for _, v := range value {
array = append(array, v)
}
case []uint16:
for _, v := range i.([]uint16) {
for _, v := range value {
array = append(array, v)
}
case []uint32:
for _, v := range i.([]uint32) {
for _, v := range value {
array = append(array, v)
}
case []uint64:
for _, v := range i.([]uint64) {
for _, v := range value {
array = append(array, v)
}
case []bool:
for _, v := range i.([]bool) {
for _, v := range value {
array = append(array, v)
}
case []float32:
for _, v := range i.([]float32) {
for _, v := range value {
array = append(array, v)
}
case []float64:
for _, v := range i.([]float64) {
for _, v := range value {
array = append(array, v)
}
default:

View File

@ -15,7 +15,10 @@ import (
// Time converts <i> to time.Time.
func Time(i interface{}, format ...string) time.Time {
return GTime(i, format...).Time
if t := GTime(i, format...); t != nil {
return t.Time
}
return time.Time{}
}
// Duration converts <i> to time.Duration.

View File

@ -609,7 +609,7 @@ func Test_Byte_All(t *testing.T) {
gtest.Case(t, func() {
gtest.AssertEQ(gconv.Byte(uint8(0)), uint8(0))
gtest.AssertEQ(gconv.Byte("s"), uint8(0))
gtest.AssertEQ(gconv.Byte([]byte("s")), uint8(0))
gtest.AssertEQ(gconv.Byte([]byte("s")), uint8(115))
})
}
@ -692,8 +692,8 @@ func Test_Slice_All(t *testing.T) {
gtest.AssertEQ(gconv.Strings([]bool{true}), []string{"true"})
gtest.AssertEQ(gconv.Strings([]float32{1, 2}), []string{"1", "2"})
gtest.AssertEQ(gconv.Strings([]float64{1, 2}), []string{"1", "2"})
var strer []interface{} = make([]interface{}, 2)
gtest.AssertEQ(gconv.Strings(strer), []string{" "})
var strer = make([]interface{}, 2)
gtest.AssertEQ(gconv.Strings(strer), []string{"", ""})
gtest.AssertEQ(gconv.Floats(value), []float64{123.456})
gtest.AssertEQ(gconv.Floats(nil), nil)
@ -711,7 +711,7 @@ func Test_Slice_All(t *testing.T) {
gtest.AssertEQ(gconv.Floats([]bool{true}), []float64{0})
gtest.AssertEQ(gconv.Floats([]float32{1, 2}), []float64{1, 2})
gtest.AssertEQ(gconv.Floats([]float64{1, 2}), []float64{1, 2})
var floer []interface{} = make([]interface{}, 2)
var floer = make([]interface{}, 2)
gtest.AssertEQ(gconv.Floats(floer), []float64{0, 0})
gtest.AssertEQ(gconv.Interfaces(value), []interface{}{123.456})
@ -739,7 +739,7 @@ func Test_Slice_All(t *testing.T) {
gtest.AssertEQ(gconv.Maps(nil), nil)
gtest.AssertEQ(gconv.Maps([]map[string]interface{}{{"a": "1"}}), []map[string]interface{}{{"a": "1"}})
gtest.AssertEQ(gconv.Maps(1223), []map[string]interface{}{{}})
gtest.AssertEQ(gconv.Maps(1223), []map[string]interface{}{nil})
gtest.AssertEQ(gconv.Maps([]int{}), nil)
})
}

View File

@ -0,0 +1,150 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench "Benchmark_Bytes_To_*" -benchmem
package gconv
import (
"testing"
"unsafe"
"github.com/gogf/gf/g/encoding/gbinary"
)
var valueBytes = gbinary.Encode(123456789)
func Benchmark_Bytes_To_String_Normal(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = string(valueBytes)
}
}
func Benchmark_Bytes_To_String_Unsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = *(*string)(unsafe.Pointer(&valueBytes))
}
}
func Benchmark_Bytes_To_String(b *testing.B) {
for i := 0; i < b.N; i++ {
String(valueBytes)
}
}
func Benchmark_Bytes_To_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueBytes)
}
}
func Benchmark_Bytes_To_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
Int8(valueBytes)
}
}
func Benchmark_Bytes_To_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
Int16(valueBytes)
}
}
func Benchmark_Bytes_To_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
Int32(valueBytes)
}
}
func Benchmark_Bytes_To_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueBytes)
}
}
func Benchmark_Bytes_To_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(valueBytes)
}
}
func Benchmark_Bytes_To_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint8(valueBytes)
}
}
func Benchmark_Bytes_To_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint16(valueBytes)
}
}
func Benchmark_Bytes_To_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint32(valueBytes)
}
}
func Benchmark_Bytes_To_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint64(valueBytes)
}
}
func Benchmark_Bytes_To_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32(valueBytes)
}
}
func Benchmark_Bytes_To_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
Float64(valueBytes)
}
}
func Benchmark_Bytes_To_Time(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(valueBytes)
}
}
func Benchmark_Bytes_To_TimeDuration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(valueBytes)
}
}
func Benchmark_Bytes_To_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(valueBytes)
}
}
func Benchmark_Bytes_To_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
Strings(valueBytes)
}
}
func Benchmark_Bytes_To_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
Ints(valueBytes)
}
}
func Benchmark_Bytes_To_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
Floats(valueBytes)
}
}
func Benchmark_Bytes_To_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
Interfaces(valueBytes)
}
}

View File

@ -0,0 +1,135 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gconv
import (
"testing"
)
var valueFloat = float64(1.23456789)
func Benchmark_Float_To_String(b *testing.B) {
for i := 0; i < b.N; i++ {
String(valueFloat)
}
}
func Benchmark_Float_To_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueFloat)
}
}
func Benchmark_Float_To_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
Int8(valueFloat)
}
}
func Benchmark_Float_To_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
Int16(valueFloat)
}
}
func Benchmark_Float_To_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
Int32(valueFloat)
}
}
func Benchmark_Float_To_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueFloat)
}
}
func Benchmark_Float_To_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(valueFloat)
}
}
func Benchmark_Float_To_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint8(valueFloat)
}
}
func Benchmark_Float_To_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint16(valueFloat)
}
}
func Benchmark_Float_To_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint32(valueFloat)
}
}
func Benchmark_Float_To_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint64(valueFloat)
}
}
func Benchmark_Float_To_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32(valueFloat)
}
}
func Benchmark_Float_To_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
Float64(valueFloat)
}
}
func Benchmark_Float_To_Time(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(valueFloat)
}
}
func Benchmark_Float_To_TimeDuration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(valueFloat)
}
}
func Benchmark_Float_To_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(valueFloat)
}
}
func Benchmark_Float_To_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
Strings(valueFloat)
}
}
func Benchmark_Float_To_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
Ints(valueFloat)
}
}
func Benchmark_Float_To_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
Floats(valueFloat)
}
}
func Benchmark_Float_To_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
Interfaces(valueFloat)
}
}

View File

@ -0,0 +1,135 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gconv
import (
"testing"
)
var valueInt = 123456789
func Benchmark_Int_To_String(b *testing.B) {
for i := 0; i < b.N; i++ {
String(valueInt)
}
}
func Benchmark_Int_To_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueInt)
}
}
func Benchmark_Int_To_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
Int8(valueInt)
}
}
func Benchmark_Int_To_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
Int16(valueInt)
}
}
func Benchmark_Int_To_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
Int32(valueInt)
}
}
func Benchmark_Int_To_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueInt)
}
}
func Benchmark_Int_To_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(valueInt)
}
}
func Benchmark_Int_To_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint8(valueInt)
}
}
func Benchmark_Int_To_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint16(valueInt)
}
}
func Benchmark_Int_To_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint32(valueInt)
}
}
func Benchmark_Int_To_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint64(valueInt)
}
}
func Benchmark_Int_To_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32(valueInt)
}
}
func Benchmark_Int_To_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
Float64(valueInt)
}
}
func Benchmark_Int_To_Time(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(valueInt)
}
}
func Benchmark_Int_To_TimeDuration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(valueInt)
}
}
func Benchmark_Int_To_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(valueInt)
}
}
func Benchmark_Int_To_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
Strings(valueInt)
}
}
func Benchmark_Int_To_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
Ints(valueInt)
}
}
func Benchmark_Int_To_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
Floats(valueInt)
}
}
func Benchmark_Int_To_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
Interfaces(valueInt)
}
}

View File

@ -0,0 +1,135 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gconv
import (
"testing"
)
var valueStr = "123456789"
func Benchmark_Str_To_String(b *testing.B) {
for i := 0; i < b.N; i++ {
String(valueStr)
}
}
func Benchmark_Str_To_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueStr)
}
}
func Benchmark_Str_To_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
Int8(valueStr)
}
}
func Benchmark_Str_To_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
Int16(valueStr)
}
}
func Benchmark_Str_To_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
Int32(valueStr)
}
}
func Benchmark_Str_To_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(valueStr)
}
}
func Benchmark_Str_To_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(valueStr)
}
}
func Benchmark_Str_To_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint8(valueStr)
}
}
func Benchmark_Str_To_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint16(valueStr)
}
}
func Benchmark_Str_To_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint32(valueStr)
}
}
func Benchmark_Str_To_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint64(valueStr)
}
}
func Benchmark_Str_To_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32(valueStr)
}
}
func Benchmark_Str_To_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
Float64(valueStr)
}
}
func Benchmark_Str_To_Time(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(valueStr)
}
}
func Benchmark_Str_To_TimeDuration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(valueStr)
}
}
func Benchmark_Str_To_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(valueStr)
}
}
func Benchmark_Str_To_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
Strings(valueStr)
}
}
func Benchmark_Str_To_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
Ints(valueStr)
}
}
func Benchmark_Str_To_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
Floats(valueStr)
}
}
func Benchmark_Str_To_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
Interfaces(valueStr)
}
}

View File

@ -1,135 +0,0 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gconv
import (
"testing"
)
var value = 123456789
func BenchmarkString(b *testing.B) {
for i := 0; i < b.N; i++ {
String(value)
}
}
func BenchmarkInt(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(value)
}
}
func BenchmarkInt8(b *testing.B) {
for i := 0; i < b.N; i++ {
Int8(value)
}
}
func BenchmarkInt16(b *testing.B) {
for i := 0; i < b.N; i++ {
Int16(value)
}
}
func BenchmarkInt32(b *testing.B) {
for i := 0; i < b.N; i++ {
Int32(value)
}
}
func BenchmarkInt64(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(value)
}
}
func BenchmarkUint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(value)
}
}
func BenchmarkUint8(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint8(value)
}
}
func BenchmarkUint16(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint16(value)
}
}
func BenchmarkUint32(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint32(value)
}
}
func BenchmarkUint64(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint64(value)
}
}
func BenchmarkFloat32(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32(value)
}
}
func BenchmarkFloat64(b *testing.B) {
for i := 0; i < b.N; i++ {
Float64(value)
}
}
func BenchmarkTime(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(value)
}
}
func BenchmarkTimeDuration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(value)
}
}
func BenchmarkBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(value)
}
}
func BenchmarkStrings(b *testing.B) {
for i := 0; i < b.N; i++ {
Strings(value)
}
}
func BenchmarkInts(b *testing.B) {
for i := 0; i < b.N; i++ {
Ints(value)
}
}
func BenchmarkFloats(b *testing.B) {
for i := 0; i < b.N; i++ {
Floats(value)
}
}
func BenchmarkInterfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
Interfaces(value)
}
}

View File

@ -7,30 +7,31 @@
package gconv_test
import (
"testing"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gconv"
"testing"
)
func Test_Basic(t *testing.T) {
gtest.Case(t, func() {
vint := float32(123.456)
vint64 := int64(1552578474888)
gtest.AssertEQ(gconv.Int(vint), int(123))
gtest.AssertEQ(gconv.Int8(vint), int8(123))
gtest.AssertEQ(gconv.Int16(vint), int16(123))
gtest.AssertEQ(gconv.Int32(vint), int32(123))
gtest.AssertEQ(gconv.Int64(vint), int64(123))
gtest.AssertEQ(gconv.Int64(vint), int64(123))
gtest.AssertEQ(gconv.Uint(vint), uint(123))
gtest.AssertEQ(gconv.Uint8(vint), uint8(123))
gtest.AssertEQ(gconv.Uint16(vint), uint16(123))
gtest.AssertEQ(gconv.Uint32(vint), uint32(123))
gtest.AssertEQ(gconv.Uint64(vint), uint64(123))
gtest.AssertEQ(gconv.Float32(vint), float32(123.456))
gtest.AssertEQ(gconv.Float64(vint), float64(123.456))
gtest.AssertEQ(gconv.Bool(vint), true)
gtest.AssertEQ(gconv.String(vint), "123.456")
gtest.AssertEQ(gconv.String(vint64), "1552578474888")
f32 := float32(123.456)
i64 := int64(1552578474888)
gtest.AssertEQ(gconv.Int(f32), int(123))
gtest.AssertEQ(gconv.Int8(f32), int8(123))
gtest.AssertEQ(gconv.Int16(f32), int16(123))
gtest.AssertEQ(gconv.Int32(f32), int32(123))
gtest.AssertEQ(gconv.Int64(f32), int64(123))
gtest.AssertEQ(gconv.Int64(f32), int64(123))
gtest.AssertEQ(gconv.Uint(f32), uint(123))
gtest.AssertEQ(gconv.Uint8(f32), uint8(123))
gtest.AssertEQ(gconv.Uint16(f32), uint16(123))
gtest.AssertEQ(gconv.Uint32(f32), uint32(123))
gtest.AssertEQ(gconv.Uint64(f32), uint64(123))
gtest.AssertEQ(gconv.Float32(f32), float32(123.456))
gtest.AssertEQ(gconv.Float64(i64), float64(i64))
gtest.AssertEQ(gconv.Bool(f32), true)
gtest.AssertEQ(gconv.String(f32), "123.456")
gtest.AssertEQ(gconv.String(i64), "1552578474888")
})
}

View File

@ -1 +1,10 @@
package main
import (
"fmt"
)
func main() {
f := int64(1552578474888)
fmt.Printf("%+v", f)
}