2021-11-18 15:13:17 +00:00
|
|
|
package refs_test
|
|
|
|
|
|
|
|
import (
|
2021-11-22 09:34:09 +00:00
|
|
|
"math"
|
2021-11-18 15:16:16 +00:00
|
|
|
"strconv"
|
2021-11-18 15:13:17 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestZeroSubnet(t *testing.T) {
|
|
|
|
id := new(refs.SubnetID)
|
|
|
|
|
|
|
|
require.True(t, refs.IsZeroSubnet(id))
|
|
|
|
|
|
|
|
id.SetValue(1)
|
|
|
|
require.False(t, refs.IsZeroSubnet(id))
|
|
|
|
|
|
|
|
refs.MakeZeroSubnet(id)
|
|
|
|
require.True(t, refs.IsZeroSubnet(id))
|
|
|
|
}
|
2021-11-18 15:16:16 +00:00
|
|
|
|
|
|
|
func TestSubnetID_MarshalText(t *testing.T) {
|
|
|
|
var id refs.SubnetID
|
|
|
|
|
|
|
|
const val = 15
|
|
|
|
|
|
|
|
id.SetValue(val)
|
|
|
|
|
|
|
|
txt, err := id.MarshalText()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := strconv.ParseUint(string(txt), 10, 32)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, val, res)
|
2021-11-22 10:22:14 +00:00
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var id *refs.SubnetID
|
|
|
|
|
|
|
|
txt, err := id.MarshalText()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := strconv.ParseUint(string(txt), 10, 32)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Zero(t, res)
|
|
|
|
})
|
2021-11-18 15:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubnetID_UnmarshalText(t *testing.T) {
|
|
|
|
const val = 15
|
|
|
|
|
|
|
|
str := strconv.FormatUint(val, 10)
|
|
|
|
|
|
|
|
var id refs.SubnetID
|
|
|
|
|
|
|
|
err := id.UnmarshalText([]byte(str))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, val, id.GetValue())
|
2021-11-22 09:34:09 +00:00
|
|
|
|
|
|
|
t.Run("uint32 overflow", func(t *testing.T) {
|
|
|
|
txt := strconv.FormatUint(math.MaxUint32+1, 10)
|
|
|
|
|
|
|
|
var id refs.SubnetID
|
|
|
|
|
|
|
|
err := id.UnmarshalText([]byte(txt))
|
|
|
|
require.ErrorIs(t, err, strconv.ErrRange)
|
|
|
|
})
|
2021-11-18 15:16:16 +00:00
|
|
|
}
|