Replace godep with dep
This commit is contained in:
parent
1e7489927c
commit
bf5616c65b
14883 changed files with 3937406 additions and 361781 deletions
336
vendor/github.com/google/btree/btree.go
generated
vendored
336
vendor/github.com/google/btree/btree.go
generated
vendored
|
|
@ -22,7 +22,7 @@
|
|||
// See some discussion on the matter here:
|
||||
// http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html
|
||||
// Note, though, that this project is in no way related to the C++ B-Tree
|
||||
// implmentation written about there.
|
||||
// implementation written about there.
|
||||
//
|
||||
// Within this tree, each node contains a slice of items and a (possibly nil)
|
||||
// slice of children. For basic numeric values or raw structs, this can cause
|
||||
|
|
@ -44,7 +44,7 @@
|
|||
// widely used ordered tree implementation in the Go ecosystem currently.
|
||||
// Its functions, therefore, exactly mirror those of
|
||||
// llrb.LLRB where possible. Unlike gollrb, though, we currently don't
|
||||
// support storing multiple equivalent values or backwards iteration.
|
||||
// support storing multiple equivalent values.
|
||||
package btree
|
||||
|
||||
import (
|
||||
|
|
@ -52,6 +52,7 @@ import (
|
|||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Item represents a single object in the tree.
|
||||
|
|
@ -68,11 +69,17 @@ const (
|
|||
DefaultFreeListSize = 32
|
||||
)
|
||||
|
||||
var (
|
||||
nilItems = make(items, 16)
|
||||
nilChildren = make(children, 16)
|
||||
)
|
||||
|
||||
// FreeList represents a free list of btree nodes. By default each
|
||||
// BTree has its own FreeList, but multiple BTrees can share the same
|
||||
// FreeList.
|
||||
// Two Btrees using the same freelist are not safe for concurrent write access.
|
||||
// Two Btrees using the same freelist are safe for concurrent write access.
|
||||
type FreeList struct {
|
||||
mu sync.Mutex
|
||||
freelist []*node
|
||||
}
|
||||
|
||||
|
|
@ -83,18 +90,25 @@ func NewFreeList(size int) *FreeList {
|
|||
}
|
||||
|
||||
func (f *FreeList) newNode() (n *node) {
|
||||
f.mu.Lock()
|
||||
index := len(f.freelist) - 1
|
||||
if index < 0 {
|
||||
f.mu.Unlock()
|
||||
return new(node)
|
||||
}
|
||||
f.freelist, n = f.freelist[:index], f.freelist[index]
|
||||
n = f.freelist[index]
|
||||
f.freelist[index] = nil
|
||||
f.freelist = f.freelist[:index]
|
||||
f.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *FreeList) freeNode(n *node) {
|
||||
f.mu.Lock()
|
||||
if len(f.freelist) < cap(f.freelist) {
|
||||
f.freelist = append(f.freelist, n)
|
||||
}
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
// ItemIterator allows callers of Ascend* to iterate in-order over portions of
|
||||
|
|
@ -116,8 +130,8 @@ func NewWithFreeList(degree int, f *FreeList) *BTree {
|
|||
panic("bad degree")
|
||||
}
|
||||
return &BTree{
|
||||
degree: degree,
|
||||
freelist: f,
|
||||
degree: degree,
|
||||
cow: ©OnWriteContext{freelist: f},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -138,8 +152,8 @@ func (s *items) insertAt(index int, item Item) {
|
|||
// back.
|
||||
func (s *items) removeAt(index int) Item {
|
||||
item := (*s)[index]
|
||||
(*s)[index] = nil
|
||||
copy((*s)[index:], (*s)[index+1:])
|
||||
(*s)[len(*s)-1] = nil
|
||||
*s = (*s)[:len(*s)-1]
|
||||
return item
|
||||
}
|
||||
|
|
@ -153,6 +167,16 @@ func (s *items) pop() (out Item) {
|
|||
return
|
||||
}
|
||||
|
||||
// truncate truncates this instance at index so that it contains only the
|
||||
// first index items. index must be less than or equal to length.
|
||||
func (s *items) truncate(index int) {
|
||||
var toClear items
|
||||
*s, toClear = (*s)[:index], (*s)[index:]
|
||||
for len(toClear) > 0 {
|
||||
toClear = toClear[copy(toClear, nilItems):]
|
||||
}
|
||||
}
|
||||
|
||||
// find returns the index where the given item should be inserted into this
|
||||
// list. 'found' is true if the item already exists in the list at the given
|
||||
// index.
|
||||
|
|
@ -183,8 +207,8 @@ func (s *children) insertAt(index int, n *node) {
|
|||
// back.
|
||||
func (s *children) removeAt(index int) *node {
|
||||
n := (*s)[index]
|
||||
(*s)[index] = nil
|
||||
copy((*s)[index:], (*s)[index+1:])
|
||||
(*s)[len(*s)-1] = nil
|
||||
*s = (*s)[:len(*s)-1]
|
||||
return n
|
||||
}
|
||||
|
|
@ -198,6 +222,16 @@ func (s *children) pop() (out *node) {
|
|||
return
|
||||
}
|
||||
|
||||
// truncate truncates this instance at index so that it contains only the
|
||||
// first index children. index must be less than or equal to length.
|
||||
func (s *children) truncate(index int) {
|
||||
var toClear children
|
||||
*s, toClear = (*s)[:index], (*s)[index:]
|
||||
for len(toClear) > 0 {
|
||||
toClear = toClear[copy(toClear, nilChildren):]
|
||||
}
|
||||
}
|
||||
|
||||
// node is an internal node in a tree.
|
||||
//
|
||||
// It must at all times maintain the invariant that either
|
||||
|
|
@ -206,7 +240,34 @@ func (s *children) pop() (out *node) {
|
|||
type node struct {
|
||||
items items
|
||||
children children
|
||||
t *BTree
|
||||
cow *copyOnWriteContext
|
||||
}
|
||||
|
||||
func (n *node) mutableFor(cow *copyOnWriteContext) *node {
|
||||
if n.cow == cow {
|
||||
return n
|
||||
}
|
||||
out := cow.newNode()
|
||||
if cap(out.items) >= len(n.items) {
|
||||
out.items = out.items[:len(n.items)]
|
||||
} else {
|
||||
out.items = make(items, len(n.items), cap(n.items))
|
||||
}
|
||||
copy(out.items, n.items)
|
||||
// Copy children
|
||||
if cap(out.children) >= len(n.children) {
|
||||
out.children = out.children[:len(n.children)]
|
||||
} else {
|
||||
out.children = make(children, len(n.children), cap(n.children))
|
||||
}
|
||||
copy(out.children, n.children)
|
||||
return out
|
||||
}
|
||||
|
||||
func (n *node) mutableChild(i int) *node {
|
||||
c := n.children[i].mutableFor(n.cow)
|
||||
n.children[i] = c
|
||||
return c
|
||||
}
|
||||
|
||||
// split splits the given node at the given index. The current node shrinks,
|
||||
|
|
@ -214,12 +275,12 @@ type node struct {
|
|||
// containing all items/children after it.
|
||||
func (n *node) split(i int) (Item, *node) {
|
||||
item := n.items[i]
|
||||
next := n.t.newNode()
|
||||
next := n.cow.newNode()
|
||||
next.items = append(next.items, n.items[i+1:]...)
|
||||
n.items = n.items[:i]
|
||||
n.items.truncate(i)
|
||||
if len(n.children) > 0 {
|
||||
next.children = append(next.children, n.children[i+1:]...)
|
||||
n.children = n.children[:i+1]
|
||||
n.children.truncate(i + 1)
|
||||
}
|
||||
return item, next
|
||||
}
|
||||
|
|
@ -230,7 +291,7 @@ func (n *node) maybeSplitChild(i, maxItems int) bool {
|
|||
if len(n.children[i].items) < maxItems {
|
||||
return false
|
||||
}
|
||||
first := n.children[i]
|
||||
first := n.mutableChild(i)
|
||||
item, second := first.split(maxItems / 2)
|
||||
n.items.insertAt(i, item)
|
||||
n.children.insertAt(i+1, second)
|
||||
|
|
@ -264,7 +325,7 @@ func (n *node) insert(item Item, maxItems int) Item {
|
|||
return out
|
||||
}
|
||||
}
|
||||
return n.children[i].insert(item, maxItems)
|
||||
return n.mutableChild(i).insert(item, maxItems)
|
||||
}
|
||||
|
||||
// get finds the given key in the subtree and returns it.
|
||||
|
|
@ -342,10 +403,10 @@ func (n *node) remove(item Item, minItems int, typ toRemove) Item {
|
|||
panic("invalid type")
|
||||
}
|
||||
// If we get to here, we have children.
|
||||
child := n.children[i]
|
||||
if len(child.items) <= minItems {
|
||||
if len(n.children[i].items) <= minItems {
|
||||
return n.growChildAndRemove(i, item, minItems, typ)
|
||||
}
|
||||
child := n.mutableChild(i)
|
||||
// Either we had enough items to begin with, or we've done some
|
||||
// merging/stealing, because we've got enough now and we're ready to return
|
||||
// stuff.
|
||||
|
|
@ -384,10 +445,10 @@ func (n *node) remove(item Item, minItems int, typ toRemove) Item {
|
|||
// whether we're in case 1 or 2), we'll have enough items and can guarantee
|
||||
// that we hit case A.
|
||||
func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove) Item {
|
||||
child := n.children[i]
|
||||
if i > 0 && len(n.children[i-1].items) > minItems {
|
||||
// Steal from left child
|
||||
stealFrom := n.children[i-1]
|
||||
child := n.mutableChild(i)
|
||||
stealFrom := n.mutableChild(i - 1)
|
||||
stolenItem := stealFrom.items.pop()
|
||||
child.items.insertAt(0, n.items[i-1])
|
||||
n.items[i-1] = stolenItem
|
||||
|
|
@ -396,7 +457,8 @@ func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove)
|
|||
}
|
||||
} else if i < len(n.items) && len(n.children[i+1].items) > minItems {
|
||||
// steal from right child
|
||||
stealFrom := n.children[i+1]
|
||||
child := n.mutableChild(i)
|
||||
stealFrom := n.mutableChild(i + 1)
|
||||
stolenItem := stealFrom.items.removeAt(0)
|
||||
child.items = append(child.items, n.items[i])
|
||||
n.items[i] = stolenItem
|
||||
|
|
@ -406,47 +468,90 @@ func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove)
|
|||
} else {
|
||||
if i >= len(n.items) {
|
||||
i--
|
||||
child = n.children[i]
|
||||
}
|
||||
child := n.mutableChild(i)
|
||||
// merge with right child
|
||||
mergeItem := n.items.removeAt(i)
|
||||
mergeChild := n.children.removeAt(i + 1)
|
||||
child.items = append(child.items, mergeItem)
|
||||
child.items = append(child.items, mergeChild.items...)
|
||||
child.children = append(child.children, mergeChild.children...)
|
||||
n.t.freeNode(mergeChild)
|
||||
n.cow.freeNode(mergeChild)
|
||||
}
|
||||
return n.remove(item, minItems, typ)
|
||||
}
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
descend = direction(-1)
|
||||
ascend = direction(+1)
|
||||
)
|
||||
|
||||
// iterate provides a simple method for iterating over elements in the tree.
|
||||
// It could probably use some work to be extra-efficient (it calls from() a
|
||||
// little more than it should), but it works pretty well for now.
|
||||
//
|
||||
// It requires that 'from' and 'to' both return true for values we should hit
|
||||
// with the iterator. It should also be the case that 'from' returns true for
|
||||
// values less than or equal to values 'to' returns true for, and 'to'
|
||||
// returns true for values greater than or equal to those that 'from'
|
||||
// does.
|
||||
func (n *node) iterate(from, to func(Item) bool, iter ItemIterator) bool {
|
||||
for i, item := range n.items {
|
||||
if !from(item) {
|
||||
continue
|
||||
// When ascending, the 'start' should be less than 'stop' and when descending,
|
||||
// the 'start' should be greater than 'stop'. Setting 'includeStart' to true
|
||||
// will force the iterator to include the first item when it equals 'start',
|
||||
// thus creating a "greaterOrEqual" or "lessThanEqual" rather than just a
|
||||
// "greaterThan" or "lessThan" queries.
|
||||
func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit bool, iter ItemIterator) (bool, bool) {
|
||||
var ok bool
|
||||
switch dir {
|
||||
case ascend:
|
||||
for i := 0; i < len(n.items); i++ {
|
||||
if start != nil && n.items[i].Less(start) {
|
||||
continue
|
||||
}
|
||||
if len(n.children) > 0 {
|
||||
if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
if !includeStart && !hit && start != nil && !start.Less(n.items[i]) {
|
||||
hit = true
|
||||
continue
|
||||
}
|
||||
hit = true
|
||||
if stop != nil && !n.items[i].Less(stop) {
|
||||
return hit, false
|
||||
}
|
||||
if !iter(n.items[i]) {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
if len(n.children) > 0 && !n.children[i].iterate(from, to, iter) {
|
||||
return false
|
||||
if len(n.children) > 0 {
|
||||
if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
if !to(item) {
|
||||
return false
|
||||
case descend:
|
||||
for i := len(n.items) - 1; i >= 0; i-- {
|
||||
if start != nil && !n.items[i].Less(start) {
|
||||
if !includeStart || hit || start.Less(n.items[i]) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(n.children) > 0 {
|
||||
if hit, ok = n.children[i+1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
if stop != nil && !stop.Less(n.items[i]) {
|
||||
return hit, false // continue
|
||||
}
|
||||
hit = true
|
||||
if !iter(n.items[i]) {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
if !iter(item) {
|
||||
return false
|
||||
if len(n.children) > 0 {
|
||||
if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {
|
||||
return hit, false
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(n.children) > 0 {
|
||||
return n.children[len(n.children)-1].iterate(from, to, iter)
|
||||
}
|
||||
return true
|
||||
return hit, true
|
||||
}
|
||||
|
||||
// Used for testing/debugging purposes.
|
||||
|
|
@ -465,12 +570,54 @@ func (n *node) print(w io.Writer, level int) {
|
|||
// Write operations are not safe for concurrent mutation by multiple
|
||||
// goroutines, but Read operations are.
|
||||
type BTree struct {
|
||||
degree int
|
||||
length int
|
||||
root *node
|
||||
degree int
|
||||
length int
|
||||
root *node
|
||||
cow *copyOnWriteContext
|
||||
}
|
||||
|
||||
// copyOnWriteContext pointers determine node ownership... a tree with a write
|
||||
// context equivalent to a node's write context is allowed to modify that node.
|
||||
// A tree whose write context does not match a node's is not allowed to modify
|
||||
// it, and must create a new, writable copy (IE: it's a Clone).
|
||||
//
|
||||
// When doing any write operation, we maintain the invariant that the current
|
||||
// node's context is equal to the context of the tree that requested the write.
|
||||
// We do this by, before we descend into any node, creating a copy with the
|
||||
// correct context if the contexts don't match.
|
||||
//
|
||||
// Since the node we're currently visiting on any write has the requesting
|
||||
// tree's context, that node is modifiable in place. Children of that node may
|
||||
// not share context, but before we descend into them, we'll make a mutable
|
||||
// copy.
|
||||
type copyOnWriteContext struct {
|
||||
freelist *FreeList
|
||||
}
|
||||
|
||||
// Clone clones the btree, lazily. Clone should not be called concurrently,
|
||||
// but the original tree (t) and the new tree (t2) can be used concurrently
|
||||
// once the Clone call completes.
|
||||
//
|
||||
// The internal tree structure of b is marked read-only and shared between t and
|
||||
// t2. Writes to both t and t2 use copy-on-write logic, creating new nodes
|
||||
// whenever one of b's original nodes would have been modified. Read operations
|
||||
// should have no performance degredation. Write operations for both t and t2
|
||||
// will initially experience minor slow-downs caused by additional allocs and
|
||||
// copies due to the aforementioned copy-on-write logic, but should converge to
|
||||
// the original performance characteristics of the original tree.
|
||||
func (t *BTree) Clone() (t2 *BTree) {
|
||||
// Create two entirely new copy-on-write contexts.
|
||||
// This operation effectively creates three trees:
|
||||
// the original, shared nodes (old b.cow)
|
||||
// the new b.cow nodes
|
||||
// the new out.cow nodes
|
||||
cow1, cow2 := *t.cow, *t.cow
|
||||
out := *t
|
||||
t.cow = &cow1
|
||||
out.cow = &cow2
|
||||
return &out
|
||||
}
|
||||
|
||||
// maxItems returns the max number of items to allow per node.
|
||||
func (t *BTree) maxItems() int {
|
||||
return t.degree*2 - 1
|
||||
|
|
@ -482,23 +629,20 @@ func (t *BTree) minItems() int {
|
|||
return t.degree - 1
|
||||
}
|
||||
|
||||
func (t *BTree) newNode() (n *node) {
|
||||
n = t.freelist.newNode()
|
||||
n.t = t
|
||||
func (c *copyOnWriteContext) newNode() (n *node) {
|
||||
n = c.freelist.newNode()
|
||||
n.cow = c
|
||||
return
|
||||
}
|
||||
|
||||
func (t *BTree) freeNode(n *node) {
|
||||
for i := range n.items {
|
||||
n.items[i] = nil // clear to allow GC
|
||||
func (c *copyOnWriteContext) freeNode(n *node) {
|
||||
if n.cow == c {
|
||||
// clear to allow GC
|
||||
n.items.truncate(0)
|
||||
n.children.truncate(0)
|
||||
n.cow = nil
|
||||
c.freelist.freeNode(n)
|
||||
}
|
||||
n.items = n.items[:0]
|
||||
for i := range n.children {
|
||||
n.children[i] = nil // clear to allow GC
|
||||
}
|
||||
n.children = n.children[:0]
|
||||
n.t = nil // clear to allow GC
|
||||
t.freelist.freeNode(n)
|
||||
}
|
||||
|
||||
// ReplaceOrInsert adds the given item to the tree. If an item in the tree
|
||||
|
|
@ -511,16 +655,19 @@ func (t *BTree) ReplaceOrInsert(item Item) Item {
|
|||
panic("nil item being added to BTree")
|
||||
}
|
||||
if t.root == nil {
|
||||
t.root = t.newNode()
|
||||
t.root = t.cow.newNode()
|
||||
t.root.items = append(t.root.items, item)
|
||||
t.length++
|
||||
return nil
|
||||
} else if len(t.root.items) >= t.maxItems() {
|
||||
item2, second := t.root.split(t.maxItems() / 2)
|
||||
oldroot := t.root
|
||||
t.root = t.newNode()
|
||||
t.root.items = append(t.root.items, item2)
|
||||
t.root.children = append(t.root.children, oldroot, second)
|
||||
} else {
|
||||
t.root = t.root.mutableFor(t.cow)
|
||||
if len(t.root.items) >= t.maxItems() {
|
||||
item2, second := t.root.split(t.maxItems() / 2)
|
||||
oldroot := t.root
|
||||
t.root = t.cow.newNode()
|
||||
t.root.items = append(t.root.items, item2)
|
||||
t.root.children = append(t.root.children, oldroot, second)
|
||||
}
|
||||
}
|
||||
out := t.root.insert(item, t.maxItems())
|
||||
if out == nil {
|
||||
|
|
@ -551,11 +698,12 @@ func (t *BTree) deleteItem(item Item, typ toRemove) Item {
|
|||
if t.root == nil || len(t.root.items) == 0 {
|
||||
return nil
|
||||
}
|
||||
t.root = t.root.mutableFor(t.cow)
|
||||
out := t.root.remove(item, t.minItems(), typ)
|
||||
if len(t.root.items) == 0 && len(t.root.children) > 0 {
|
||||
oldroot := t.root
|
||||
t.root = t.root.children[0]
|
||||
t.freeNode(oldroot)
|
||||
t.cow.freeNode(oldroot)
|
||||
}
|
||||
if out != nil {
|
||||
t.length--
|
||||
|
|
@ -569,10 +717,7 @@ func (t *BTree) AscendRange(greaterOrEqual, lessThan Item, iterator ItemIterator
|
|||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(
|
||||
func(a Item) bool { return !a.Less(greaterOrEqual) },
|
||||
func(a Item) bool { return a.Less(lessThan) },
|
||||
iterator)
|
||||
t.root.iterate(ascend, greaterOrEqual, lessThan, true, false, iterator)
|
||||
}
|
||||
|
||||
// AscendLessThan calls the iterator for every value in the tree within the range
|
||||
|
|
@ -581,10 +726,7 @@ func (t *BTree) AscendLessThan(pivot Item, iterator ItemIterator) {
|
|||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(
|
||||
func(a Item) bool { return true },
|
||||
func(a Item) bool { return a.Less(pivot) },
|
||||
iterator)
|
||||
t.root.iterate(ascend, nil, pivot, false, false, iterator)
|
||||
}
|
||||
|
||||
// AscendGreaterOrEqual calls the iterator for every value in the tree within
|
||||
|
|
@ -593,10 +735,7 @@ func (t *BTree) AscendGreaterOrEqual(pivot Item, iterator ItemIterator) {
|
|||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(
|
||||
func(a Item) bool { return !a.Less(pivot) },
|
||||
func(a Item) bool { return true },
|
||||
iterator)
|
||||
t.root.iterate(ascend, pivot, nil, true, false, iterator)
|
||||
}
|
||||
|
||||
// Ascend calls the iterator for every value in the tree within the range
|
||||
|
|
@ -605,10 +744,43 @@ func (t *BTree) Ascend(iterator ItemIterator) {
|
|||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(
|
||||
func(a Item) bool { return true },
|
||||
func(a Item) bool { return true },
|
||||
iterator)
|
||||
t.root.iterate(ascend, nil, nil, false, false, iterator)
|
||||
}
|
||||
|
||||
// DescendRange calls the iterator for every value in the tree within the range
|
||||
// [lessOrEqual, greaterThan), until iterator returns false.
|
||||
func (t *BTree) DescendRange(lessOrEqual, greaterThan Item, iterator ItemIterator) {
|
||||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(descend, lessOrEqual, greaterThan, true, false, iterator)
|
||||
}
|
||||
|
||||
// DescendLessOrEqual calls the iterator for every value in the tree within the range
|
||||
// [pivot, first], until iterator returns false.
|
||||
func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator) {
|
||||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(descend, pivot, nil, true, false, iterator)
|
||||
}
|
||||
|
||||
// DescendGreaterThan calls the iterator for every value in the tree within
|
||||
// the range (pivot, last], until iterator returns false.
|
||||
func (t *BTree) DescendGreaterThan(pivot Item, iterator ItemIterator) {
|
||||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(descend, nil, pivot, false, false, iterator)
|
||||
}
|
||||
|
||||
// Descend calls the iterator for every value in the tree within the range
|
||||
// [last, first], until iterator returns false.
|
||||
func (t *BTree) Descend(iterator ItemIterator) {
|
||||
if t.root == nil {
|
||||
return
|
||||
}
|
||||
t.root.iterate(descend, nil, nil, false, false, iterator)
|
||||
}
|
||||
|
||||
// Get looks for the key item in the tree, returning it. It returns nil if
|
||||
|
|
|
|||
689
vendor/github.com/google/btree/btree_test.go
generated
vendored
Normal file
689
vendor/github.com/google/btree/btree_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,689 @@
|
|||
// Copyright 2014 Google Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package btree
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
seed := time.Now().Unix()
|
||||
fmt.Println(seed)
|
||||
rand.Seed(seed)
|
||||
}
|
||||
|
||||
// perm returns a random permutation of n Int items in the range [0, n).
|
||||
func perm(n int) (out []Item) {
|
||||
for _, v := range rand.Perm(n) {
|
||||
out = append(out, Int(v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// rang returns an ordered list of Int items in the range [0, n).
|
||||
func rang(n int) (out []Item) {
|
||||
for i := 0; i < n; i++ {
|
||||
out = append(out, Int(i))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// all extracts all items from a tree in order as a slice.
|
||||
func all(t *BTree) (out []Item) {
|
||||
t.Ascend(func(a Item) bool {
|
||||
out = append(out, a)
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// rangerev returns a reversed ordered list of Int items in the range [0, n).
|
||||
func rangrev(n int) (out []Item) {
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
out = append(out, Int(i))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// allrev extracts all items from a tree in reverse order as a slice.
|
||||
func allrev(t *BTree) (out []Item) {
|
||||
t.Descend(func(a Item) bool {
|
||||
out = append(out, a)
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var btreeDegree = flag.Int("degree", 32, "B-Tree degree")
|
||||
|
||||
func TestBTree(t *testing.T) {
|
||||
tr := New(*btreeDegree)
|
||||
const treeSize = 10000
|
||||
for i := 0; i < 10; i++ {
|
||||
if min := tr.Min(); min != nil {
|
||||
t.Fatalf("empty min, got %+v", min)
|
||||
}
|
||||
if max := tr.Max(); max != nil {
|
||||
t.Fatalf("empty max, got %+v", max)
|
||||
}
|
||||
for _, item := range perm(treeSize) {
|
||||
if x := tr.ReplaceOrInsert(item); x != nil {
|
||||
t.Fatal("insert found item", item)
|
||||
}
|
||||
}
|
||||
for _, item := range perm(treeSize) {
|
||||
if x := tr.ReplaceOrInsert(item); x == nil {
|
||||
t.Fatal("insert didn't find item", item)
|
||||
}
|
||||
}
|
||||
if min, want := tr.Min(), Item(Int(0)); min != want {
|
||||
t.Fatalf("min: want %+v, got %+v", want, min)
|
||||
}
|
||||
if max, want := tr.Max(), Item(Int(treeSize-1)); max != want {
|
||||
t.Fatalf("max: want %+v, got %+v", want, max)
|
||||
}
|
||||
got := all(tr)
|
||||
want := rang(treeSize)
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("mismatch:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
|
||||
gotrev := allrev(tr)
|
||||
wantrev := rangrev(treeSize)
|
||||
if !reflect.DeepEqual(gotrev, wantrev) {
|
||||
t.Fatalf("mismatch:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
|
||||
for _, item := range perm(treeSize) {
|
||||
if x := tr.Delete(item); x == nil {
|
||||
t.Fatalf("didn't find %v", item)
|
||||
}
|
||||
}
|
||||
if got = all(tr); len(got) > 0 {
|
||||
t.Fatalf("some left!: %v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleBTree() {
|
||||
tr := New(*btreeDegree)
|
||||
for i := Int(0); i < 10; i++ {
|
||||
tr.ReplaceOrInsert(i)
|
||||
}
|
||||
fmt.Println("len: ", tr.Len())
|
||||
fmt.Println("get3: ", tr.Get(Int(3)))
|
||||
fmt.Println("get100: ", tr.Get(Int(100)))
|
||||
fmt.Println("del4: ", tr.Delete(Int(4)))
|
||||
fmt.Println("del100: ", tr.Delete(Int(100)))
|
||||
fmt.Println("replace5: ", tr.ReplaceOrInsert(Int(5)))
|
||||
fmt.Println("replace100:", tr.ReplaceOrInsert(Int(100)))
|
||||
fmt.Println("min: ", tr.Min())
|
||||
fmt.Println("delmin: ", tr.DeleteMin())
|
||||
fmt.Println("max: ", tr.Max())
|
||||
fmt.Println("delmax: ", tr.DeleteMax())
|
||||
fmt.Println("len: ", tr.Len())
|
||||
// Output:
|
||||
// len: 10
|
||||
// get3: 3
|
||||
// get100: <nil>
|
||||
// del4: 4
|
||||
// del100: <nil>
|
||||
// replace5: 5
|
||||
// replace100: <nil>
|
||||
// min: 0
|
||||
// delmin: 0
|
||||
// max: 100
|
||||
// delmax: 100
|
||||
// len: 8
|
||||
}
|
||||
|
||||
func TestDeleteMin(t *testing.T) {
|
||||
tr := New(3)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
for v := tr.DeleteMin(); v != nil; v = tr.DeleteMin() {
|
||||
got = append(got, v)
|
||||
}
|
||||
if want := rang(100); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteMax(t *testing.T) {
|
||||
tr := New(3)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
for v := tr.DeleteMax(); v != nil; v = tr.DeleteMax() {
|
||||
got = append(got, v)
|
||||
}
|
||||
// Reverse our list.
|
||||
for i := 0; i < len(got)/2; i++ {
|
||||
got[i], got[len(got)-i-1] = got[len(got)-i-1], got[i]
|
||||
}
|
||||
if want := rang(100); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAscendRange(t *testing.T) {
|
||||
tr := New(2)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.AscendRange(Int(40), Int(60), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[40:60]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.AscendRange(Int(40), Int(60), func(a Item) bool {
|
||||
if a.(Int) > 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[40:51]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescendRange(t *testing.T) {
|
||||
tr := New(2)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.DescendRange(Int(60), Int(40), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[39:59]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.DescendRange(Int(60), Int(40), func(a Item) bool {
|
||||
if a.(Int) < 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[39:50]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
func TestAscendLessThan(t *testing.T) {
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.AscendLessThan(Int(60), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[:60]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.AscendLessThan(Int(60), func(a Item) bool {
|
||||
if a.(Int) > 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[:51]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescendLessOrEqual(t *testing.T) {
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.DescendLessOrEqual(Int(40), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[59:]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendlessorequal:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.DescendLessOrEqual(Int(60), func(a Item) bool {
|
||||
if a.(Int) < 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[39:50]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendlessorequal:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
func TestAscendGreaterOrEqual(t *testing.T) {
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.AscendGreaterOrEqual(Int(40), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[40:]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.AscendGreaterOrEqual(Int(40), func(a Item) bool {
|
||||
if a.(Int) > 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rang(100)[40:51]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescendGreaterThan(t *testing.T) {
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range perm(100) {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
var got []Item
|
||||
tr.DescendGreaterThan(Int(40), func(a Item) bool {
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[:59]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendgreaterthan:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
got = got[:0]
|
||||
tr.DescendGreaterThan(Int(40), func(a Item) bool {
|
||||
if a.(Int) < 50 {
|
||||
return false
|
||||
}
|
||||
got = append(got, a)
|
||||
return true
|
||||
})
|
||||
if want := rangrev(100)[:50]; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("descendgreaterthan:\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
const benchmarkTreeSize = 10000
|
||||
|
||||
func BenchmarkInsert(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
b.StartTimer()
|
||||
i := 0
|
||||
for i < b.N {
|
||||
tr := New(*btreeDegree)
|
||||
for _, item := range insertP {
|
||||
tr.ReplaceOrInsert(item)
|
||||
i++
|
||||
if i >= b.N {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDeleteInsert(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, item := range insertP {
|
||||
tr.ReplaceOrInsert(item)
|
||||
}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tr.Delete(insertP[i%benchmarkTreeSize])
|
||||
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize])
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDeleteInsertCloneOnce(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, item := range insertP {
|
||||
tr.ReplaceOrInsert(item)
|
||||
}
|
||||
tr = tr.Clone()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tr.Delete(insertP[i%benchmarkTreeSize])
|
||||
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize])
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDeleteInsertCloneEachTime(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, item := range insertP {
|
||||
tr.ReplaceOrInsert(item)
|
||||
}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tr = tr.Clone()
|
||||
tr.Delete(insertP[i%benchmarkTreeSize])
|
||||
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize])
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDelete(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
removeP := perm(benchmarkTreeSize)
|
||||
b.StartTimer()
|
||||
i := 0
|
||||
for i < b.N {
|
||||
b.StopTimer()
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range insertP {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
b.StartTimer()
|
||||
for _, item := range removeP {
|
||||
tr.Delete(item)
|
||||
i++
|
||||
if i >= b.N {
|
||||
return
|
||||
}
|
||||
}
|
||||
if tr.Len() > 0 {
|
||||
panic(tr.Len())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGet(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
removeP := perm(benchmarkTreeSize)
|
||||
b.StartTimer()
|
||||
i := 0
|
||||
for i < b.N {
|
||||
b.StopTimer()
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range insertP {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
b.StartTimer()
|
||||
for _, item := range removeP {
|
||||
tr.Get(item)
|
||||
i++
|
||||
if i >= b.N {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetCloneEachTime(b *testing.B) {
|
||||
b.StopTimer()
|
||||
insertP := perm(benchmarkTreeSize)
|
||||
removeP := perm(benchmarkTreeSize)
|
||||
b.StartTimer()
|
||||
i := 0
|
||||
for i < b.N {
|
||||
b.StopTimer()
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range insertP {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
b.StartTimer()
|
||||
for _, item := range removeP {
|
||||
tr = tr.Clone()
|
||||
tr.Get(item)
|
||||
i++
|
||||
if i >= b.N {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type byInts []Item
|
||||
|
||||
func (a byInts) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
|
||||
func (a byInts) Less(i, j int) bool {
|
||||
return a[i].(Int) < a[j].(Int)
|
||||
}
|
||||
|
||||
func (a byInts) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
|
||||
func BenchmarkAscend(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := 0
|
||||
tr.Ascend(func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j++
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDescend(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := len(arr) - 1
|
||||
tr.Descend(func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j--
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
func BenchmarkAscendRange(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := 100
|
||||
tr.AscendRange(Int(100), arr[len(arr)-100], func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j++
|
||||
return true
|
||||
})
|
||||
if j != len(arr)-100 {
|
||||
b.Fatalf("expected: %v, got %v", len(arr)-100, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDescendRange(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := len(arr) - 100
|
||||
tr.DescendRange(arr[len(arr)-100], Int(100), func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j--
|
||||
return true
|
||||
})
|
||||
if j != 100 {
|
||||
b.Fatalf("expected: %v, got %v", len(arr)-100, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
func BenchmarkAscendGreaterOrEqual(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := 100
|
||||
k := 0
|
||||
tr.AscendGreaterOrEqual(Int(100), func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j++
|
||||
k++
|
||||
return true
|
||||
})
|
||||
if j != len(arr) {
|
||||
b.Fatalf("expected: %v, got %v", len(arr), j)
|
||||
}
|
||||
if k != len(arr)-100 {
|
||||
b.Fatalf("expected: %v, got %v", len(arr)-100, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
func BenchmarkDescendLessOrEqual(b *testing.B) {
|
||||
arr := perm(benchmarkTreeSize)
|
||||
tr := New(*btreeDegree)
|
||||
for _, v := range arr {
|
||||
tr.ReplaceOrInsert(v)
|
||||
}
|
||||
sort.Sort(byInts(arr))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
j := len(arr) - 100
|
||||
k := len(arr)
|
||||
tr.DescendLessOrEqual(arr[len(arr)-100], func(item Item) bool {
|
||||
if item.(Int) != arr[j].(Int) {
|
||||
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
|
||||
}
|
||||
j--
|
||||
k--
|
||||
return true
|
||||
})
|
||||
if j != -1 {
|
||||
b.Fatalf("expected: %v, got %v", -1, j)
|
||||
}
|
||||
if k != 99 {
|
||||
b.Fatalf("expected: %v, got %v", 99, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cloneTestSize = 10000
|
||||
|
||||
func cloneTest(t *testing.T, b *BTree, start int, p []Item, wg *sync.WaitGroup, trees *[]*BTree) {
|
||||
t.Logf("Starting new clone at %v", start)
|
||||
*trees = append(*trees, b)
|
||||
for i := start; i < cloneTestSize; i++ {
|
||||
b.ReplaceOrInsert(p[i])
|
||||
if i%(cloneTestSize/5) == 0 {
|
||||
wg.Add(1)
|
||||
go cloneTest(t, b.Clone(), i+1, p, wg, trees)
|
||||
}
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
func TestCloneConcurrentOperations(t *testing.T) {
|
||||
b := New(*btreeDegree)
|
||||
trees := []*BTree{}
|
||||
p := perm(cloneTestSize)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go cloneTest(t, b, 0, p, &wg, &trees)
|
||||
wg.Wait()
|
||||
want := rang(cloneTestSize)
|
||||
t.Logf("Starting equality checks on %d trees", len(trees))
|
||||
for i, tree := range trees {
|
||||
if !reflect.DeepEqual(want, all(tree)) {
|
||||
t.Errorf("tree %v mismatch", i)
|
||||
}
|
||||
}
|
||||
t.Log("Removing half from first half")
|
||||
toRemove := rang(cloneTestSize)[cloneTestSize/2:]
|
||||
for i := 0; i < len(trees)/2; i++ {
|
||||
tree := trees[i]
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for _, item := range toRemove {
|
||||
tree.Delete(item)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
t.Log("Checking all values again")
|
||||
for i, tree := range trees {
|
||||
var wantpart []Item
|
||||
if i < len(trees)/2 {
|
||||
wantpart = want[:cloneTestSize/2]
|
||||
} else {
|
||||
wantpart = want
|
||||
}
|
||||
if got := all(tree); !reflect.DeepEqual(wantpart, got) {
|
||||
t.Errorf("tree %v mismatch, want %v got %v", i, len(want), len(got))
|
||||
}
|
||||
}
|
||||
}
|
||||
225
vendor/github.com/google/gofuzz/example_test.go
generated
vendored
Normal file
225
vendor/github.com/google/gofuzz/example_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package fuzz_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
func ExampleSimple() {
|
||||
type MyType struct {
|
||||
A string
|
||||
B string
|
||||
C int
|
||||
D struct {
|
||||
E float64
|
||||
}
|
||||
}
|
||||
|
||||
f := fuzz.New()
|
||||
object := MyType{}
|
||||
|
||||
uniqueObjects := map[MyType]int{}
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
f.Fuzz(&object)
|
||||
uniqueObjects[object]++
|
||||
}
|
||||
fmt.Printf("Got %v unique objects.\n", len(uniqueObjects))
|
||||
// Output:
|
||||
// Got 1000 unique objects.
|
||||
}
|
||||
|
||||
func ExampleCustom() {
|
||||
type MyType struct {
|
||||
A int
|
||||
B string
|
||||
}
|
||||
|
||||
counter := 0
|
||||
f := fuzz.New().Funcs(
|
||||
func(i *int, c fuzz.Continue) {
|
||||
*i = counter
|
||||
counter++
|
||||
},
|
||||
)
|
||||
object := MyType{}
|
||||
|
||||
uniqueObjects := map[MyType]int{}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
f.Fuzz(&object)
|
||||
if object.A != i {
|
||||
fmt.Printf("Unexpected value: %#v\n", object)
|
||||
}
|
||||
uniqueObjects[object]++
|
||||
}
|
||||
fmt.Printf("Got %v unique objects.\n", len(uniqueObjects))
|
||||
// Output:
|
||||
// Got 100 unique objects.
|
||||
}
|
||||
|
||||
func ExampleComplex() {
|
||||
type OtherType struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
type MyType struct {
|
||||
Pointer *OtherType
|
||||
Map map[string]OtherType
|
||||
PointerMap *map[string]OtherType
|
||||
Slice []OtherType
|
||||
SlicePointer []*OtherType
|
||||
PointerSlicePointer *[]*OtherType
|
||||
}
|
||||
|
||||
f := fuzz.New().RandSource(rand.NewSource(0)).NilChance(0).NumElements(1, 1).Funcs(
|
||||
func(o *OtherType, c fuzz.Continue) {
|
||||
o.A = "Foo"
|
||||
o.B = "Bar"
|
||||
},
|
||||
func(op **OtherType, c fuzz.Continue) {
|
||||
*op = &OtherType{"A", "B"}
|
||||
},
|
||||
func(m map[string]OtherType, c fuzz.Continue) {
|
||||
m["Works Because"] = OtherType{
|
||||
"Fuzzer",
|
||||
"Preallocated",
|
||||
}
|
||||
},
|
||||
)
|
||||
object := MyType{}
|
||||
f.Fuzz(&object)
|
||||
bytes, err := json.MarshalIndent(&object, "", " ")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v\n", err)
|
||||
}
|
||||
fmt.Printf("%s\n", string(bytes))
|
||||
// Output:
|
||||
// {
|
||||
// "Pointer": {
|
||||
// "A": "A",
|
||||
// "B": "B"
|
||||
// },
|
||||
// "Map": {
|
||||
// "Works Because": {
|
||||
// "A": "Fuzzer",
|
||||
// "B": "Preallocated"
|
||||
// }
|
||||
// },
|
||||
// "PointerMap": {
|
||||
// "Works Because": {
|
||||
// "A": "Fuzzer",
|
||||
// "B": "Preallocated"
|
||||
// }
|
||||
// },
|
||||
// "Slice": [
|
||||
// {
|
||||
// "A": "Foo",
|
||||
// "B": "Bar"
|
||||
// }
|
||||
// ],
|
||||
// "SlicePointer": [
|
||||
// {
|
||||
// "A": "A",
|
||||
// "B": "B"
|
||||
// }
|
||||
// ],
|
||||
// "PointerSlicePointer": [
|
||||
// {
|
||||
// "A": "A",
|
||||
// "B": "B"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
}
|
||||
|
||||
func ExampleMap() {
|
||||
f := fuzz.New().NilChance(0).NumElements(1, 1)
|
||||
var myMap map[struct{ A, B, C int }]string
|
||||
f.Fuzz(&myMap)
|
||||
fmt.Printf("myMap has %v element(s).\n", len(myMap))
|
||||
// Output:
|
||||
// myMap has 1 element(s).
|
||||
}
|
||||
|
||||
func ExampleSingle() {
|
||||
f := fuzz.New()
|
||||
var i int
|
||||
f.Fuzz(&i)
|
||||
|
||||
// Technically, we'd expect this to fail one out of 2 billion attempts...
|
||||
fmt.Printf("(i == 0) == %v", i == 0)
|
||||
// Output:
|
||||
// (i == 0) == false
|
||||
}
|
||||
|
||||
func ExampleEnum() {
|
||||
type MyEnum string
|
||||
const (
|
||||
A MyEnum = "A"
|
||||
B MyEnum = "B"
|
||||
)
|
||||
type MyInfo struct {
|
||||
Type MyEnum
|
||||
AInfo *string
|
||||
BInfo *string
|
||||
}
|
||||
|
||||
f := fuzz.New().NilChance(0).Funcs(
|
||||
func(e *MyInfo, c fuzz.Continue) {
|
||||
// Note c's embedded Rand allows for direct use.
|
||||
// We could also use c.RandBool() here.
|
||||
switch c.Intn(2) {
|
||||
case 0:
|
||||
e.Type = A
|
||||
c.Fuzz(&e.AInfo)
|
||||
case 1:
|
||||
e.Type = B
|
||||
c.Fuzz(&e.BInfo)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
var myObject MyInfo
|
||||
f.Fuzz(&myObject)
|
||||
switch myObject.Type {
|
||||
case A:
|
||||
if myObject.AInfo == nil {
|
||||
fmt.Println("AInfo should have been set!")
|
||||
}
|
||||
if myObject.BInfo != nil {
|
||||
fmt.Println("BInfo should NOT have been set!")
|
||||
}
|
||||
case B:
|
||||
if myObject.BInfo == nil {
|
||||
fmt.Println("BInfo should have been set!")
|
||||
}
|
||||
if myObject.AInfo != nil {
|
||||
fmt.Println("AInfo should NOT have been set!")
|
||||
}
|
||||
default:
|
||||
fmt.Println("Invalid enum value!")
|
||||
}
|
||||
}
|
||||
// Output:
|
||||
}
|
||||
96
vendor/github.com/google/gofuzz/fuzz.go
generated
vendored
96
vendor/github.com/google/gofuzz/fuzz.go
generated
vendored
|
|
@ -34,21 +34,27 @@ type Fuzzer struct {
|
|||
nilChance float64
|
||||
minElements int
|
||||
maxElements int
|
||||
maxDepth int
|
||||
}
|
||||
|
||||
// New returns a new Fuzzer. Customize your Fuzzer further by calling Funcs,
|
||||
// RandSource, NilChance, or NumElements in any order.
|
||||
func New() *Fuzzer {
|
||||
return NewWithSeed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func NewWithSeed(seed int64) *Fuzzer {
|
||||
f := &Fuzzer{
|
||||
defaultFuzzFuncs: fuzzFuncMap{
|
||||
reflect.TypeOf(&time.Time{}): reflect.ValueOf(fuzzTime),
|
||||
},
|
||||
|
||||
fuzzFuncs: fuzzFuncMap{},
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
r: rand.New(rand.NewSource(seed)),
|
||||
nilChance: .2,
|
||||
minElements: 1,
|
||||
maxElements: 10,
|
||||
maxDepth: 100,
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
|
@ -136,6 +142,14 @@ func (f *Fuzzer) genShouldFill() bool {
|
|||
return f.r.Float64() > f.nilChance
|
||||
}
|
||||
|
||||
// MaxDepth sets the maximum number of recursive fuzz calls that will be made
|
||||
// before stopping. This includes struct members, pointers, and map and slice
|
||||
// elements.
|
||||
func (f *Fuzzer) MaxDepth(d int) *Fuzzer {
|
||||
f.maxDepth = d
|
||||
return f
|
||||
}
|
||||
|
||||
// Fuzz recursively fills all of obj's fields with something random. First
|
||||
// this tries to find a custom fuzz function (see Funcs). If there is no
|
||||
// custom function this tests whether the object implements fuzz.Interface and,
|
||||
|
|
@ -144,17 +158,19 @@ func (f *Fuzzer) genShouldFill() bool {
|
|||
// fails, this will generate random values for all primitive fields and then
|
||||
// recurse for all non-primitives.
|
||||
//
|
||||
// Not safe for cyclic or tree-like structs!
|
||||
// This is safe for cyclic or tree-like structs, up to a limit. Use the
|
||||
// MaxDepth method to adjust how deep you need it to recurse.
|
||||
//
|
||||
// obj must be a pointer. Only exported (public) fields can be set (thanks, golang :/ )
|
||||
// Intended for tests, so will panic on bad input or unimplemented fields.
|
||||
// obj must be a pointer. Only exported (public) fields can be set (thanks,
|
||||
// golang :/ ) Intended for tests, so will panic on bad input or unimplemented
|
||||
// fields.
|
||||
func (f *Fuzzer) Fuzz(obj interface{}) {
|
||||
v := reflect.ValueOf(obj)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
panic("needed ptr!")
|
||||
}
|
||||
v = v.Elem()
|
||||
f.doFuzz(v, 0)
|
||||
f.fuzzWithContext(v, 0)
|
||||
}
|
||||
|
||||
// FuzzNoCustom is just like Fuzz, except that any custom fuzz function for
|
||||
|
|
@ -170,7 +186,7 @@ func (f *Fuzzer) FuzzNoCustom(obj interface{}) {
|
|||
panic("needed ptr!")
|
||||
}
|
||||
v = v.Elem()
|
||||
f.doFuzz(v, flagNoCustomFuzz)
|
||||
f.fuzzWithContext(v, flagNoCustomFuzz)
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
@ -178,69 +194,87 @@ const (
|
|||
flagNoCustomFuzz uint64 = 1 << iota
|
||||
)
|
||||
|
||||
func (f *Fuzzer) doFuzz(v reflect.Value, flags uint64) {
|
||||
func (f *Fuzzer) fuzzWithContext(v reflect.Value, flags uint64) {
|
||||
fc := &fuzzerContext{fuzzer: f}
|
||||
fc.doFuzz(v, flags)
|
||||
}
|
||||
|
||||
// fuzzerContext carries context about a single fuzzing run, which lets Fuzzer
|
||||
// be thread-safe.
|
||||
type fuzzerContext struct {
|
||||
fuzzer *Fuzzer
|
||||
curDepth int
|
||||
}
|
||||
|
||||
func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) {
|
||||
if fc.curDepth >= fc.fuzzer.maxDepth {
|
||||
return
|
||||
}
|
||||
fc.curDepth++
|
||||
defer func() { fc.curDepth-- }()
|
||||
|
||||
if !v.CanSet() {
|
||||
return
|
||||
}
|
||||
|
||||
if flags&flagNoCustomFuzz == 0 {
|
||||
// Check for both pointer and non-pointer custom functions.
|
||||
if v.CanAddr() && f.tryCustom(v.Addr()) {
|
||||
if v.CanAddr() && fc.tryCustom(v.Addr()) {
|
||||
return
|
||||
}
|
||||
if f.tryCustom(v) {
|
||||
if fc.tryCustom(v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if fn, ok := fillFuncMap[v.Kind()]; ok {
|
||||
fn(v, f.r)
|
||||
fn(v, fc.fuzzer.r)
|
||||
return
|
||||
}
|
||||
switch v.Kind() {
|
||||
case reflect.Map:
|
||||
if f.genShouldFill() {
|
||||
if fc.fuzzer.genShouldFill() {
|
||||
v.Set(reflect.MakeMap(v.Type()))
|
||||
n := f.genElementCount()
|
||||
n := fc.fuzzer.genElementCount()
|
||||
for i := 0; i < n; i++ {
|
||||
key := reflect.New(v.Type().Key()).Elem()
|
||||
f.doFuzz(key, 0)
|
||||
fc.doFuzz(key, 0)
|
||||
val := reflect.New(v.Type().Elem()).Elem()
|
||||
f.doFuzz(val, 0)
|
||||
fc.doFuzz(val, 0)
|
||||
v.SetMapIndex(key, val)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Ptr:
|
||||
if f.genShouldFill() {
|
||||
if fc.fuzzer.genShouldFill() {
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
f.doFuzz(v.Elem(), 0)
|
||||
fc.doFuzz(v.Elem(), 0)
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Slice:
|
||||
if f.genShouldFill() {
|
||||
n := f.genElementCount()
|
||||
if fc.fuzzer.genShouldFill() {
|
||||
n := fc.fuzzer.genElementCount()
|
||||
v.Set(reflect.MakeSlice(v.Type(), n, n))
|
||||
for i := 0; i < n; i++ {
|
||||
f.doFuzz(v.Index(i), 0)
|
||||
fc.doFuzz(v.Index(i), 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Array:
|
||||
if f.genShouldFill() {
|
||||
if fc.fuzzer.genShouldFill() {
|
||||
n := v.Len()
|
||||
for i := 0; i < n; i++ {
|
||||
f.doFuzz(v.Index(i), 0)
|
||||
fc.doFuzz(v.Index(i), 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Struct:
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
f.doFuzz(v.Field(i), 0)
|
||||
fc.doFuzz(v.Field(i), 0)
|
||||
}
|
||||
case reflect.Chan:
|
||||
fallthrough
|
||||
|
|
@ -255,20 +289,20 @@ func (f *Fuzzer) doFuzz(v reflect.Value, flags uint64) {
|
|||
|
||||
// tryCustom searches for custom handlers, and returns true iff it finds a match
|
||||
// and successfully randomizes v.
|
||||
func (f *Fuzzer) tryCustom(v reflect.Value) bool {
|
||||
func (fc *fuzzerContext) tryCustom(v reflect.Value) bool {
|
||||
// First: see if we have a fuzz function for it.
|
||||
doCustom, ok := f.fuzzFuncs[v.Type()]
|
||||
doCustom, ok := fc.fuzzer.fuzzFuncs[v.Type()]
|
||||
if !ok {
|
||||
// Second: see if it can fuzz itself.
|
||||
if v.CanInterface() {
|
||||
intf := v.Interface()
|
||||
if fuzzable, ok := intf.(Interface); ok {
|
||||
fuzzable.Fuzz(Continue{f: f, Rand: f.r})
|
||||
fuzzable.Fuzz(Continue{fc: fc, Rand: fc.fuzzer.r})
|
||||
return true
|
||||
}
|
||||
}
|
||||
// Finally: see if there is a default fuzz function.
|
||||
doCustom, ok = f.defaultFuzzFuncs[v.Type()]
|
||||
doCustom, ok = fc.fuzzer.defaultFuzzFuncs[v.Type()]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
|
@ -294,8 +328,8 @@ func (f *Fuzzer) tryCustom(v reflect.Value) bool {
|
|||
}
|
||||
|
||||
doCustom.Call([]reflect.Value{v, reflect.ValueOf(Continue{
|
||||
f: f,
|
||||
Rand: f.r,
|
||||
fc: fc,
|
||||
Rand: fc.fuzzer.r,
|
||||
})})
|
||||
return true
|
||||
}
|
||||
|
|
@ -310,7 +344,7 @@ type Interface interface {
|
|||
// Continue can be passed to custom fuzzing functions to allow them to use
|
||||
// the correct source of randomness and to continue fuzzing their members.
|
||||
type Continue struct {
|
||||
f *Fuzzer
|
||||
fc *fuzzerContext
|
||||
|
||||
// For convenience, Continue implements rand.Rand via embedding.
|
||||
// Use this for generating any randomness if you want your fuzzing
|
||||
|
|
@ -325,7 +359,7 @@ func (c Continue) Fuzz(obj interface{}) {
|
|||
panic("needed ptr!")
|
||||
}
|
||||
v = v.Elem()
|
||||
c.f.doFuzz(v, 0)
|
||||
c.fc.doFuzz(v, 0)
|
||||
}
|
||||
|
||||
// FuzzNoCustom continues fuzzing obj, except that any custom fuzz function for
|
||||
|
|
@ -338,7 +372,7 @@ func (c Continue) FuzzNoCustom(obj interface{}) {
|
|||
panic("needed ptr!")
|
||||
}
|
||||
v = v.Elem()
|
||||
c.f.doFuzz(v, flagNoCustomFuzz)
|
||||
c.fc.doFuzz(v, flagNoCustomFuzz)
|
||||
}
|
||||
|
||||
// RandString makes a random string up to 20 characters long. The returned string
|
||||
|
|
|
|||
472
vendor/github.com/google/gofuzz/fuzz_test.go
generated
vendored
Normal file
472
vendor/github.com/google/gofuzz/fuzz_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,472 @@
|
|||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package fuzz
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFuzz_basic(t *testing.T) {
|
||||
obj := &struct {
|
||||
I int
|
||||
I8 int8
|
||||
I16 int16
|
||||
I32 int32
|
||||
I64 int64
|
||||
U uint
|
||||
U8 uint8
|
||||
U16 uint16
|
||||
U32 uint32
|
||||
U64 uint64
|
||||
Uptr uintptr
|
||||
S string
|
||||
B bool
|
||||
T time.Time
|
||||
}{}
|
||||
|
||||
failed := map[string]int{}
|
||||
for i := 0; i < 10; i++ {
|
||||
New().Fuzz(obj)
|
||||
|
||||
if n, v := "i", obj.I; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "i8", obj.I8; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "i16", obj.I16; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "i32", obj.I32; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "i64", obj.I64; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "u", obj.U; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "u8", obj.U8; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "u16", obj.U16; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "u32", obj.U32; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "u64", obj.U64; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "uptr", obj.Uptr; v == 0 {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "s", obj.S; v == "" {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "b", obj.B; v == false {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "t", obj.T; v.IsZero() {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
}
|
||||
checkFailed(t, failed)
|
||||
}
|
||||
|
||||
func checkFailed(t *testing.T, failed map[string]int) {
|
||||
for k, v := range failed {
|
||||
if v > 8 {
|
||||
t.Errorf("%v seems to not be getting set, was zero value %v times", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFuzz_structptr(t *testing.T) {
|
||||
obj := &struct {
|
||||
A *struct {
|
||||
S string
|
||||
}
|
||||
}{}
|
||||
|
||||
f := New().NilChance(.5)
|
||||
failed := map[string]int{}
|
||||
for i := 0; i < 10; i++ {
|
||||
f.Fuzz(obj)
|
||||
|
||||
if n, v := "a not nil", obj.A; v == nil {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "a nil", obj.A; v != nil {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
if n, v := "as", obj.A; v == nil || v.S == "" {
|
||||
failed[n] = failed[n] + 1
|
||||
}
|
||||
}
|
||||
checkFailed(t, failed)
|
||||
}
|
||||
|
||||
// tryFuzz tries fuzzing up to 20 times. Fail if check() never passes, report the highest
|
||||
// stage it ever got to.
|
||||
func tryFuzz(t *testing.T, f *Fuzzer, obj interface{}, check func() (stage int, passed bool)) {
|
||||
maxStage := 0
|
||||
for i := 0; i < 20; i++ {
|
||||
f.Fuzz(obj)
|
||||
stage, passed := check()
|
||||
if stage > maxStage {
|
||||
maxStage = stage
|
||||
}
|
||||
if passed {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("Only ever got to stage %v", maxStage)
|
||||
}
|
||||
|
||||
func TestFuzz_structmap(t *testing.T) {
|
||||
obj := &struct {
|
||||
A map[struct {
|
||||
S string
|
||||
}]struct {
|
||||
S2 string
|
||||
}
|
||||
B map[string]string
|
||||
}{}
|
||||
|
||||
tryFuzz(t, New(), obj, func() (int, bool) {
|
||||
if obj.A == nil {
|
||||
return 1, false
|
||||
}
|
||||
if len(obj.A) == 0 {
|
||||
return 2, false
|
||||
}
|
||||
for k, v := range obj.A {
|
||||
if k.S == "" {
|
||||
return 3, false
|
||||
}
|
||||
if v.S2 == "" {
|
||||
return 4, false
|
||||
}
|
||||
}
|
||||
|
||||
if obj.B == nil {
|
||||
return 5, false
|
||||
}
|
||||
if len(obj.B) == 0 {
|
||||
return 6, false
|
||||
}
|
||||
for k, v := range obj.B {
|
||||
if k == "" {
|
||||
return 7, false
|
||||
}
|
||||
if v == "" {
|
||||
return 8, false
|
||||
}
|
||||
}
|
||||
return 9, true
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_structslice(t *testing.T) {
|
||||
obj := &struct {
|
||||
A []struct {
|
||||
S string
|
||||
}
|
||||
B []string
|
||||
}{}
|
||||
|
||||
tryFuzz(t, New(), obj, func() (int, bool) {
|
||||
if obj.A == nil {
|
||||
return 1, false
|
||||
}
|
||||
if len(obj.A) == 0 {
|
||||
return 2, false
|
||||
}
|
||||
for _, v := range obj.A {
|
||||
if v.S == "" {
|
||||
return 3, false
|
||||
}
|
||||
}
|
||||
|
||||
if obj.B == nil {
|
||||
return 4, false
|
||||
}
|
||||
if len(obj.B) == 0 {
|
||||
return 5, false
|
||||
}
|
||||
for _, v := range obj.B {
|
||||
if v == "" {
|
||||
return 6, false
|
||||
}
|
||||
}
|
||||
return 7, true
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_structarray(t *testing.T) {
|
||||
obj := &struct {
|
||||
A [3]struct {
|
||||
S string
|
||||
}
|
||||
B [2]int
|
||||
}{}
|
||||
|
||||
tryFuzz(t, New(), obj, func() (int, bool) {
|
||||
for _, v := range obj.A {
|
||||
if v.S == "" {
|
||||
return 1, false
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range obj.B {
|
||||
if v == 0 {
|
||||
return 2, false
|
||||
}
|
||||
}
|
||||
return 3, true
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_custom(t *testing.T) {
|
||||
obj := &struct {
|
||||
A string
|
||||
B *string
|
||||
C map[string]string
|
||||
D *map[string]string
|
||||
}{}
|
||||
|
||||
testPhrase := "gotcalled"
|
||||
testMap := map[string]string{"C": "D"}
|
||||
f := New().Funcs(
|
||||
func(s *string, c Continue) {
|
||||
*s = testPhrase
|
||||
},
|
||||
func(m map[string]string, c Continue) {
|
||||
m["C"] = "D"
|
||||
},
|
||||
)
|
||||
|
||||
tryFuzz(t, f, obj, func() (int, bool) {
|
||||
if obj.A != testPhrase {
|
||||
return 1, false
|
||||
}
|
||||
if obj.B == nil {
|
||||
return 2, false
|
||||
}
|
||||
if *obj.B != testPhrase {
|
||||
return 3, false
|
||||
}
|
||||
if e, a := testMap, obj.C; !reflect.DeepEqual(e, a) {
|
||||
return 4, false
|
||||
}
|
||||
if obj.D == nil {
|
||||
return 5, false
|
||||
}
|
||||
if e, a := testMap, *obj.D; !reflect.DeepEqual(e, a) {
|
||||
return 6, false
|
||||
}
|
||||
return 7, true
|
||||
})
|
||||
}
|
||||
|
||||
type SelfFuzzer string
|
||||
|
||||
// Implement fuzz.Interface.
|
||||
func (sf *SelfFuzzer) Fuzz(c Continue) {
|
||||
*sf = selfFuzzerTestPhrase
|
||||
}
|
||||
|
||||
const selfFuzzerTestPhrase = "was fuzzed"
|
||||
|
||||
func TestFuzz_interface(t *testing.T) {
|
||||
f := New()
|
||||
|
||||
var obj1 SelfFuzzer
|
||||
tryFuzz(t, f, &obj1, func() (int, bool) {
|
||||
if obj1 != selfFuzzerTestPhrase {
|
||||
return 1, false
|
||||
}
|
||||
return 1, true
|
||||
})
|
||||
|
||||
var obj2 map[int]SelfFuzzer
|
||||
tryFuzz(t, f, &obj2, func() (int, bool) {
|
||||
for _, v := range obj2 {
|
||||
if v != selfFuzzerTestPhrase {
|
||||
return 1, false
|
||||
}
|
||||
}
|
||||
return 1, true
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_interfaceAndFunc(t *testing.T) {
|
||||
const privateTestPhrase = "private phrase"
|
||||
f := New().Funcs(
|
||||
// This should take precedence over SelfFuzzer.Fuzz().
|
||||
func(s *SelfFuzzer, c Continue) {
|
||||
*s = privateTestPhrase
|
||||
},
|
||||
)
|
||||
|
||||
var obj1 SelfFuzzer
|
||||
tryFuzz(t, f, &obj1, func() (int, bool) {
|
||||
if obj1 != privateTestPhrase {
|
||||
return 1, false
|
||||
}
|
||||
return 1, true
|
||||
})
|
||||
|
||||
var obj2 map[int]SelfFuzzer
|
||||
tryFuzz(t, f, &obj2, func() (int, bool) {
|
||||
for _, v := range obj2 {
|
||||
if v != privateTestPhrase {
|
||||
return 1, false
|
||||
}
|
||||
}
|
||||
return 1, true
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_noCustom(t *testing.T) {
|
||||
type Inner struct {
|
||||
Str string
|
||||
}
|
||||
type Outer struct {
|
||||
Str string
|
||||
In Inner
|
||||
}
|
||||
|
||||
testPhrase := "gotcalled"
|
||||
f := New().Funcs(
|
||||
func(outer *Outer, c Continue) {
|
||||
outer.Str = testPhrase
|
||||
c.Fuzz(&outer.In)
|
||||
},
|
||||
func(inner *Inner, c Continue) {
|
||||
inner.Str = testPhrase
|
||||
},
|
||||
)
|
||||
c := Continue{fc: &fuzzerContext{fuzzer: f}, Rand: f.r}
|
||||
|
||||
// Fuzzer.Fuzz()
|
||||
obj1 := Outer{}
|
||||
f.Fuzz(&obj1)
|
||||
if obj1.Str != testPhrase {
|
||||
t.Errorf("expected Outer custom function to have been called")
|
||||
}
|
||||
if obj1.In.Str != testPhrase {
|
||||
t.Errorf("expected Inner custom function to have been called")
|
||||
}
|
||||
|
||||
// Continue.Fuzz()
|
||||
obj2 := Outer{}
|
||||
c.Fuzz(&obj2)
|
||||
if obj2.Str != testPhrase {
|
||||
t.Errorf("expected Outer custom function to have been called")
|
||||
}
|
||||
if obj2.In.Str != testPhrase {
|
||||
t.Errorf("expected Inner custom function to have been called")
|
||||
}
|
||||
|
||||
// Fuzzer.FuzzNoCustom()
|
||||
obj3 := Outer{}
|
||||
f.FuzzNoCustom(&obj3)
|
||||
if obj3.Str == testPhrase {
|
||||
t.Errorf("expected Outer custom function to not have been called")
|
||||
}
|
||||
if obj3.In.Str != testPhrase {
|
||||
t.Errorf("expected Inner custom function to have been called")
|
||||
}
|
||||
|
||||
// Continue.FuzzNoCustom()
|
||||
obj4 := Outer{}
|
||||
c.FuzzNoCustom(&obj4)
|
||||
if obj4.Str == testPhrase {
|
||||
t.Errorf("expected Outer custom function to not have been called")
|
||||
}
|
||||
if obj4.In.Str != testPhrase {
|
||||
t.Errorf("expected Inner custom function to have been called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFuzz_NumElements(t *testing.T) {
|
||||
f := New().NilChance(0).NumElements(0, 1)
|
||||
obj := &struct {
|
||||
A []int
|
||||
}{}
|
||||
|
||||
tryFuzz(t, f, obj, func() (int, bool) {
|
||||
if obj.A == nil {
|
||||
return 1, false
|
||||
}
|
||||
return 2, len(obj.A) == 0
|
||||
})
|
||||
tryFuzz(t, f, obj, func() (int, bool) {
|
||||
if obj.A == nil {
|
||||
return 3, false
|
||||
}
|
||||
return 4, len(obj.A) == 1
|
||||
})
|
||||
}
|
||||
|
||||
func TestFuzz_Maxdepth(t *testing.T) {
|
||||
type S struct {
|
||||
S *S
|
||||
}
|
||||
|
||||
f := New().NilChance(0)
|
||||
|
||||
f.MaxDepth(1)
|
||||
for i := 0; i < 100; i++ {
|
||||
obj := S{}
|
||||
f.Fuzz(&obj)
|
||||
|
||||
if obj.S != nil {
|
||||
t.Errorf("Expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
f.MaxDepth(3) // field, ptr
|
||||
for i := 0; i < 100; i++ {
|
||||
obj := S{}
|
||||
f.Fuzz(&obj)
|
||||
|
||||
if obj.S == nil {
|
||||
t.Errorf("Expected obj.S not nil")
|
||||
} else if obj.S.S != nil {
|
||||
t.Errorf("Expected obj.S.S nil")
|
||||
}
|
||||
}
|
||||
|
||||
f.MaxDepth(5) // field, ptr, field, ptr
|
||||
for i := 0; i < 100; i++ {
|
||||
obj := S{}
|
||||
f.Fuzz(&obj)
|
||||
|
||||
if obj.S == nil {
|
||||
t.Errorf("Expected obj.S not nil")
|
||||
} else if obj.S.S == nil {
|
||||
t.Errorf("Expected obj.S.S not nil")
|
||||
} else if obj.S.S.S != nil {
|
||||
t.Errorf("Expected obj.S.S.S nil")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue