gitee.com/quant1x/gox@v1.21.2/cache/fast_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/gox/api"
     6  	"testing"
     7  	"unsafe"
     8  )
     9  
    10  const (
    11  	filename = "data.dat"
    12  )
    13  
    14  type TT struct {
    15  	A int
    16  	B [6]byte
    17  }
    18  
    19  func TestToSlices(t *testing.T) {
    20  	num := 2
    21  	size := int64(unsafe.Sizeof(TT{}))
    22  
    23  	fc, err := OpenCache(filename, size*int64(num))
    24  	if err != nil {
    25  		fmt.Println(fc, err)
    26  		return
    27  	}
    28  	defer fc.Close()
    29  	list := ToSlices[TT](fc)
    30  	for i := 0; i < num; i++ {
    31  		d := i + 1
    32  		var tt TT
    33  		tt.A = d
    34  		str := fmt.Sprintf("%d", d)
    35  		b := [6]byte{}
    36  		copy(b[:], api.String2Bytes(str))
    37  		copy(tt.B[:], b[:])
    38  		list[i] = tt
    39  	}
    40  	fmt.Println(len(list))
    41  }