2021-11-27 20:13:00 +00:00
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2022-01-22 16:05:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-27 20:13:00 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllocateFree(t *testing.T) {
|
|
|
|
buf := Allocate(12)
|
|
|
|
Free(buf)
|
|
|
|
if cap(buf) != min_size {
|
|
|
|
t.Errorf("min size error allocated capacity=%d", cap(buf))
|
|
|
|
}
|
|
|
|
if len(buf) != 12 {
|
|
|
|
t.Errorf("size error")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = Allocate(4883)
|
|
|
|
Free(buf)
|
|
|
|
if cap(buf) != 1024<<bitCount(4883) {
|
|
|
|
t.Errorf("min size error allocated capacity=%d", cap(buf))
|
|
|
|
}
|
|
|
|
if len(buf) != 4883 {
|
|
|
|
t.Errorf("size error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:05:04 +00:00
|
|
|
func TestAllocateFreeEdgeCases(t *testing.T) {
|
|
|
|
assert.Equal(t, 1, bitCount(2048))
|
|
|
|
assert.Equal(t, 2, bitCount(2049))
|
|
|
|
|
|
|
|
buf := Allocate(2048)
|
|
|
|
Free(buf)
|
|
|
|
buf = Allocate(2049)
|
|
|
|
Free(buf)
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:13:00 +00:00
|
|
|
func TestBitCount(t *testing.T) {
|
|
|
|
count := bitCount(12)
|
|
|
|
if count != 0 {
|
|
|
|
t.Errorf("bitCount error count=%d", count)
|
|
|
|
}
|
|
|
|
if count != bitCount(min_size) {
|
|
|
|
t.Errorf("bitCount error count=%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|