50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
|
package pmap
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestInplace(t *testing.T) {
|
||
|
p := New(hash).(InplaceMap)
|
||
|
const numElems = 100
|
||
|
for i := range iter(numElems) {
|
||
|
p = p.SetInplace(i, i)
|
||
|
}
|
||
|
if p.Len() != numElems {
|
||
|
t.Fatalf("Len() = %v, want %v", p.Len(), numElems)
|
||
|
}
|
||
|
for i := range iter(numElems) {
|
||
|
v, ok := p.Get(i)
|
||
|
if v != i || !ok {
|
||
|
t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
|
||
|
}
|
||
|
}
|
||
|
fmt.Print(p.(pmap).stats())
|
||
|
}
|
||
|
|
||
|
func BenchmarkSetInplace(b *testing.B) {
|
||
|
b.Run("size=10", func(b *testing.B) { benchmarkSetInplace(b, 10) })
|
||
|
b.Run("size=100", func(b *testing.B) { benchmarkSetInplace(b, 100) })
|
||
|
b.Run("size=1000", func(b *testing.B) { benchmarkSetInplace(b, 1000) })
|
||
|
b.Run("size=10000", func(b *testing.B) { benchmarkSetInplace(b, 10000) })
|
||
|
if !testing.Short() {
|
||
|
b.Run("size=100000", func(b *testing.B) { benchmarkSetInplace(b, 100000) })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func benchmarkSetInplace(b *testing.B, numElems int) {
|
||
|
p := New(hash).(InplaceMap)
|
||
|
for i := range iter(numElems) {
|
||
|
p = p.SetInplace(i, i)
|
||
|
}
|
||
|
if p.Len() != numElems {
|
||
|
b.Fatalf("Len() = %v, want %v", p.Len(), numElems)
|
||
|
}
|
||
|
b.ResetTimer()
|
||
|
for i := range iter(b.N) {
|
||
|
k := numElems
|
||
|
p = p.SetInplace(k, i)
|
||
|
}
|
||
|
}
|