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

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/labs"
     6  	"reflect"
     7  	"testing"
     8  	"unsafe"
     9  )
    10  
    11  //func Float64bits(f float64) uint64 {
    12  //	return *(*uint64)(unsafe.Pointer(&f))
    13  //}
    14  //
    15  //func Uint(i int) uint {
    16  //	return *(*uint)(unsafe.Pointer(&i))
    17  //}
    18  //
    19  //func Uint1(i any) uint {
    20  //	return *(*uint)(unsafe.Pointer(&i))
    21  //}
    22  
    23  func TestAnyPointer(t *testing.T) {
    24  	a := 1
    25  	var b any
    26  	b = &a
    27  	var c int
    28  	c = *(*int)(unsafe.Pointer(reflect.ValueOf(b).Pointer()))
    29  	fmt.Println("c =", c)
    30  	v := AnyToSlice[int](b, 2)
    31  	fmt.Println(v)
    32  }
    33  
    34  type tt1 struct {
    35  	data any
    36  }
    37  
    38  func (x tt1) Values() any {
    39  	return x.data
    40  }
    41  
    42  func TestAnyToSlice(t *testing.T) {
    43  	funcName := "AnyToSlice"
    44  
    45  	type args struct {
    46  		x any
    47  		y int
    48  	}
    49  	type testCase struct {
    50  		Name     string
    51  		Args     args
    52  		Want     any
    53  		TestFunc func(v any) any
    54  	}
    55  	tests := []testCase{
    56  		{
    57  			Name: "int32",
    58  			Args: args{
    59  				x: []int32{-1, 1, -2, -3},
    60  				y: 4,
    61  			},
    62  			Want: []int32{-1, 1, -2, -3},
    63  			TestFunc: func(v any) any {
    64  				vs := v.(args)
    65  				return AnyToSlice[int32](vs.x.([]int32), vs.y)
    66  			},
    67  		},
    68  		{
    69  			Name: "int32-align",
    70  			Args: args{
    71  				x: []int32{-1, 1, -2, -3, 1, 5},
    72  				y: 5,
    73  			},
    74  			Want: []int32{-1, 1, -2, -3, 1},
    75  			TestFunc: func(v any) any {
    76  				vs := v.(args)
    77  				return AnyToSlice[int32](vs.x.([]int32), vs.y)
    78  			},
    79  		},
    80  		{
    81  			Name: "float32",
    82  			Args: args{
    83  				x: []float32{-0.1, 1.0, -2.00, -3},
    84  				y: 5,
    85  			},
    86  			Want: []float32{-0.1, 1.0, -2.00, -3, Float32NaN()},
    87  			TestFunc: func(v any) any {
    88  				vs := v.(args)
    89  				return AnyToSlice[float32](vs.x.([]float32), vs.y)
    90  			},
    91  		},
    92  		{
    93  			Name: "float64",
    94  			Args: args{
    95  				x: []float64{-0.1, 1.0, -2.00, -3},
    96  				y: 5,
    97  			},
    98  			Want: []float64{-0.1, 1.0, -2.00, -3, Float64NaN()},
    99  			TestFunc: func(v any) any {
   100  				vs := v.(args)
   101  				return AnyToSlice[float64](vs.x.([]float64), vs.y)
   102  			},
   103  		},
   104  		{
   105  			Name: "float64-to-float32",
   106  			Args: args{
   107  				x: []float64{-0.1, 1.0, -2.00, -3},
   108  				y: 4,
   109  			},
   110  			Want: []float32{-0.1, 1.0, -2.00, -3},
   111  			TestFunc: func(v any) any {
   112  				vs := v.(args)
   113  				return AnyToSlice[float32](vs.x.([]float64), vs.y)
   114  			},
   115  		},
   116  		{
   117  			Name: "float64-const-equal-length",
   118  			Args: args{
   119  				x: float64(-1.23),
   120  				y: 1,
   121  			},
   122  			Want: []float64{-1.23},
   123  			TestFunc: func(v any) any {
   124  				vs := v.(args)
   125  				return AnyToSlice[float64](vs.x.(float64), vs.y)
   126  			},
   127  		},
   128  		{
   129  			Name: "float64-const-fill",
   130  			Args: args{
   131  				x: float64(-1.23),
   132  				y: 3,
   133  			},
   134  			Want: []float64{-1.23, -1.23, -1.23},
   135  			TestFunc: func(v any) any {
   136  				vs := v.(args)
   137  				return AnyToSlice[float64](vs.x.(float64), vs.y)
   138  			},
   139  		},
   140  		{
   141  			Name: "float64-const-empty",
   142  			Args: args{
   143  				x: float64(-1.23),
   144  				y: 0,
   145  			},
   146  			Want: []float64{},
   147  			TestFunc: func(v any) any {
   148  				vs := v.(args)
   149  				return AnyToSlice[float64](vs.x.(float64), vs.y)
   150  			},
   151  		},
   152  		{
   153  			Name: "float64-to-int64-cut",
   154  			Args: args{
   155  				x: []float64{-0.1, 1.0, -2.00, -3},
   156  				y: 3,
   157  			},
   158  			Want: []int64{0, 1, -2},
   159  			TestFunc: func(v any) any {
   160  				vs := v.(args)
   161  				return AnyToSlice[int64](vs.x.([]float64), vs.y)
   162  			},
   163  		},
   164  		{
   165  			Name: "float64-to-int64-fill",
   166  			Args: args{
   167  				x: []float64{-0.1, 1.0, -2.00, -3},
   168  				y: 5,
   169  			},
   170  			Want: []int64{0, 1, -2, -3, 0},
   171  			TestFunc: func(v any) any {
   172  				vs := v.(args)
   173  				return AnyToSlice[int64](vs.x.([]float64), vs.y)
   174  			},
   175  		},
   176  		{
   177  			Name: "float64-to-string",
   178  			Args: args{
   179  				x: []float64{-0.1, 1.0, -2.00, -3},
   180  				y: 5,
   181  			},
   182  			Want: []string{"-0.1", "1", "-2", "-3", "NaN"},
   183  			TestFunc: func(v any) any {
   184  				vs := v.(args)
   185  				return AnyToSlice[string](vs.x.([]float64), vs.y)
   186  			},
   187  		},
   188  		{
   189  			Name: "float64-to-bool",
   190  			Args: args{
   191  				x: []float64{-0.1, 1.0, -2.00, -3},
   192  				y: 5,
   193  			},
   194  			Want: []bool{true, true, true, true, false},
   195  			TestFunc: func(v any) any {
   196  				vs := v.(args)
   197  				return AnyToSlice[bool](vs.x.([]float64), vs.y)
   198  			},
   199  		},
   200  		{
   201  			Name: "float64(vector)-to-bool",
   202  			Args: args{
   203  				x: tt1{data: []float64{-0.1, 1.0, -2.00, -3}},
   204  				y: 5,
   205  			},
   206  			Want: []bool{true, true, true, true, false},
   207  			TestFunc: func(v any) any {
   208  				vs := v.(args)
   209  				return AnyToSlice[bool](vs.x.(tt1), vs.y)
   210  			},
   211  		},
   212  	}
   213  
   214  	for _, tt := range tests {
   215  		t.Run(tt.Name, func(t *testing.T) {
   216  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
   217  				t.Errorf("%s() = %v(%T), want %v(%T)", funcName, got, got, tt.Want, tt.Want)
   218  			}
   219  		})
   220  	}
   221  }