-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix differentiating nil and empty subvalues
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package node | ||
|
||
import "bytes" | ||
|
||
// SubValueEqual returns true if the node subvalue is equal to the | ||
// subvalue given as argument. In particular, it returns false | ||
// if one subvalue is nil and the other subvalue is the empty slice. | ||
func (n Node) SubValueEqual(subValue []byte) (equal bool) { | ||
if len(subValue) == 0 && len(n.SubValue) == 0 { | ||
return (subValue == nil && n.SubValue == nil) || | ||
(subValue != nil && n.SubValue != nil) | ||
} | ||
return bytes.Equal(n.SubValue, subValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package node | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Node_SubValueEqual(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
node Node | ||
subValue []byte | ||
equal bool | ||
}{ | ||
"nil node subvalue and nil subvalue": { | ||
equal: true, | ||
}, | ||
"empty node subvalue and empty subvalue": { | ||
node: Node{SubValue: []byte{}}, | ||
subValue: []byte{}, | ||
equal: true, | ||
}, | ||
"nil node subvalue and empty subvalue": { | ||
subValue: []byte{}, | ||
}, | ||
"empty node subvalue and nil subvalue": { | ||
node: Node{SubValue: []byte{}}, | ||
}, | ||
"equal non empty values": { | ||
node: Node{SubValue: []byte{1, 2}}, | ||
subValue: []byte{1, 2}, | ||
equal: true, | ||
}, | ||
"not equal non empty values": { | ||
node: Node{SubValue: []byte{1, 2}}, | ||
subValue: []byte{1, 3}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
node := testCase.node | ||
|
||
equal := node.SubValueEqual(testCase.subValue) | ||
|
||
assert.Equal(t, testCase.equal, equal) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters