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

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/labs"
     6  	"math/rand"
     7  	"slices"
     8  	"sync"
     9  	"testing"
    10  )
    11  
    12  func Test_Align(t *testing.T) {
    13  	xl := 5
    14  	nl := xl + 10
    15  	x := make([]float64, xl)
    16  	for i := 0; i < xl; i++ {
    17  		x[i] = rand.Float64()
    18  	}
    19  	y := Align[float64](x, __nilToFloat64, nl)
    20  	fmt.Println(y)
    21  }
    22  
    23  func TestAlign(t *testing.T) {
    24  	type args struct {
    25  		x any
    26  		a any
    27  		n int
    28  	}
    29  	type testCase struct {
    30  		Name     string
    31  		Args     args
    32  		Want     any
    33  		TestFunc func(v any) any
    34  	}
    35  	tests := []testCase{
    36  		{
    37  			Name: "bool",
    38  			Args: args{
    39  				x: []bool{false, true},
    40  				a: false,
    41  				n: 5,
    42  			},
    43  			Want: []bool{false, true, false, false, false},
    44  			TestFunc: func(v any) any {
    45  				vs := v.(args)
    46  				return Align(vs.x.([]bool), vs.a.(bool), vs.n)
    47  			},
    48  		},
    49  		{
    50  			Name: "string",
    51  			Args: args{
    52  				x: []string{"1", "2"},
    53  				a: "x",
    54  				n: 5,
    55  			},
    56  			Want: []string{"1", "2", "x", "x", "x"},
    57  			TestFunc: func(v any) any {
    58  				vs := v.(args)
    59  				return Align(vs.x.([]string), vs.a.(string), vs.n)
    60  			},
    61  		},
    62  		{
    63  			Name: "float32",
    64  			Args: args{
    65  				x: []float32{-0.1, 1.0, -2.00, -3},
    66  				a: Float32NaN(),
    67  				n: 5,
    68  			},
    69  			Want: []float32{-0.1, 1.0, -2.00, -3, Float32NaN()},
    70  			TestFunc: func(v any) any {
    71  				vs := v.(args)
    72  				return Align(vs.x.([]float32), vs.a.(float32), vs.n)
    73  			},
    74  		},
    75  		{
    76  			Name: "float64",
    77  			Args: args{
    78  				x: []float64{-0.1, 1.0, -2.00, -3},
    79  				a: Float64NaN(),
    80  				n: 4,
    81  			},
    82  			Want: []float64{-0.1, 1.0, -2.00, -3},
    83  			TestFunc: func(v any) any {
    84  				vs := v.(args)
    85  				return Align(vs.x.([]float64), vs.a.(float64), vs.n)
    86  			},
    87  		},
    88  		{
    89  			Name: "float64-cut",
    90  			Args: args{
    91  				x: []float64{-0.1, 1.0, -2.00, -3},
    92  				a: Float64NaN(),
    93  				n: 3,
    94  			},
    95  			Want: []float64{-0.1, 1.0, -2.00},
    96  			TestFunc: func(v any) any {
    97  				vs := v.(args)
    98  				return Align(vs.x.([]float64), vs.a.(float64), vs.n)
    99  			},
   100  		},
   101  		{
   102  			Name: "float64-fill",
   103  			Args: args{
   104  				x: []float64{-0.1, 1.0, -2.00, -3},
   105  				a: Float64NaN(),
   106  				n: 10,
   107  			},
   108  			Want: []float64{-0.1, 1.0, -2.00, -3, Float64NaN(), Float64NaN(), Float64NaN(), Float64NaN(), Float64NaN(), Float64NaN()},
   109  			TestFunc: func(v any) any {
   110  				vs := v.(args)
   111  				return Align(vs.x.([]float64), vs.a.(float64), vs.n)
   112  			},
   113  		},
   114  	}
   115  
   116  	for _, tt := range tests {
   117  		t.Run(tt.Name, func(t *testing.T) {
   118  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
   119  				t.Errorf("Align() = %v, want %v", got, tt.Want)
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  const (
   126  	benchAlignLength  = 5000
   127  	benchAlignInitNum = 1000
   128  )
   129  
   130  var (
   131  	testalignOnce    sync.Once
   132  	testDataFloat32  []float32
   133  	testDataFloat32y []float32
   134  	testDataFloat64  []float64
   135  	testDataFloat64y []float64
   136  )
   137  
   138  func initTestData() {
   139  	testDataFloat32 = make([]float32, benchAlignInitNum)
   140  	testDataFloat32y = make([]float32, benchAlignInitNum)
   141  	testDataFloat64 = make([]float64, benchAlignInitNum)
   142  	testDataFloat64y = make([]float64, benchAlignInitNum)
   143  	for i := 0; i < benchAlignInitNum; i++ {
   144  		testDataFloat32[i] = rand.Float32()
   145  		testDataFloat32y[i] = rand.Float32()
   146  		testDataFloat64[i] = rand.Float64()
   147  		testDataFloat64y[i] = rand.Float64()
   148  	}
   149  }
   150  
   151  func BenchmarkAlign_init(b *testing.B) {
   152  	testalignOnce.Do(initTestData)
   153  }
   154  
   155  func BenchmarkAlign_release(b *testing.B) {
   156  	testalignOnce.Do(initTestData)
   157  	length := benchAlignInitNum + benchAlignLength
   158  	x := slices.Clone(testDataFloat64)
   159  	for n := 0; n < b.N; n++ {
   160  		Align[float64](x, __nilToFloat64, length)
   161  	}
   162  }
   163  
   164  func BenchmarkAlign_v1(b *testing.B) {
   165  	testalignOnce.Do(initTestData)
   166  	length := benchAlignInitNum + benchAlignLength
   167  	x := slices.Clone(testDataFloat64)
   168  	for n := 0; n < b.N; n++ {
   169  		v1Align[float64](x, __nilToFloat64, length)
   170  	}
   171  }
   172  
   173  func BenchmarkAlign_v2_float32(b *testing.B) {
   174  	testalignOnce.Do(initTestData)
   175  	length := benchAlignInitNum + benchAlignLength
   176  	x := slices.Clone(testDataFloat32)
   177  	for n := 0; n < b.N; n++ {
   178  		v2Align[float32](x, __nilToFloat32, length)
   179  	}
   180  }
   181  
   182  func BenchmarkAlign_v2_float64(b *testing.B) {
   183  	testalignOnce.Do(initTestData)
   184  	length := benchAlignInitNum + benchAlignLength
   185  	x := slices.Clone(testDataFloat64)
   186  	for n := 0; n < b.N; n++ {
   187  		v2Align[float64](x, __nilToFloat64, length)
   188  	}
   189  }
   190  
   191  func BenchmarkAlign_v2_avx2_float32(b *testing.B) {
   192  	testalignOnce.Do(initTestData)
   193  	SetAvx2Enabled(true)
   194  	length := benchAlignInitNum + benchAlignLength
   195  	x := slices.Clone(testDataFloat32)
   196  	for n := 0; n < b.N; n++ {
   197  		v2Align[float32](x, __nilToFloat32, length)
   198  	}
   199  }
   200  
   201  func BenchmarkAlign_v2_avx2_float64(b *testing.B) {
   202  	testalignOnce.Do(initTestData)
   203  	SetAvx2Enabled(true)
   204  	length := benchAlignInitNum + benchAlignLength
   205  	x := slices.Clone(testDataFloat64)
   206  	for n := 0; n < b.N; n++ {
   207  		v2Align[float64](x, __nilToFloat64, length)
   208  	}
   209  }