Files
prevara/pools_test.go
2025-11-30 16:45:22 +00:00

197 lines
3.9 KiB
Go

package gel
import (
"testing"
)
func TestPoolGetBool(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
// First allocation should work
b1 := pool.GetBool()
if b1 == nil {
t.Fatal("GetBool returned nil")
}
if pool.boolsInUse != 1 {
t.Errorf("boolsInUse = %d, want 1", pool.boolsInUse)
}
// Second allocation should also work
b2 := pool.GetBool()
if b2 == nil {
t.Fatal("second GetBool returned nil")
}
if b1 == b2 {
t.Error("GetBool returned same pointer twice")
}
if pool.boolsInUse != 2 {
t.Errorf("boolsInUse = %d, want 2", pool.boolsInUse)
}
}
func TestPoolFreeBool(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
b1 := pool.GetBool()
b2 := pool.GetBool()
if pool.boolsInUse != 2 {
t.Fatalf("boolsInUse = %d, want 2", pool.boolsInUse)
}
// Free first one
pool.FreeBool(b1)
if pool.boolsInUse != 1 {
t.Errorf("after FreeBool: boolsInUse = %d, want 1", pool.boolsInUse)
}
// Free second one
pool.FreeBool(b2)
if pool.boolsInUse != 0 {
t.Errorf("after second FreeBool: boolsInUse = %d, want 0", pool.boolsInUse)
}
}
func TestPoolReset(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
// Allocate several widgets
pool.GetBool()
pool.GetBool()
pool.GetList()
pool.GetClickable()
if pool.boolsInUse != 2 || pool.listsInUse != 1 || pool.clickablesInUse != 1 {
t.Fatal("initial allocations failed")
}
// Reset should mark all as available
pool.Reset()
if pool.boolsInUse != 0 {
t.Errorf("after Reset: boolsInUse = %d, want 0", pool.boolsInUse)
}
if pool.listsInUse != 0 {
t.Errorf("after Reset: listsInUse = %d, want 0", pool.listsInUse)
}
if pool.clickablesInUse != 0 {
t.Errorf("after Reset: clickablesInUse = %d, want 0", pool.clickablesInUse)
}
}
func TestPoolReuse(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
// Allocate and free
b1 := pool.GetBool()
pool.FreeBool(b1)
// Should get a reused widget
b2 := pool.GetBool()
if b2 == nil {
t.Fatal("GetBool after Free returned nil")
}
// The pool should have reused the freed widget
if pool.boolsInUse != 1 {
t.Errorf("boolsInUse = %d, want 1", pool.boolsInUse)
}
}
func TestPoolGrowth(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
// Allocate more than initial batch (16 items)
widgets := make([]*Bool, 20)
for i := 0; i < 20; i++ {
widgets[i] = pool.GetBool()
if widgets[i] == nil {
t.Fatalf("GetBool returned nil at iteration %d", i)
}
}
if pool.boolsInUse != 20 {
t.Errorf("boolsInUse = %d, want 20", pool.boolsInUse)
}
// All widgets should be different
seen := make(map[*Bool]bool)
for i, w := range widgets {
if seen[w] {
t.Errorf("duplicate widget at index %d", i)
}
seen[w] = true
}
}
func TestPoolGetList(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
list := pool.GetList()
if list == nil {
t.Fatal("GetList returned nil")
}
if pool.listsInUse != 1 {
t.Errorf("listsInUse = %d, want 1", pool.listsInUse)
}
}
func TestPoolGetClickable(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
click := pool.GetClickable()
if click == nil {
t.Fatal("GetClickable returned nil")
}
if pool.clickablesInUse != 1 {
t.Errorf("clickablesInUse = %d, want 1", pool.clickablesInUse)
}
}
func TestPoolGetEditor(t *testing.T) {
w := testWindow(t)
pool := w.NewPool()
editor := pool.GetEditor()
if editor == nil {
t.Fatal("GetEditor returned nil")
}
if pool.editorsInUse != 1 {
t.Errorf("editorsInUse = %d, want 1", pool.editorsInUse)
}
}
func BenchmarkPoolGetBool(b *testing.B) {
w := testWindow(&testing.T{})
pool := w.NewPool()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pool.GetBool()
if pool.boolsInUse > 100 {
pool.Reset()
}
}
}
func BenchmarkPoolGetFreeReset(b *testing.B) {
w := testWindow(&testing.T{})
pool := w.NewPool()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Simulate a frame: allocate, use, reset
for j := 0; j < 10; j++ {
pool.GetBool()
pool.GetClickable()
}
pool.Reset()
}
}