github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/testdata/array_test.go (about) 1 package main 2 3 import "testing" 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(t *testing.T) { 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, test := range tests { 47 if l, c := test.fn(a, test.i, test.j); l != test.l && c != test.c { 48 t.Errorf("#%d len(a[%d:%d]), cap(a[%d:%d]) = %d %d, want %d %d", i, test.i, test.j, test.i, test.j, l, c, test.l, test.c) 49 } 50 } 51 } 52 53 //go:noinline 54 func testSliceGetElement_ssa(a [10]int, i, j, p int) int { 55 return a[i:j][p] 56 } 57 58 func testSliceGetElement(t *testing.T) { 59 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 60 tests := [...]struct { 61 i, j, p int 62 want int // a[i:j][p] 63 }{ 64 {0, 10, 2, 20}, 65 {0, 5, 4, 40}, 66 {5, 10, 3, 80}, 67 {1, 9, 7, 80}, 68 } 69 70 for i, test := range tests { 71 if got := testSliceGetElement_ssa(a, test.i, test.j, test.p); got != test.want { 72 t.Errorf("#%d a[%d:%d][%d] = %d, wanted %d", i, test.i, test.j, test.p, got, test.want) 73 } 74 } 75 } 76 77 //go:noinline 78 func testSliceSetElement_ssa(a *[10]int, i, j, p, x int) { 79 (*a)[i:j][p] = x 80 } 81 82 func testSliceSetElement(t *testing.T) { 83 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 84 tests := [...]struct { 85 i, j, p int 86 want int // a[i:j][p] 87 }{ 88 {0, 10, 2, 17}, 89 {0, 5, 4, 11}, 90 {5, 10, 3, 28}, 91 {1, 9, 7, 99}, 92 } 93 94 for i, test := range tests { 95 testSliceSetElement_ssa(&a, test.i, test.j, test.p, test.want) 96 if got := a[test.i+test.p]; got != test.want { 97 t.Errorf("#%d a[%d:%d][%d] = %d, wanted %d", i, test.i, test.j, test.p, got, test.want) 98 } 99 } 100 } 101 102 func testSlicePanic1(t *testing.T) { 103 defer func() { 104 if r := recover(); r != nil { 105 //println("panicked as expected") 106 } 107 }() 108 109 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 110 testSliceLenCap12_ssa(a, 3, 12) 111 t.Errorf("expected to panic, but didn't") 112 } 113 114 func testSlicePanic2(t *testing.T) { 115 defer func() { 116 if r := recover(); r != nil { 117 //println("panicked as expected") 118 } 119 }() 120 121 a := [10]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} 122 testSliceGetElement_ssa(a, 3, 7, 4) 123 t.Errorf("expected to panic, but didn't") 124 } 125 126 func TestArray(t *testing.T) { 127 testSliceLenCap(t) 128 testSliceGetElement(t) 129 testSliceSetElement(t) 130 testSlicePanic1(t) 131 testSlicePanic2(t) 132 }