testAtomics.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) 2017 Mozilla Corporation. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: |
  5. Collection of functions used to assert the correctness of SharedArrayBuffer objects.
  6. ---*/
  7. /**
  8. * Calls the provided function for a each bad index that should throw a
  9. * RangeError when passed to an Atomics method on a SAB-backed view where
  10. * index 125 is out of range.
  11. *
  12. * @param f - the function to call for each bad index.
  13. */
  14. function testWithAtomicsOutOfBoundsIndices(f) {
  15. var bad_indices = [
  16. function(view) { return -1; },
  17. function(view) { return view.length; },
  18. function(view) { return view.length * 2; },
  19. function(view) { return Number.POSITIVE_INFINITY; },
  20. function(view) { return Number.NEGATIVE_INFINITY; },
  21. function(view) { return { valueOf: function() { return 125; } }; },
  22. function(view) { return { toString: function() { return '125'; }, valueOf: false }; }, // non-callable valueOf triggers invocation of toString
  23. ];
  24. for (var i = 0; i < bad_indices.length; ++i) {
  25. var IdxGen = bad_indices[i];
  26. try {
  27. f(IdxGen);
  28. } catch (e) {
  29. e.message += ' (Testing with index gen ' + IdxGen + '.)';
  30. throw e;
  31. }
  32. }
  33. }
  34. /**
  35. * Calls the provided function for each good index that should not throw when
  36. * passed to an Atomics method on a SAB-backed view.
  37. *
  38. * The view must have length greater than zero.
  39. *
  40. * @param f - the function to call for each good index.
  41. */
  42. function testWithAtomicsInBoundsIndices(f) {
  43. // Most of these are eventually coerced to +0 by ToIndex.
  44. var good_indices = [
  45. function(view) { return 0/-1; },
  46. function(view) { return '-0'; },
  47. function(view) { return undefined; },
  48. function(view) { return NaN; },
  49. function(view) { return 0.5; },
  50. function(view) { return '0.5'; },
  51. function(view) { return -0.9; },
  52. function(view) { return { password: 'qumquat' }; },
  53. function(view) { return view.length - 1; },
  54. function(view) { return { valueOf: function() { return 0; } }; },
  55. function(view) { return { toString: function() { return '0'; }, valueOf: false }; }, // non-callable valueOf triggers invocation of toString
  56. ];
  57. for (var i = 0; i < good_indices.length; ++i) {
  58. var IdxGen = good_indices[i];
  59. try {
  60. f(IdxGen);
  61. } catch (e) {
  62. e.message += ' (Testing with index gen ' + IdxGen + '.)';
  63. throw e;
  64. }
  65. }
  66. }
  67. /**
  68. * Calls the provided function for each value that should throw a TypeError
  69. * when passed to an Atomics method as a view.
  70. *
  71. * @param f - the function to call for each non-view value.
  72. */
  73. function testWithAtomicsNonViewValues(f) {
  74. var values = [
  75. null,
  76. undefined,
  77. true,
  78. false,
  79. new Boolean(true),
  80. 10,
  81. 3.14,
  82. new Number(4),
  83. 'Hi there',
  84. new Date,
  85. /a*utomaton/g,
  86. { password: 'qumquat' },
  87. new DataView(new ArrayBuffer(10)),
  88. new ArrayBuffer(128),
  89. new SharedArrayBuffer(128),
  90. new Error('Ouch'),
  91. [1,1,2,3,5,8],
  92. function(x) { return -x; },
  93. Symbol('halleluja'),
  94. // TODO: Proxy?
  95. Object,
  96. Int32Array,
  97. Date,
  98. Math,
  99. Atomics
  100. ];
  101. for (var i = 0; i < values.length; ++i) {
  102. var nonView = values[i];
  103. try {
  104. f(nonView);
  105. } catch (e) {
  106. e.message += ' (Testing with non-view value ' + nonView + '.)';
  107. throw e;
  108. }
  109. }
  110. }