github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitree/bdb/quick_test.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bdb_test 16 17 import ( 18 "bytes" 19 "flag" 20 "fmt" 21 "math/rand" 22 "os" 23 "reflect" 24 "testing" 25 "testing/quick" 26 "time" 27 ) 28 29 var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int 30 31 func TestMain(m *testing.M) { 32 flag.IntVar(&qcount, "quick.count", 5, "") 33 flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "") 34 flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "") 35 flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "") 36 flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "") 37 flag.Parse() 38 fmt.Fprintln(os.Stderr, "seed:", qseed) 39 fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize) 40 41 os.Exit(m.Run()) 42 } 43 44 func qconfig() *quick.Config { 45 return &quick.Config{ 46 MaxCount: qcount, 47 Rand: rand.New(rand.NewSource(int64(qseed))), 48 } 49 } 50 51 type testdata []testdataitem 52 53 func (t testdata) Len() int { return len(t) } 54 func (t testdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] } 55 func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 } 56 57 func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value { 58 n := rand.Intn(qmaxitems-1) + 1 59 items := make(testdata, n) 60 used := make(map[string]bool) 61 for i := 0; i < n; i++ { 62 item := &items[i] 63 for { 64 item.Key = randByteSlice(rand, 1, qmaxksize) 65 if !used[string(item.Key)] { 66 used[string(item.Key)] = true 67 break 68 } 69 } 70 item.Value = randByteSlice(rand, 0, qmaxvsize) 71 } 72 return reflect.ValueOf(items) 73 } 74 75 type revtestdata []testdataitem 76 77 func (t revtestdata) Len() int { return len(t) } 78 func (t revtestdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] } 79 func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 } 80 81 type testdataitem struct { 82 Key []byte 83 Value []byte 84 } 85 86 func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte { 87 n := rand.Intn(maxSize-minSize) + minSize 88 b := make([]byte, n) 89 for i := 0; i < n; i++ { 90 b[i] = byte(rand.Intn(255)) 91 } 92 return b 93 }