github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/cache/alloc_test.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package cache
     6  
     7  import (
     8  	"testing"
     9  	"unsafe"
    10  )
    11  
    12  func TestAllocCache(t *testing.T) {
    13  	c := &allocCache{}
    14  	for i := 0; i < 64; i++ {
    15  		c.free(make([]byte, 1025))
    16  		if c.size == 0 {
    17  			t.Fatalf("expected cache size to be non-zero")
    18  		}
    19  	}
    20  	m := make(map[unsafe.Pointer]bool)
    21  	for i := 0; i < 64; i++ {
    22  		b := c.alloc(1025)
    23  		p := unsafe.Pointer(&b[0])
    24  		if m[p] {
    25  			t.Fatalf("%p already allocated", p)
    26  		}
    27  		m[p] = true
    28  	}
    29  	if c.size != 0 {
    30  		t.Fatalf("expected cache size to be zero, found %d", c.size)
    31  	}
    32  }