1 package main 2 3 var failed = false 4 5 //go:noinline 6 func testSliceLenCap12_ssa(a [10]int, i, j int) (int, int) { 7 b := a[i:j] 8 return len(b), cap(b) 9 } 10 11 //go:noinline 12 func testSliceLenCap1_ssa(a [10]int, i, j int) (int, int) { 13 b := a[i:] 14 return len(b), cap(b) 15 } 16 17 //go:noinline 18 func testSliceLenCap2_ssa(a [10]int, i, j int) (int, int) { 19 b := a[:j] 20 return len(b), cap(b) 21 } 22 23 func testSliceLenCap() { 24 a := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 25 tests := [...]struct { 26 fn func(a [10]int, i, j int) (int, int) 27 i, j int // slice range 28 l, c int // len, cap 29 }{ 30 // -1 means the value is not used. 31 {testSliceLenCap12_ssa, 0, 0, 0, 10}, 32 {testSliceLenCap12_ssa, 0, 1, 1, 10}, 33 {testSliceLenCap12_ssa, 0, 10, 10, 10}, 34 {testSliceLenCap12_ssa, 10, 10, 0, 0}, 35 {testSliceLenCap12_ssa, 0, 5, 5, 10}, 36 {testSliceLenCap12_ssa, 5, 5, 0, 5}, 37 {testSliceLenCap12_ssa, 5, 10, 5, 5}, 38 {testSliceLenCap1_ssa, 0, -1, 0, 10}, 39 {testSliceLenCap1_ssa, 5, -1, 5, 5}, 40 {testSliceLenCap1_ssa, 10, -1, 0, 0}, 41 {testSliceLenCap2_ssa, -1, 0, 0, 10}, 42 {testSliceLenCap2_ssa, -1, 5, 5, 10}, 43 {testSliceLenCap2_ssa, -1, 10, 10, 10}, 44 } 45 46 for i, t := range tests { 47 if l, c := t.fn(a, t.i, t.j); l != t.l && c != t.c { 48 println("#", i, " len(a[", t.i, ":", t.j, "]), cap(a[", t.i, ":", t.j, "]) =", l, c, 49 ", want", t.l, t.c) 50 failed = true 51 } 52 } 53 } 54 55 //go:noinline 56 func testSliceGetElement_ssa(a [10]int, i, j, p int) int { 57 return a[i:j][p] 58 } 59 60 func testSliceGetElement() { 61 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 62 tests := [...]struct { 63 i, j, p int 64 want int // a[i:j][p] 65 }{ 66 {0, 10, 2, 20}, 67 {0, 5, 4, 40}, 68 {5, 10, 3, 80}, 69 {1, 9, 7, 80}, 70 } 71 72 for i, t := range tests { 73 if got := testSliceGetElement_ssa(a, t.i, t.j, t.p); got != t.want { 74 println("#", i, " a[", t.i, ":", t.j, "][", t.p, "] = ", got, " wanted ", t.want) 75 failed = true 76 } 77 } 78 } 79 80 //go:noinline 81 func testSliceSetElement_ssa(a *[10]int, i, j, p, x int) { 82 (*a)[i:j][p] = x 83 } 84 85 func testSliceSetElement() { 86 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 87 tests := [...]struct { 88 i, j, p int 89 want int // a[i:j][p] 90 }{ 91 {0, 10, 2, 17}, 92 {0, 5, 4, 11}, 93 {5, 10, 3, 28}, 94 {1, 9, 7, 99}, 95 } 96 97 for i, t := range tests { 98 testSliceSetElement_ssa(&a, t.i, t.j, t.p, t.want) 99 if got := a[t.i+t.p]; got != t.want { 100 println("#", i, " a[", t.i, ":", t.j, "][", t.p, "] = ", got, " wanted ", t.want) 101 failed = true 102 } 103 } 104 } 105 106 func testSlicePanic1() { 107 defer func() { 108 if r := recover(); r != nil { 109 println("panicked as expected") 110 } 111 }() 112 113 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 114 testSliceLenCap12_ssa(a, 3, 12) 115 println("expected to panic, but didn't") 116 failed = true 117 } 118 119 func testSlicePanic2() { 120 defer func() { 121 if r := recover(); r != nil { 122 println("panicked as expected") 123 } 124 }() 125 126 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 127 testSliceGetElement_ssa(a, 3, 7, 4) 128 println("expected to panic, but didn't") 129 failed = true 130 } 131 132 func main() { 133 testSliceLenCap() 134 testSliceGetElement() 135 testSliceSetElement() 136 testSlicePanic1() 137 testSlicePanic2() 138 139 if failed { 140 panic("failed") 141 } 142 } 143