~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/runtime/map_test.go

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
409
409
                t.Fatalf("missing value")
410
410
        }
411
411
}
 
412
 
 
413
func TestMapIterOrder(t *testing.T) {
 
414
        for _, n := range [...]int{3, 7, 9, 15} {
 
415
                // Make m be {0: true, 1: true, ..., n-1: true}.
 
416
                m := make(map[int]bool)
 
417
                for i := 0; i < n; i++ {
 
418
                        m[i] = true
 
419
                }
 
420
                // Check that iterating over the map produces at least two different orderings.
 
421
                ord := func() []int {
 
422
                        var s []int
 
423
                        for key := range m {
 
424
                                s = append(s, key)
 
425
                        }
 
426
                        return s
 
427
                }
 
428
                first := ord()
 
429
                ok := false
 
430
                for try := 0; try < 100; try++ {
 
431
                        if !reflect.DeepEqual(first, ord()) {
 
432
                                ok = true
 
433
                                break
 
434
                        }
 
435
                }
 
436
                if !ok {
 
437
                        t.Errorf("Map with n=%d elements had consistent iteration order: %v", n, first)
 
438
                }
 
439
        }
 
440
}
 
441
 
 
442
func TestMapStringBytesLookup(t *testing.T) {
 
443
        // Use large string keys to avoid small-allocation coalescing,
 
444
        // which can cause AllocsPerRun to report lower counts than it should.
 
445
        m := map[string]int{
 
446
                "1000000000000000000000000000000000000000000000000": 1,
 
447
                "2000000000000000000000000000000000000000000000000": 2,
 
448
        }
 
449
        buf := []byte("1000000000000000000000000000000000000000000000000")
 
450
        if x := m[string(buf)]; x != 1 {
 
451
                t.Errorf(`m[string([]byte("1"))] = %d, want 1`, x)
 
452
        }
 
453
        buf[0] = '2'
 
454
        if x := m[string(buf)]; x != 2 {
 
455
                t.Errorf(`m[string([]byte("2"))] = %d, want 2`, x)
 
456
        }
 
457
 
 
458
        var x int
 
459
        n := testing.AllocsPerRun(100, func() {
 
460
                x += m[string(buf)]
 
461
        })
 
462
        if n != 0 {
 
463
                t.Errorf("AllocsPerRun for m[string(buf)] = %v, want 0", n)
 
464
        }
 
465
 
 
466
        x = 0
 
467
        n = testing.AllocsPerRun(100, func() {
 
468
                y, ok := m[string(buf)]
 
469
                if !ok {
 
470
                        panic("!ok")
 
471
                }
 
472
                x += y
 
473
        })
 
474
        if n != 0 {
 
475
                t.Errorf("AllocsPerRun for x,ok = m[string(buf)] = %v, want 0", n)
 
476
        }
 
477
}