loadTests.js 2.9 KB

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