summaryrefslogtreecommitdiffstats
path: root/vendor/wasm-bindgen/tests/wasm/bigint.js
blob: 3971d2279d4cdedcdc7f4a6f92ebd2b263e40dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');

exports.i64_js_identity = a => a;
exports.u64_js_identity = a => a;

exports.js_works = () => {
    assert.strictEqual(wasm.zero(), BigInt('0'));
    assert.strictEqual(wasm.one(), BigInt('1'));
    assert.strictEqual(wasm.neg_one(), BigInt('-1'));
    assert.strictEqual(wasm.i32_min(), BigInt('-2147483648'));
    assert.strictEqual(wasm.u32_max(), BigInt('4294967295'));
    assert.strictEqual(wasm.i64_min(), BigInt('-9223372036854775808'));
    assert.strictEqual(wasm.u64_max(), BigInt('18446744073709551615'));

    const i64_min = BigInt('-9223372036854775808');
    const u64_max = BigInt('18446744073709551615');

    const identityTestI64Values = [
        BigInt('0'),
        BigInt('1'),
        BigInt('-1'),
        i64_min,
    ];
    for (const value of identityTestI64Values) {
        assert.strictEqual(wasm.i64_rust_identity(value), value);
        assert.strictEqual(wasm.i64_jsvalue_identity(value), value);
    }

    const identityTestU64Values = [
        BigInt('0'),
        BigInt('1'),
        u64_max,
    ];
    for (const value of identityTestU64Values) {
        assert.strictEqual(wasm.u64_rust_identity(value), value);
        assert.strictEqual(wasm.u64_jsvalue_identity(value), value);
    }

    assert.strictEqual(wasm.u64_rust_identity(BigInt('1') << BigInt('64')), BigInt('0'));
    assert.strictEqual(wasm.i128_min_jsvalue(), BigInt('-170141183460469231731687303715884105728'));
    assert.strictEqual(wasm.u128_max_jsvalue(), BigInt('340282366920938463463374607431768211455'));

    assert.deepStrictEqual(wasm.u64_slice([]), new BigUint64Array());
    assert.deepStrictEqual(wasm.i64_slice([]), new BigInt64Array());
    const arr1 = new BigUint64Array([BigInt('1'), BigInt('2')]);
    assert.deepStrictEqual(wasm.u64_slice([BigInt('1'), BigInt('2')]), arr1);
    const arr2 = new BigInt64Array([BigInt('1'), BigInt('2')]);
    assert.deepStrictEqual(wasm.i64_slice([BigInt('1'), BigInt('2')]), arr2);

    assert.deepStrictEqual(wasm.i64_slice([i64_min]), new BigInt64Array([i64_min]));
    assert.deepStrictEqual(wasm.u64_slice([u64_max]), new BigUint64Array([u64_max]));
};