Skip to content

Commit 0e5c155

Browse files
committed
added String method to SetMap
Signed-off-by: Tim Henderson <[email protected]>
1 parent cd0c1de commit 0e5c155

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

set/setmap.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package set
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/timtadh/data-structures/errors"
58
"github.com/timtadh/data-structures/types"
69
)
@@ -13,6 +16,17 @@ func NewSetMap(m types.Map) *SetMap {
1316
return &SetMap{m}
1417
}
1518

19+
func (s *SetMap) String() string {
20+
if s.Size() <= 0 {
21+
return "{}"
22+
}
23+
items := make([]string, 0, s.Size())
24+
for item, next := s.Items()(); next != nil; item, next = next() {
25+
items = append(items, fmt.Sprintf("%v", item))
26+
}
27+
return "{" + strings.Join(items, ", ") + "}"
28+
}
29+
1630
func (s *SetMap) Items() types.KIterator {
1731
return s.Keys()
1832
}

set/sortedset_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAddMarshalUnmarshalHas(x *testing.T) {
4949
t.assert_nil(set.Add(item))
5050
}
5151
for i, item := range items {
52-
t.assert(fmt.Sprintf("!set.Has(item)", i), set.Has(item))
52+
t.assert(fmt.Sprintf("!set.Has(%v) ", i), set.Has(item))
5353
}
5454
marshal, unmarshal := types.IntMarshals()
5555
mset1 := NewMSortedSet(set, marshal, unmarshal)
@@ -59,7 +59,7 @@ func TestAddMarshalUnmarshalHas(x *testing.T) {
5959
t.assert_nil(mset2.UnmarshalBinary(bytes))
6060
set2 := mset2.SortedSet()
6161
for i, item := range items {
62-
t.assert(fmt.Sprintf("!set.Has(item)", i), set2.Has(item))
62+
t.assert(fmt.Sprintf("!set.Has(%v)", i), set2.Has(item))
6363
}
6464
}
6565

@@ -82,7 +82,7 @@ func TestAddHasDeleteRandom(x *testing.T) {
8282
for i := 0; i < 10; i++ {
8383
item, err := set.Random()
8484
t.assert_nil(err)
85-
t.assert(fmt.Sprintf("!set.Has(item)", i), set.Has(item))
85+
t.assert(fmt.Sprintf("!set.Has(%v)", i), set.Has(item))
8686
}
8787
for _, item := range items {
8888
t.assert_nil(set.Delete(item))

0 commit comments

Comments
 (0)