github.com/higress-group/nottinygc@v0.0.0-20231101025119-e93c4c2f8520/gc_test.go (about) 1 // Copyright wasilibs authors 2 // SPDX-License-Identifier: MIT 3 4 package nottinygc_test 5 6 import ( 7 "runtime" 8 "testing" 9 ) 10 11 // Some tests copied from Go that don't rely on internals 12 13 func TestGcDeepNesting(t *testing.T) { 14 type T [2][2][2][2][2][2][2][2][2][2]*int 15 a := new(T) 16 17 // Prevent the compiler from applying escape analysis. 18 // This makes sure new(T) is allocated on heap, not on the stack. 19 t.Logf("%p", a) 20 21 a[0][0][0][0][0][0][0][0][0][0] = new(int) 22 *a[0][0][0][0][0][0][0][0][0][0] = 13 23 runtime.GC() 24 if *a[0][0][0][0][0][0][0][0][0][0] != 13 { 25 t.Fail() 26 } 27 } 28 29 func TestGcArraySlice(t *testing.T) { 30 type X struct { 31 buf [1]byte 32 nextbuf []byte 33 next *X 34 } 35 var head *X 36 for i := 0; i < 10; i++ { 37 p := &X{} 38 p.buf[0] = 42 39 p.next = head 40 if head != nil { 41 p.nextbuf = head.buf[:] 42 } 43 head = p 44 runtime.GC() 45 } 46 for p := head; p != nil; p = p.next { 47 if p.buf[0] != 42 { 48 t.Fatal("corrupted heap") 49 } 50 } 51 } 52 53 func TestGcRescan(t *testing.T) { 54 type X struct { 55 c chan error 56 nextx *X 57 } 58 type Y struct { 59 X 60 nexty *Y 61 p *int 62 } 63 var head *Y 64 for i := 0; i < 10; i++ { 65 p := &Y{} 66 p.c = make(chan error) 67 if head != nil { 68 p.nextx = &head.X 69 } 70 p.nexty = head 71 p.p = new(int) 72 *p.p = 42 73 head = p 74 runtime.GC() 75 } 76 for p := head; p != nil; p = p.nexty { 77 if *p.p != 42 { 78 t.Fatal("corrupted heap") 79 } 80 } 81 }