go-study/iterator/set_test.go

49 lines
657 B
Go
Raw Normal View History

2024-11-12 14:52:48 +08:00
package iterator
import (
"github.com/stretchr/testify/assert"
"iter"
"slices"
"testing"
)
func TestSet(t *testing.T) {
s := NewSet[string]()
list := []string{
"fantasticbin",
"phper",
"gopher",
"javaer",
}
var pull, push []string
expected := list[:3]
for v := range slices.Values(list) {
s.Add(v)
}
// 只推
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()
}
}
assert.Equal(t, expected, push)
assert.Equal(t, expected, pull)
}