1
2
3
4
5
6
7 package main
8
9 import "testing"
10
11
12 func testLoadStoreOrder(t *testing.T) {
13 z := uint32(1000)
14 if testLoadStoreOrder_ssa(&z, 100) == 0 {
15 t.Errorf("testLoadStoreOrder failed")
16 }
17 }
18
19
20 func testLoadStoreOrder_ssa(z *uint32, prec uint) int {
21 old := *z
22 *z = uint32(prec)
23 if *z < old {
24 return 1
25 }
26 return 0
27 }
28
29 func testStoreSize(t *testing.T) {
30 a := [4]uint16{11, 22, 33, 44}
31 testStoreSize_ssa(&a[0], &a[2], 77)
32 want := [4]uint16{77, 22, 33, 44}
33 if a != want {
34 t.Errorf("testStoreSize failed. want = %d, got = %d", want, a)
35 }
36 }
37
38
39 func testStoreSize_ssa(p *uint16, q *uint16, v uint32) {
40
41
42
43
44
45
46 w := uint16(v)
47 if p != nil {
48 *p = w
49 } else {
50 *q = w
51 }
52 }
53
54
55 func testExtStore_ssa(p *byte, b bool) int {
56 x := *p
57 *p = 7
58 if b {
59 return int(x)
60 }
61 return 0
62 }
63
64 func testExtStore(t *testing.T) {
65 const start = 8
66 var b byte = start
67 if got := testExtStore_ssa(&b, true); got != start {
68 t.Errorf("testExtStore failed. want = %d, got = %d", start, got)
69 }
70 }
71
72 var b int
73
74
75
76
77 func testDeadStorePanic_ssa(a int) (r int) {
78 defer func() {
79 recover()
80 r = a
81 }()
82 a = 2
83 b := a - a
84 c := 4
85 a = c / b
86 a = 3
87 r = a
88 return
89 }
90
91 func testDeadStorePanic(t *testing.T) {
92 if want, got := 2, testDeadStorePanic_ssa(1); want != got {
93 t.Errorf("testDeadStorePanic failed. want = %d, got = %d", want, got)
94 }
95 }
96
97
98 func loadHitStore8(x int8, p *int8) int32 {
99 x *= x
100 *p = x
101 return int32(*p)
102 }
103
104
105 func loadHitStoreU8(x uint8, p *uint8) uint32 {
106 x *= x
107 *p = x
108 return uint32(*p)
109 }
110
111
112 func loadHitStore16(x int16, p *int16) int32 {
113 x *= x
114 *p = x
115 return int32(*p)
116 }
117
118
119 func loadHitStoreU16(x uint16, p *uint16) uint32 {
120 x *= x
121 *p = x
122 return uint32(*p)
123 }
124
125
126 func loadHitStore32(x int32, p *int32) int64 {
127 x *= x
128 *p = x
129 return int64(*p)
130 }
131
132
133 func loadHitStoreU32(x uint32, p *uint32) uint64 {
134 x *= x
135 *p = x
136 return uint64(*p)
137 }
138
139 func testLoadHitStore(t *testing.T) {
140
141
142 {
143 var in int8 = (1 << 6) + 1
144 var p int8
145 got := loadHitStore8(in, &p)
146 want := int32(in * in)
147 if got != want {
148 t.Errorf("testLoadHitStore (int8) failed. want = %d, got = %d", want, got)
149 }
150 }
151 {
152 var in uint8 = (1 << 6) + 1
153 var p uint8
154 got := loadHitStoreU8(in, &p)
155 want := uint32(in * in)
156 if got != want {
157 t.Errorf("testLoadHitStore (uint8) failed. want = %d, got = %d", want, got)
158 }
159 }
160 {
161 var in int16 = (1 << 10) + 1
162 var p int16
163 got := loadHitStore16(in, &p)
164 want := int32(in * in)
165 if got != want {
166 t.Errorf("testLoadHitStore (int16) failed. want = %d, got = %d", want, got)
167 }
168 }
169 {
170 var in uint16 = (1 << 10) + 1
171 var p uint16
172 got := loadHitStoreU16(in, &p)
173 want := uint32(in * in)
174 if got != want {
175 t.Errorf("testLoadHitStore (uint16) failed. want = %d, got = %d", want, got)
176 }
177 }
178 {
179 var in int32 = (1 << 30) + 1
180 var p int32
181 got := loadHitStore32(in, &p)
182 want := int64(in * in)
183 if got != want {
184 t.Errorf("testLoadHitStore (int32) failed. want = %d, got = %d", want, got)
185 }
186 }
187 {
188 var in uint32 = (1 << 30) + 1
189 var p uint32
190 got := loadHitStoreU32(in, &p)
191 want := uint64(in * in)
192 if got != want {
193 t.Errorf("testLoadHitStore (uint32) failed. want = %d, got = %d", want, got)
194 }
195 }
196 }
197
198 func TestLoadStore(t *testing.T) {
199 testLoadStoreOrder(t)
200 testStoreSize(t)
201 testExtStore(t)
202 testDeadStorePanic(t)
203 testLoadHitStore(t)
204 }
205
View as plain text