Skip to content

Commit

Permalink
[#79] subnet/id: Add function which resets ID to zero subnet
Browse files Browse the repository at this point in the history
Add `MakeZero` function which makes `ID` instance to refer to zero
subnet.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich authored and cthulhu-rider committed Nov 25, 2021
1 parent e7ac7f3 commit 755c012
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions subnet/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions subnet/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 755c012

Please sign in to comment.