Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils/bag: print generic type for bag elements #1507

Merged
merged 9 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions snow/consensus/snowman/poll/early_term_no_traversal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func TestEarlyTermNoTraversalString(t *testing.T) {

poll.Vote(vdr1, vtxID)

expected := `waiting on Bag: (Size = 1)
expected := `waiting on Bag[ids.NodeID]: (Size = 1)
NodeID-BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp: 1
received Bag: (Size = 1)
received Bag[ids.ID]: (Size = 1)
SYXsAycDPUu4z2ZksJD5fh5nTDcH3vCFHnpcVye5XuJ2jArg: 1`
if result := poll.String(); expected != result {
t.Fatalf("Poll should have returned %s but returned %s", expected, result)
Expand Down
4 changes: 2 additions & 2 deletions snow/consensus/snowman/poll/no_early_term_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestNoEarlyTermString(t *testing.T) {

poll.Vote(vdr1, vtxID)

expected := `waiting on Bag: (Size = 1)
expected := `waiting on Bag[ids.NodeID]: (Size = 1)
NodeID-BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp: 1
received Bag: (Size = 1)
received Bag[ids.ID]: (Size = 1)
SYXsAycDPUu4z2ZksJD5fh5nTDcH3vCFHnpcVye5XuJ2jArg: 1`
if result := poll.String(); expected != result {
t.Fatalf("Poll should have returned %s but returned %s", expected, result)
Expand Down
4 changes: 2 additions & 2 deletions snow/consensus/snowman/poll/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func TestSetString(t *testing.T) {

expected := `current polls: (Size = 1)
RequestID 0:
waiting on Bag: (Size = 1)
waiting on Bag[ids.NodeID]: (Size = 1)
NodeID-6HgC8KRBEhXYbF4riJyJFLSHt37UNuRt: 1
received Bag: (Size = 0)`
received Bag[ids.ID]: (Size = 0)`
if !s.Add(0, vdrs) {
t.Fatalf("Should have been able to add a new poll")
} else if str := s.String(); expected != str {
Expand Down
5 changes: 3 additions & 2 deletions utils/bag/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/set"
)

Expand Down Expand Up @@ -154,10 +155,10 @@ func (b *Bag[T]) Remove(elt T) {
b.size -= count
}

func (b *Bag[_]) PrefixedString(prefix string) string {
func (b *Bag[T]) PrefixedString(prefix string) string {
sb := strings.Builder{}

sb.WriteString(fmt.Sprintf("Bag: (Size = %d)", b.Len()))
sb.WriteString(fmt.Sprintf("Bag[%T]: (Size = %d)", utils.Zero[T](), b.Len()))
for elt, count := range b.counts {
sb.WriteString(fmt.Sprintf("\n%s %v: %d", prefix, elt, count))
}
Expand Down
2 changes: 1 addition & 1 deletion utils/bag/bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestBagString(t *testing.T) {

bag.AddCount(elt0, 1337)

expected := "Bag: (Size = 1337)\n" +
expected := "Bag[int]: (Size = 1337)\n" +
" 123: 1337"

require.Equal(t, expected, bag.String())
Expand Down
5 changes: 3 additions & 2 deletions utils/bag/unique_bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/set"
)

Expand Down Expand Up @@ -93,10 +94,10 @@ func (b *UniqueBag[T]) Bag(threshold int) Bag[T] {
return bag
}

func (b *UniqueBag[_]) PrefixedString(prefix string) string {
func (b *UniqueBag[T]) PrefixedString(prefix string) string {
sb := strings.Builder{}

sb.WriteString(fmt.Sprintf("UniqueBag: (Size = %d)", len(*b)))
sb.WriteString(fmt.Sprintf("UniqueBag[%T]: (Size = %d)", utils.Zero[T](), len(*b)))
for key, set := range *b {
sb.WriteString(fmt.Sprintf("\n%s %v: %s", prefix, key, set))
}
Expand Down