github.com/bhojpur/cache@v0.0.4/pkg/memory/quick_test.go (about)

     1  package memory_test
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"bytes"
    25  	"flag"
    26  	"fmt"
    27  	"math/rand"
    28  	"os"
    29  	"reflect"
    30  	"testing/quick"
    31  	"time"
    32  )
    33  
    34  // testing/quick defaults to 5 iterations and a random seed.
    35  // You can override these settings from the command line:
    36  //
    37  //   -quick.count     The number of iterations to perform.
    38  //   -quick.seed      The seed to use for randomizing.
    39  //   -quick.maxitems  The maximum number of items to insert into a DB.
    40  //   -quick.maxksize  The maximum size of a key.
    41  //   -quick.maxvsize  The maximum size of a value.
    42  //
    43  
    44  var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int
    45  
    46  func init() {
    47  	flag.IntVar(&qcount, "quick.count", 5, "")
    48  	flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "")
    49  	flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "")
    50  	flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "")
    51  	flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "")
    52  	flag.Parse()
    53  	fmt.Fprintln(os.Stderr, "seed:", qseed)
    54  	fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize)
    55  }
    56  
    57  func qconfig() *quick.Config {
    58  	return &quick.Config{
    59  		MaxCount: qcount,
    60  		Rand:     rand.New(rand.NewSource(int64(qseed))),
    61  	}
    62  }
    63  
    64  type testdata []testdataitem
    65  
    66  func (t testdata) Len() int           { return len(t) }
    67  func (t testdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }
    68  func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }
    69  
    70  func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {
    71  	n := rand.Intn(qmaxitems-1) + 1
    72  	items := make(testdata, n)
    73  	used := make(map[string]bool)
    74  	for i := 0; i < n; i++ {
    75  		item := &items[i]
    76  		// Ensure that keys are unique by looping until we find one that we have not already used.
    77  		for {
    78  			item.Key = randByteSlice(rand, 1, qmaxksize)
    79  			if !used[string(item.Key)] {
    80  				used[string(item.Key)] = true
    81  				break
    82  			}
    83  		}
    84  		item.Value = randByteSlice(rand, 0, qmaxvsize)
    85  	}
    86  	return reflect.ValueOf(items)
    87  }
    88  
    89  type revtestdata []testdataitem
    90  
    91  func (t revtestdata) Len() int           { return len(t) }
    92  func (t revtestdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }
    93  func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }
    94  
    95  type testdataitem struct {
    96  	Key   []byte
    97  	Value []byte
    98  }
    99  
   100  func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {
   101  	n := rand.Intn(maxSize-minSize) + minSize
   102  	b := make([]byte, n)
   103  	for i := 0; i < n; i++ {
   104  		b[i] = byte(rand.Intn(255))
   105  	}
   106  	return b
   107  }