[#79] subnet/id: Add function which resets ID to zero subnet

Add `MakeZero` function which makes `ID` instance to refer to zero
subnet.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-11-23 21:17:58 +03:00 committed by LeL
parent e7ac7f339e
commit 755c01264e
2 changed files with 19 additions and 0 deletions

View file

@ -87,3 +87,10 @@ func (x *ID) SetNumber(num uint32) {
func IsZero(id ID) bool { func IsZero(id ID) bool {
return id.Equals(nil) return id.Equals(nil)
} }
// MakeZero makes ID to refer to zero subnet. Arg must not be nil (it is already zero).
//
// Makes no sense to call on zero value (e.g. declared using var).
func MakeZero(id *ID) {
id.SetNumber(0)
}

View file

@ -92,3 +92,15 @@ func TestSubnetIDEncoding(t *testing.T) {
require.True(t, id2.Equals(id)) require.True(t, id2.Equals(id))
}) })
} }
func TestMakeZero(t *testing.T) {
var id subnetid.ID
id.SetNumber(13)
require.False(t, subnetid.IsZero(id))
subnetid.MakeZero(&id)
require.True(t, subnetid.IsZero(id))
}