quick_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package bbolt_test
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "math/rand"
  7. "os"
  8. "reflect"
  9. "testing/quick"
  10. "time"
  11. )
  12. // testing/quick defaults to 5 iterations and a random seed.
  13. // You can override these settings from the command line:
  14. //
  15. // -quick.count The number of iterations to perform.
  16. // -quick.seed The seed to use for randomizing.
  17. // -quick.maxitems The maximum number of items to insert into a DB.
  18. // -quick.maxksize The maximum size of a key.
  19. // -quick.maxvsize The maximum size of a value.
  20. //
  21. var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int
  22. func init() {
  23. flag.IntVar(&qcount, "quick.count", 5, "")
  24. flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "")
  25. flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "")
  26. flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "")
  27. flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "")
  28. flag.Parse()
  29. fmt.Fprintln(os.Stderr, "seed:", qseed)
  30. fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize)
  31. }
  32. func qconfig() *quick.Config {
  33. return &quick.Config{
  34. MaxCount: qcount,
  35. Rand: rand.New(rand.NewSource(int64(qseed))),
  36. }
  37. }
  38. type testdata []testdataitem
  39. func (t testdata) Len() int { return len(t) }
  40. func (t testdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
  41. func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }
  42. func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {
  43. n := rand.Intn(qmaxitems-1) + 1
  44. items := make(testdata, n)
  45. used := make(map[string]bool)
  46. for i := 0; i < n; i++ {
  47. item := &items[i]
  48. // Ensure that keys are unique by looping until we find one that we have not already used.
  49. for {
  50. item.Key = randByteSlice(rand, 1, qmaxksize)
  51. if !used[string(item.Key)] {
  52. used[string(item.Key)] = true
  53. break
  54. }
  55. }
  56. item.Value = randByteSlice(rand, 0, qmaxvsize)
  57. }
  58. return reflect.ValueOf(items)
  59. }
  60. type revtestdata []testdataitem
  61. func (t revtestdata) Len() int { return len(t) }
  62. func (t revtestdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
  63. func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }
  64. type testdataitem struct {
  65. Key []byte
  66. Value []byte
  67. }
  68. func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {
  69. n := rand.Intn(maxSize-minSize) + minSize
  70. b := make([]byte, n)
  71. for i := 0; i < n; i++ {
  72. b[i] = byte(rand.Intn(255))
  73. }
  74. return b
  75. }