Daily bump.
[official-gcc.git] / libgo / go / runtime / map_test.go
blobc53066aea6ade47267d26068ac0cda964cc39fc7
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package runtime_test
7 import (
8 "fmt"
9 "math"
10 "reflect"
11 "runtime"
12 "sort"
13 "strings"
14 "sync"
15 "testing"
18 // negative zero is a good test because:
19 // 1) 0 and -0 are equal, yet have distinct representations.
20 // 2) 0 is represented as all zeros, -0 isn't.
21 // I'm not sure the language spec actually requires this behavior,
22 // but it's what the current map implementation does.
23 func TestNegativeZero(t *testing.T) {
24 m := make(map[float64]bool, 0)
26 m[+0.0] = true
27 m[math.Copysign(0.0, -1.0)] = true // should overwrite +0 entry
29 if len(m) != 1 {
30 t.Error("length wrong")
33 /* gccgo fails this test; this is not required by the spec.
34 for k := range m {
35 if math.Copysign(1.0, k) > 0 {
36 t.Error("wrong sign")
41 m = make(map[float64]bool, 0)
42 m[math.Copysign(0.0, -1.0)] = true
43 m[+0.0] = true // should overwrite -0.0 entry
45 if len(m) != 1 {
46 t.Error("length wrong")
49 /* gccgo fails this test; this is not required by the spec.
50 for k := range m {
51 if math.Copysign(1.0, k) < 0 {
52 t.Error("wrong sign")
58 // nan is a good test because nan != nan, and nan has
59 // a randomized hash value.
60 func TestNan(t *testing.T) {
61 m := make(map[float64]int, 0)
62 nan := math.NaN()
63 m[nan] = 1
64 m[nan] = 2
65 m[nan] = 4
66 if len(m) != 3 {
67 t.Error("length wrong")
69 s := 0
70 for k, v := range m {
71 if k == k {
72 t.Error("nan disappeared")
74 if (v & (v - 1)) != 0 {
75 t.Error("value wrong")
77 s |= v
79 if s != 7 {
80 t.Error("values wrong")
84 // Maps aren't actually copied on assignment.
85 func TestAlias(t *testing.T) {
86 m := make(map[int]int, 0)
87 m[0] = 5
88 n := m
89 n[0] = 6
90 if m[0] != 6 {
91 t.Error("alias didn't work")
95 func TestGrowWithNaN(t *testing.T) {
96 t.Skip("fails with gccgo")
97 m := make(map[float64]int, 4)
98 nan := math.NaN()
99 m[nan] = 1
100 m[nan] = 2
101 m[nan] = 4
102 cnt := 0
103 s := 0
104 growflag := true
105 for k, v := range m {
106 if growflag {
107 // force a hashtable resize
108 for i := 0; i < 100; i++ {
109 m[float64(i)] = i
111 growflag = false
113 if k != k {
114 cnt++
115 s |= v
118 t.Log("cnt:", cnt, "s:", s)
119 if cnt != 3 {
120 t.Error("NaN keys lost during grow")
122 if s != 7 {
123 t.Error("NaN values lost during grow")
127 type FloatInt struct {
128 x float64
129 y int
132 func TestGrowWithNegativeZero(t *testing.T) {
133 t.Skip("fails with gccgo")
134 negzero := math.Copysign(0.0, -1.0)
135 m := make(map[FloatInt]int, 4)
136 m[FloatInt{0.0, 0}] = 1
137 m[FloatInt{0.0, 1}] = 2
138 m[FloatInt{0.0, 2}] = 4
139 m[FloatInt{0.0, 3}] = 8
140 growflag := true
141 s := 0
142 cnt := 0
143 negcnt := 0
144 // The first iteration should return the +0 key.
145 // The subsequent iterations should return the -0 key.
146 // I'm not really sure this is required by the spec,
147 // but it makes sense.
148 // TODO: are we allowed to get the first entry returned again???
149 for k, v := range m {
150 if v == 0 {
151 continue
152 } // ignore entries added to grow table
153 cnt++
154 if math.Copysign(1.0, k.x) < 0 {
155 if v&16 == 0 {
156 t.Error("key/value not updated together 1")
158 negcnt++
159 s |= v & 15
160 } else {
161 if v&16 == 16 {
162 t.Error("key/value not updated together 2", k, v)
164 s |= v
166 if growflag {
167 // force a hashtable resize
168 for i := 0; i < 100; i++ {
169 m[FloatInt{3.0, i}] = 0
171 // then change all the entries
172 // to negative zero
173 m[FloatInt{negzero, 0}] = 1 | 16
174 m[FloatInt{negzero, 1}] = 2 | 16
175 m[FloatInt{negzero, 2}] = 4 | 16
176 m[FloatInt{negzero, 3}] = 8 | 16
177 growflag = false
180 if s != 15 {
181 t.Error("entry missing", s)
183 if cnt != 4 {
184 t.Error("wrong number of entries returned by iterator", cnt)
186 if negcnt != 3 {
187 t.Error("update to negzero missed by iteration", negcnt)
191 func TestIterGrowAndDelete(t *testing.T) {
192 m := make(map[int]int, 4)
193 for i := 0; i < 100; i++ {
194 m[i] = i
196 growflag := true
197 for k := range m {
198 if growflag {
199 // grow the table
200 for i := 100; i < 1000; i++ {
201 m[i] = i
203 // delete all odd keys
204 for i := 1; i < 1000; i += 2 {
205 delete(m, i)
207 growflag = false
208 } else {
209 if k&1 == 1 {
210 t.Error("odd value returned")
216 // make sure old bucket arrays don't get GCd while
217 // an iterator is still using them.
218 func TestIterGrowWithGC(t *testing.T) {
219 m := make(map[int]int, 4)
220 for i := 0; i < 16; i++ {
221 m[i] = i
223 growflag := true
224 bitmask := 0
225 for k := range m {
226 if k < 16 {
227 bitmask |= 1 << uint(k)
229 if growflag {
230 // grow the table
231 for i := 100; i < 1000; i++ {
232 m[i] = i
234 // trigger a gc
235 runtime.GC()
236 growflag = false
239 if bitmask != 1<<16-1 {
240 t.Error("missing key", bitmask)
244 func testConcurrentReadsAfterGrowth(t *testing.T, useReflect bool) {
245 if runtime.GOMAXPROCS(-1) == 1 {
246 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(16))
248 numLoop := 10
249 numGrowStep := 250
250 numReader := 16
251 if testing.Short() {
252 numLoop, numGrowStep = 2, 500
254 for i := 0; i < numLoop; i++ {
255 m := make(map[int]int, 0)
256 for gs := 0; gs < numGrowStep; gs++ {
257 m[gs] = gs
258 var wg sync.WaitGroup
259 wg.Add(numReader * 2)
260 for nr := 0; nr < numReader; nr++ {
261 go func() {
262 defer wg.Done()
263 for _ = range m {
266 go func() {
267 defer wg.Done()
268 for key := 0; key < gs; key++ {
269 _ = m[key]
272 if useReflect {
273 wg.Add(1)
274 go func() {
275 defer wg.Done()
276 mv := reflect.ValueOf(m)
277 keys := mv.MapKeys()
278 for _, k := range keys {
279 mv.MapIndex(k)
284 wg.Wait()
289 func TestConcurrentReadsAfterGrowth(t *testing.T) {
290 testConcurrentReadsAfterGrowth(t, false)
293 func TestConcurrentReadsAfterGrowthReflect(t *testing.T) {
294 testConcurrentReadsAfterGrowth(t, true)
297 func TestBigItems(t *testing.T) {
298 var key [256]string
299 for i := 0; i < 256; i++ {
300 key[i] = "foo"
302 m := make(map[[256]string][256]string, 4)
303 for i := 0; i < 100; i++ {
304 key[37] = fmt.Sprintf("string%02d", i)
305 m[key] = key
307 var keys [100]string
308 var values [100]string
309 i := 0
310 for k, v := range m {
311 keys[i] = k[37]
312 values[i] = v[37]
315 sort.Strings(keys[:])
316 sort.Strings(values[:])
317 for i := 0; i < 100; i++ {
318 if keys[i] != fmt.Sprintf("string%02d", i) {
319 t.Errorf("#%d: missing key: %v", i, keys[i])
321 if values[i] != fmt.Sprintf("string%02d", i) {
322 t.Errorf("#%d: missing value: %v", i, values[i])
327 type empty struct {
330 func TestEmptyKeyAndValue(t *testing.T) {
331 a := make(map[int]empty, 4)
332 b := make(map[empty]int, 4)
333 c := make(map[empty]empty, 4)
334 a[0] = empty{}
335 b[empty{}] = 0
336 b[empty{}] = 1
337 c[empty{}] = empty{}
339 if len(a) != 1 {
340 t.Errorf("empty value insert problem")
342 if b[empty{}] != 1 {
343 t.Errorf("empty key returned wrong value")
347 // Tests a map with a single bucket, with same-lengthed short keys
348 // ("quick keys") as well as long keys.
349 func TestSingleBucketMapStringKeys_DupLen(t *testing.T) {
350 testMapLookups(t, map[string]string{
351 "x": "x1val",
352 "xx": "x2val",
353 "foo": "fooval",
354 "bar": "barval", // same key length as "foo"
355 "xxxx": "x4val",
356 strings.Repeat("x", 128): "longval1",
357 strings.Repeat("y", 128): "longval2",
361 // Tests a map with a single bucket, with all keys having different lengths.
362 func TestSingleBucketMapStringKeys_NoDupLen(t *testing.T) {
363 testMapLookups(t, map[string]string{
364 "x": "x1val",
365 "xx": "x2val",
366 "foo": "fooval",
367 "xxxx": "x4val",
368 "xxxxx": "x5val",
369 "xxxxxx": "x6val",
370 strings.Repeat("x", 128): "longval",
374 func testMapLookups(t *testing.T, m map[string]string) {
375 for k, v := range m {
376 if m[k] != v {
377 t.Fatalf("m[%q] = %q; want %q", k, m[k], v)
382 // Tests whether the iterator returns the right elements when
383 // started in the middle of a grow, when the keys are NaNs.
384 func TestMapNanGrowIterator(t *testing.T) {
385 m := make(map[float64]int)
386 nan := math.NaN()
387 const nBuckets = 16
388 // To fill nBuckets buckets takes LOAD * nBuckets keys.
389 nKeys := int(nBuckets * *runtime.HashLoad)
391 // Get map to full point with nan keys.
392 for i := 0; i < nKeys; i++ {
393 m[nan] = i
395 // Trigger grow
396 m[1.0] = 1
397 delete(m, 1.0)
399 // Run iterator
400 found := make(map[int]struct{})
401 for _, v := range m {
402 if v != -1 {
403 if _, repeat := found[v]; repeat {
404 t.Fatalf("repeat of value %d", v)
406 found[v] = struct{}{}
408 if len(found) == nKeys/2 {
409 // Halfway through iteration, finish grow.
410 for i := 0; i < nBuckets; i++ {
411 delete(m, 1.0)
415 if len(found) != nKeys {
416 t.Fatalf("missing value")