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

     1  package binary
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/labs"
     6  	"testing"
     7  	"unsafe"
     8  )
     9  
    10  func TestSizeOf(t *testing.T) {
    11  	fmt.Printf("bool: %d\n", unsafe.Sizeof(true))
    12  	realPart := float32(3.14)
    13  	fmt.Printf("float32: %d\n", unsafe.Sizeof(realPart))
    14  	up := uintptr(0)
    15  	fmt.Printf("uintptr: %d\n", unsafe.Sizeof(up))
    16  	imaginaryPart := float32(2.71)
    17  	complex64Num := complex(realPart, imaginaryPart)
    18  	fmt.Printf("complex64: %d\n", unsafe.Sizeof(complex64Num))
    19  	complex128Num := complex(float64(realPart), float64(imaginaryPart))
    20  	fmt.Printf("complex128: %d\n", unsafe.Sizeof(complex128Num))
    21  
    22  	str := "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
    23  	str = ""
    24  	fmt.Printf("string: %d\n", unsafe.Sizeof(str))
    25  }
    26  
    27  func TestToBytes2(t *testing.T) {
    28  
    29  	x := 1.1
    30  	fmt.Printf("x: %p\n", &x)
    31  	b := ToBytes(x)
    32  	fmt.Printf("p1: %p\n", b)
    33  	m := Mem2(b)
    34  	fmt.Printf("p2: %p\n", m.address())
    35  	fmt.Println(m)
    36  }
    37  
    38  func TestToBytesV0(t *testing.T) {
    39  	x := 1.1
    40  	//b1 := *(*[]byte)unsafe.Slice()
    41  	b := v1ToBytes_nocopy(x)
    42  	fmt.Printf("p1: %p\n", b)
    43  	m := Mem2(b)
    44  	fmt.Printf("p2: %p\n", m.address())
    45  	fmt.Println(m)
    46  }
    47  
    48  func TestToBytesV00(t *testing.T) {
    49  	x := 1.1
    50  	b := f64ToBytes(x)
    51  	fmt.Printf("p1: %p\n", b)
    52  	m := Mem2(b)
    53  	fmt.Printf("p2: %p\n", m.address())
    54  	fmt.Println(m)
    55  }
    56  
    57  func TestToBytes(t *testing.T) {
    58  	type args struct {
    59  		x any
    60  	}
    61  	type testCase struct {
    62  		name     string
    63  		args     args
    64  		testFunc func(x any) []byte
    65  		want     []byte
    66  	}
    67  	tests := []testCase{
    68  		{
    69  			name: "float64",
    70  			args: args{x: 1.1},
    71  			testFunc: func(x any) []byte {
    72  				args := x.(args)
    73  				return ToBytes[float64](args.x.(float64))
    74  			},
    75  			want: []byte{154, 153, 153, 153, 153, 153, 241, 63},
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			if got := tt.testFunc(tt.args); !labs.DeepEqual(got, tt.want) {
    81  				t.Errorf("Align() = %v, want %v", got, tt.want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  const (
    88  	benchToBytesTimes = 5000
    89  )
    90  
    91  func BenchmarkToBytes_release(b *testing.B) {
    92  	x := float64(1.1)
    93  	for n := 0; n < b.N; n++ {
    94  		ToBytes[float64](x)
    95  	}
    96  }
    97  
    98  func BenchmarkToBytes_v0_float64(b *testing.B) {
    99  	x := float64(1.1)
   100  	for n := 0; n < b.N; n++ {
   101  		v0ToBytes[float64](x)
   102  	}
   103  }
   104  
   105  func BenchmarkToBytes_v1copy_float64(b *testing.B) {
   106  	x := float64(1.1)
   107  	for n := 0; n < b.N; n++ {
   108  		v1ToBytes_copy(x)
   109  	}
   110  }
   111  func BenchmarkToBytes_v1nocopy_float64(b *testing.B) {
   112  	x := float64(1.1)
   113  	for n := 0; n < b.N; n++ {
   114  		v1ToBytes_nocopy(x)
   115  	}
   116  }
   117  
   118  func BenchmarkToBytes_v1_float64_2(b *testing.B) {
   119  	x := float64(1.1)
   120  	for n := 0; n < b.N; n++ {
   121  		f64ToBytes(x)
   122  	}
   123  }
   124  
   125  func BenchmarkToBytes_v2_float64(b *testing.B) {
   126  	x := float64(1.1)
   127  	for n := 0; n < b.N; n++ {
   128  		v2ToBytes[float64](x)
   129  	}
   130  }
   131  
   132  func BenchmarkToBytes_v3_float64(b *testing.B) {
   133  	x := float64(1.1)
   134  	for n := 0; n < b.N; n++ {
   135  		v3ToBytes[float64](x)
   136  	}
   137  }
   138  
   139  func BenchmarkToSlice_release(b *testing.B) {
   140  	data := ToBytes[float64](1.1)
   141  	for i := 0; i < b.N; i++ {
   142  		ToSlice[float64](data, 1)
   143  	}
   144  }
   145  
   146  func BenchmarkToSlice_v0(b *testing.B) {
   147  	data := ToBytes[float64](1.1)
   148  	for i := 0; i < b.N; i++ {
   149  		v0ToSlice[float64](data, 1)
   150  	}
   151  }
   152  
   153  func BenchmarkToSlice_v1(b *testing.B) {
   154  	data := ToBytes[float64](1.1)
   155  	for i := 0; i < b.N; i++ {
   156  		v1ToSlice[float64](data, 1)
   157  	}
   158  }
   159  
   160  func TestToSlice(t *testing.T) {
   161  	type args struct {
   162  		data []byte
   163  		n    int
   164  	}
   165  	type testCase struct {
   166  		name     string
   167  		args     args
   168  		want     any
   169  		testFunc func(x any, n int) any
   170  	}
   171  	tests := []testCase{
   172  		{
   173  			name: "float64",
   174  			args: args{data: []byte{154, 153, 153, 153, 153, 153, 241, 63}, n: 1},
   175  			want: []float64{1.1},
   176  			testFunc: func(x any, n int) any {
   177  				return v1ToSlice[float64](x.([]byte), n)
   178  			},
   179  		},
   180  	}
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			if got := tt.testFunc(tt.args.data, tt.args.n); !labs.DeepEqual(got, tt.want) {
   184  				t.Errorf("ToSlice() = %v, want %v", got, tt.want)
   185  			}
   186  		})
   187  	}
   188  }
   189  
   190  //func Test_v0ToBytes(t *testing.T) {
   191  //	type xx struct {
   192  //		a int
   193  //	}
   194  //	ToBytes[xx](xx{})
   195  //}