From 755c01264e54b951b2a73936c37d762b4fac322e Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 23 Nov 2021 21:17:58 +0300 Subject: [PATCH] [#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 --- subnet/id/id.go | 7 +++++++ subnet/id/id_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/subnet/id/id.go b/subnet/id/id.go index 3ae4a86..d1f530c 100644 --- a/subnet/id/id.go +++ b/subnet/id/id.go @@ -87,3 +87,10 @@ func (x *ID) SetNumber(num uint32) { func IsZero(id ID) bool { 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) +} diff --git a/subnet/id/id_test.go b/subnet/id/id_test.go index 21e0f12..f15e7f8 100644 --- a/subnet/id/id_test.go +++ b/subnet/id/id_test.go @@ -92,3 +92,15 @@ func TestSubnetIDEncoding(t *testing.T) { 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)) +}