github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/dict/serialize_test.go (about) 1 package dict 2 3 import ( 4 "bytes" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 ) 9 10 var serialized = []byte("\x01\x00\x02\x03foo\x01\x03bar\x00\x03bar\x00") 11 12 var _ = Describe("dict", func() { 13 Describe("Serialize", func() { 14 It("returns correct results", func() { 15 dict := New() 16 Expect(dict.Put(Value("foo"))).To(Equal(Key{0, 3})) 17 Expect(dict.Put(Value("bar"))).To(Equal(Key{1, 3})) 18 Expect(dict.Put(Value("foobar"))).To(Equal(Key{0, 3, 0, 3})) 19 20 var b bytes.Buffer 21 dict.Serialize(&b) 22 Expect(b.Bytes()).To(Equal(serialized)) 23 }) 24 }) 25 26 Describe("Deserialize", func() { 27 // TODO: add a case with a real dictionary 28 It("returns correct results", func() { 29 r := bytes.NewReader(serialized) 30 d, err := Deserialize(r) 31 Expect(err).ToNot(HaveOccurred()) 32 v1, _ := d.Get(Key{0, 3}) 33 Expect(v1).To(Equal(Value("foo"))) 34 v2, _ := d.Get(Key{1, 3}) 35 Expect(v2).To(Equal(Value("bar"))) 36 v3, _ := d.Get(Key{0, 3, 0, 3}) 37 Expect(v3).To(Equal(Value("foobar"))) 38 }) 39 }) 40 })