libgo: Update to Go 1.1.1.
[official-gcc.git] / libgo / go / runtime / gc_test.go
blob05ee34869cce4ddad72a8b91eaeee6da1ea759f3
1 // Copyright 2011 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 // "os"
9 "runtime"
10 "runtime/debug"
11 "testing"
14 func TestGcSys(t *testing.T) {
15 /* gccgo does not have a go command
16 if os.Getenv("GOGC") == "off" {
17 t.Skip("skipping test; GOGC=off in environment")
19 data := struct{ Short bool }{testing.Short()}
20 got := executeTest(t, testGCSysSource, &data)
21 want := "OK\n"
22 if got != want {
23 t.Fatalf("expected %q, but got %q", want, got)
28 const testGCSysSource = `
29 package main
31 import (
32 "fmt"
33 "runtime"
36 func main() {
37 runtime.GOMAXPROCS(1)
38 memstats := new(runtime.MemStats)
39 runtime.GC()
40 runtime.ReadMemStats(memstats)
41 sys := memstats.Sys
43 runtime.MemProfileRate = 0 // disable profiler
45 itercount := 1000000
46 {{if .Short}}
47 itercount = 100000
48 {{end}}
49 for i := 0; i < itercount; i++ {
50 workthegc()
53 // Should only be using a few MB.
54 // We allocated 100 MB or (if not short) 1 GB.
55 runtime.ReadMemStats(memstats)
56 if sys > memstats.Sys {
57 sys = 0
58 } else {
59 sys = memstats.Sys - sys
61 if sys > 16<<20 {
62 fmt.Printf("using too much memory: %d bytes\n", sys)
63 return
65 fmt.Printf("OK\n")
68 func workthegc() []byte {
69 return make([]byte, 1029)
73 func TestGcDeepNesting(t *testing.T) {
74 type T [2][2][2][2][2][2][2][2][2][2]*int
75 a := new(T)
77 // Prevent the compiler from applying escape analysis.
78 // This makes sure new(T) is allocated on heap, not on the stack.
79 t.Logf("%p", a)
81 a[0][0][0][0][0][0][0][0][0][0] = new(int)
82 *a[0][0][0][0][0][0][0][0][0][0] = 13
83 runtime.GC()
84 if *a[0][0][0][0][0][0][0][0][0][0] != 13 {
85 t.Fail()
89 func TestGcHashmapIndirection(t *testing.T) {
90 defer debug.SetGCPercent(debug.SetGCPercent(1))
91 runtime.GC()
92 type T struct {
93 a [256]int
95 m := make(map[T]T)
96 for i := 0; i < 2000; i++ {
97 var a T
98 a.a[0] = i
99 m[a] = T{}
103 func TestGcArraySlice(t *testing.T) {
104 type X struct {
105 buf [1]byte
106 nextbuf []byte
107 next *X
109 var head *X
110 for i := 0; i < 10; i++ {
111 p := &X{}
112 p.buf[0] = 42
113 p.next = head
114 if head != nil {
115 p.nextbuf = head.buf[:]
117 head = p
118 runtime.GC()
120 for p := head; p != nil; p = p.next {
121 if p.buf[0] != 42 {
122 t.Fatal("corrupted heap")
127 func TestGcRescan(t *testing.T) {
128 type X struct {
129 c chan error
130 nextx *X
132 type Y struct {
134 nexty *Y
135 p *int
137 var head *Y
138 for i := 0; i < 10; i++ {
139 p := &Y{}
140 p.c = make(chan error)
141 p.nextx = &head.X
142 p.nexty = head
143 p.p = new(int)
144 *p.p = 42
145 head = p
146 runtime.GC()
148 for p := head; p != nil; p = p.nexty {
149 if *p.p != 42 {
150 t.Fatal("corrupted heap")