]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add a test for randomised map iteration order.
authorDavid Symonds <dsymonds@golang.org>
Thu, 2 Jan 2014 23:10:54 +0000 (10:10 +1100)
committerDavid Symonds <dsymonds@golang.org>
Thu, 2 Jan 2014 23:10:54 +0000 (10:10 +1100)
Technically the spec does not guarantee that the iteration order is random,
but it is a property that we have consciously pursued, and so it seems
right to verify that our implementation does indeed randomise.

Update #6719.

R=khr, bradfitz
CC=golang-codereviews
https://golang.org/cl/47010043

src/pkg/runtime/map_test.go

index a221cb28cf20dc0a8540cb2f3a09dd851f8c984b..f57d1f57c1aefc9bf3cd47b45249937421447071 100644 (file)
@@ -409,3 +409,33 @@ func TestMapNanGrowIterator(t *testing.T) {
                t.Fatalf("missing value")
        }
 }
+
+func TestMapIterOrder(t *testing.T) {
+       // TODO: For issue 6719, add 3 and 7 to this list.
+       for _, n := range [...]int{9, 15} {
+               // Make m be {0: true, 1: true, ..., n-1: true}.
+               m := make(map[int]bool)
+               for i := 0; i < n; i++ {
+                       m[i] = true
+               }
+               // Check that iterating over the map produces at least two different orderings.
+               ord := func() []int {
+                       var s []int
+                       for key := range m {
+                               s = append(s, key)
+                       }
+                       return s
+               }
+               first := ord()
+               ok := false
+               for try := 0; try < 5; try++ {
+                       if !reflect.DeepEqual(first, ord()) {
+                               ok = true
+                               break
+                       }
+               }
+               if !ok {
+                       t.Errorf("Map with n=%d elements had consistent iteration order: %v", n, first)
+               }
+       }
+}