github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/encoding/delta/length_byte_array_test.go (about) 1 package delta 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 8 "github.com/vc42/parquet-go/encoding/plain" 9 ) 10 11 func TestDecodeLengthByteArray(t *testing.T) { 12 const characters = "1234567890qwertyuiopasdfghjklzxcvbnm" 13 const numValues = 1000 14 15 src := []byte{} 16 dst := []byte{} 17 lengths := []int32{} 18 19 for i := 0; i < numValues; i++ { 20 n := i % len(characters) 21 src = append(src, characters[:n]...) 22 lengths = append(lengths, int32(n)) 23 } 24 25 dst, err := decodeLengthByteArray(dst, src, lengths) 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 index := 0 31 err = plain.RangeByteArray(dst, func(got []byte) error { 32 want := characters[:index%len(characters)] 33 34 if want != string(got) { 35 return fmt.Errorf("wrong value at index %d: want=%q got=%q", index, want, got) 36 } 37 38 index++ 39 return nil 40 }) 41 if err != nil { 42 t.Fatal(err) 43 } 44 } 45 46 func BenchmarkDecodeLengthByteArray(b *testing.B) { 47 const padding = 64 48 49 for _, maxLen := range []int{0, 10, 20, 100, 1000} { 50 b.Run(fmt.Sprintf("maxLen=%d", maxLen), func(b *testing.B) { 51 lengths := make([]int32, 1000) 52 totalLength := 0 53 prng := rand.New(rand.NewSource(int64(maxLen))) 54 55 if maxLen > 0 { 56 for i := range lengths { 57 lengths[i] = prng.Int31n(int32(maxLen)) + 1 58 totalLength += int(lengths[i]) 59 } 60 } 61 62 size := plain.ByteArrayLengthSize*len(lengths) + totalLength 63 dst := make([]byte, size+padding) 64 src := make([]byte, totalLength) 65 b.SetBytes(int64(size)) 66 b.ResetTimer() 67 68 for i := 0; i < b.N; i++ { 69 dst, _ = decodeLengthByteArray(dst[:0], src, lengths) 70 } 71 }) 72 } 73 }