mirror of
https://github.com/bloeys/nset.git
synced 2025-12-29 06:28:19 +00:00
Continue+Docs
This commit is contained in:
264
nlookup_test.go
264
nlookup_test.go
@ -1,14 +1,24 @@
|
||||
package nlookup_test
|
||||
package nset_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/bloeys/nlookup"
|
||||
"github.com/bloeys/nset"
|
||||
)
|
||||
|
||||
func TestNLookup(t *testing.T) {
|
||||
const (
|
||||
maxBenchSize = 10_000_000
|
||||
RandSeed = 9_812_938_704
|
||||
)
|
||||
|
||||
n := nlookup.NewNLookup[uint]()
|
||||
var (
|
||||
dump int
|
||||
)
|
||||
|
||||
func TestNSet(t *testing.T) {
|
||||
|
||||
n := nset.NewNSet[uint32]()
|
||||
IsEq(t, 1, cap(n.Data))
|
||||
|
||||
n.Add(0)
|
||||
@ -22,7 +32,7 @@ func TestNLookup(t *testing.T) {
|
||||
n.Remove(1)
|
||||
AllTrue(t, n.Contains(0), n.Contains(63), !n.Contains(1))
|
||||
|
||||
n = nlookup.NewNLookupWithMax[uint](100)
|
||||
n = nset.NewNSetWithMax[uint32](100)
|
||||
IsEq(t, 2, cap(n.Data))
|
||||
}
|
||||
|
||||
@ -46,3 +56,247 @@ func IsEq[T comparable](t *testing.T, expected, val T) bool {
|
||||
t.Errorf("Expected '%v' but got '%v'\n", expected, val)
|
||||
return false
|
||||
}
|
||||
|
||||
func BenchmarkNSetAdd(b *testing.B) {
|
||||
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
n.Add(i % maxBenchSize)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapAdd(b *testing.B) {
|
||||
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
hMap[i%maxBenchSize] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNSetAddRand(b *testing.B) {
|
||||
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
n.Add(rand.Uint32() % maxBenchSize)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapAddRand(b *testing.B) {
|
||||
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
hMap[rand.Uint32()%maxBenchSize] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNSetAddPresized(b *testing.B) {
|
||||
|
||||
n := nset.NewNSetWithMax[uint32](maxBenchSize - 1)
|
||||
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
n.Add(i % maxBenchSize)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapAddPresized(b *testing.B) {
|
||||
|
||||
hMap := make(map[uint32]struct{}, maxBenchSize-1)
|
||||
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
hMap[i%maxBenchSize] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNSetAddPresizedRand(b *testing.B) {
|
||||
|
||||
n := nset.NewNSetWithMax[uint32](maxBenchSize - 1)
|
||||
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
n.Add(rand.Uint32() % maxBenchSize)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapAddPresizedRand(b *testing.B) {
|
||||
|
||||
hMap := make(map[uint32]struct{}, maxBenchSize-1)
|
||||
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
hMap[rand.Uint32()%maxBenchSize] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNSetContains(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
n.Add(i)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
found := 0
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
if n.Contains(i) {
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
dump = found
|
||||
}
|
||||
|
||||
func BenchmarkMapContains(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
hMap[i] = struct{}{}
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
found := 0
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
if _, ok := hMap[i]; ok {
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
dump = found
|
||||
}
|
||||
|
||||
func BenchmarkNSetContainsRand(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
n.Add(i)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
found := 0
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
randVal := rand.Uint32()
|
||||
if n.Contains(randVal) {
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
dump = found
|
||||
}
|
||||
|
||||
func BenchmarkMapContainsRand(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
hMap[i] = struct{}{}
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
found := 0
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
randVal := rand.Uint32()
|
||||
if _, ok := hMap[randVal]; ok {
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
dump = found
|
||||
}
|
||||
|
||||
func BenchmarkNSetDelete(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
n.Add(i)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
n.Remove(i)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapDelete(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
hMap[i] = struct{}{}
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
for i := uint32(0); i < uint32(b.N); i++ {
|
||||
delete(hMap, i)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNSetDeleteRand(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
n := nset.NewNSet[uint32]()
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
n.Add(i)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
randVal := rand.Uint32()
|
||||
n.Remove(randVal)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapDeleteRand(b *testing.B) {
|
||||
|
||||
//Init
|
||||
b.StopTimer()
|
||||
hMap := map[uint32]struct{}{}
|
||||
|
||||
for i := uint32(0); i < maxBenchSize; i++ {
|
||||
hMap[i] = struct{}{}
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
//Work
|
||||
rand.Seed(RandSeed)
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
randVal := rand.Uint32()
|
||||
delete(hMap, randVal)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user