Skip to content

Commit bce0c92

Browse files
authored
utils/bag: print type of bag elements (ava-labs#1507)
1 parent 396f975 commit bce0c92

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

snow/consensus/snowman/poll/early_term_no_traversal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ func TestEarlyTermNoTraversalString(t *testing.T) {
5757

5858
poll.Vote(vdr1, vtxID)
5959

60-
expected := `waiting on Bag: (Size = 1)
60+
expected := `waiting on Bag[ids.NodeID]: (Size = 1)
6161
NodeID-BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp: 1
62-
received Bag: (Size = 1)
62+
received Bag[ids.ID]: (Size = 1)
6363
SYXsAycDPUu4z2ZksJD5fh5nTDcH3vCFHnpcVye5XuJ2jArg: 1`
6464
if result := poll.String(); expected != result {
6565
t.Fatalf("Poll should have returned %s but returned %s", expected, result)

snow/consensus/snowman/poll/no_early_term_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func TestNoEarlyTermString(t *testing.T) {
5353

5454
poll.Vote(vdr1, vtxID)
5555

56-
expected := `waiting on Bag: (Size = 1)
56+
expected := `waiting on Bag[ids.NodeID]: (Size = 1)
5757
NodeID-BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp: 1
58-
received Bag: (Size = 1)
58+
received Bag[ids.ID]: (Size = 1)
5959
SYXsAycDPUu4z2ZksJD5fh5nTDcH3vCFHnpcVye5XuJ2jArg: 1`
6060
if result := poll.String(); expected != result {
6161
t.Fatalf("Poll should have returned %s but returned %s", expected, result)

snow/consensus/snowman/poll/set_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ func TestSetString(t *testing.T) {
316316

317317
expected := `current polls: (Size = 1)
318318
RequestID 0:
319-
waiting on Bag: (Size = 1)
319+
waiting on Bag[ids.NodeID]: (Size = 1)
320320
NodeID-6HgC8KRBEhXYbF4riJyJFLSHt37UNuRt: 1
321-
received Bag: (Size = 0)`
321+
received Bag[ids.ID]: (Size = 0)`
322322
if !s.Add(0, vdrs) {
323323
t.Fatalf("Should have been able to add a new poll")
324324
} else if str := s.String(); expected != str {

utils/bag/bag.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"golang.org/x/exp/maps"
1111

12+
"github.com/ava-labs/avalanchego/utils"
1213
"github.com/ava-labs/avalanchego/utils/set"
1314
)
1415

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

157-
func (b *Bag[_]) PrefixedString(prefix string) string {
158+
func (b *Bag[T]) PrefixedString(prefix string) string {
158159
sb := strings.Builder{}
159160

160-
sb.WriteString(fmt.Sprintf("Bag: (Size = %d)", b.Len()))
161+
sb.WriteString(fmt.Sprintf("Bag[%T]: (Size = %d)", utils.Zero[T](), b.Len()))
161162
for elt, count := range b.counts {
162163
sb.WriteString(fmt.Sprintf("\n%s %v: %d", prefix, elt, count))
163164
}

utils/bag/bag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestBagString(t *testing.T) {
152152

153153
bag.AddCount(elt0, 1337)
154154

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

158158
require.Equal(t, expected, bag.String())

utils/bag/unique_bag.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"golang.org/x/exp/maps"
1111

12+
"github.com/ava-labs/avalanchego/utils"
1213
"github.com/ava-labs/avalanchego/utils/set"
1314
)
1415

@@ -93,10 +94,10 @@ func (b *UniqueBag[T]) Bag(threshold int) Bag[T] {
9394
return bag
9495
}
9596

96-
func (b *UniqueBag[_]) PrefixedString(prefix string) string {
97+
func (b *UniqueBag[T]) PrefixedString(prefix string) string {
9798
sb := strings.Builder{}
9899

99-
sb.WriteString(fmt.Sprintf("UniqueBag: (Size = %d)", len(*b)))
100+
sb.WriteString(fmt.Sprintf("UniqueBag[%T]: (Size = %d)", utils.Zero[T](), len(*b)))
100101
for key, set := range *b {
101102
sb.WriteString(fmt.Sprintf("\n%s %v: %s", prefix, key, set))
102103
}

0 commit comments

Comments
 (0)