Atomics.sub.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. test("invariants", () => {
  2. expect(Atomics.sub).toHaveLength(3);
  3. });
  4. test("error cases", () => {
  5. expect(() => {
  6. Atomics.sub("not an array", 0, 1);
  7. }).toThrow(TypeError);
  8. expect(() => {
  9. const bad_array_type = new Float32Array(4);
  10. Atomics.sub(bad_array_type, 0, 1);
  11. }).toThrow(TypeError);
  12. expect(() => {
  13. const bad_array_type = new Uint8ClampedArray(4);
  14. Atomics.sub(bad_array_type, 0, 1);
  15. }).toThrow(TypeError);
  16. expect(() => {
  17. const array = new Int32Array(4);
  18. Atomics.sub(array, 100, 1);
  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.sub(array, 0, 1)).toBe(1);
  29. expect(array).toEqual([0, 2, 3, 4]);
  30. expect(Atomics.sub(array, 1, 1)).toBe(2);
  31. expect(array).toEqual([0, 1, 3, 4]);
  32. expect(Atomics.sub(array, 1, 1)).toBe(1);
  33. expect(array).toEqual([0, 0, 3, 4]);
  34. expect(Atomics.sub(array, 2, 3.14)).toBe(3);
  35. expect(array).toEqual([0, 0, 0, 4]);
  36. expect(Atomics.sub(array, 3, "1")).toBe(4);
  37. expect(array).toEqual([0, 0, 0, 3]);
  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.sub(array, 0, 1n)).toBe(1n);
  48. expect(array).toEqual([0n, 2n, 3n, 4n]);
  49. expect(Atomics.sub(array, 1, 1n)).toBe(2n);
  50. expect(array).toEqual([0n, 1n, 3n, 4n]);
  51. expect(Atomics.sub(array, 1, 1n)).toBe(1n);
  52. expect(array).toEqual([0n, 0n, 3n, 4n]);
  53. expect(Atomics.sub(array, 2, 3n)).toBe(3n);
  54. expect(array).toEqual([0n, 0n, 0n, 4n]);
  55. expect(Atomics.sub(array, 3, 1n)).toBe(4n);
  56. expect(array).toEqual([0n, 0n, 0n, 3n]);
  57. });
  58. });