func TestBucket_Delete_Quick(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	f := func(items testdata) bool {
		withOpenDB(func(db *DB, path string) {
			db.Update(func(tx *Tx) error {
				_, err := tx.CreateBucket([]byte("widgets"))
				return err
			})
			err := db.Update(func(tx *Tx) error {
				b := tx.Bucket([]byte("widgets"))
				for _, item := range items {
					assert.NoError(t, b.Put(item.Key, item.Value))
				}
				return nil
			})
			assert.NoError(t, err)
			for i, item := range items {
				err := db.Update(func(tx *Tx) error {
					return tx.Bucket([]byte("widgets")).Delete(item.Key)
				})
				assert.NoError(t, err)
				db.View(func(tx *Tx) error {
					b := tx.Bucket([]byte("widgets"))
					for j, exp := range items {
						value := b.Get(exp.Key)
						if j > i {
							if !assert.Equal(t, exp.Value, value) {
								t.FailNow()
							}
						} else {
							if !assert.Nil(t, value) {
								t.FailNow()
							}
						}
					}
					return nil
				})
			}
		})
		return true
	}
	if err := quick.Check(f, qconfig()); err != nil {
		t.Error(err)
	}
}
Ensure that a transaction can delete all key/value pairs and return to a single leaf page.
package bolt
import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"
	"testing"
	"testing/quick"
	"github.com/stretchr/testify/assert"
)
func TestBucket_Get_NonExistent(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Nil(t, value)
			return nil
		})
	})
}
func TestBucket_Get_FromNode(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			b.Put([]byte("foo"), []byte("bar"))
			value := b.Get([]byte("foo"))
			assert.Equal(t, value, []byte("bar"))
			return nil
		})
	})
}
func TestBucket_Get_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Nil(t, tx.Bucket([]byte("widgets")).Get([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_Put(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			err := tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
			assert.NoError(t, err)
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Equal(t, value, []byte("bar"))
			return nil
		})
	})
}
func TestBucket_Put_Repeat(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Put([]byte("foo"), []byte("bar")))
			assert.NoError(t, b.Put([]byte("foo"), []byte("baz")))
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Equal(t, value, []byte("baz"))
			return nil
		})
	})
}
func TestBucket_Put_Large(t *testing.T) {
	var count = 100
	var factor = 200
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			for i := 1; i < count; i++ {
				assert.NoError(t, b.Put([]byte(strings.Repeat("0", i*factor)), []byte(strings.Repeat("X", (count-i)*factor))))
			}
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			for i := 1; i < count; i++ {
				value := b.Get([]byte(strings.Repeat("0", i*factor)))
				assert.Equal(t, []byte(strings.Repeat("X", (count-i)*factor)), value)
			}
			return nil
		})
	})
}
func TestBucket_Put_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Equal(t, ErrIncompatibleValue, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			return nil
		})
	})
}
func TestBucket_Put_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		assert.Equal(t, ErrTxClosed, b.Put([]byte("foo"), []byte("bar")))
	})
}
func TestBucket_Put_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			err := b.Put([]byte("foo"), []byte("bar"))
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
func TestBucket_Delete(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
			err := tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
			assert.NoError(t, err)
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Nil(t, value)
			return nil
		})
	})
}
func TestBucket_Delete_Large(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			var b, _ = tx.CreateBucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.NoError(t, b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat("*", 1024))))
			}
			return nil
		})
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.NoError(t, b.Delete([]byte(strconv.Itoa(i))))
			}
			return nil
		})
		db.View(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.Nil(t, b.Get([]byte(strconv.Itoa(i))))
			}
			return nil
		})
	})
}
func TestBucket_Nested(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			b, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			_, err = b.CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.NoError(t, b.Put([]byte("bar"), []byte("0000")))
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Put([]byte("bar"), []byte("xxxx")))
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 10000; i++ {
				assert.NoError(t, b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))))
			}
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")))
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.Equal(t, []byte("yyyy"), b.Bucket([]byte("foo")).Get([]byte("baz")))
			assert.Equal(t, []byte("xxxx"), b.Get([]byte("bar")))
			for i := 0; i < 10000; i++ {
				assert.Equal(t, []byte(strconv.Itoa(i)), b.Get([]byte(strconv.Itoa(i))))
			}
			return nil
		})
	})
}
func TestBucket_Delete_Bucket(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			_, err := b.CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Equal(t, ErrIncompatibleValue, b.Delete([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_Delete_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			err := b.Delete([]byte("foo"))
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
func TestBucket_Delete_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		assert.Equal(t, ErrTxClosed, b.Delete([]byte("foo")))
	})
}
func TestBucket_DeleteBucket_Nested(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).CreateBucket([]byte("bar"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Put([]byte("baz"), []byte("bat")))
			assert.NoError(t, tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_DeleteBucket_Nested2(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).CreateBucket([]byte("bar"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Put([]byte("baz"), []byte("bat")))
			return nil
		})
		db.Update(func(tx *Tx) error {
			assert.NotNil(t, tx.Bucket([]byte("widgets")))
			assert.NotNil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")))
			assert.NotNil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")))
			assert.Equal(t, []byte("bat"), tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Get([]byte("baz")))
			assert.NoError(t, tx.DeleteBucket([]byte("widgets")))
			return nil
		})
		db.View(func(tx *Tx) error {
			assert.Nil(t, tx.Bucket([]byte("widgets")))
			return nil
		})
	})
}
func TestBucket_DeleteBucket_Large(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			b := tx.Bucket([]byte("widgets")).Bucket([]byte("foo"))
			for i := 0; i < 1000; i++ {
				assert.NoError(t, b.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%0100d", i))))
			}
			return nil
		})
		db.Update(func(tx *Tx) error {
			assert.NoError(t, tx.DeleteBucket([]byte("widgets")))
			return nil
		})
	})
}
func TestBucket_Bucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			assert.Nil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			_, err = tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.Equal(t, ErrIncompatibleValue, err)
			return nil
		})
	})
}
func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			assert.Equal(t, ErrIncompatibleValue, tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_NextSequence(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.CreateBucket([]byte("woojits"))
			seq, err := tx.Bucket([]byte("widgets")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 1)
			seq, err = tx.Bucket([]byte("widgets")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 2)
			seq, err = tx.Bucket([]byte("woojits")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 1)
			return nil
		})
	})
}
func TestBucket_NextSequence_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			i, err := b.NextSequence()
			assert.Equal(t, i, 0)
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
func TestBucket_NextSequence_Overflow(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			return nil
		})
		db.Update(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			b.bucket.sequence = uint64(maxInt)
			seq, err := b.NextSequence()
			assert.Equal(t, err, ErrSequenceOverflow)
			assert.Equal(t, seq, 0)
			return nil
		})
	})
}
func TestBucket_NextSequence_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		_, err := b.NextSequence()
		assert.Equal(t, ErrTxClosed, err)
	})
}
func TestBucket_ForEach(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("0000"))
			tx.Bucket([]byte("widgets")).Put([]byte("baz"), []byte("0001"))
			tx.Bucket([]byte("widgets")).Put([]byte("bar"), []byte("0002"))
			var index int
			err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
				switch index {
				case 0:
					assert.Equal(t, k, []byte("bar"))
					assert.Equal(t, v, []byte("0002"))
				case 1:
					assert.Equal(t, k, []byte("baz"))
					assert.Equal(t, v, []byte("0001"))
				case 2:
					assert.Equal(t, k, []byte("foo"))
					assert.Equal(t, v, []byte("0000"))
				}
				index++
				return nil
			})
			assert.NoError(t, err)
			assert.Equal(t, index, 3)
			return nil
		})
	})
}
func TestBucket_ForEach_ShortCircuit(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.Bucket([]byte("widgets")).Put([]byte("bar"), []byte("0000"))
			tx.Bucket([]byte("widgets")).Put([]byte("baz"), []byte("0000"))
			tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("0000"))
			var index int
			err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
				index++
				if bytes.Equal(k, []byte("baz")) {
					return errors.New("marker")
				}
				return nil
			})
			assert.Equal(t, errors.New("marker"), err)
			assert.Equal(t, 2, index)
			return nil
		})
	})
}
func TestBucket_ForEach_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		err := b.ForEach(func(k, v []byte) error { return nil })
		assert.Equal(t, ErrTxClosed, err)
	})
}
func TestBucket_Put_EmptyKey(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			err := tx.Bucket([]byte("widgets")).Put([]byte(""), []byte("bar"))
			assert.Equal(t, err, ErrKeyRequired)
			err = tx.Bucket([]byte("widgets")).Put(nil, []byte("bar"))
			assert.Equal(t, err, ErrKeyRequired)
			return nil
		})
	})
}
func TestBucket_Put_KeyTooLarge(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			err := tx.Bucket([]byte("widgets")).Put(make([]byte, 32769), []byte("bar"))
			assert.Equal(t, err, ErrKeyTooLarge)
			return nil
		})
	})
}
func TestBucket_Stats(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		big_key := []byte("really-big-value")
		db.Update(func(tx *Tx) error {
			b, err := tx.CreateBucket([]byte("woojits"))
			assert.NoError(t, err)
			for i := 0; i < 500; i++ {
				b.Put([]byte(fmt.Sprintf("%03d", i)), []byte(strconv.Itoa(i)))
			}
			b.Put(big_key, []byte(strings.Repeat("*", 10000)))
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("woojits"))
			stats := b.Stats()
			assert.Equal(t, 1, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 6, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 2, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 501, stats.KeyN, "KeyN")
			assert.Equal(t, 2, stats.Depth, "Depth")
			branchInuse := pageHeaderSize             
			branchInuse += 6 * branchPageElementSize  
			branchInuse += 6 * 3                      
			assert.Equal(t, branchInuse, stats.BranchInuse, "BranchInuse")
			leafInuse := 6 * pageHeaderSize           
			leafInuse += 501 * leafPageElementSize    
			leafInuse += 500*3 + len(big_key)         
			leafInuse += 1*10 + 2*90 + 3*400 + 10000  
			assert.Equal(t, leafInuse, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				assert.Equal(t, 4096, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 32768, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 1, stats.BucketN, "BucketN")
			assert.Equal(t, 0, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, 0, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
func TestBucket_Stats_Small(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			b, err := tx.CreateBucket([]byte("whozawhats"))
			assert.NoError(t, err)
			b.Put([]byte("foo"), []byte("bar"))
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("whozawhats"))
			stats := b.Stats()
			assert.Equal(t, 0, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 0, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 1, stats.KeyN, "KeyN")
			assert.Equal(t, 1, stats.Depth, "Depth")
			assert.Equal(t, 0, stats.BranchInuse, "BranchInuse")
			assert.Equal(t, 0, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 0, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 1, stats.BucketN, "BucketN")
			assert.Equal(t, 1, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, pageHeaderSize+leafPageElementSize+6, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
func TestBucket_Stats_EmptyBucket(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("whozawhats"))
			assert.NoError(t, err)
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("whozawhats"))
			stats := b.Stats()
			assert.Equal(t, 0, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 0, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 0, stats.KeyN, "KeyN")
			assert.Equal(t, 1, stats.Depth, "Depth")
			assert.Equal(t, 0, stats.BranchInuse, "BranchInuse")
			assert.Equal(t, 0, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 0, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 1, stats.BucketN, "BucketN")
			assert.Equal(t, 1, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, pageHeaderSize, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
func TestBucket_Stats_Nested(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			b, err := tx.CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			for i := 0; i < 100; i++ {
				b.Put([]byte(fmt.Sprintf("%02d", i)), []byte(fmt.Sprintf("%02d", i)))
			}
			bar, err := b.CreateBucket([]byte("bar"))
			assert.NoError(t, err)
			for i := 0; i < 10; i++ {
				bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
			}
			baz, err := bar.CreateBucket([]byte("baz"))
			assert.NoError(t, err)
			for i := 0; i < 10; i++ {
				baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
			}
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("foo"))
			stats := b.Stats()
			assert.Equal(t, 0, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 2, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 122, stats.KeyN, "KeyN")
			assert.Equal(t, 3, stats.Depth, "Depth")
			assert.Equal(t, 0, stats.BranchInuse, "BranchInuse")
			foo := pageHeaderSize             
			foo += 101 * leafPageElementSize  
			foo += 100*2 + 100*2              
			foo += 3 + bucketHeaderSize       
			bar := pageHeaderSize            
			bar += 11 * leafPageElementSize  
			bar += 10 + 10                   
			bar += 3 + bucketHeaderSize      
			baz := pageHeaderSize            
			baz += 10 * leafPageElementSize  
			baz += 10 + 10                   
			assert.Equal(t, foo+bar+baz, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 8192, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 3, stats.BucketN, "BucketN")
			assert.Equal(t, 1, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, baz, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
func TestBucket_Stats_Large(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	withOpenDB(func(db *DB, path string) {
		var index int
		for i := 0; i < 1000; i++ {
			db.Update(func(tx *Tx) error {
				b, _ := tx.CreateBucketIfNotExists([]byte("widgets"))
				for i := 0; i < 100; i++ {
					b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index)))
					index++
				}
				return nil
			})
		}
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			stats := b.Stats()
			assert.Equal(t, 19, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 1291, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 100000, stats.KeyN, "KeyN")
			assert.Equal(t, 3, stats.Depth, "Depth")
			assert.Equal(t, 27007, stats.BranchInuse, "BranchInuse")
			assert.Equal(t, 2598436, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				assert.Equal(t, 77824, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 5287936, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 1, stats.BucketN, "BucketN")
			assert.Equal(t, 0, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, 0, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
func TestBucket_Put_Single(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	index := 0
	f := func(items testdata) bool {
		withOpenDB(func(db *DB, path string) {
			m := make(map[string][]byte)
			db.Update(func(tx *Tx) error {
				_, err := tx.CreateBucket([]byte("widgets"))
				return err
			})
			for _, item := range items {
				db.Update(func(tx *Tx) error {
					if err := tx.Bucket([]byte("widgets")).Put(item.Key, item.Value); err != nil {
						panic("put error: " + err.Error())
					}
					m[string(item.Key)] = item.Value
					return nil
				})
				db.View(func(tx *Tx) error {
					i := 0
					for k, v := range m {
						value := tx.Bucket([]byte("widgets")).Get([]byte(k))
						if !bytes.Equal(value, v) {
							t.Logf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v)
							copyAndFailNow(t, db)
						}
						i++
					}
					return nil
				})
			}
		})
		index++
		return true
	}
	if err := quick.Check(f, qconfig()); err != nil {
		t.Error(err)
	}
}
func TestBucket_Put_Multiple(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	f := func(items testdata) bool {
		withOpenDB(func(db *DB, path string) {
			db.Update(func(tx *Tx) error {
				_, err := tx.CreateBucket([]byte("widgets"))
				return err
			})
			err := db.Update(func(tx *Tx) error {
				b := tx.Bucket([]byte("widgets"))
				for _, item := range items {
					assert.NoError(t, b.Put(item.Key, item.Value))
				}
				return nil
			})
			assert.NoError(t, err)
			db.View(func(tx *Tx) error {
				b := tx.Bucket([]byte("widgets"))
				for _, item := range items {
					value := b.Get(item.Key)
					if !assert.Equal(t, item.Value, value) {
						copyAndFailNow(t, db)
					}
				}
				return nil
			})
		})
		return true
	}
	if err := quick.Check(f, qconfig()); err != nil {
		t.Error(err)
	}
}