maps: slightly improve iter tests

Change-Id: I330a06539e36f442470690187df9c3988c12bd50
Reviewed-on: https://go-review.googlesource.com/c/go/+/586855
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Jes Cok 2024-05-21 01:11:18 +08:00 committed by Gopher Robot
parent bb88d28bf5
commit 62711d61e5

View file

@ -38,13 +38,13 @@ func TestKeys(t *testing.T) {
want = append(want, i)
}
var got1 []int
var got []int
for k := range Keys(m) {
got1 = append(got1, k)
got = append(got, k)
}
slices.Sort(got1)
if !slices.Equal(got1, want) {
t.Errorf("Keys(%v) = %v, want %v", m, got1, want)
slices.Sort(got)
if !slices.Equal(got, want) {
t.Errorf("Keys(%v) = %v, want %v", m, got, want)
}
}
}
@ -58,50 +58,46 @@ func TestValues(t *testing.T) {
want = append(want, i)
}
var got1 []int
var got []int
for v := range Values(m) {
got1 = append(got1, v)
got = append(got, v)
}
slices.Sort(got1)
if !slices.Equal(got1, want) {
t.Errorf("Values(%v) = %v, want %v", m, got1, want)
slices.Sort(got)
if !slices.Equal(got, want) {
t.Errorf("Values(%v) = %v, want %v", m, got, want)
}
}
}
func testSeq(yield func(int, int) bool) {
for i := 0; i < 10; i += 2 {
if !yield(i, i+1) {
return
}
}
}
var testSeqResult = map[int]int{
0: 1,
2: 3,
4: 5,
6: 7,
8: 9,
}
func TestInsert(t *testing.T) {
got := map[int]int{
1: 1,
2: 1,
}
Insert(got, testSeq)
Insert(got, func(yield func(int, int) bool) {
for i := 0; i < 10; i += 2 {
if !yield(i, i+1) {
return
}
}
})
want := map[int]int{
1: 1,
2: 1,
}
for i, v := range testSeqResult {
for i, v := range map[int]int{
0: 1,
2: 3,
4: 5,
6: 7,
8: 9,
} {
want[i] = v
}
if !Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
t.Errorf("Insert got: %v, want: %v", got, want)
}
}
@ -115,6 +111,6 @@ func TestCollect(t *testing.T) {
}
got := Collect(All(m))
if !Equal(got, m) {
t.Errorf("got %v, want %v", got, m)
t.Errorf("Collect got: %v, want: %v", got, m)
}
}