OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 const JS = false; // for testing the tests. | 10 const JS = false; // for testing the tests. |
11 const WRONG1 = 0x0DEDFACE; | 11 const WRONG1 = 0x0DEDFACE; |
12 const WRONG2 = 0x0DEDBABE; | 12 const WRONG2 = 0x0DEDBABE; |
13 const WRONG3 = 0x0DEDD011 | 13 const WRONG3 = 0x0DEDD011 |
14 | 14 |
15 function makeSelect(type, args, which) { | 15 function makeSelect(type, args, which) { |
16 if (JS) { | 16 if (JS) { |
17 // For testing the tests. | 17 // For testing the tests. |
18 return function() { | 18 return function() { |
19 var val = +arguments[which]; | 19 var val = +arguments[which]; |
20 print(" " + val); | 20 print(" " + val); |
21 if (type == kAstI32) return val | 0; | 21 if (type == kWasmI32) return val | 0; |
22 if (type == kAstF32) return Math.fround(val); | 22 if (type == kWasmF32) return Math.fround(val); |
23 if (type == kAstF64) return val; | 23 if (type == kWasmF64) return val; |
24 return undefined; | 24 return undefined; |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 var builder = new WasmModuleBuilder(); | 28 var builder = new WasmModuleBuilder(); |
29 var params = []; | 29 var params = []; |
30 for (var i = 0; i < args; i++) params.push(type); | 30 for (var i = 0; i < args; i++) params.push(type); |
31 builder.addFunction("select", makeSig(params, [type])) | 31 builder.addFunction("select", makeSig(params, [type])) |
32 .addBody([kExprGetLocal, which]) | 32 .addBody([kExprGetLocal, which]) |
33 .exportFunc(); | 33 .exportFunc(); |
34 | 34 |
35 return builder.instantiate().exports.select; | 35 return builder.instantiate().exports.select; |
36 } | 36 } |
37 | 37 |
38 const inputs = [ | 38 const inputs = [ |
39 -1, 0, 2.2, 3.3, 3000.11, Infinity, -Infinity, NaN | 39 -1, 0, 2.2, 3.3, 3000.11, Infinity, -Infinity, NaN |
40 ]; | 40 ]; |
41 | 41 |
42 (function TestInt1() { | 42 (function TestInt1() { |
43 print("i32 1(0)..."); | 43 print("i32 1(0)..."); |
44 var C = function(v) { return v | 0; } | 44 var C = function(v) { return v | 0; } |
45 var select1 = makeSelect(kAstI32, 1, 0); | 45 var select1 = makeSelect(kWasmI32, 1, 0); |
46 | 46 |
47 for (val of inputs) { | 47 for (val of inputs) { |
48 assertEquals(C(val), select1(val)); | 48 assertEquals(C(val), select1(val)); |
49 | 49 |
50 // under args | 50 // under args |
51 assertEquals(C(undefined), select1()); | 51 assertEquals(C(undefined), select1()); |
52 // over args | 52 // over args |
53 assertEquals(C(val), select1(val, WRONG1)); | 53 assertEquals(C(val), select1(val, WRONG1)); |
54 assertEquals(C(val), select1(val, WRONG1, WRONG2)); | 54 assertEquals(C(val), select1(val, WRONG1, WRONG2)); |
55 } | 55 } |
56 })(); | 56 })(); |
57 | 57 |
58 (function TestInt2() { | 58 (function TestInt2() { |
59 print("i32 2(0)..."); | 59 print("i32 2(0)..."); |
60 var C = function(v) { return v | 0; } | 60 var C = function(v) { return v | 0; } |
61 var select = makeSelect(kAstI32, 2, 0); | 61 var select = makeSelect(kWasmI32, 2, 0); |
62 | 62 |
63 for (val of inputs) { | 63 for (val of inputs) { |
64 assertEquals(C(val), select(val, WRONG1)); | 64 assertEquals(C(val), select(val, WRONG1)); |
65 | 65 |
66 // under args | 66 // under args |
67 assertEquals(C(undefined), select()); | 67 assertEquals(C(undefined), select()); |
68 assertEquals(C(val), select(val)); | 68 assertEquals(C(val), select(val)); |
69 // over args | 69 // over args |
70 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 70 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
71 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 71 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
72 } | 72 } |
73 | 73 |
74 print("i32 2(1)..."); | 74 print("i32 2(1)..."); |
75 var select = makeSelect(kAstI32, 2, 1); | 75 var select = makeSelect(kWasmI32, 2, 1); |
76 | 76 |
77 for (val of inputs) { | 77 for (val of inputs) { |
78 assertEquals(C(val), select(WRONG1, val)); | 78 assertEquals(C(val), select(WRONG1, val)); |
79 | 79 |
80 // under args | 80 // under args |
81 assertEquals(C(undefined), select()); | 81 assertEquals(C(undefined), select()); |
82 assertEquals(C(undefined), select(val)); | 82 assertEquals(C(undefined), select(val)); |
83 // over args | 83 // over args |
84 assertEquals(C(val), select(WRONG1, val)); | 84 assertEquals(C(val), select(WRONG1, val)); |
85 assertEquals(C(val), select(WRONG1, val, WRONG2)); | 85 assertEquals(C(val), select(WRONG1, val, WRONG2)); |
86 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 86 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
87 } | 87 } |
88 })(); | 88 })(); |
89 | 89 |
90 (function TestInt3() { | 90 (function TestInt3() { |
91 print("i32 3(0)..."); | 91 print("i32 3(0)..."); |
92 var C = function(v) { return v | 0; } | 92 var C = function(v) { return v | 0; } |
93 var select = makeSelect(kAstI32, 3, 0); | 93 var select = makeSelect(kWasmI32, 3, 0); |
94 | 94 |
95 for (val of inputs) { | 95 for (val of inputs) { |
96 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 96 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
97 | 97 |
98 // under args | 98 // under args |
99 assertEquals(C(undefined), select()); | 99 assertEquals(C(undefined), select()); |
100 assertEquals(C(val), select(val)); | 100 assertEquals(C(val), select(val)); |
101 assertEquals(C(val), select(val, WRONG1)); | 101 assertEquals(C(val), select(val, WRONG1)); |
102 // over args | 102 // over args |
103 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 103 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
104 } | 104 } |
105 | 105 |
106 print("i32 3(1)..."); | 106 print("i32 3(1)..."); |
107 var select = makeSelect(kAstI32, 3, 1); | 107 var select = makeSelect(kWasmI32, 3, 1); |
108 | 108 |
109 for (val of inputs) { | 109 for (val of inputs) { |
110 assertEquals(val | 0, select(WRONG1, val, WRONG2)); | 110 assertEquals(val | 0, select(WRONG1, val, WRONG2)); |
111 | 111 |
112 // under args | 112 // under args |
113 assertEquals(C(undefined), select()); | 113 assertEquals(C(undefined), select()); |
114 assertEquals(C(undefined), select(0xDEDFACE)); | 114 assertEquals(C(undefined), select(0xDEDFACE)); |
115 assertEquals(C(val), select(WRONG1, val)); | 115 assertEquals(C(val), select(WRONG1, val)); |
116 // over args | 116 // over args |
117 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 117 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
118 } | 118 } |
119 | 119 |
120 print("i32 3(2)..."); | 120 print("i32 3(2)..."); |
121 var select = makeSelect(kAstI32, 3, 2); | 121 var select = makeSelect(kWasmI32, 3, 2); |
122 | 122 |
123 for (val of inputs) { | 123 for (val of inputs) { |
124 assertEquals(C(val), select(WRONG1, WRONG2, val)); | 124 assertEquals(C(val), select(WRONG1, WRONG2, val)); |
125 | 125 |
126 // under args | 126 // under args |
127 assertEquals(C(undefined), select()); | 127 assertEquals(C(undefined), select()); |
128 assertEquals(C(undefined), select(0xDEDFACE)); | 128 assertEquals(C(undefined), select(0xDEDFACE)); |
129 assertEquals(C(undefined), select(WRONG1, WRONG2)); | 129 assertEquals(C(undefined), select(WRONG1, WRONG2)); |
130 // over args | 130 // over args |
131 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); | 131 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); |
132 } | 132 } |
133 })(); | 133 })(); |
134 | 134 |
135 (function TestFloat32_1() { | 135 (function TestFloat32_1() { |
136 print("f32 1(0)..."); | 136 print("f32 1(0)..."); |
137 var C = function(v) { return Math.fround(v); } | 137 var C = function(v) { return Math.fround(v); } |
138 var select1 = makeSelect(kAstF32, 1, 0); | 138 var select1 = makeSelect(kWasmF32, 1, 0); |
139 | 139 |
140 for (val of inputs) { | 140 for (val of inputs) { |
141 assertEquals(C(val), select1(val)); | 141 assertEquals(C(val), select1(val)); |
142 | 142 |
143 // under args | 143 // under args |
144 assertEquals(C(undefined), select1()); | 144 assertEquals(C(undefined), select1()); |
145 // over args | 145 // over args |
146 assertEquals(C(val), select1(val, WRONG1)); | 146 assertEquals(C(val), select1(val, WRONG1)); |
147 assertEquals(C(val), select1(val, WRONG1, WRONG2)); | 147 assertEquals(C(val), select1(val, WRONG1, WRONG2)); |
148 } | 148 } |
149 })(); | 149 })(); |
150 | 150 |
151 (function TestFloat32_2() { | 151 (function TestFloat32_2() { |
152 print("f32 2(0)..."); | 152 print("f32 2(0)..."); |
153 var C = function(v) { return Math.fround(v); } | 153 var C = function(v) { return Math.fround(v); } |
154 var select = makeSelect(kAstF32, 2, 0); | 154 var select = makeSelect(kWasmF32, 2, 0); |
155 | 155 |
156 for (val of inputs) { | 156 for (val of inputs) { |
157 assertEquals(C(val), select(val, WRONG1)); | 157 assertEquals(C(val), select(val, WRONG1)); |
158 | 158 |
159 // under args | 159 // under args |
160 assertEquals(C(undefined), select()); | 160 assertEquals(C(undefined), select()); |
161 assertEquals(C(val), select(val)); | 161 assertEquals(C(val), select(val)); |
162 // over args | 162 // over args |
163 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 163 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
164 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 164 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
165 } | 165 } |
166 | 166 |
167 print("f32 2(1)..."); | 167 print("f32 2(1)..."); |
168 var select = makeSelect(kAstF32, 2, 1); | 168 var select = makeSelect(kWasmF32, 2, 1); |
169 | 169 |
170 for (val of inputs) { | 170 for (val of inputs) { |
171 assertEquals(C(val), select(WRONG1, val)); | 171 assertEquals(C(val), select(WRONG1, val)); |
172 | 172 |
173 // under args | 173 // under args |
174 assertEquals(C(undefined), select()); | 174 assertEquals(C(undefined), select()); |
175 assertEquals(C(undefined), select(val)); | 175 assertEquals(C(undefined), select(val)); |
176 // over args | 176 // over args |
177 assertEquals(C(val), select(WRONG1, val)); | 177 assertEquals(C(val), select(WRONG1, val)); |
178 assertEquals(C(val), select(WRONG1, val, WRONG2)); | 178 assertEquals(C(val), select(WRONG1, val, WRONG2)); |
179 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 179 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
180 } | 180 } |
181 })(); | 181 })(); |
182 | 182 |
183 (function TestFloat32_2() { | 183 (function TestFloat32_2() { |
184 print("f32 3(0)..."); | 184 print("f32 3(0)..."); |
185 var C = function(v) { return Math.fround(v); } | 185 var C = function(v) { return Math.fround(v); } |
186 var select = makeSelect(kAstF32, 3, 0); | 186 var select = makeSelect(kWasmF32, 3, 0); |
187 | 187 |
188 for (val of inputs) { | 188 for (val of inputs) { |
189 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 189 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
190 | 190 |
191 // under args | 191 // under args |
192 assertEquals(C(undefined), select()); | 192 assertEquals(C(undefined), select()); |
193 assertEquals(C(val), select(val)); | 193 assertEquals(C(val), select(val)); |
194 assertEquals(C(val), select(val, WRONG1)); | 194 assertEquals(C(val), select(val, WRONG1)); |
195 // over args | 195 // over args |
196 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 196 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
197 } | 197 } |
198 | 198 |
199 print("f32 3(1)..."); | 199 print("f32 3(1)..."); |
200 var select = makeSelect(kAstF32, 3, 1); | 200 var select = makeSelect(kWasmF32, 3, 1); |
201 | 201 |
202 for (val of inputs) { | 202 for (val of inputs) { |
203 assertEquals(C(val), select(WRONG1, val, WRONG2)); | 203 assertEquals(C(val), select(WRONG1, val, WRONG2)); |
204 | 204 |
205 // under args | 205 // under args |
206 assertEquals(C(undefined), select()); | 206 assertEquals(C(undefined), select()); |
207 assertEquals(C(undefined), select(0xDEDFACE)); | 207 assertEquals(C(undefined), select(0xDEDFACE)); |
208 assertEquals(C(val), select(WRONG1, val)); | 208 assertEquals(C(val), select(WRONG1, val)); |
209 // over args | 209 // over args |
210 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 210 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
211 } | 211 } |
212 | 212 |
213 print("f32 3(2)..."); | 213 print("f32 3(2)..."); |
214 var select = makeSelect(kAstF32, 3, 2); | 214 var select = makeSelect(kWasmF32, 3, 2); |
215 | 215 |
216 for (val of inputs) { | 216 for (val of inputs) { |
217 assertEquals(C(val), select(WRONG1, WRONG2, val)); | 217 assertEquals(C(val), select(WRONG1, WRONG2, val)); |
218 | 218 |
219 // under args | 219 // under args |
220 assertEquals(C(undefined), select()); | 220 assertEquals(C(undefined), select()); |
221 assertEquals(C(undefined), select(0xDEDFACE)); | 221 assertEquals(C(undefined), select(0xDEDFACE)); |
222 assertEquals(C(undefined), select(WRONG1, WRONG2)); | 222 assertEquals(C(undefined), select(WRONG1, WRONG2)); |
223 // over args | 223 // over args |
224 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); | 224 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); |
225 } | 225 } |
226 })(); | 226 })(); |
227 | 227 |
228 | 228 |
229 (function TestFloat64_1() { | 229 (function TestFloat64_1() { |
230 print("f64 1(0)..."); | 230 print("f64 1(0)..."); |
231 var C = function(v) { return +v; } | 231 var C = function(v) { return +v; } |
232 var select1 = makeSelect(kAstF64, 1, 0); | 232 var select1 = makeSelect(kWasmF64, 1, 0); |
233 | 233 |
234 for (val of inputs) { | 234 for (val of inputs) { |
235 assertEquals(C(val), select1(val)); | 235 assertEquals(C(val), select1(val)); |
236 | 236 |
237 // under args | 237 // under args |
238 assertEquals(C(undefined), select1()); | 238 assertEquals(C(undefined), select1()); |
239 // over args | 239 // over args |
240 assertEquals(C(val), select1(val, WRONG1)); | 240 assertEquals(C(val), select1(val, WRONG1)); |
241 assertEquals(C(val), select1(val, WRONG1, WRONG2)); | 241 assertEquals(C(val), select1(val, WRONG1, WRONG2)); |
242 } | 242 } |
243 })(); | 243 })(); |
244 | 244 |
245 (function TestFloat64_2() { | 245 (function TestFloat64_2() { |
246 print("f64 2(0)..."); | 246 print("f64 2(0)..."); |
247 var C = function(v) { return +v; } | 247 var C = function(v) { return +v; } |
248 var select = makeSelect(kAstF64, 2, 0); | 248 var select = makeSelect(kWasmF64, 2, 0); |
249 | 249 |
250 for (val of inputs) { | 250 for (val of inputs) { |
251 assertEquals(C(val), select(val, WRONG1)); | 251 assertEquals(C(val), select(val, WRONG1)); |
252 | 252 |
253 // under args | 253 // under args |
254 assertEquals(C(undefined), select()); | 254 assertEquals(C(undefined), select()); |
255 assertEquals(C(val), select(val)); | 255 assertEquals(C(val), select(val)); |
256 // over args | 256 // over args |
257 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 257 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
258 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 258 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
259 } | 259 } |
260 | 260 |
261 print("f64 2(1)..."); | 261 print("f64 2(1)..."); |
262 var select = makeSelect(kAstF64, 2, 1); | 262 var select = makeSelect(kWasmF64, 2, 1); |
263 | 263 |
264 for (val of inputs) { | 264 for (val of inputs) { |
265 assertEquals(C(val), select(WRONG1, val)); | 265 assertEquals(C(val), select(WRONG1, val)); |
266 | 266 |
267 // under args | 267 // under args |
268 assertEquals(C(undefined), select()); | 268 assertEquals(C(undefined), select()); |
269 assertEquals(C(undefined), select(val)); | 269 assertEquals(C(undefined), select(val)); |
270 // over args | 270 // over args |
271 assertEquals(C(val), select(WRONG1, val)); | 271 assertEquals(C(val), select(WRONG1, val)); |
272 assertEquals(C(val), select(WRONG1, val, WRONG2)); | 272 assertEquals(C(val), select(WRONG1, val, WRONG2)); |
273 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 273 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
274 } | 274 } |
275 })(); | 275 })(); |
276 | 276 |
277 (function TestFloat64_2() { | 277 (function TestFloat64_2() { |
278 print("f64 3(0)..."); | 278 print("f64 3(0)..."); |
279 var C = function(v) { return +v; } | 279 var C = function(v) { return +v; } |
280 var select = makeSelect(kAstF64, 3, 0); | 280 var select = makeSelect(kWasmF64, 3, 0); |
281 | 281 |
282 for (val of inputs) { | 282 for (val of inputs) { |
283 assertEquals(C(val), select(val, WRONG1, WRONG2)); | 283 assertEquals(C(val), select(val, WRONG1, WRONG2)); |
284 | 284 |
285 // under args | 285 // under args |
286 assertEquals(C(undefined), select()); | 286 assertEquals(C(undefined), select()); |
287 assertEquals(C(val), select(val)); | 287 assertEquals(C(val), select(val)); |
288 assertEquals(C(val), select(val, WRONG1)); | 288 assertEquals(C(val), select(val, WRONG1)); |
289 // over args | 289 // over args |
290 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); | 290 assertEquals(C(val), select(val, WRONG1, WRONG2, WRONG3)); |
291 } | 291 } |
292 | 292 |
293 print("f64 3(1)..."); | 293 print("f64 3(1)..."); |
294 var select = makeSelect(kAstF64, 3, 1); | 294 var select = makeSelect(kWasmF64, 3, 1); |
295 | 295 |
296 for (val of inputs) { | 296 for (val of inputs) { |
297 assertEquals(C(val), select(WRONG1, val, WRONG2)); | 297 assertEquals(C(val), select(WRONG1, val, WRONG2)); |
298 | 298 |
299 // under args | 299 // under args |
300 assertEquals(C(undefined), select()); | 300 assertEquals(C(undefined), select()); |
301 assertEquals(C(undefined), select(0xDEDFACE)); | 301 assertEquals(C(undefined), select(0xDEDFACE)); |
302 assertEquals(C(val), select(WRONG1, val)); | 302 assertEquals(C(val), select(WRONG1, val)); |
303 // over args | 303 // over args |
304 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); | 304 assertEquals(C(val), select(WRONG1, val, WRONG2, WRONG3)); |
305 } | 305 } |
306 | 306 |
307 print("f64 3(2)..."); | 307 print("f64 3(2)..."); |
308 var select = makeSelect(kAstF64, 3, 2); | 308 var select = makeSelect(kWasmF64, 3, 2); |
309 | 309 |
310 for (val of inputs) { | 310 for (val of inputs) { |
311 assertEquals(C(val), select(WRONG1, WRONG2, val)); | 311 assertEquals(C(val), select(WRONG1, WRONG2, val)); |
312 | 312 |
313 // under args | 313 // under args |
314 assertEquals(C(undefined), select()); | 314 assertEquals(C(undefined), select()); |
315 assertEquals(C(undefined), select(0xDEDFACE)); | 315 assertEquals(C(undefined), select(0xDEDFACE)); |
316 assertEquals(C(undefined), select(WRONG1, WRONG2)); | 316 assertEquals(C(undefined), select(WRONG1, WRONG2)); |
317 // over args | 317 // over args |
318 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); | 318 assertEquals(C(val), select(WRONG1, WRONG2, val, WRONG3)); |
319 } | 319 } |
320 })(); | 320 })(); |
OLD | NEW |