Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

1.1 KB
Raw Blame
package mem

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

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

}

func TestAllocationSize(t *testing.T) {
	for _, size := range []int{0, 1, 1024, 1025, 4883, 2 << 20} {
		buf := Allocate(size)
		assert.Equal(t, cap(buf), AllocationSize(size))
		Free(buf)
	}
	assert.Equal(t, 257<<20, AllocationSize(257<<20))
}