pmap/pmap_test.go

109 lines
2.5 KiB
Go
Raw Normal View History

2022-01-22 09:49:46 +00:00
package pmap
import "testing"
func TestPmap(t *testing.T) {
p := New()
2022-01-22 21:04:29 +00:00
const numElems = 100
2022-01-23 00:23:45 +00:00
for i := range iter(numElems) {
2022-01-22 09:49:46 +00:00
p = p.Set(i, i)
}
if p.Len() != numElems {
t.Fatalf("Len() = %v, want %v", p.Len(), numElems)
}
2022-01-23 00:23:45 +00:00
for i := range iter(numElems) {
2022-01-22 09:49:46 +00:00
v, ok := p.Get(i)
if v != i || !ok {
t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
}
}
}
2022-01-23 00:23:45 +00:00
func BenchmarkGet(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkGet(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkGet(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkGet(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkGet(b, 10000) })
}
func BenchmarkGetAbsent(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkGetAbsent(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkGetAbsent(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkGetAbsent(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkGetAbsent(b, 10000) })
}
func benchmarkGet(b *testing.B, numElems int) {
p := New()
for i := range iter(numElems) {
p = p.Set(i, i)
}
if p.Len() != numElems {
b.Fatalf("Len() = %v, want %v", p.Len(), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := p.Get(i)
if v != i || !ok {
b.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
}
i++
if i >= numElems {
i = 0
}
}
}
func benchmarkGetAbsent(b *testing.B, numElems int) {
p := New()
for i := range iter(numElems) {
p = p.Set(-i-1, i)
}
if p.Len() != numElems {
b.Fatalf("Len() = %v, want %v", p.Len(), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := p.Get(i)
if ok {
b.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, 0, false)
}
i++
}
}
func iter(n int) []struct{} {
return make([]struct{}, n)
}
func BenchmarkHmapGet_baseline(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkHmapGet(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkHmapGet(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkHmapGet(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkHmapGet(b, 10000) })
}
func benchmarkHmapGet(b *testing.B, numElems int) {
h := make(map[int]int)
for i := range iter(numElems) {
h[i] = i
}
if len(h) != numElems {
b.Fatalf("Len() = %v, want %v", len(h), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := h[i]
if v != i || !ok {
b.Errorf("h[%d] = %v %v, want %v %v", i, v, ok, i, true)
}
i++
if i >= numElems {
i = 0
}
}
}