Skip to content

Commit

Permalink
runtime: add a test for randomised map iteration order.
Browse files Browse the repository at this point in the history
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 golang#6719.

R=khr, bradfitz
CC=golang-codereviews
https://golang.org/cl/47010043
  • Loading branch information
dsymonds committed Jan 2, 2014
1 parent 3a765be commit d4c66a3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/pkg/runtime/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit d4c66a3

Please sign in to comment.