2021-11-23 12:41:55 +00:00
|
|
|
package subnetid_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
subnetid "github.com/TrueCloudLab/frostfs-sdk-go/subnet/id"
|
|
|
|
subnetidtest "github.com/TrueCloudLab/frostfs-sdk-go/subnet/id/test"
|
2021-11-23 12:41:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsZero(t *testing.T) {
|
|
|
|
var id subnetid.ID
|
|
|
|
|
|
|
|
require.True(t, subnetid.IsZero(id))
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(13)
|
2021-11-23 12:41:55 +00:00
|
|
|
require.False(t, subnetid.IsZero(id))
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(0)
|
2021-11-23 12:41:55 +00:00
|
|
|
require.True(t, subnetid.IsZero(id))
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
func TestID_ReadFromV2(t *testing.T) {
|
2021-11-23 12:41:55 +00:00
|
|
|
const num = 13
|
|
|
|
|
|
|
|
var id1 subnetid.ID
|
2022-06-15 07:12:35 +00:00
|
|
|
id1.SetNumeric(num)
|
2021-11-23 12:41:55 +00:00
|
|
|
|
|
|
|
var idv2 refs.SubnetID
|
|
|
|
idv2.SetValue(num)
|
|
|
|
|
|
|
|
var id2 subnetid.ID
|
2022-06-15 07:12:35 +00:00
|
|
|
require.NoError(t, id2.ReadFromV2(idv2))
|
2021-11-23 12:41:55 +00:00
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
require.True(t, id1.Equals(id2))
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_WriteToV2(t *testing.T) {
|
|
|
|
const num = 13
|
|
|
|
|
|
|
|
var (
|
|
|
|
id subnetid.ID
|
|
|
|
idv2 refs.SubnetID
|
|
|
|
)
|
|
|
|
|
|
|
|
id.WriteToV2(&idv2)
|
|
|
|
require.Zero(t, idv2.GetValue())
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(num)
|
2021-11-23 12:41:55 +00:00
|
|
|
|
|
|
|
id.WriteToV2(&idv2)
|
|
|
|
require.EqualValues(t, num, idv2.GetValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_Equals(t *testing.T) {
|
|
|
|
const num = 13
|
|
|
|
|
|
|
|
var id1, id2, idOther, id0 subnetid.ID
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id0.Equals(subnetid.ID{})
|
2021-11-23 12:41:55 +00:00
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id1.SetNumeric(num)
|
|
|
|
id2.SetNumeric(num)
|
|
|
|
idOther.SetNumeric(num + 1)
|
2021-11-23 12:41:55 +00:00
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
require.True(t, id1.Equals(id2))
|
|
|
|
require.False(t, id1.Equals(idOther))
|
|
|
|
require.False(t, id2.Equals(idOther))
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubnetIDEncoding(t *testing.T) {
|
2021-11-12 11:19:16 +00:00
|
|
|
id := subnetidtest.ID()
|
2021-11-23 12:41:55 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
var id2 subnetid.ID
|
2022-06-15 07:12:35 +00:00
|
|
|
require.NoError(t, id2.Unmarshal(id.Marshal()))
|
2021-11-23 12:41:55 +00:00
|
|
|
|
|
|
|
require.True(t, id2.Equals(id))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("text", func(t *testing.T) {
|
|
|
|
var id2 subnetid.ID
|
2022-06-15 07:12:35 +00:00
|
|
|
require.NoError(t, id2.DecodeString(id.EncodeToString()))
|
2021-11-23 12:41:55 +00:00
|
|
|
|
|
|
|
require.True(t, id2.Equals(id))
|
|
|
|
})
|
|
|
|
}
|
2021-11-23 18:17:58 +00:00
|
|
|
|
|
|
|
func TestMakeZero(t *testing.T) {
|
|
|
|
var id subnetid.ID
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(13)
|
2021-11-23 18:17:58 +00:00
|
|
|
|
|
|
|
require.False(t, subnetid.IsZero(id))
|
|
|
|
|
|
|
|
subnetid.MakeZero(&id)
|
|
|
|
|
|
|
|
require.True(t, subnetid.IsZero(id))
|
2022-06-15 07:12:35 +00:00
|
|
|
require.Equal(t, subnetid.ID{}, id)
|
2021-11-23 18:17:58 +00:00
|
|
|
}
|