sharedarraybuffer.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // ES7 SharedArrayBuffer tests
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var typedArrayList = [
  8. Int8Array,
  9. Uint8Array,
  10. Int16Array,
  11. Uint16Array,
  12. Int32Array,
  13. Uint32Array,
  14. Uint8ClampedArray,
  15. Float32Array,
  16. Float64Array
  17. ];
  18. var tests = [{
  19. name : "SharedArrayBuffer sanity validation",
  20. body : function () {
  21. assert.doesNotThrow(() => new SharedArrayBuffer(1), "SharedArrayBuffer is valid type");
  22. assert.areEqual(typeof SharedArrayBuffer.prototype, "object", "SharedArrayBuffer has prototype object");
  23. assert.throws(() => SharedArrayBuffer(1), TypeError, "Calling SharedArrayBuffer without 'new' is an error", "SharedArrayBuffer: cannot be called without the new keyword");
  24. var sab = new SharedArrayBuffer(1);
  25. assert.areEqual(sab.byteLength, 1, "SharedArrayBuffer's object has byteLength property");
  26. assert.areEqual(sab.constructor, SharedArrayBuffer, "SharedArrayBuffer's object has constructor property");
  27. assert.areEqual(sab.slice, SharedArrayBuffer.prototype.slice, "SharedArrayBuffer's object has slice property");
  28. assert.areEqual(typeof sab.slice, "function", "slice is function");
  29. assert.areEqual(sab.slice.length, 2, "SharedArrayBuffer.prototype.slice.length == 2");
  30. assert.areEqual(String(sab), "[object SharedArrayBuffer]", "Calling toString on SharedArrayBuffer's object will return '[object SharedArrayBuffer]'");
  31. assert.doesNotThrow(() => new SharedArrayBuffer(), "Calling SharedArrayBuffer without any arguments is valid syntax");
  32. }
  33. },
  34. {
  35. name: "SharedArrayBuffer.prototype.slice behavior with non-SharedArrayBuffer parameters",
  36. body: function() {
  37. assert.throws(function () { SharedArrayBuffer.prototype.slice.apply('string'); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if this parameter is not an SharedArrayBuffer", "SharedArrayBuffer object expected");
  38. assert.throws(function () { SharedArrayBuffer.prototype.slice.apply(); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if there is no this parameter", "SharedArrayBuffer object expected");
  39. assert.throws(function () { SharedArrayBuffer.prototype.slice.call(); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if it is called directly", "SharedArrayBuffer object expected");
  40. assert.throws(function () { SharedArrayBuffer.prototype.slice.call(undefined); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if it is called directly", "SharedArrayBuffer object expected");
  41. assert.throws(function () { SharedArrayBuffer.prototype.slice(); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if it is called directly", "SharedArrayBuffer object expected");
  42. assert.throws(function () { SharedArrayBuffer.prototype.slice(undefined); }, TypeError, "SharedArrayBuffer.prototype.slice throws TypeError if it is called directly", "SharedArrayBuffer object expected");
  43. }
  44. },
  45. {
  46. name : "Validating SharedArrayBuffer with different size",
  47. body : function () {
  48. function validate(size, expectedSize) {
  49. var sab = new SharedArrayBuffer(size);
  50. assert.areEqual(sab.byteLength, expectedSize, "byteLength should be " + expectedSize);
  51. }
  52. validate(0, 0);
  53. validate(1024, 1024);
  54. validate(false, 0);
  55. validate(undefined, 0);
  56. validate(NaN, 0);
  57. validate("16", 16);
  58. validate("hello", 0);
  59. validate(1.1, 1);
  60. validate({ valueOf : () => 10 }, 10);
  61. validate({ toString : () => 10 }, 10);
  62. assert.doesNotThrow(() => new SharedArrayBuffer(1, 2, 3), "Calling SharedArrayBuffer with more than one arguments is a valid syntax");
  63. assert.throws(() => new SharedArrayBuffer(-1), RangeError, "Calling SharedArrayBuffer with negative size is an error", "Array length must be a finite positive integer");
  64. }
  65. },
  66. {
  67. name : "Validating SharedArrayBuffer's byteLength does not change",
  68. body : function () {
  69. var sab = new SharedArrayBuffer(1);
  70. sab.byteLength = 12;
  71. assert.areEqual(sab.byteLength, 1, "Explicit change to byteLength does not modify the byteLength");
  72. }
  73. },
  74. {
  75. name : "TypedArray on SharedArrayBuffer",
  76. body : function () {
  77. function validate(constructor, elements, range) {
  78. var sab = new SharedArrayBuffer(elements*constructor.BYTES_PER_ELEMENT);
  79. var view = new constructor(sab);
  80. assert.areEqual(view.length, elements, constructor.name + " with length " + elements);
  81. assert.areEqual(view.buffer, sab, constructor.name + " with buffer");
  82. assert.areEqual(view.byteOffset, 0, constructor.name + " with byteOffset " + view.byteOffset);
  83. assert.areEqual(view.byteLength, elements*constructor.BYTES_PER_ELEMENT, constructor.name + " with byteLength " + elements*constructor.BYTES_PER_ELEMENT);
  84. for ([offset, len] of range) {
  85. var v = new constructor(sab, offset*constructor.BYTES_PER_ELEMENT, len);
  86. assert.areEqual(v.length, len, constructor.name + " with length " + len);
  87. assert.areEqual(v.buffer, sab, constructor.name + " with buffer");
  88. assert.areEqual(v.byteOffset, offset*constructor.BYTES_PER_ELEMENT, constructor.name + " with byteOffset " + view.byteOffset);
  89. assert.areEqual(v.byteLength, len * constructor.BYTES_PER_ELEMENT, constructor.name + " with byteLength " + elements*constructor.BYTES_PER_ELEMENT);
  90. if (len > 0) {
  91. v[0] = 10;
  92. assert.areEqual(v[0], view[offset], constructor.name);
  93. }
  94. }
  95. };
  96. for (var i of typedArrayList) {
  97. validate(i, 8, [[0, 0], [0, 1], [6, 0], [6, 2], [0, 8]]);
  98. }
  99. function validateThrows(constructor, elements, range) {
  100. var sab = new SharedArrayBuffer(elements*constructor.BYTES_PER_ELEMENT);
  101. for ([offset, len] of range) {
  102. assert.throws(() => new constructor(sab, offset*constructor.BYTES_PER_ELEMENT, len), RangeError);
  103. }
  104. }
  105. for (var i of typedArrayList) {
  106. validateThrows(i, 8, [[-1, 0], [20, 1], [6, -1], [6, 5]]);
  107. }
  108. }
  109. },
  110. {
  111. name : "TypedArray indices validation on SharedArrayBuffer",
  112. body : function () {
  113. function validate(constructor, elements, data) {
  114. var sab = new SharedArrayBuffer(elements*constructor.BYTES_PER_ELEMENT);
  115. var view = new constructor(sab);
  116. for ([index, test, expected] of data) {
  117. view[index] = test;
  118. assert.areEqual(view[index], expected, constructor.name);
  119. }
  120. };
  121. validate(Int8Array, 8, [[0, 10, 10], [7, 0x7F, 0x7F], [7, 0x80, -128], [1, 1000, -24]]);
  122. validate(Uint8Array, 8, [[0, 10, 10], [7, 0xFF, 0xFF], [7, -20, 236], [1, 1000, 232]]);
  123. validate(Int16Array, 8, [[0, 10, 10], [7, 0x7FFF, 0x7FFF], [7, 0x8000, -0x8000], [1, (0xFFFF + 1) *2 + 23, 23]]);
  124. validate(Uint16Array, 8, [[0, 10, 10], [7, 0xFFFF, 0xFFFF], [7, -120, 0x10000 - 120], [1, (0xFFFF + 1) *2 + 23, 23]]);
  125. validate(Int32Array, 8, [[0, 10, 10], [7, 0x7FFFFFFF, 0x7FFFFFFF], [7, 0x80000000, -0x80000000], [1, (0xFFFFFFFF + 1) *2 + 23, 23]]);
  126. validate(Uint32Array, 8, [[0, 10, 10], [7, 0xFFFFFFFF, 0xFFFFFFFF], [7, -20, 0xFFFFFFFF - 20 + 1], [1, (0xFFFFFFFF + 1) *2 + 23, 23]]);
  127. for (var i of typedArrayList) {
  128. validate(i, 8, [[-1, 20, undefined], ["-1", 20, undefined], [NaN, 20, undefined]]);
  129. }
  130. }
  131. },
  132. {
  133. name : "DataView functionality on SharedArrayBuffer",
  134. body : function () {
  135. function validate(dataView, setFunc, getFunc, offset, value) {
  136. dataView[setFunc](offset, value);
  137. assert.areEqual(value, dataView[getFunc](offset), "DataView "+ setFunc + " and " + getFunc + " validation");
  138. }
  139. [['setInt8', 'getInt8', 0x10], ['setUint8', 'getUint8', 0x9F], ['setInt16', 'getInt16', 0x6FFF],
  140. ['setUint16', 'getUint16', 0x9FFF], ['setInt32', 'getInt32', 0x6FFFFFFF], ['setUint32', 'getUint32', 0x9FFFFFFF],
  141. ['setFloat32', 'getFloat32', 2], ['setFloat64', 'getFloat64', 4]].forEach(function([setFunc, getFunc, v]) {
  142. [[0, 8], [8, 8]].forEach(function([byteOffset, byteLength]) {
  143. var sab = new SharedArrayBuffer(16);
  144. var dv = new DataView(sab, byteOffset, byteLength);
  145. validate(dv, setFunc, getFunc, 0, v);
  146. });
  147. });
  148. }
  149. },
  150. ];
  151. testRunner.runTests(tests, {
  152. verbose : WScript.Arguments[0] != "summary"
  153. });