aboutsummaryrefslogtreecommitdiff
path: root/weed/util/mem/slot_pool_test.go
blob: 44f9ec0045af53b83088651b6819f4aacca56540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package mem

import (
	"github.com/stretchr/testify/assert"
	"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")
	}

}

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)
}

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)
	}

}