54 lines
787 B
Go
54 lines
787 B
Go
package iterator
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"iter"
|
|
"testing"
|
|
)
|
|
|
|
func TestSet(t *testing.T) {
|
|
s := NewOrderedSet[string]()
|
|
list := []string{
|
|
"fantasticbin",
|
|
"phper",
|
|
"gopher",
|
|
"javaer",
|
|
}
|
|
s.FromSlice(list)
|
|
var pull, push, filter []string
|
|
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)
|
|
}
|
|
|
|
assert.Equal(t, expected, push)
|
|
assert.Equal(t, expected, pull)
|
|
assert.Equal(t, expected, filter)
|
|
}
|