jldb/mdb/change/encoding_test.go

65 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-10-13 09:43:27 +00:00
package change
import (
"bytes"
crand "crypto/rand"
"math/rand"
"testing"
)
func randChange() Change {
c := Change{
CollectionID: rand.Uint64(),
ItemID: rand.Uint64(),
Store: rand.Float32() < 0.5,
}
if c.Store {
data := make([]byte, 1+rand.Intn(100))
crand.Read(data)
c.Data = data
}
c.WritePageIDs = make([]uint64, rand.Intn(10))
for i := range c.WritePageIDs {
c.WritePageIDs[i] = rand.Uint64()
}
c.ClearPageIDs = make([]uint64, rand.Intn(10))
for i := range c.ClearPageIDs {
c.ClearPageIDs[i] = rand.Uint64()
}
return c
}
func randChangeSlice() []Change {
changes := make([]Change, 1+rand.Intn(10))
for i := range changes {
changes[i] = randChange()
}
return changes
}
func TestWriteRead(t *testing.T) {
in := randChangeSlice()
var out []Change
buf := &bytes.Buffer{}
if err := Write(in, buf); err != nil {
t.Fatal(err)
}
out, err := Read(out, buf)
if err != nil {
t.Fatal(err)
}
if len(in) != len(out) {
t.Fatal(len(in), len(out))
}
for i := range in {
in[i].AssertEqual(t, out[i])
}
}