splatNegTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. let check = function(expected, funName, ...args) {
  13. let fun = eval(funName);
  14. var result;
  15. try {
  16. result = fun(...args);
  17. }
  18. catch (e) {
  19. result = e.name;
  20. }
  21. if(result != expected)
  22. {
  23. passed = false;
  24. print(`${funName}(${[...args]}) produced ${result}, expected ${expected}`);
  25. }
  26. }
  27. const INITIAL_SIZE = 1;
  28. const memObj = new WebAssembly.Memory({initial:INITIAL_SIZE});
  29. const arrays = {
  30. "i32x4" : { arr : new Int32Array (memObj.buffer) , len : 4 } ,
  31. "i16x8" : { arr : new Int16Array (memObj.buffer) , len : 8 } ,
  32. "i8x16" : { arr : new Int8Array (memObj.buffer) , len : 16 } ,
  33. "f32x4" : { arr : new Float32Array (memObj.buffer) , len : 4 },
  34. "f64x2" : { arr : new Float64Array (memObj.buffer) , len : 2 }
  35. };
  36. //SPLAT
  37. const splatModule = new WebAssembly.Module(readbuffer('splat.wasm'));
  38. const splatInstance = new WebAssembly.Instance(splatModule, { "dummy" : { "memory" : memObj } }).exports;
  39. let testSplat = function (funcname, val) {
  40. const type = funcname.split('_')[0];
  41. const arr = arrays [type].arr;
  42. const len = arrays [type].len;
  43. for (let i = 0; i < len; i++) {
  44. arr[i] = val;
  45. }
  46. splatInstance[funcname](0, val);
  47. for (let i = 0; i < len; i++) {
  48. assertEquals (val, arr[i]);
  49. }
  50. }
  51. testSplat("i32x4_splat", 0);
  52. testSplat("i32x4_splat", -1);
  53. testSplat("i32x4_splat", -2147483648);
  54. testSplat("i16x8_splat", 0);
  55. testSplat("i16x8_splat", -1);
  56. testSplat("i16x8_splat", 32766);
  57. testSplat("i8x16_splat", 0);
  58. testSplat("i8x16_splat", -1);
  59. testSplat("i8x16_splat", -128);
  60. testSplat("f32x4_splat", 0.125);
  61. testSplat("f32x4_splat", 1001.0);
  62. testSplat("f32x4_splat", Number.POSITIVE_INFINITY);
  63. testSplat("f32x4_splat", 1.7014118346046924e+38);
  64. testSplat("f64x2_splat", 0.125);
  65. testSplat("f64x2_splat", 1001.0);
  66. testSplat("f64x2_splat", Number.POSITIVE_INFINITY);
  67. testSplat("f64x2_splat", 1.7014118346046924e+38);
  68. //NEG
  69. const module = new WebAssembly.Module(readbuffer('neg.wasm'));
  70. const instance = new WebAssembly.Instance(module, { "dummy" : { "memory" : memObj } }).exports;
  71. let testNeg = function (funcname, val) {
  72. const type = funcname.split('_')[0];
  73. const arr = arrays [type].arr;
  74. const len = arrays [type].len;
  75. for (let i = 0; i < len; i++) {
  76. arr[i] = val;
  77. }
  78. instance[funcname](0, val);
  79. for (let i = 0; i < len; i++) {
  80. assertEquals(-val, arr[i]);
  81. }
  82. };
  83. testNeg("i32x4_neg", 0);
  84. testNeg("i32x4_neg", 1);
  85. testNeg("i32x4_neg", 2147483647);
  86. testNeg("i16x8_neg", 0);
  87. testNeg("i16x8_neg", 1);
  88. testNeg("i16x8_neg", 32767);
  89. testNeg("i8x16_neg", 0);
  90. testNeg("i8x16_neg", 1);
  91. testNeg("i8x16_neg", 127);
  92. testNeg("f32x4_neg", 0.0);
  93. testNeg("f32x4_neg", 1.0);
  94. testNeg("f32x4_neg", 1000.0);
  95. testNeg("f32x4_neg", Number.POSITIVE_INFINITY, 32);
  96. testNeg("f64x2_neg", -0.0);
  97. testNeg("f64x2_neg", 1.0);
  98. testNeg("f64x2_neg", 1234.56);
  99. testNeg("f64x2_neg", Number.POSITIVE_INFINITY);
  100. if (passed) {
  101. print("Passed");
  102. }