github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/dict/dict_test.go (about)

     1  package dict
     2  
     3  import (
     4  	"math/rand"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  // TODO: DRY
    11  func randStr() []byte {
    12  	buf := make([]byte, 10)
    13  	for i := 0; i < 10; i++ {
    14  		buf[i] = byte(97) + byte(rand.Uint32()%10)
    15  	}
    16  	return buf
    17  }
    18  
    19  var _ = Describe("dict package", func() {
    20  	Context("Put / Get", func() {
    21  		Context("Puts same value twice", func() {
    22  			It("Get returns things Put puts in", func() {
    23  				dict := New()
    24  				k1 := dict.Put([]byte("foo"))
    25  				k2 := dict.Put([]byte("aff"))
    26  				k3 := dict.Put([]byte("aff"))
    27  				v1, _ := dict.Get(k1)
    28  				Expect(v1).To(BeEquivalentTo([]byte("foo")))
    29  				v2, _ := dict.Get(k2)
    30  				Expect(v2).To(BeEquivalentTo([]byte("aff")))
    31  				v3, _ := dict.Get(k3)
    32  				Expect(v3).To(BeEquivalentTo([]byte("aff")))
    33  			})
    34  		})
    35  
    36  		Context("Random strings", func() {
    37  			It("Get returns things Put puts in", func() {
    38  				dict := New()
    39  				insertedData := [][][]byte{}
    40  				for i := 0; i < 10000; i++ {
    41  					expected := randStr()
    42  					key := dict.Put(expected)
    43  					insertedData = append(insertedData, [][]byte{key, expected})
    44  					actual, ok := dict.Get(key)
    45  					Expect(ok).To(BeTrue())
    46  					Expect(actual).To(BeEquivalentTo(expected))
    47  				}
    48  
    49  				for _, pair := range insertedData {
    50  					key := pair[0]
    51  					expected := pair[1]
    52  					actual, ok := dict.Get(key)
    53  					Expect(ok).To(BeTrue())
    54  					Expect(actual).To(BeEquivalentTo(expected))
    55  				}
    56  			})
    57  		})
    58  	})
    59  })