mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 03:38:35 +00:00
1b83dc2476
Mostly it's about Go 1.22+ syntax with ranging over integers, but it also prefers ranging over slices where possible (it makes code a little better to read). Notice that we have a number of dangerous loops where slices are mutated during loop execution, many of these can't be converted since we need proper length evalutation at every iteration. Signed-off-by: Roman Khimov <roman@nspcc.ru>
49 lines
1,000 B
Go
49 lines
1,000 B
Go
package bigint
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func FuzzFromBytes(f *testing.F) {
|
|
for _, tc := range testCases {
|
|
f.Add(tc.buf)
|
|
}
|
|
for range 50 {
|
|
for j := 1; j < MaxBytesLen; j++ {
|
|
b := make([]byte, j)
|
|
_, err := rand.Read(b)
|
|
require.NoError(f, err)
|
|
f.Add(b)
|
|
}
|
|
}
|
|
f.Fuzz(func(t *testing.T, raw []byte) {
|
|
var bi *big.Int
|
|
require.NotPanics(t, func() { bi = FromBytes(raw) })
|
|
|
|
var actual []byte
|
|
require.NotPanics(t, func() { actual = ToBytes(bi) })
|
|
require.True(t, len(actual) <= len(raw), "actual: %x, raw: %x", actual, raw)
|
|
|
|
require.True(t, bytes.Equal(actual, raw[:len(actual)]), "actual: %x, raw: %x", actual, raw)
|
|
if len(actual) == len(raw) {
|
|
return
|
|
}
|
|
|
|
var b byte
|
|
if bi.Sign() == -1 {
|
|
b = 0xFF
|
|
}
|
|
for i := len(actual); i < len(raw); i++ {
|
|
require.Equal(t, b, raw[i], "invalid prefix")
|
|
}
|
|
|
|
newRaw := ToBytes(bi)
|
|
newBi := FromBytes(newRaw)
|
|
require.Equal(t, bi, newBi)
|
|
})
|
|
}
|