replaceLaneTests.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. let passed = true;
  6. function assertEquals(expected, actual) {
  7. if (expected != actual) {
  8. passed = false;
  9. throw `Expected ${expected}, received ${actual}`;
  10. }
  11. }
  12. const INITIAL_SIZE = 1;
  13. const memObj = new WebAssembly.Memory({initial:INITIAL_SIZE});
  14. const arrays = {
  15. "i32x4" : { arr : new Int32Array (memObj.buffer) , len : 4 } ,
  16. "i16x8" : { arr : new Int16Array (memObj.buffer) , len : 8 } ,
  17. "i8x16" : { arr : new Int8Array (memObj.buffer) , len : 16 } ,
  18. "f32x4" : { arr : new Float32Array (memObj.buffer) , len : 4 } ,
  19. "f64x2" : { arr : new Float64Array (memObj.buffer) , len : 2 }
  20. };
  21. const module = new WebAssembly.Module(readbuffer('replace.wasm'));
  22. const instance = new WebAssembly.Instance(module, { "dummy" : { "memory" : memObj } }).exports;
  23. let testIntFloatReplace = function (funcname, splat, val) {
  24. const type = funcname.split('_')[0];
  25. const arr = arrays [type].arr;
  26. const len = arrays [type].len;
  27. for (let i = 0; i < len; i++) {
  28. instance[funcname+i](splat, val);
  29. for (let j = 0; j < len; j++) {
  30. if (i != j) {
  31. assertEquals(splat, arr[j]);
  32. }
  33. else {
  34. assertEquals(val, arr[j]);
  35. }
  36. }
  37. }
  38. }
  39. testIntFloatReplace("i32x4_replace", -1, -2147483648);
  40. testIntFloatReplace("i32x4_replace", 7, -1);
  41. testIntFloatReplace("i32x4_replace", 0, 2147483647);
  42. testIntFloatReplace("i16x8_replace", -1, -32768);
  43. testIntFloatReplace("i16x8_replace", 1001, -1);
  44. testIntFloatReplace("i16x8_replace", 0, 32767);
  45. testIntFloatReplace("i8x16_replace", -1, -128);
  46. testIntFloatReplace("i8x16_replace", 100, -1);
  47. testIntFloatReplace("i8x16_replace", 0, 127);
  48. testIntFloatReplace("f32x4_replace", Number.NEGATIVE_INFINITY, 0.125);
  49. testIntFloatReplace("f32x4_replace", 777.0, 1001.0);
  50. testIntFloatReplace("f32x4_replace", -1.0, Number.POSITIVE_INFINITY);
  51. testIntFloatReplace("f32x4_replace", -100.0, 1.7014118346046924e+38);
  52. testIntFloatReplace("f64x2_replace", Number.NEGATIVE_INFINITY, 0.125);
  53. testIntFloatReplace("f64x2_replace", 777.0, 1001.0);
  54. testIntFloatReplace("f64x2_replace", -1.0, Number.POSITIVE_INFINITY);
  55. testIntFloatReplace("f64x2_replace", -100.0, 1.7014118346046924e+38);
  56. if (passed) {
  57. print("Passed");
  58. }