go-study/iterator/set_test.go

54 lines
787 B
Go
Raw Normal View History

2024-11-12 14:52:48 +08:00
package iterator
import (
"github.com/stretchr/testify/assert"
"iter"
"testing"
)
func TestSet(t *testing.T) {
s := NewOrderedSet[string]()
2024-11-12 14:52:48 +08:00
list := []string{
"fantasticbin",
"phper",
"gopher",
"javaer",
}
s.FromSlice(list)
var pull, push, filter []string
2024-11-12 14:52:48 +08:00
expected := list[:3]
// 只推
for v := range s.All() {
if v == "javaer" {
break
}
push = append(push, v)
}
// 既推又拉
next, stop := iter.Pull(s.All())
for {
v, ok := next()
if !ok {
break
}
pull = append(pull, v)
if v == "gopher" {
stop()
}
}
// 筛选
for v := range s.FilterMap(func(v string) bool {
return v != "javaer"
}) {
filter = append(filter, v)
}
2024-11-12 14:52:48 +08:00
assert.Equal(t, expected, push)
assert.Equal(t, expected, pull)
assert.Equal(t, expected, filter)
2024-11-12 14:52:48 +08:00
}