Skip to content

Commit

Permalink
test: 100% coverage on blocks/set
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Kubuxu committed Aug 15, 2016
1 parent 685cd28 commit 9437b3a
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions blocks/set/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package set

import (
"testing"

bu "github.com/ipfs/go-ipfs/blocks/blocksutil"
k "github.com/ipfs/go-ipfs/blocks/key"
)

func exampleKeys() []k.Key {
res := make([]k.Key, 1<<8)
gen := bu.NewBlockGenerator()
for i := uint64(0); i < 1<<8; i++ {
res[i] = gen.Next().Key()
}
return res
}
func checkSet(set BlockSet, keySlice []k.Key, t *testing.T) {
for i, key := range keySlice {
if i&(1<<2) == 0 {
if set.HasKey(key) == false {
t.Error("key should be in the set")
}
} else if i&(1<<1) == 0 {
if set.HasKey(key) == true {
t.Error("key shouldn't be in the set")
}
} else if i&(1<<0) == 0 {
if set.HasKey(key) == false {
t.Error("key should be in the set")
}
}
}
}

func TestSetWorks(t *testing.T) {
set := NewSimpleBlockSet()
keys := exampleKeys()

for i, key := range keys {
if i&(1<<0) == 0 {
set.AddBlock(key)
}
}
for i, key := range keys {
if i&(1<<1) == 0 {
set.RemoveBlock(key)
}
}
for i, key := range keys {
if (i)&(1<<2) == 0 {
set.AddBlock(key)
}
}

checkSet(set, keys, t)
addedKeys := set.GetKeys()

newSet := SimpleSetFromKeys(addedKeys)
// same check works on a new set
checkSet(newSet, keys, t)

bloom := set.GetBloomFilter()

for _, key := range addedKeys {
if bloom.Find([]byte(key)) == false {
t.Error("bloom doesn't contain expected key")
}
}

}

0 comments on commit 9437b3a

Please sign in to comment.