Atomics.exchange.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. test("invariants", () => {
  2. expect(Atomics.exchange).toHaveLength(3);
  3. });
  4. test("error cases", () => {
  5. expect(() => {
  6. Atomics.exchange("not an array", 0, 0);
  7. }).toThrow(TypeError);
  8. expect(() => {
  9. const bad_array_type = new Float32Array(4);
  10. Atomics.exchange(bad_array_type, 0, 0);
  11. }).toThrow(TypeError);
  12. expect(() => {
  13. const bad_array_type = new Uint8ClampedArray(4);
  14. Atomics.exchange(bad_array_type, 0, 0);
  15. }).toThrow(TypeError);
  16. expect(() => {
  17. const array = new Int32Array(4);
  18. Atomics.exchange(array, 100, 0);
  19. }).toThrow(RangeError);
  20. });
  21. test("basic functionality (non-BigInt)", () => {
  22. [Int8Array, Int16Array, Int32Array, Uint8Array, Uint16Array, Uint32Array].forEach(ArrayType => {
  23. const array = new ArrayType(4);
  24. array[0] = 1;
  25. array[1] = 2;
  26. array[2] = 3;
  27. array[3] = 4;
  28. expect(Atomics.exchange(array, 0, 5)).toBe(1);
  29. expect(array).toEqual([5, 2, 3, 4]);
  30. expect(Atomics.exchange(array, 0, 6)).toBe(5);
  31. expect(array).toEqual([6, 2, 3, 4]);
  32. expect(Atomics.exchange(array, "1", 7)).toBe(2);
  33. expect(array).toEqual([6, 7, 3, 4]);
  34. expect(Atomics.exchange(array, 2, "8")).toBe(3);
  35. expect(array).toEqual([6, 7, 8, 4]);
  36. expect(Atomics.exchange(array, 3.14, 9)).toBe(4);
  37. expect(array).toEqual([6, 7, 8, 9]);
  38. });
  39. });
  40. test("basic functionality (BigInt)", () => {
  41. [BigInt64Array, BigUint64Array].forEach(ArrayType => {
  42. const array = new ArrayType(4);
  43. array[0] = 1n;
  44. array[1] = 2n;
  45. array[2] = 3n;
  46. array[3] = 4n;
  47. expect(Atomics.exchange(array, 0, 5n)).toBe(1n);
  48. expect(array).toEqual([5n, 2n, 3n, 4n]);
  49. expect(Atomics.exchange(array, 0, 6n)).toBe(5n);
  50. expect(array).toEqual([6n, 2n, 3n, 4n]);
  51. expect(Atomics.exchange(array, 1, 7n)).toBe(2n);
  52. expect(array).toEqual([6n, 7n, 3n, 4n]);
  53. expect(Atomics.exchange(array, 2, 8n)).toBe(3n);
  54. expect(array).toEqual([6n, 7n, 8n, 4n]);
  55. expect(Atomics.exchange(array, 3, 9n)).toBe(4n);
  56. expect(array).toEqual([6n, 7n, 8n, 9n]);
  57. });
  58. });