Skip to content

Commit 0e30159

Browse files
committed
Add fuzzing
1 parent 993e322 commit 0e30159

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

unpack_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,58 @@ func TestUnpackInt64(t *testing.T) {
7979
}
8080
}
8181

82+
func FuzzUnpackUint64(f *testing.F) {
83+
// Add seed corpus
84+
f.Add(uint(10), uint(3), int64(6))
85+
f.Add(uint(20), uint(8), int64(0))
86+
f.Add(uint(30), uint(23), int64(-300))
87+
88+
f.Fuzz(func(t *testing.T, size uint, bitWidth uint, seed int64) {
89+
src := make([]int64, size)
90+
gen := rand.New(rand.NewSource(seed))
91+
bitMask := int64(bitWidth<<1) - 1
92+
for i := range src {
93+
src[i] = gen.Int63() & bitMask
94+
}
95+
96+
packed := make([]byte, size*8+bitpack.PaddingInt64)
97+
bitpack.PackInt64(packed, src[:], bitWidth)
98+
99+
unpacked := make([]int64, size)
100+
bitpack.UnpackInt64(unpacked[:], packed[:], bitWidth)
101+
102+
if !slices.Equal(unpacked, src) {
103+
t.Fatalf("Roundtrip failed: got %v, want %v", unpacked, src)
104+
}
105+
})
106+
}
107+
108+
func FuzzUnpackUint32(f *testing.F) {
109+
// Add seed corpus
110+
f.Add(uint(10), uint(3), int64(6))
111+
f.Add(uint(20), uint(8), int64(0))
112+
f.Add(uint(30), uint(23), int64(-300))
113+
114+
f.Fuzz(func(t *testing.T, size uint, bitWidth uint, seed int64) {
115+
src := make([]int32, size)
116+
gen := rand.New(rand.NewSource(seed))
117+
bitMask := int32(bitWidth<<1) - 1
118+
for i := range src {
119+
src[i] = gen.Int31() & bitMask
120+
}
121+
122+
packed := make([]byte, size*8+bitpack.PaddingInt64)
123+
bitpack.PackInt32(packed, src[:], bitWidth)
124+
125+
unpacked := make([]int32, size)
126+
bitpack.UnpackInt32(unpacked[:], packed[:], bitWidth)
127+
128+
if !slices.Equal(unpacked, src) {
129+
t.Fatalf("Roundtrip failed: got %v, want %v", unpacked, src)
130+
}
131+
})
132+
}
133+
82134
func BenchmarkUnpackInt32(b *testing.B) {
83135
for bitWidth := uint(1); bitWidth <= 32; bitWidth++ {
84136
block := [blockSize]int32{}

0 commit comments

Comments
 (0)