Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at e2cebd539403475e2f4e79d55ca1a84a0ce3510d 84 lines 2.4 kB view raw
1import { describe, it } from 'node:test'; 2import assert from 'node:assert/strict'; 3import { uint64atomic } from 'moroutine'; 4 5describe('Uint64Atomic', () => { 6 it('self-allocates and initializes to zero', () => { 7 const a = uint64atomic(); 8 assert.equal(a.load(), 0n); 9 }); 10 11 it('stores and loads a value', () => { 12 const a = uint64atomic(); 13 a.store(18000000000000000000n); 14 assert.equal(a.load(), 18000000000000000000n); 15 }); 16 17 it('exposes byteSize of 8', () => { 18 assert.equal(uint64atomic.byteSize, 8); 19 }); 20 21 it('add returns previous value and updates', () => { 22 const a = uint64atomic(); 23 a.store(10n); 24 const prev = a.add(5n); 25 assert.equal(prev, 10n); 26 assert.equal(a.load(), 15n); 27 }); 28 29 it('sub returns previous value and updates', () => { 30 const a = uint64atomic(); 31 a.store(10n); 32 const prev = a.sub(3n); 33 assert.equal(prev, 10n); 34 assert.equal(a.load(), 7n); 35 }); 36 37 it('and returns previous value and updates', () => { 38 const a = uint64atomic(); 39 a.store(0b1100n); 40 const prev = a.and(0b1010n); 41 assert.equal(prev, 0b1100n); 42 assert.equal(a.load(), 0b1000n); 43 }); 44 45 it('or returns previous value and updates', () => { 46 const a = uint64atomic(); 47 a.store(0b1100n); 48 const prev = a.or(0b0011n); 49 assert.equal(prev, 0b1100n); 50 assert.equal(a.load(), 0b1111n); 51 }); 52 53 it('xor returns previous value and updates', () => { 54 const a = uint64atomic(); 55 a.store(0b1100n); 56 const prev = a.xor(0b1010n); 57 assert.equal(prev, 0b1100n); 58 assert.equal(a.load(), 0b0110n); 59 }); 60 61 it('exchange returns previous value and sets new', () => { 62 const a = uint64atomic(); 63 a.store(18000000000000000000n); 64 const prev = a.exchange(99n); 65 assert.equal(prev, 18000000000000000000n); 66 assert.equal(a.load(), 99n); 67 }); 68 69 it('compareExchange succeeds when expected matches', () => { 70 const a = uint64atomic(); 71 a.store(18000000000000000000n); 72 const actual = a.compareExchange(18000000000000000000n, 99n); 73 assert.equal(actual, 18000000000000000000n); 74 assert.equal(a.load(), 99n); 75 }); 76 77 it('compareExchange fails when expected does not match', () => { 78 const a = uint64atomic(); 79 a.store(18000000000000000000n); 80 const actual = a.compareExchange(0n, 99n); 81 assert.equal(actual, 18000000000000000000n); 82 assert.equal(a.load(), 18000000000000000000n); 83 }); 84});