]> Cypherpunks repositories - gostls13.git/commitdiff
maps: slightly improve iter tests
authorJes Cok <xigua67damn@gmail.com>
Mon, 20 May 2024 17:11:18 +0000 (01:11 +0800)
committerGopher Robot <gobot@golang.org>
Thu, 23 May 2024 03:44:19 +0000 (03:44 +0000)
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>
src/maps/iter_test.go

index 125a0247262a61942bb4c2eec3b248023c27ce20..64ea42aeaf6b7b4ab48995ccdbfaeee57e9f86a9 100644 (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)
        }
 }