2024-11-12 14:52:48 +08:00
|
|
|
package iterator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"iter"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSet(t *testing.T) {
|
2024-11-13 14:26:19 +08:00
|
|
|
s := NewOrderedSet[string]()
|
2024-11-12 14:52:48 +08:00
|
|
|
list := []string{
|
|
|
|
"fantasticbin",
|
|
|
|
"phper",
|
|
|
|
"gopher",
|
|
|
|
"javaer",
|
|
|
|
}
|
2024-11-13 15:21:50 +08:00
|
|
|
s.FromSlice(list)
|
2024-11-12 15:34:44 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-12 15:34:44 +08:00
|
|
|
// 筛选
|
|
|
|
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)
|
2024-11-12 15:34:44 +08:00
|
|
|
assert.Equal(t, expected, filter)
|
2024-11-12 14:52:48 +08:00
|
|
|
}
|