From: David Symonds Date: Thu, 2 Jan 2014 23:10:54 +0000 (+1100) Subject: runtime: add a test for randomised map iteration order. X-Git-Tag: go1.3beta1~1070 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d4c66a35baba191b5857960d75fe66d766f6d573;p=gostls13.git runtime: add a test for randomised map iteration order. 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 --- diff --git a/src/pkg/runtime/map_test.go b/src/pkg/runtime/map_test.go index a221cb28cf..f57d1f57c1 100644 --- a/src/pkg/runtime/map_test.go +++ b/src/pkg/runtime/map_test.go @@ -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) + } + } +}