github.com/imgk/memory-go@v0.0.0-20220328012817-37cdd311f1a3/memory_test.go (about)

     1  package memory
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  )
     7  
     8  func TestAllocator(t *testing.T) {
     9  	Pool := NewAllocator()
    10  	for k, v := range map[int]int{
    11  		1:    1,
    12  		2:    2,
    13  		3:    4,
    14  		4:    4,
    15  		5:    8,
    16  		6:    8,
    17  		7:    8,
    18  		8:    8,
    19  		9:    16,
    20  		31:   32,
    21  		63:   64,
    22  		65:   128,
    23  		127:  128,
    24  		129:  256,
    25  		257:  512,
    26  		513:  1024,
    27  		1025: 2048,
    28  		2049: 4096,
    29  	} {
    30  		p := Pool.Get(k)
    31  		if len(*p.Pointer) != v {
    32  			t.Errorf("Pool.Get error, size: %v, length: %v", k, v)
    33  		}
    34  		Pool.Put(p)
    35  	}
    36  }
    37  
    38  func TestAlloc(t *testing.T) {
    39  	p, b := Alloc[byte](1024)
    40  	buf := *p.Pointer
    41  	if uintptr(unsafe.Pointer(&buf[0])) != *(*uintptr)(unsafe.Pointer(&b)) {
    42  		t.Errorf("Alloc error")
    43  	}
    44  }