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
78 func testDeadStorePanic_ssa(a int) (r int) {
79 defer func() {
80 recover()
81 r = a
82 }()
83 a = 2
84 b := a - a
85 c := 4
86 a = c / b
87 a = 3
88 r = a
89 return
90 }
91
92 func testDeadStorePanic(t *testing.T) {
93 if want, got := 2, testDeadStorePanic_ssa(1); want != got {
94 t.Errorf("testDeadStorePanic failed. want = %d, got = %d", want, got)
95 }
96 }
97
98
99 func loadHitStore8(x int8, p *int8) int32 {
100 x *= x
101 *p = x
102 return int32(*p)
103 }
104
105
106 func loadHitStoreU8(x uint8, p *uint8) uint32 {
107 x *= x
108 *p = x
109 return uint32(*p)
110 }
111
112
113 func loadHitStore16(x int16, p *int16) int32 {
114 x *= x
115 *p = x
116 return int32(*p)
117 }
118
119
120 func loadHitStoreU16(x uint16, p *uint16) uint32 {
121 x *= x
122 *p = x
123 return uint32(*p)
124 }
125
126
127 func loadHitStore32(x int32, p *int32) int64 {
128 x *= x
129 *p = x
130 return int64(*p)
131 }
132
133
134 func loadHitStoreU32(x uint32, p *uint32) uint64 {
135 x *= x
136 *p = x
137 return uint64(*p)
138 }
139
140 func testLoadHitStore(t *testing.T) {
141
142
143 {
144 var in int8 = (1 << 6) + 1
145 var p int8
146 got := loadHitStore8(in, &p)
147 want := int32(in * in)
148 if got != want {
149 t.Errorf("testLoadHitStore (int8) failed. want = %d, got = %d", want, got)
150 }
151 }
152 {
153 var in uint8 = (1 << 6) + 1
154 var p uint8
155 got := loadHitStoreU8(in, &p)
156 want := uint32(in * in)
157 if got != want {
158 t.Errorf("testLoadHitStore (uint8) failed. want = %d, got = %d", want, got)
159 }
160 }
161 {
162 var in int16 = (1 << 10) + 1
163 var p int16
164 got := loadHitStore16(in, &p)
165 want := int32(in * in)
166 if got != want {
167 t.Errorf("testLoadHitStore (int16) failed. want = %d, got = %d", want, got)
168 }
169 }
170 {
171 var in uint16 = (1 << 10) + 1
172 var p uint16
173 got := loadHitStoreU16(in, &p)
174 want := uint32(in * in)
175 if got != want {
176 t.Errorf("testLoadHitStore (uint16) failed. want = %d, got = %d", want, got)
177 }
178 }
179 {
180 var in int32 = (1 << 30) + 1
181 var p int32
182 got := loadHitStore32(in, &p)
183 want := int64(in * in)
184 if got != want {
185 t.Errorf("testLoadHitStore (int32) failed. want = %d, got = %d", want, got)
186 }
187 }
188 {
189 var in uint32 = (1 << 30) + 1
190 var p uint32
191 got := loadHitStoreU32(in, &p)
192 want := uint64(in * in)
193 if got != want {
194 t.Errorf("testLoadHitStore (uint32) failed. want = %d, got = %d", want, got)
195 }
196 }
197 }
198
199 func TestLoadStore(t *testing.T) {
200 testLoadStoreOrder(t)
201 testStoreSize(t)
202 testExtStore(t)
203 testDeadStorePanic(t)
204 testLoadHitStore(t)
205 }
206
View as plain text