aboutsummaryrefslogtreecommitdiff
path: root/tests/quick_test.go
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-25 10:19:35 -0300
committerEuAndreh <eu@euandre.org>2024-10-25 10:19:35 -0300
commite7905fdf275fbb520de97df1eccdcd20e3d9aa6f (patch)
treed211118bf0c7bb20415367765f5eda66c8bdec86 /tests/quick_test.go
parentMerge pull request #748 from Chyroc/add/tx-copy-deprecated (diff)
downloaddedo-e7905fdf275fbb520de97df1eccdcd20e3d9aa6f.tar.gz
dedo-e7905fdf275fbb520de97df1eccdcd20e3d9aa6f.tar.xz
Move code to src/ and tests/
Diffstat (limited to 'tests/quick_test.go')
-rw-r--r--tests/quick_test.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/quick_test.go b/tests/quick_test.go
new file mode 100644
index 0000000..9e27792
--- /dev/null
+++ b/tests/quick_test.go
@@ -0,0 +1,87 @@
+package bolt_test
+
+import (
+ "bytes"
+ "flag"
+ "fmt"
+ "math/rand"
+ "os"
+ "reflect"
+ "testing/quick"
+ "time"
+)
+
+// testing/quick defaults to 5 iterations and a random seed.
+// You can override these settings from the command line:
+//
+// -quick.count The number of iterations to perform.
+// -quick.seed The seed to use for randomizing.
+// -quick.maxitems The maximum number of items to insert into a DB.
+// -quick.maxksize The maximum size of a key.
+// -quick.maxvsize The maximum size of a value.
+//
+
+var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int
+
+func init() {
+ flag.IntVar(&qcount, "quick.count", 5, "")
+ flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "")
+ flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "")
+ flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "")
+ flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "")
+ flag.Parse()
+ fmt.Fprintln(os.Stderr, "seed:", qseed)
+ fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize)
+}
+
+func qconfig() *quick.Config {
+ return &quick.Config{
+ MaxCount: qcount,
+ Rand: rand.New(rand.NewSource(int64(qseed))),
+ }
+}
+
+type testdata []testdataitem
+
+func (t testdata) Len() int { return len(t) }
+func (t testdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
+func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }
+
+func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {
+ n := rand.Intn(qmaxitems-1) + 1
+ items := make(testdata, n)
+ used := make(map[string]bool)
+ for i := 0; i < n; i++ {
+ item := &items[i]
+ // Ensure that keys are unique by looping until we find one that we have not already used.
+ for {
+ item.Key = randByteSlice(rand, 1, qmaxksize)
+ if !used[string(item.Key)] {
+ used[string(item.Key)] = true
+ break
+ }
+ }
+ item.Value = randByteSlice(rand, 0, qmaxvsize)
+ }
+ return reflect.ValueOf(items)
+}
+
+type revtestdata []testdataitem
+
+func (t revtestdata) Len() int { return len(t) }
+func (t revtestdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
+func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }
+
+type testdataitem struct {
+ Key []byte
+ Value []byte
+}
+
+func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {
+ n := rand.Intn(maxSize-minSize) + minSize
+ b := make([]byte, n)
+ for i := 0; i < n; i++ {
+ b[i] = byte(rand.Intn(255))
+ }
+ return b
+}