github.com/zhiqiangxu/util@v0.0.0-20230112053021-0a7aee056cd5/bytes/aligned_test.go (about)

     1  package bytes
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  
     7  	"gotest.tools/assert"
     8  )
     9  
    10  func TestAligned(t *testing.T) {
    11  	s1 := AlignedTo8(1)
    12  	assert.Assert(t, uintptr(unsafe.Pointer(&s1[0]))%8 == 0 && len(s1) == 1)
    13  	s2 := AlignedTo4(1)
    14  	assert.Assert(t, uintptr(unsafe.Pointer(&s2[0]))%4 == 0 && len(s2) == 1)
    15  }
    16  
    17  func BenchmarkAligned(b *testing.B) {
    18  	sizes := []uint32{10, 20, 30, 40}
    19  	b.RunParallel(func(pb *testing.PB) {
    20  		for pb.Next() {
    21  			for _, size := range sizes {
    22  				_ = make([]byte, size)
    23  				// _ = AlignedTo8(size)
    24  			}
    25  		}
    26  
    27  	})
    28  }