Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 100% coverage on blocks/set #3084

Merged
merged 2 commits into from
Aug 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions blocks/set/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package set

import (
"testing"

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

const (
tAdd int = 1 << iota
tRemove
tReAdd
)

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&tReAdd == 0 {
if set.HasKey(key) == false {
t.Error("key should be in the set")
}
} else if i&tRemove == 0 {
if set.HasKey(key) == true {
t.Error("key shouldn't be in the set")
}
} else if i&tAdd == 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&tAdd == 0 {
set.AddBlock(key)
}
}
for i, key := range keys {
if i&tRemove == 0 {
set.RemoveBlock(key)
}
}
for i, key := range keys {
if i&tReAdd == 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")
}
}

}