package stable_test import ( "runtime" "testing" "git.brut.systems/judah/xx/stable" ) func TestArray_StableWithGC(t *testing.T) { type valuewithptr struct { value int ptr *int } var arr stable.Array[valuewithptr] aptr := arr.Append(valuewithptr{value: 10, ptr: nil}) bptr := arr.Append(valuewithptr{value: 20, ptr: &aptr.value}) const N = 1000 for i := range N { arr.Append(valuewithptr{value: i}) runtime.GC() } expect(t, arr.Get(0) == aptr) expect(t, arr.Get(1) == bptr) expect(t, arr.Len() == N+2, "len was %d", arr.Len()) expect(t, bptr.ptr != nil && bptr.value == 20) expect(t, bptr.ptr == &aptr.value, "%p vs. %p", bptr.ptr, &aptr.value) } func BenchmarkArray_RandomAccess(b *testing.B) { var arr stable.Array[int] for i := range b.N { arr.Append(i * i) } b.ResetTimer() for i := range b.N { arr.Get(i % 10000) } } func BenchmarkArray_Append(b *testing.B) { var arr stable.Array[int] for i := range b.N { arr.Append(i * i) } arr.Reset() for i := range b.N { arr.Append(i * i) } } func BenchmarkArray_Iteration(b *testing.B) { var arr stable.Array[int] for i := range b.N { arr.Append(i * i) } b.ResetTimer() sum := 0 for _, v := range arr.Values() { sum += v } } func expect(t *testing.T, cond bool, message ...any) { t.Helper() if !cond { if len(message) == 0 { message = append(message, "assertion failed") } str := message[0].(string) t.Fatalf(str, message[1:]...) } }