github.com/sandwich-go/boost@v1.3.29/xconv/float_test.go (about)

     1  package xconv
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  )
     7  
     8  type testFloat struct {
     9  	f1 float32
    10  	f2 float64
    11  }
    12  
    13  func (b testFloat) Float32() float32 { return b.f1 }
    14  func (b testFloat) Float64() float64 { return b.f2 }
    15  func TestFloat32(t *testing.T) {
    16  	Convey(`test float32`, t, func() {
    17  		for _, v := range []struct {
    18  			i  interface{}
    19  			is float32
    20  		}{
    21  			{nil, 0}, {float32(3.14), 3.14}, {3.14, 3.14}, {[]byte{0x00, 0x00, 0x48, 0x42}, 50},
    22  			{testFloat{f1: 3.14, f2: 3.15}, 3.14}, {"3.14", 3.14},
    23  		} {
    24  			So(Float32(v.i), ShouldEqual, v.is)
    25  		}
    26  	})
    27  }
    28  
    29  func TestFloat64(t *testing.T) {
    30  	Convey(`test float64`, t, func() {
    31  		for _, v := range []struct {
    32  			i  interface{}
    33  			is float64
    34  		}{
    35  			{nil, 0}, {3.14, 3.14}, {[]byte{24, 45, 68, 84, 251, 33, 9, 64}, 3.141592653589793},
    36  			{testFloat{f1: 3.14, f2: 3.15}, 3.15}, {"3.14", 3.14},
    37  		} {
    38  			So(Float64(v.i), ShouldEqual, v.is)
    39  		}
    40  	})
    41  }