From 832cc7105e6040bbe9a9562467de244f71386230 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Fri, 18 Oct 2024 22:08:14 -0300 Subject: [PATCH] add tests --- network/records/subnets_test.go | 78 ++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/network/records/subnets_test.go b/network/records/subnets_test.go index 25a8de143d..72008414ab 100644 --- a/network/records/subnets_test.go +++ b/network/records/subnets_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/libp2p/go-libp2p/core/crypto" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/ssvlabs/ssv/network/commons" @@ -87,7 +88,7 @@ func TestSubnetsParsing(t *testing.T) { } } -func TestSharedSubnets(t *testing.T) { +func TestSharedSubnetsFromString(t *testing.T) { s1, err := Subnets{}.FromString("0xffffffffffffffffffffffffffffffff") require.NoError(t, err) s2, err := Subnets{}.FromString("0x57b080fffd743d9878dc41a184ab160a") @@ -103,6 +104,81 @@ func TestSharedSubnets(t *testing.T) { require.Equal(t, expectedShared, shared) } +func TestSharedSubnets(t *testing.T) { + // Test cases + tests := []struct { + name string + a []byte + b []byte + maxLen int + expected []int + }{ + { + name: "All subnets shared", + a: []byte{1, 1, 1}, + b: []byte{1, 1, 1}, + maxLen: 0, + expected: []int{0, 1, 2}, + }, + { + name: "Some subnets shared", + a: []byte{1, 0, 1}, + b: []byte{1, 1, 0}, + maxLen: 0, + expected: []int{0}, + }, + { + name: "Different lengths (b shorter)", + a: []byte{1, 1, 1, 1}, + b: []byte{1, 1}, + maxLen: 0, + expected: []int{0, 1}, // Should only match within length of b + }, + { + name: "Different lengths (a shorter)", + a: []byte{1, 1}, + b: []byte{1, 1, 1, 1}, + maxLen: 0, + expected: []int{0, 1}, // Should only match within length of a + }, + { + name: "MaxLen constraint", + a: []byte{1, 1, 1, 1}, + b: []byte{1, 1, 1, 1}, + maxLen: 2, + expected: []int{0, 1}, // Limit the results to maxLen 2 + }, + { + name: "No shared subnets", + a: []byte{1, 0, 1}, + b: []byte{0, 1, 0}, + maxLen: 0, + expected: nil, // No shared subnets + }, + { + name: "Empty subnet list", + a: []byte{}, + b: []byte{}, + maxLen: 0, + expected: nil, // Empty input should return empty result + }, + { + name: "One empty subnet list", + a: []byte{1, 0, 1}, + b: []byte{}, + maxLen: 0, + expected: nil, // If one list is empty, no subnets are shared + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := SharedSubnets(test.a, test.b, test.maxLen) + assert.Equal(t, test.expected, result) + }) + } +} + func TestDiffSubnets(t *testing.T) { s1, err := Subnets{}.FromString("0xffffffffffffffffffffffffffffffff") require.NoError(t, err)