gitee.com/quant1x/num@v0.3.2/type_dtype_test.go (about)

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func Test_point_to_1float32(t *testing.T) {
     9  	var p1 *int8
    10  	f1 := AnyToFloat32(p1)
    11  	fmt.Printf("*int8 to float32=%f\n", f1)
    12  
    13  	var v1 int8 = 1
    14  	p1 = &v1
    15  	f1 = AnyToFloat32(p1)
    16  	fmt.Printf("*int8 to float32=%f\n", f1)
    17  }
    18  
    19  func Test_point_to_float32(t *testing.T) {
    20  	type args struct {
    21  		v any
    22  	}
    23  
    24  	// 指针声明
    25  	var pInt8 *int8
    26  	var pUint8 *uint8
    27  	var pInt16 *int16
    28  	var pUint16 *uint16
    29  	var pInt32 *int32
    30  	var pUint32 *uint32
    31  	var pInt64 *int64
    32  	var pUint64 *uint64
    33  	var pInt *int
    34  	var pUint *uint
    35  	var pFloat32 *float32
    36  	var pFloat64 *float64
    37  	var pBoolTrue *bool
    38  	var pBoolFalse *bool
    39  	var pStr1 *string
    40  	var pStr2 *string
    41  
    42  	vInt8 := int8(1)
    43  	pInt8 = &vInt8
    44  	vUint8 := uint8(1)
    45  	pUint8 = &vUint8
    46  
    47  	vInt16 := int16(1)
    48  	pInt16 = &vInt16
    49  	vUint16 := uint16(1)
    50  	pUint16 = &vUint16
    51  
    52  	vInt32 := int32(1)
    53  	pInt32 = &vInt32
    54  	vUint32 := uint32(1)
    55  	pUint32 = &vUint32
    56  
    57  	vInt64 := int64(1)
    58  	pInt64 = &vInt64
    59  	vUint64 := uint64(1)
    60  	pUint64 = &vUint64
    61  
    62  	vInt := int(1)
    63  	pInt = &vInt
    64  	vUint := uint(1)
    65  	pUint = &vUint
    66  
    67  	vFloat32 := float32(1)
    68  	pFloat32 = &vFloat32
    69  	vFloat64 := float64(1)
    70  	pFloat64 = &vFloat64
    71  
    72  	vBoolTrue := true
    73  	pBoolTrue = &vBoolTrue
    74  	vBoolFalse := false
    75  	pBoolFalse = &vBoolFalse
    76  
    77  	vStr1 := "abc"
    78  	pStr1 = &vStr1
    79  	vStr2 := "1.23"
    80  	pStr2 = &vStr2
    81  
    82  	tests := []struct {
    83  		name string
    84  		args args
    85  		want float32
    86  	}{
    87  		{
    88  			name: "T01: int8",
    89  			args: args{
    90  				pInt8,
    91  			},
    92  			want: float32(1),
    93  		},
    94  		{
    95  			name: "T02: uint8",
    96  			args: args{
    97  				pUint8,
    98  			},
    99  			want: float32(1),
   100  		},
   101  		{
   102  			name: "T03: int16",
   103  			args: args{
   104  				pInt16,
   105  			},
   106  			want: float32(1),
   107  		},
   108  		{
   109  			name: "T04: uint16",
   110  			args: args{
   111  				pUint16,
   112  			},
   113  			want: float32(1),
   114  		},
   115  		{
   116  			name: "T05: int32",
   117  			args: args{
   118  				pInt32,
   119  			},
   120  			want: float32(1),
   121  		},
   122  		{
   123  			name: "T06: uint32",
   124  			args: args{
   125  				pUint32,
   126  			},
   127  			want: float32(1),
   128  		},
   129  		{
   130  			name: "T07: int64",
   131  			args: args{
   132  				pInt64,
   133  			},
   134  			want: float32(1),
   135  		},
   136  		{
   137  			name: "T08: uint64",
   138  			args: args{
   139  				pUint64,
   140  			},
   141  			want: float32(1),
   142  		},
   143  		{
   144  			name: "T09: int",
   145  			args: args{
   146  				pInt,
   147  			},
   148  			want: float32(1),
   149  		},
   150  		{
   151  			name: "T10: uint",
   152  			args: args{
   153  				pUint,
   154  			},
   155  			want: float32(1),
   156  		},
   157  		{
   158  			name: "T11: float32",
   159  			args: args{
   160  				pFloat32,
   161  			},
   162  			want: float32(1),
   163  		},
   164  		{
   165  			name: "T12: float64",
   166  			args: args{
   167  				pFloat64,
   168  			},
   169  			want: float32(1),
   170  		},
   171  		{
   172  			name: "T13: true",
   173  			args: args{
   174  				pBoolTrue,
   175  			},
   176  			want: float32(1),
   177  		},
   178  		{
   179  			name: "T14: false",
   180  			args: args{
   181  				pBoolFalse,
   182  			},
   183  			want: float32(0),
   184  		},
   185  		{
   186  			name: "T15: str1",
   187  			args: args{
   188  				pStr1,
   189  			},
   190  			want: __nilToFloat32,
   191  		},
   192  		{
   193  			name: "T16: str2",
   194  			args: args{
   195  				pStr2,
   196  			},
   197  			want: float32(1.23),
   198  		},
   199  	}
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			//got := point_to_float32(tt.args.v)
   203  			//if got != tt.want {
   204  			//	if !IsNaN(float64(tt.want)) {
   205  			//		t.Errorf("point_to_float32() = %v, want %v", got, tt.want)
   206  			//	} else if !IsNaN(float64(got)) {
   207  			//		t.Errorf("point_to_float32() = %v, want %v", got, tt.want)
   208  			//	}
   209  			//}
   210  			if got := AnyToFloat32(tt.args.v); got != tt.want && !(Float64IsNaN(float64(tt.want)) && Float64IsNaN(float64(got))) {
   211  				t.Errorf("AnyToFloat32() = %v, want %v", got, tt.want)
   212  			}
   213  		})
   214  	}
   215  }
   216  
   217  func Test_value_to_float32(t *testing.T) {
   218  	type args struct {
   219  		v any
   220  	}
   221  
   222  	vInt8 := int8(1)
   223  	vUint8 := uint8(1)
   224  
   225  	vInt16 := int16(1)
   226  	vUint16 := uint16(1)
   227  
   228  	vInt32 := int32(1)
   229  	vUint32 := uint32(1)
   230  
   231  	vInt64 := int64(1)
   232  	vUint64 := uint64(1)
   233  
   234  	vInt := int(1)
   235  	vUint := uint(1)
   236  
   237  	vFloat32 := float32(1)
   238  	vFloat64 := float64(1)
   239  
   240  	//vBoolTrue := true
   241  	//vBoolFalse := false
   242  
   243  	vStr1 := "abc"
   244  	vStr2 := "1.23"
   245  
   246  	// 组装测试用例
   247  	tests := []struct {
   248  		name string
   249  		args args
   250  		want float32
   251  	}{
   252  		{
   253  			name: "T01: int8",
   254  			args: args{
   255  				vInt8,
   256  			},
   257  			want: float32(1),
   258  		},
   259  		{
   260  			name: "T02: uint8",
   261  			args: args{
   262  				vUint8,
   263  			},
   264  			want: float32(1),
   265  		},
   266  		{
   267  			name: "T03: int16",
   268  			args: args{
   269  				vInt16,
   270  			},
   271  			want: float32(1),
   272  		},
   273  		{
   274  			name: "T04: uint16",
   275  			args: args{
   276  				vUint16,
   277  			},
   278  			want: float32(1),
   279  		},
   280  		{
   281  			name: "T05: int32",
   282  			args: args{
   283  				vInt32,
   284  			},
   285  			want: float32(1),
   286  		},
   287  		{
   288  			name: "T06: uint32",
   289  			args: args{
   290  				vUint32,
   291  			},
   292  			want: float32(1),
   293  		},
   294  		{
   295  			name: "T07: int64",
   296  			args: args{
   297  				vInt64,
   298  			},
   299  			want: float32(1),
   300  		},
   301  		{
   302  			name: "T08: uint64",
   303  			args: args{
   304  				vUint64,
   305  			},
   306  			want: float32(1),
   307  		},
   308  		{
   309  			name: "T09: int",
   310  			args: args{
   311  				vInt,
   312  			},
   313  			want: float32(1),
   314  		},
   315  		{
   316  			name: "T10: uint",
   317  			args: args{
   318  				vUint,
   319  			},
   320  			want: float32(1),
   321  		},
   322  		{
   323  			name: "T11: float32",
   324  			args: args{
   325  				vFloat32,
   326  			},
   327  			want: float32(1),
   328  		},
   329  		{
   330  			name: "T12: float64",
   331  			args: args{
   332  				vFloat64,
   333  			},
   334  			want: float32(1),
   335  		},
   336  		{
   337  			name: "T13: true",
   338  			args: args{
   339  				true,
   340  			},
   341  			want: float32(1),
   342  		},
   343  		{
   344  			name: "T14: false",
   345  			args: args{
   346  				false,
   347  			},
   348  			want: float32(0),
   349  		},
   350  		{
   351  			name: "T15: str1",
   352  			args: args{
   353  				vStr1,
   354  			},
   355  			want: __nilToFloat32,
   356  		},
   357  		{
   358  			name: "T16: str2",
   359  			args: args{
   360  				vStr2,
   361  			},
   362  			want: float32(1.23),
   363  		},
   364  	}
   365  
   366  	for _, tt := range tests {
   367  		t.Run(tt.name, func(t *testing.T) {
   368  			//got := value_to_float32(tt.args.v)
   369  			//if got != tt.want {
   370  			//	if !IsNaN(float64(tt.want)) {
   371  			//		t.Errorf("value_to_float32() = %v, want %v", got, tt.want)
   372  			//	} else if !IsNaN(float64(got)) {
   373  			//		t.Errorf("value_to_float32() = %v, want %v", got, tt.want)
   374  			//	}
   375  			//}
   376  			if got := AnyToFloat32(tt.args.v); !(got == tt.want || (Float64IsNaN(float64(tt.want)) && Float64IsNaN(float64(got)))) {
   377  				t.Errorf("AnyToFloat32() = %v, want %v", got, tt.want)
   378  			}
   379  		})
   380  	}
   381  }