github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_float_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"math"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/gogf/gf/v2/container/gvar"
    15  	"github.com/gogf/gf/v2/test/gtest"
    16  	"github.com/gogf/gf/v2/util/gconv"
    17  )
    18  
    19  var (
    20  	// Please Note:
    21  	// When the type is float32 or a custom type defined based on float32,
    22  	// switching to float64 may result in a few extra decimal places.
    23  	float32TestValue = float32(123)
    24  	float64TestValue = float64(123.456)
    25  )
    26  
    27  var floatTests = []struct {
    28  	value    interface{}
    29  	expect32 float32
    30  	expect64 float64
    31  }{
    32  	{true, 1, 1},
    33  	{false, 0, 0},
    34  
    35  	{int(0), 0, 0},
    36  	{int(123), 123, 123},
    37  	{int8(123), 123, 123},
    38  	{int16(123), 123, 123},
    39  	{int32(123), 123, 123},
    40  	{int64(123), 123, 123},
    41  
    42  	{uint(0), 0, 0},
    43  	{uint(123), 123, 123},
    44  	{uint8(123), 123, 123},
    45  	{uint16(123), 123, 123},
    46  	{uint32(123), 123, 123},
    47  	{uint64(123), 123, 123},
    48  
    49  	{uintptr(0), 0, 0},
    50  	{uintptr(123), 123, 123},
    51  
    52  	{rune(0), 0, 0},
    53  	{rune(49), 49, 49},
    54  
    55  	{float32(123), 123, 123},
    56  	{float64(123.456), 123.456, 123.456},
    57  
    58  	{[]byte(""), 0, 0},
    59  
    60  	{"0", 0, 0},
    61  	{"", 0, 0},
    62  	{"1", 1, 1},
    63  	{"123.456", 123.456, 123.456},
    64  	{"true", 0, 0},
    65  	{"false", 0, 0},
    66  	{"on", 0, 0},
    67  	{"off", 0, 0},
    68  	{"NaN", float32(math.NaN()), math.NaN()},
    69  
    70  	{complex(1, 2), 0, 0},
    71  	{complex(123.456, 789.123), 0, 0},
    72  
    73  	{[3]int{1, 2, 3}, 0, 0},
    74  	{[]int{1, 2, 3}, 0, 0},
    75  
    76  	{map[int]int{1: 1}, 0, 0},
    77  	{map[string]string{"Earth": "太平洋"}, 0, 0},
    78  
    79  	{struct{}{}, 0, 0},
    80  	{nil, 0, 0},
    81  	{(*float32)(nil), 0, 0},
    82  	{(*float64)(nil), 0, 0},
    83  
    84  	{gvar.New(123), 123, 123},
    85  	{gvar.New(123.456), 123.456, 123.456},
    86  
    87  	{&float32TestValue, 123, 123},
    88  	{&float64TestValue, 123.456, 123.456},
    89  
    90  	{myFloat32(123), 123, 123},
    91  	{myFloat64(123.456), 123.456, 123.456},
    92  
    93  	{(*myFloat32)(&float32TestValue), 123, 123},
    94  	{(*myFloat64)(&float64TestValue), 123.456, 123.456},
    95  
    96  	{(*myFloat32)(nil), 0, 0},
    97  	{(*myFloat64)(nil), 0, 0},
    98  }
    99  
   100  func TestFloat32(t *testing.T) {
   101  	gtest.C(t, func(t *gtest.T) {
   102  		for _, test := range floatTests {
   103  			t.AssertEQ(gconv.Float32(test.value), test.expect32)
   104  		}
   105  	})
   106  }
   107  
   108  func TestFloat64(t *testing.T) {
   109  	gtest.C(t, func(t *gtest.T) {
   110  		for _, test := range floatTests {
   111  			t.AssertEQ(gconv.Float64(test.value), test.expect64)
   112  		}
   113  	})
   114  }
   115  
   116  func TestFloat32s(t *testing.T) {
   117  	gtest.C(t, func(t *gtest.T) {
   118  		for _, test := range floatTests {
   119  			if test.value == nil {
   120  				t.AssertNil(gconv.Float32s(test.value))
   121  				continue
   122  			}
   123  
   124  			var (
   125  				sliceType = reflect.SliceOf(reflect.TypeOf(test.value))
   126  				float32s  = reflect.MakeSlice(sliceType, 0, 0)
   127  				expects   = []float32{
   128  					test.expect32, test.expect32,
   129  				}
   130  			)
   131  			float32s = reflect.Append(float32s, reflect.ValueOf(test.value))
   132  			float32s = reflect.Append(float32s, reflect.ValueOf(test.value))
   133  
   134  			t.AssertEQ(gconv.Float32s(float32s.Interface()), expects)
   135  			t.AssertEQ(gconv.SliceFloat32(float32s.Interface()), expects)
   136  		}
   137  	})
   138  
   139  	// Test for special types.
   140  	gtest.C(t, func(t *gtest.T) {
   141  		// string
   142  		t.AssertEQ(gconv.Float32s(""), []float32{})
   143  		t.AssertEQ(gconv.Float32s("123"), []float32{123})
   144  
   145  		// []int8 json
   146  		t.AssertEQ(gconv.Float32s([]uint8(`{"Name":"Earth"}"`)),
   147  			[]float32{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34})
   148  
   149  		// []interface
   150  		t.AssertEQ(gconv.Float32s([]interface{}{1, 2, 3}), []float32{1, 2, 3})
   151  
   152  		// gvar.Var
   153  		t.AssertEQ(gconv.Float32s(
   154  			gvar.New([]float32{1, 2, 3}),
   155  		), []float32{1, 2, 3})
   156  	})
   157  }
   158  
   159  func TestFloat64s(t *testing.T) {
   160  	gtest.C(t, func(t *gtest.T) {
   161  		for _, test := range floatTests {
   162  			if test.value == nil {
   163  				t.AssertNil(gconv.Float64s(test.value))
   164  				continue
   165  			}
   166  
   167  			var (
   168  				sliceType = reflect.SliceOf(reflect.TypeOf(test.value))
   169  				float64s  = reflect.MakeSlice(sliceType, 0, 0)
   170  				expects   = []float64{
   171  					test.expect64, test.expect64,
   172  				}
   173  			)
   174  			float64s = reflect.Append(float64s, reflect.ValueOf(test.value))
   175  			float64s = reflect.Append(float64s, reflect.ValueOf(test.value))
   176  
   177  			t.AssertEQ(gconv.Float64s(float64s.Interface()), expects)
   178  			t.AssertEQ(gconv.SliceFloat64(float64s.Interface()), expects)
   179  		}
   180  	})
   181  
   182  	// Test for special types.
   183  	gtest.C(t, func(t *gtest.T) {
   184  		// string
   185  		t.AssertEQ(gconv.Float64s(""), []float64{})
   186  		t.AssertEQ(gconv.Float64s("123"), []float64{123})
   187  
   188  		// []int8 json
   189  		t.AssertEQ(gconv.Float64s([]uint8(`{"Name":"Earth"}"`)),
   190  			[]float64{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34})
   191  
   192  		// []interface
   193  		t.AssertEQ(gconv.Float64s([]interface{}{1, 2, 3}), []float64{1, 2, 3})
   194  
   195  		// gvar.Var
   196  		t.AssertEQ(gconv.Float64s(
   197  			gvar.New([]float64{1, 2, 3}),
   198  		), []float64{1, 2, 3})
   199  	})
   200  }
   201  
   202  // gconv.Floats uses gconv.Float64s.
   203  func TestFloats(t *testing.T) {
   204  	gtest.C(t, func(t *gtest.T) {
   205  		for _, test := range floatTests {
   206  			if test.value == nil {
   207  				t.AssertNil(gconv.Floats(test.value))
   208  				continue
   209  			}
   210  
   211  			var (
   212  				sliceType = reflect.SliceOf(reflect.TypeOf(test.value))
   213  				float64s  = reflect.MakeSlice(sliceType, 0, 0)
   214  				expects   = []float64{
   215  					test.expect64, test.expect64,
   216  				}
   217  			)
   218  			float64s = reflect.Append(float64s, reflect.ValueOf(test.value))
   219  			float64s = reflect.Append(float64s, reflect.ValueOf(test.value))
   220  
   221  			t.AssertEQ(gconv.Floats(float64s.Interface()), expects)
   222  			t.AssertEQ(gconv.SliceFloat(float64s.Interface()), expects)
   223  		}
   224  	})
   225  }