mirror of
https://github.com/bloeys/nset.git
synced 2025-12-29 06:28:19 +00:00
Tests
This commit is contained in:
33
nlookup.go
33
nlookup.go
@ -41,13 +41,34 @@ func (n *NLookup[T]) Remove(x T) {
|
||||
}
|
||||
|
||||
func (n *NLookup[T]) Contains(x T) bool {
|
||||
return n.isSet(x)
|
||||
}
|
||||
|
||||
unitIndex := n.GetStorageUnitIndex(x)
|
||||
if unitIndex >= n.Size() {
|
||||
return false
|
||||
func (n *NLookup[T]) ContainsAny(values ...T) bool {
|
||||
|
||||
for _, x := range values {
|
||||
if n.isSet(x) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return n.Data[unitIndex]&(1<<x%StorageTypeBits) != 0
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *NLookup[T]) ContainsAll(values ...T) bool {
|
||||
|
||||
for _, x := range values {
|
||||
if !n.isSet(x) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *NLookup[T]) isSet(x T) bool {
|
||||
unitIndex := n.GetStorageUnitIndex(x)
|
||||
return unitIndex < n.Size() && n.Data[unitIndex]&(1<<(x%StorageTypeBits)) != 0
|
||||
}
|
||||
|
||||
func (n *NLookup[T]) GetStorageUnitIndex(x T) uint64 {
|
||||
@ -101,7 +122,9 @@ func NewNLookup[T IntsIf]() NLookup[T] {
|
||||
}
|
||||
}
|
||||
|
||||
func NewNLookupWithSize[T IntsIf](largestNum T) NLookup[T] {
|
||||
//NewNLookupWithMax creates a nlookup that already has capacity to hold till at least largestNum without resizing.
|
||||
//Note that this is NOT the count of elements you want to store, instead you input the largest value you want to store. You can store larger values as well.
|
||||
func NewNLookupWithMax[T IntsIf](largestNum T) NLookup[T] {
|
||||
return NLookup[T]{
|
||||
Data: make([]StorageType, largestNum/StorageTypeBits+1),
|
||||
}
|
||||
|
||||
@ -9,9 +9,21 @@ import (
|
||||
func TestNLookup(t *testing.T) {
|
||||
|
||||
n := nlookup.NewNLookup[uint]()
|
||||
|
||||
IsEq(t, 1, cap(n.Data))
|
||||
|
||||
n.Add(0)
|
||||
n.Add(1)
|
||||
n.Add(63)
|
||||
|
||||
AllTrue(t, n.Contains(0), n.Contains(1), n.Contains(63), !n.Contains(10), !n.Contains(599))
|
||||
AllTrue(t, n.ContainsAll(0, 1, 63), !n.ContainsAll(9, 0, 1), !n.ContainsAll(0, 1, 63, 99))
|
||||
AllTrue(t, n.ContainsAny(0, 1, 63), n.ContainsAny(9, 99, 999, 1), !n.ContainsAny(9, 99, 999))
|
||||
|
||||
n.Remove(1)
|
||||
AllTrue(t, n.Contains(0), n.Contains(63), !n.Contains(1))
|
||||
|
||||
n = nlookup.NewNLookupWithMax[uint](100)
|
||||
IsEq(t, 2, cap(n.Data))
|
||||
}
|
||||
|
||||
func AllTrue(t *testing.T, values ...bool) bool {
|
||||
|
||||
Reference in New Issue
Block a user