loadTests.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. function testLoadOpsForType(funcname, module, laneValues, expectedResults, startPositions) {
  13. let memObj = new WebAssembly.Memory({initial:INITIAL_SIZE});
  14. const instance = new WebAssembly.Instance(module, { "dummy" : { "memory" : memObj } }).exports;
  15. let intArray = new Int32Array (memObj.buffer);
  16. let int8Array = new Int8Array (memObj.buffer);
  17. let forEachTestPosition = (action) => {
  18. for (const pos of startPositions) {
  19. for (let i = 0; i < 4; i++) {
  20. action(pos, i);
  21. }
  22. }
  23. };
  24. forEachTestPosition ((pos, i) => {intArray[pos + i] = laneValues[i];});
  25. instance[funcname](0);
  26. forEachTestPosition((pos, i) => {assertEquals(expectedResults[i], intArray[pos + i]);});
  27. const MEM_SIZE_IN_BYTES = 1024 * 64;
  28. let check = function(expected, funName, ...args) {
  29. let fun = eval(funName);
  30. var result;
  31. try {
  32. result = fun(...args);
  33. }
  34. catch (e) {
  35. if (e.message === "Access index is out of range" ||
  36. e.message === "Simd typed array access: argument out of range" ||
  37. e.message === "argument out of range" ||
  38. e.message === "Memory index is out of range"
  39. ) {
  40. result = "Access index is out of range";
  41. }
  42. else {
  43. result = e.message;
  44. }
  45. }
  46. if(result != expected)
  47. {
  48. passed = false;
  49. print(`${funName}(${[...args]}) produced ${result}, expected ${expected}`);
  50. }
  51. }
  52. check(0, "instance.v128_load4", MEM_SIZE_IN_BYTES - 32);
  53. check(0, "instance.v128_load4", MEM_SIZE_IN_BYTES - 16);
  54. check("Access index is out of range", "instance.v128_load4", MEM_SIZE_IN_BYTES - 8);
  55. check("Access index is out of range", "instance.v128_load4", MEM_SIZE_IN_BYTES - 4);
  56. check("Access index is out of range", "instance.v128_load4_offset", 0xFFFFFFFC);
  57. check("Access index is out of range", "instance.v128_load4_offset", -1);
  58. // Verify read at "odd" index
  59. int8Array[0] = 1;
  60. int8Array[1] = 0;
  61. int8Array[2] = 0;
  62. int8Array[3] = 0;
  63. int8Array[4] = 0;
  64. check(0, "instance.v128_load4", 1);
  65. }
  66. const INITIAL_SIZE = 1;
  67. const module = new WebAssembly.Module(readbuffer('loads.wasm'));
  68. const laneValues = [0xAAAAAAAA, 0xFFFFFFFF, 0X80000000, 0x90A762A6];
  69. const expectedResults = [16, 32, 1, 14]; //i32.popcnt
  70. const startPositions = [0, 5, 11, 17];
  71. testLoadOpsForType("v128_load_test", module, laneValues, expectedResults, startPositions);
  72. if (passed) {
  73. print("Passed");
  74. }