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"
|
|
|
|
|
2022-12-09 11:16:24 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
2021-11-18 15:13:17 +00:00
|
|
|
"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
|
|
|
}
|