Add a test for memclr.

geesefs-0-30-9
Aaron Jacobs 2016-12-19 10:19:29 +11:00
parent 92e3407014
commit d31e0a4eae
1 changed files with 51 additions and 0 deletions

View File

@ -1,11 +1,62 @@
package buffer
import (
"crypto/rand"
"fmt"
"io"
"testing"
"unsafe"
)
func randBytes(n int) (b []byte, err error) {
b = make([]byte, n)
_, err = io.ReadFull(rand.Reader, b)
return
}
func TestMemclr(t *testing.T) {
// All sizes up to 32 bytes.
var sizes []int
for i := 0; i <= 32; i++ {
sizes = append(sizes, i)
}
// And a few hand-chosen sizes.
sizes = append(sizes, []int{
39, 41, 64, 127, 128, 129,
1<<20 - 1,
1 << 20,
1<<20 + 1,
}...)
// For each size, fill a buffer with random bytes and then zero it.
for _, size := range sizes {
size := size
t.Run(fmt.Sprintf("size=%d", size), func(t *testing.T) {
// Generate
b, err := randBytes(size)
if err != nil {
t.Fatalf("randBytes: %v", err)
}
// Clear
var p unsafe.Pointer
if len(b) != 0 {
p = unsafe.Pointer(&b[0])
}
memclr(p, uintptr(len(b)))
// Check
for i, x := range b {
if x != 0 {
t.Fatalf("non-zero byte %d at offset %d", x, i)
}
}
})
}
}
func BenchmarkOutMessageReset(b *testing.B) {
// A single buffer, which should fit in some level of CPU cache.
b.Run("Single buffer", func(b *testing.B) {