diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/README.md b/lib/node_modules/@stdlib/number/uint64/ctor/README.md new file mode 100644 index 000000000000..f98ded1b6e01 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/README.md @@ -0,0 +1,217 @@ + + +# Uint64 + +> Unsigned 64-bit integer. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +``` + +#### Uint64( value ) + +Unsigned 64-bit integer constructor. + +```javascript +var x = new Uint64( 5 ); +// returns +``` + +* * * + +## Properties + +#### Uint64.name + +Static property returning the constructor name. + +```javascript +var str = Uint64.name; +// returns 'Uint64' +``` + +#### Uint64.BYTES_PER_ELEMENT + +Size (in bytes) of the underlying value. + +```javascript +var nbytes = Uint64.BYTES_PER_ELEMENT; +// returns 8 +``` + +#### Uint64.prototype.BYTES_PER_ELEMENT + +Size (in bytes) of the underlying value. + +```javascript +var x = new Uint64( 5 ); + +var nbytes = x.BYTES_PER_ELEMENT; +// returns 8 +``` + +### Instance + +A `Uint64` instance has the following methods... + +## Methods + +### Accessor Methods + +These methods do **not** mutate a `Uint64` instance and, instead return an unsigned 64-bit integer representation. + +#### Uint64.prototype.toString() + +Returns a string representation of a `Uint64` instance. + +```javascript +var x = new Uint64( 5 ); +var str = x.toString(); +// returns '5' +``` + +#### Uint64.prototype.toJSON() + +Returns a [JSON][json] representation of a `Uint64` instance. [`JSON.stringify()`][mdn-json-stringify] implicitly calls this method when stringifying a `Uint64` instance. + +```javascript +var x = new Uint64( 5 ); + +var o = x.toJSON(); +/* + { + "type": "Uint64", + "high": 0, + "low": 5 + } +*/ +``` + +To [revive][mdn-json-parse] a `Uint64` number from a [JSON][json] `string`, see [@stdlib/number/uint64/reviver][@stdlib/number/uint64/reviver]. + +#### Uint64.prototype.valueOf() + +Converts a `Uint64` instance to a primitive value. + +```javascript +var x = new Uint64( 5 ); +var v = x.valueOf(); +// returns 5.0 +``` + +
+ + + +* * * + + + +
+ +## Notes + +- The underlying value is stored as an unsigned 64-bit integer. +- An unsigned 64-bit integer has a range of \[`0`, `2^64-1`\]. + +
+ + + +* * * + + + +
+ +## Examples + + + +```javascript +var Uint64 = require( '@stdlib/number/uint64/ctor' ); + +var x = new Uint64( 1234 ); + +console.log( 'type: %s', typeof x ); +// => 'type: object' + +console.log( 'str: %s', x ); +// => 'str: 1234' + +console.log( 'JSON: %s', JSON.stringify( x ) ); +// => 'JSON: {"type":"Uint64","high":0,"low":1234}' +``` + +
+ + + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/benchmark/benchmark.js b/lib/node_modules/@stdlib/number/uint64/ctor/benchmark/benchmark.js new file mode 100644 index 000000000000..49c9acc899be --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/benchmark/benchmark.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randi = require( '@stdlib/random/base/randi' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var Uint64 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = new Uint64( i ); + if ( isnan( z ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !( z instanceof Uint64 ) ) { + b.fail( 'should return a Uint64 instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +// TODO: disabled because `value` should be private? + +/* +bench( format( '%s::get:value', pkg ), function benchmark( b ) { + var v; + var z; + var i; + + z = new Uint64( randu() ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = z.value; + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); +*/ + +bench( format( '%s:toString', pkg ), function benchmark( b ) { + var o; + var z; + var i; + + z = new Uint64( randi() ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = z.toString(); + if ( typeof o !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof o !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:toJSON', pkg ), function benchmark( b ) { + var o; + var z; + var i; + + z = new Uint64( randi() ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = z.toJSON(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/docs/repl.txt b/lib/node_modules/@stdlib/number/uint64/ctor/docs/repl.txt new file mode 100644 index 000000000000..e994f8b39a08 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/docs/repl.txt @@ -0,0 +1,63 @@ + +{{alias}}( value ) + Unsigned 64-bit integer constructor. + + Parameters + ---------- + value: number | string | Array | Uint32Array | BigInt + Numeric or BigInt or composite value. + + Returns + ------- + v: Uint64 + Unsigned 64-bit integer. + + Examples + -------- + > var x = new {{alias}}( 5 ) + + > x.toString() + '5' + + +{{alias}}.name + Constructor name. + + Examples + -------- + > var str = {{alias}}.name + 'Uint64' + + +{{alias}}.BYTES_PER_ELEMENT + Size (in bytes) of the underlying value. + + Returns + ------- + s: integer + Size (in bytes) of the underlying value. + + Examples + -------- + > var s = {{alias}}.BYTES_PER_ELEMENT + 8 + + +{{alias}}.prototype.BYTES_PER_ELEMENT + Size (in bytes) of the underlying value. + + Returns + ------- + s: integer + Size (in bytes) of the underlying value. + + Examples + -------- + > var x = new {{alias}}( 5 ) + + > var s = x.BYTES_PER_ELEMENT + 8 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/index.d.ts b/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/index.d.ts new file mode 100644 index 000000000000..ade92f613987 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/index.d.ts @@ -0,0 +1,142 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Unsigned 64-bit integer. +*/ +declare class Uint64 { + /** + * Unsigned 64-bit integer constructor. + * + * @param value - numeric value or string or word array or BigInt + * @returns unsigned 64-bit integer + * + * @example + * var x = new Uint64( 5 ); + * // returns + */ + constructor( value: number | string | Array | Uint32Array | BigInt ); + // TODO: do we need to import Uint32Array and BigInt here in this file as well? + + + /** + * Size (in bytes) of the underlying value. + * + * @returns size of the underlying value. + * + * @example + * var nbytes = Uint64.BYTES_PER_ELEMENT; + * // returns 8 + */ + readonly BYTES_PER_ELEMENT: 8; + + /** + * Serializes an unsigned 64-bit integer as a string. + * + * @param radix - output radix/base (integer from 2 to 36) + * @returns serialized unsigned 64-bit integer + * + * @example + * var x = new Uint64( 5 ); + * + * var str = x.toString(); + * // returns '5' + */ + toString( radix?: number ): string; + + /** + * Serializes an unsigned 64-bit integer as a JSON object. + * + * ## Notes + * + * - `JSON.stringify()` implicitly calls this method when stringifying a `Uint64` instance. + * + * + * @returns serialized unsigned 64-bit integer + * + * @example + * var x = new Uint64( 5 ); + * + * var obj = x.toJSON(); + * // returns { 'type': 'Uint64', 'high': 0, 'low': 5 } + */ + toJSON(): any; + + /** + * Converts an unsigned 64-bit integer to a primitive value. + * + * @returns primitive value + * + * @example + * var x = new Uint64( 5 ); + * + * var v = x.valueOf(); + * // returns 5.0 + */ + valueOf(): number; + + /** + * Returns the high 32-bit word of an unsigned 64-bit integer. + * + * @returns high 32-bit word + * + * @example + * var x = new Uint64( [ 1, 2 ] ); + * + * var w = x.getHighWord(); + * // returns 1 + */ + getHighWord(): number; + + /** + * Returns the low 32-bit word of an unsigned 64-bit integer. + * + * @returns low 32-bit word + * + * @example + * var x = new Uint64( [ 1, 2 ] ); + * + * var w = x.getLowWord(); + * // returns 2 + */ + getLowWord(): number; + + /** + * Converts an unsigned 64-bit integer to a BigInt. + * + * ## Notes + * + * - This method is only available in environments supporting BigInt. + * + * @returns BigInt value + * + * @example + * var x = new Uint64( [ 1, 2 ] ); + * + * var v = x.toBigInt().toString(); + * // returns '4294967298' + */ + toBigInt?(): bigint; +} + + +// EXPORTS // + +export = Uint64; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/test.ts b/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/test.ts new file mode 100644 index 000000000000..5185ec865317 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/docs/types/test.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions */ + +import Uint64 = require( './index' ); + + +// TESTS // + +// The function returns a Unsigned 64-bit integer with the expected properties... +{ + const x = new Uint64( 5 ); // $ExpectType Uint64 + + // x.value; // $ExpectType number + // TODO: delete later cz private + x.BYTES_PER_ELEMENT; // $ExpectType 8 +} + +// Unsigned 64-bit integer comes with a `toString` method to serialize a number as a string... +{ + const x = new Uint64( 5 ); // $ExpectType Uint64 + + x.toString(); // $ExpectType string +} + +// Unsigned 64-bit integer comes with a `toJSON` method to serialize a number as a JSON object.... +{ + const x = new Uint64( 5 ); // $ExpectType Uint64 + + x.toJSON(); // $ExpectType any +} + +// Unsigned 64-bit integer comes with a `valueOf` method to serialize a number to a primitive value... +{ + const x = new Uint64( 5 ); // $ExpectType Uint64 + + x.valueOf(); // $ExpectType number +} + +// The compiler throws an error if the constructor is invoked without the `new` keyword... +{ + Uint64( 5 ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided an unsupported number of arguments... +{ + new Uint64( ); // $ExpectError + new Uint64( 5, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/examples/index.js b/lib/node_modules/@stdlib/number/uint64/ctor/examples/index.js new file mode 100644 index 000000000000..c494b474cbc7 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Uint64 = require( './../lib' ); + +var x = new Uint64( 5 ); + +console.log( 'type: %s', typeof x ); +// => 'type: object' + +console.log( 'str: %s', x ); +// => 'str: 5' + +console.log( 'JSON: %s', JSON.stringify( x ) ); +// => 'JSON: {"type":"Uint64","high":0,"low":5}' diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/asbiguint64.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/asbiguint64.js new file mode 100644 index 000000000000..5fcce08cda6d --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/asbiguint64.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var BigInt = require( '@stdlib/bigint/ctor' ); + + +// MAIN // + +/** +* Truncates a BigInt value to 64 least significant bits as an unsigned integer. +* +* @private +* @param {BigInt} value - BigInt value to truncate +* @returns {BigInt} Unsigned 64-bit BigInt +* +* @example +* var x = asBigUint64( 0x0123456789abcdef0000n ).toString(16); +* // returns '456789abcdef0000' +*/ +function asBigUint64( value ) { + return BigInt.asUintN( 64, value ); +} + + +// EXPORTS // + +module.exports = asBigUint64; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/gethighword.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/gethighword.js new file mode 100644 index 000000000000..c760c44cb9ab --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/gethighword.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var BigInt = require( '@stdlib/bigint/ctor' ); +var Number = require( '@stdlib/number/ctor' ); + + +// MAIN // + +/** +* Returns the high 32-bit word of an unsigned 64-bit integer number. +* +* @private +* @returns {number} 32-bit unsigned integer +* +*/ +function getHighWord() { + /* eslint-disable no-invalid-this */ + if ( typeof this._raw === 'bigint' ) { + return Number( this._raw >> BigInt( 32 ) ); + } + + return this._raw[ 0 ]; +} + + +// EXPORTS // + +module.exports = getHighWord; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/getlowword.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/getlowword.js new file mode 100644 index 000000000000..a2dd998d01e8 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/getlowword.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var BigInt = require( '@stdlib/bigint/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); + + +// MAIN // + +/** +* Returns the low 32-bit word of an unsigned 64-bit integer number. +* +* @private +* @returns {number} 32-bit unsigned integer +* +*/ +function getLowWord() { + /* eslint-disable no-invalid-this */ + if ( typeof this._raw === 'bigint' ) { + return Number( this._raw & BigInt( UINT32_MAX ) ); + } + + return this._raw[ 1 ]; +} + + +// EXPORTS // + +module.exports = getLowWord; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/index.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/index.js new file mode 100644 index 000000000000..34c9e26b59a8 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Unsigned 64-bit integer constructor. +* +* @module @stdlib/number/uint64/ctor +* +* @example +* var Uint64 = require( '@stdlib/number/uint64/ctor' ); +* +* var x = new Uint64( 5 ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/main.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/main.js new file mode 100644 index 000000000000..460cc05e0748 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/main.js @@ -0,0 +1,327 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var BigInt = require( '@stdlib/bigint/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var format = require( '@stdlib/string/format' ); +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isBigInt = require( '@stdlib/assert/is-bigint' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isNan = require( '@stdlib/assert/is-nan' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isString = require( '@stdlib/assert/is-string' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var asBigUint64 = require( './asbiguint64.js' ); +var getHighWord = require( './gethighword.js' ); +var getLowWord = require( './getlowword.js' ); +var parseUint64dw = require( './parseuint64dw.js' ); +var toBigInt = require( './tobigint.js' ); +var toJSON = require( './tojson.js' ); +var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare +var valueOf = require( './valueof.js' ); // eslint-disable-line stdlib/no-redeclare + + +// VARIABLES // + +var TWO_32 = 0x100000000; +var hasBigInt = hasBigIntSupport(); + + +// MAIN // + +/** +* Unsigned 64-bit integer constructor. +* +* @constructor +* @param {number|string|Array|Uint32Array|BigInt} value - numeric value or high low words or BigInt +* @throws {TypeError} +* @returns {Uint64} Unsigned 64-bit integer +* +* @example // TODO: add more example +* var x = new Uint64( 5 ); +* // returns +*/ +function Uint64( value ) { + var raw; + var tmp; + + if ( !( this instanceof Uint64 ) ) { + throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' ); + } + + if ( isBigInt( value ) ) { + raw = asBigUint64( value ); + } + else if ( isUint32Array( value ) ) { + if ( value.length === 2 ) { + if ( hasBigInt ) { + raw = ( BigInt( value[ 0 ] ) << BigInt( 32 ) ) | BigInt( value[ 1 ] ); + } + else { + raw = value; // TODO: should we copy it instead? + } + } + else { + throw new TypeError( format( 'invalid argument. Provided Uint32Array must have a length of 2. Value: `%s`.', value ) ); + } + } + else if ( isArray( value ) ) { + if ( value.length === 2 ) { + if ( isInteger( value[ 0 ] ) && isInteger( value[ 1 ] ) && value[ 0 ] >= 0 && value[ 0 ] <= UINT32_MAX && value[ 1 ] >= 0 && value[ 1 ] <= UINT32_MAX ) { + if ( hasBigInt ) { + raw = ( BigInt( value[ 0 ] ) << BigInt( 32 ) ) | BigInt( value[ 1 ] ); + } + else { + raw = new Uint32Array( value ); + } + } + else { + throw new TypeError( format( 'invalid argument. Provided array must contain only integer values between 0 and %s. Value: `%s`.', UINT32_MAX, value ) ); + } + } + else { + throw new TypeError( format( 'invalid argument. Provided array must have a length of 2. Value: `%s`.', value ) ); + } + } + else if ( isString( value ) ) { + if ( value[ 0 ] === '-' ) { + throw new TypeError( format( 'invalid argument. Provided value must be non-negative. Value: `%s`.', value ) ); + } + + if ( hasBigInt ) { + try { + raw = asBigUint64( BigInt( value ) ); + } + catch ( error ) { // eslint-disable-line no-unused-vars + throw new TypeError( format( 'invalid argument. Could not convert string to Uint64. Value: `%s`.', value ) ); + } + } + else { + if ( value[ 0 ] === '+' ) { + tmp = value.slice( 1 ); + } + else { + tmp = value; + } + + tmp = parseUint64dw( tmp ); + + if ( !isNaN( tmp[ 0 ] ) && !isNan( tmp[ 1 ] ) ) { + raw = new Uint32Array( tmp ); + } + else { + throw new TypeError( format( 'invalid argument. Could not convert string to Uint64. Value: `%s`.', value ) ); + } + } + } + else if ( isNumber( value ) ) { + if ( value >= 0 && Number.isSafeInteger( value ) ) { + if ( hasBigInt ) { + raw = asBigUint64( BigInt( value ) ); + } + else { + raw = new Uint32Array( 2 ); + raw[ 0 ] = ( value / TWO_32 ) >>> 0; + raw[ 1 ] = value >>> 0; + } + } + else { + throw new TypeError( format( 'invalid argument. Provided number must be between 0 and %s. Value: `%s`.', Number.MAX_SAFE_INTEGER, value ) ); + } + } + else { + throw new TypeError( format( 'invalid argument. Must provide a number or string or Array or Uint32Array or BigInt. Value: `%s`.', value ) ); + } + + setReadOnly( this, '_raw', raw ); + return this; +} + +/** +* Constructor name. +* +* @name name +* @memberof Uint64 +* @readonly +* @type {string} +* @default 'Uint64' +* +* @example +* var name = Uint64.name; +* // returns 'Uint64' +*/ +setReadOnly( Uint64, 'name', 'Uint64' ); + +/** +* Size (in bytes) of the underlying value. +* +* @name BYTES_PER_ELEMENT +* @memberof Uint64 +* @type {integer} +* @returns {integer} size in bytes +* +* @example +* var nbytes = Uint64.BYTES_PER_ELEMENT; +* // returns 8 +*/ +setReadOnly( Uint64, 'BYTES_PER_ELEMENT', 8 ); + +/** +* Size (in bytes) of the underlying value. +* +* @name BYTES_PER_ELEMENT +* @memberof Uint64.prototype +* @type {integer} +* @returns {integer} size in bytes +* +* @example +* var x = new Uint64( 5 ); +* +* var nbytes = x.BYTES_PER_ELEMENT; +* // returns 8 +*/ +setReadOnly( Uint64.prototype, 'BYTES_PER_ELEMENT', 8 ); + +/** +* Serializes an unsigned 64-bit integer number as a string. +* +* @name toString +* @memberof Uint64.prototype +* @type {Function} +* @returns {string} serialized unsigned 64-bit integer number +* +* @example +* var str = new Uint64( 5 ).toString(); +* // returns '5' +* +* str = new Uint64( [ 1, 0 ] ).toString(); +* // returns '4294967296' +* +* str = new Uint64( 100000000001 ).toString(); +* // returns '100000000001' +* +*/ +setReadOnly( Uint64.prototype, 'toString', toString ); + +/** +* Serializes an unsigned 64-bit integer number as a JSON object. +* +* ## Notes +* +* - `JSON.stringify()` implicitly calls this method when stringifying a `Uint64` instance. +* +* @name toJSON +* @memberof Uint64.prototype +* @type {Function} +* @returns {Object} serialized unsigned 64-bit integer number +* +* @example +* var x = new Uint64( 5 ); +* +* var y = x.toJSON(); +* // returns { 'type': 'Uint64', 'high': 0, 'low': 5 } +*/ +setReadOnly( Uint64.prototype, 'toJSON', toJSON ); + +/** +* Converts an unsigned 64-bit integer number to a primitive value. +* +* @name valueOf +* @memberof Uint64.prototype +* @type {Function} +* @returns {number} primitive value +* +* @example +* var x = new Uint64( 5 ); +* +* var v = x.valueOf(); +* // returns 5.0 +*/ +setReadOnly( Uint64.prototype, 'valueOf', valueOf ); + +/** +* Returns the high 32-bit word of an unsigned 64-bit integer number. +* +* @name getHighWord +* @memberof Uint64.prototype +* @type {Function} +* @returns {number} 32-bit unsigned integer +* +* @example +* +* var x = new Uint64( [ 1234, 5678 ] ); +* +* var y = x.getHighWord(); +* // returns 1234.0 +*/ +setReadOnly( Uint64.prototype, 'getHighWord', getHighWord ); + +/** +* Returns the low 32-bit word of an unsigned 64-bit integer number. +* +* @name getLowWord +* @memberof Uint64.prototype +* @type {Function} +* @returns {number} 32-bit unsigned integer +* +* @example +* +* var x = new Uint64( [ 1234, 5678 ] ); +* +* var y = x.getLowWord(); +* // returns 5678.0 +*/ +setReadOnly( Uint64.prototype, 'getLowWord', getLowWord ); + +/** +* Converts an unsigned 64-bit integer number to a BigInt. +* +* @name toBigInt +* @memberof Uint64.prototype +* @type {Function} +* @returns {BigInt} BigInt value +* +* @example +* var hasBigInt = require( '@stdlib/assert/has-bigint-support' ); +* +* var x = new Uint64( 5 ); +* +* var v; +* if ( hasBigInt() ) { +* v = x.toBigInt(); +* // returns 5.0 +* } +*/ +if ( hasBigInt ) { + setReadOnly( Uint64.prototype, 'toBigInt', toBigInt ); +} + + +// EXPORTS // + +module.exports = Uint64; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/parseuint64dw.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/parseuint64dw.js new file mode 100644 index 000000000000..3d151a0f75c0 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/parseuint64dw.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Number = require( '@stdlib/number/ctor' ); +var max = require( '@stdlib/math/base/special/max' ); +var trim = require( '@stdlib/string/trim' ); + + +// VARIABLES // + +var TWO_32 = 0x100000000; +var RE_HEX = /^0x/i; +var RE_OCT = /^0o/i; +var RE_BIN = /^0b/i; +var RE_INV = /[^0-9]/; // Invalid decimal integer + + +// MAIN // + +/** +* Parses an arbitrary-length string representation of an unsigned integer and converts to double word representation of unsigned 64 bit integer. +* +* @private +* @param {string} value - string representation of an unsigned integer +* @returns {Uint32Array} Unsigned 64-bit integer as high and low words +* +* @example +* var x = parseUint64dw( '0xffffffff0000ffff' ); +* // returns [ 4294967295, 65535 ] +* +* x = parseUint64dw( '0x1ffffffff0000ffff' ); +* // returns [ 4294967295, 65535 ] +* +* x = parseUint64dw( '0xffff' ); +* // returns [ 0, 65535 ] +* +* x = parseUint64dw( '0o100' ); +* // returns [ 0, 64 ] +* +* x = parseUint64dw( '0b100' ); +* // returns [ 0, 4 ] +* +* x = parseUint64dw( '4294967296' ); +* // returns [ 1, 0 ] +*/ +function parseUint64dw( value ) { + var high; + var low; + var tmp; + var j; + var i; + + value = trim( value ); + if ( RE_HEX.test( value ) ) { + j = max( 2, value.length - 8 ); + i = max( 2, value.length - 16 ); + high = Number( '0x0' + value.slice( i, j ) ); + low = Number( '0x' + value.slice( j ) ); + } + else if ( RE_OCT.test( value ) ) { + j = max( 2, value.length - 16 ); + i = max( 2, value.length - 32 ); + high = Number( '0o0' + value.slice( i, j ) ); + low = Number( '0o' + value.slice( j ) ); + } + else if ( RE_BIN.test( value ) ) { + j = max( 2, value.length - 32 ); + i = max( 2, value.length - 64 ); + high = Number( '0b0' + value.slice( i, j ) ); + low = Number( '0b' + value.slice( j ) ); + } + else if ( RE_INV.test( value ) ) { + high = low = NaN; + } + else { + // Chunked parsing for decimal string + i = ( value.length % 6 ) || 6; // Processes the first chunk such as the remaining chunks are all evenly sized (6digit) + if ( i + 6 <= 9 ) { // Takes big chunk when possible + i += 6; + } + high = 0; + low = Number( value.slice( 0, i ) ); + for ( ; i < value.length; i += 6 ) { + tmp = Number( value.slice( i, i + 6 ) ); + low = ( low * 1e6 ) + tmp; + tmp = ( low / TWO_32 ) >>> 0; + low -= tmp * TWO_32; + high = ( high * 1e6 ) + tmp; + tmp = ( high / TWO_32 ) >>> 0; + high -= tmp * TWO_32; + } + } + + return [ high, low ]; +} + + +// EXPORTS // + +module.exports = parseUint64dw; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/tobigint.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tobigint.js new file mode 100644 index 000000000000..419992e0f880 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tobigint.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Converts an unsigned 64-bit integer to a BigInt. +* +* @private +* @returns {BigInt} BigInt value +*/ +function toBigInt() { + /* eslint-disable no-invalid-this */ + if ( typeof this._raw === 'bigint' ) { + return this._raw; + } +} + + +// EXPORTS // + +module.exports = toBigInt; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/tojson.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tojson.js new file mode 100644 index 000000000000..c04d4fbebacf --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tojson.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Serializes an unsigned 64-bit integer as a JSON object. +* +* @private +* @returns {Object} JSON representation +*/ +function toJSON() { + /* eslint-disable no-invalid-this */ + var out = {}; + out.type = 'Uint64'; + out.high = this.getHighWord(); + out.low = this.getLowWord(); + return out; +} + + +// EXPORTS // + +module.exports = toJSON; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/tostring.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tostring.js new file mode 100644 index 000000000000..9c2848b16bc1 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/tostring.js @@ -0,0 +1,264 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var format = require( '@stdlib/string/format' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var lpad = require( '@stdlib/string/left-pad' ); + + +// VARIABLES // + +var TWO_16 = 0x10000; +var TWO_32 = 0x100000000; + +var POWERMAP = { + '2': { + 'power': 32, + 'divisor': 4294967296 + }, + '3': { + 'power': 20, + 'divisor': 3486784401 + }, + '4': { + 'power': 16, + 'divisor': 4294967296 + }, + '5': { + 'power': 13, + 'divisor': 1220703125 + }, + '6': { + 'power': 12, + 'divisor': 2176782336 + }, + '7': { + 'power': 11, + 'divisor': 1977326743 + }, + '8': { + 'power': 10, + 'divisor': 1073741824 + }, + '9': { + 'power': 10, + 'divisor': 3486784401 + }, + '10': { + 'power': 9, + 'divisor': 1000000000 + }, + '11': { + 'power': 9, + 'divisor': 2357947691 + }, + '12': { + 'power': 8, + 'divisor': 429981696 + }, + '13': { + 'power': 8, + 'divisor': 815730721 + }, + '14': { + 'power': 8, + 'divisor': 1475789056 + }, + '15': { + 'power': 8, + 'divisor': 2562890625 + }, + '16': { + 'power': 8, + 'divisor': 4294967296 + }, + '17': { + 'power': 7, + 'divisor': 410338673 + }, + '18': { + 'power': 7, + 'divisor': 612220032 + }, + '19': { + 'power': 7, + 'divisor': 893871739 + }, + '20': { + 'power': 7, + 'divisor': 1280000000 + }, + '21': { + 'power': 7, + 'divisor': 1801088541 + }, + '22': { + 'power': 7, + 'divisor': 2494357888 + }, + '23': { + 'power': 7, + 'divisor': 3404825447 + }, + '24': { + 'power': 6, + 'divisor': 191102976 + }, + '25': { + 'power': 6, + 'divisor': 244140625 + }, + '26': { + 'power': 6, + 'divisor': 308915776 + }, + '27': { + 'power': 6, + 'divisor': 387420489 + }, + '28': { + 'power': 6, + 'divisor': 481890304 + }, + '29': { + 'power': 6, + 'divisor': 594823321 + }, + '30': { + 'power': 6, + 'divisor': 729000000 + }, + '31': { + 'power': 6, + 'divisor': 887503681 + }, + '32': { + 'power': 6, + 'divisor': 1073741824 + }, + '33': { + 'power': 6, + 'divisor': 1291467969 + }, + '34': { + 'power': 6, + 'divisor': 1544804416 + }, + '35': { + 'power': 6, + 'divisor': 1838265625 + }, + '36': { + 'power': 6, + 'divisor': 2176782336 + } +}; + +/** +* Performs division and modulo for an unsigned 64-bit integer represented as high-low words. +* +* @private +* @param {Uint32Array|Array} words - [high, low] 32-bit words +* @param {number} divisor - positive integer close to `2^32` (actually works for 2^11 to 2^37) +* @returns {Array} [ quotient, remainder ] +* +* @example +* var x = chunkedDivMod( [ 1, 0 ], 1e9 ); +* // returns [ 4, 294967296 ] +*/ +function chunkedDivMod( words, divisor ) { + var high; + var low; + var quo; + var rem; + var qrd; + + if (divisor === TWO_32) { + return words; + } + + high = words[0] >>> 0; + low = words[1] >>> 0; + quo = ( high / divisor ) >>> 0; + rem = high - ( divisor * quo ); + + rem = ( rem * TWO_16 ) + ( low >>> 16 ); + qrd = ( rem / divisor ) >>> 0; + quo = ( quo * TWO_16 ) + qrd; + rem -= divisor * qrd; + + rem = ( rem * TWO_16 ) + ( low & 0xffff ); + qrd = ( rem / divisor ) >>> 0; + quo = ( quo * TWO_16 ) + qrd; + rem -= divisor * qrd; + + return [quo, rem]; +} + + +// MAIN // + +/** +* Serializes an unsigned 64-bit integer as a string in the specified base. +* +* @param {number} [radix] - The radix (base) to use for string conversion (2-36). Defaults to 10 if not provided. +* @throws {TypeError} Throws an error if radix is not an integer between 2 and 36. +* @returns {string} The serialized unsigned 64-bit integer as a string. +* +*/ +function toString( radix ) { // eslint-disable-line stdlib/no-redeclare + var res; + var quo; + var rem; + + if ( radix === undefined ) { // eslint-disable-line no-undefined + radix = 10; + } + else if ( !isInteger( radix ) || radix < 2 || radix > 36 ) { + throw new TypeError( format( 'invalid argument. Provided radix must be an integer between 2 and 36. Radix: `%s`.', radix ) ); + } + + /* eslint-disable no-invalid-this */ + if ( typeof this._raw === 'bigint' ) { + return this._raw.toString( radix ); + } + + // For non-BigInt mode, the conversion uses a precomputed POWERMAP to select a divisor and chunk size for efficient base conversion. + // The divisor values are chosen to be a power of the radix closest to 2^32 for efficient chunked conversion. + res = chunkedDivMod( this._raw, POWERMAP[ radix ].divisor ); + quo = res[ 0 ].toString( radix ); + rem = res[ 1 ].toString( radix ); + + if ( quo === '0' ) { + quo = ''; + } + else { + rem = lpad( rem, POWERMAP[ radix ].power, '0' ); + } + + return quo + rem; +} + + +// EXPORTS // + +module.exports = toString; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/lib/valueof.js b/lib/node_modules/@stdlib/number/uint64/ctor/lib/valueof.js new file mode 100644 index 000000000000..9f7941132b6b --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/lib/valueof.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Number = require( '@stdlib/number/ctor' ); + + +// VARIABLES // + +var TWO_32 = 0x100000000; + + +// MAIN // + +/** +* Converts an unsigned 64-bit integer to a primitive value. +* +* @private +* @returns {number} primitive value +*/ +function valueOf() { // eslint-disable-line stdlib/no-redeclare + /* eslint-disable no-invalid-this */ + if ( typeof this._raw === 'bigint' ) { + return Number( this._raw ); + } + + return ( this._raw[ 0 ] * TWO_32 ) + this._raw[ 1 ]; +} + + +// EXPORTS // + +module.exports = valueOf; diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/package.json b/lib/node_modules/@stdlib/number/uint64/ctor/package.json new file mode 100644 index 000000000000..b7753d345528 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/number/uint64/ctor", + "version": "0.0.0", + "description": "Unsigned 64-bit integer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "constructor", + "ctor", + "uint64", + "unsigned", + "64-bit", + "integer", + "int" + ] +} diff --git a/lib/node_modules/@stdlib/number/uint64/ctor/test/test.js b/lib/node_modules/@stdlib/number/uint64/ctor/test/test.js new file mode 100644 index 000000000000..aac20877d244 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/ctor/test/test.js @@ -0,0 +1,404 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-unused-vars */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var parseUint64dw = require( './../lib/parseuint64dw.js' ); +var toString = require( './../lib/tostring.js' ); // eslint-disable-line stdlib/no-redeclare +var Uint64 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Uint64, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function is a constructor', function test( t ) { + var x = new Uint64( 5 ); + t.strictEqual( x instanceof Uint64, true, 'is an instance' ); + t.end(); +}); + +tape( 'the constructor throws an error if not provided a number or string or Array or Uint32Array or BigInt', function test( t ) { + var values; + var i; + + values = [ + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var x = new Uint64( value ); + }; + } +}); + +tape( 'the constructor requires the `new` keyword', function test( t ) { + var ctor = Uint64; + t.throws( foo, TypeError, 'throws an error' ); + t.end(); + + function foo() { + ctor( 5 ); + } +}); + +tape( 'the constructor has a read-only `name` property', function test( t ) { + t.strictEqual( hasOwnProp( Uint64, 'name' ), true, 'has property' ); + t.strictEqual( Uint64.name, 'Uint64', 'returns expected value' ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + Uint64.name = 'Foo'; + } +}); + +tape( 'the constructor has a read-only `BYTES_PER_ELEMENT` property', function test( t ) { + t.strictEqual( hasOwnProp( Uint64, 'BYTES_PER_ELEMENT' ), true, 'has property' ); + t.strictEqual( Uint64.BYTES_PER_ELEMENT, 8, 'returns expected value' ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + Uint64.BYTES_PER_ELEMENT = 4; + } +}); + +tape( 'the constructor prototype has a read-only `BYTES_PER_ELEMENT` property', function test( t ) { + t.strictEqual( hasOwnProp( Uint64.prototype, 'BYTES_PER_ELEMENT' ), true, 'has property' ); + t.strictEqual( Uint64.prototype.BYTES_PER_ELEMENT, 8, 'returns expected value' ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + Uint64.prototype.BYTES_PER_ELEMENT = 4; + } +}); + +tape( 'the constructor stores an internal read-only `_raw` representation', function test( t ) { + var x = new Uint64( 5 ); + t.strictEqual( hasOwnProp( x, '_raw' ), true, 'has property' ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + x._raw = 1; // eslint-disable-line no-underscore-dangle + } +}); + +tape( 'if provided a BigInt, the constructor stores the lower 64 bits', function test( t ) { + var x; + + if ( !hasBigIntSupport() ) { + t.pass( 'environment does not support BigInt' ); + t.end(); + return; + } + + x = new Uint64( BigInt( '18446744073709551616' ) ); + t.strictEqual( x.toString(), '0', 'returns expected value' ); + + x = new Uint64( BigInt( '18446744073709551615' ) ); + t.strictEqual( x.toString(), '18446744073709551615', 'returns expected value' ); + + x = new Uint64( BigInt( '5' ) ); + t.strictEqual( x.toString(), '5', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a Uint32Array of length 2, the constructor stores the expected numeric value', function test( t ) { + var x = new Uint64( new Uint32Array( [ 1, 2 ] ) ); + t.strictEqual( x.toString(), '4294967298', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a Uint32Array which does not have length 2, the constructor throws an error', function test( t ) { + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + var x = new Uint64( new Uint32Array( 1 ) ); + } +}); + +tape( 'if provided an array of two valid words, the constructor stores the expected numeric value', function test( t ) { + var x = new Uint64( [ 1, 2 ] ); + t.strictEqual( x.toString(), '4294967298', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an invalid word array, the constructor throws an error', function test( t ) { + var values; + var i; + + values = [ + [ 1 ], + [ 1.9, 2 ], + [ '1', 2 ], + [ -1, 2 ], + [ UINT32_MAX + 1, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var x = new Uint64( value ); + }; + } +}); + +tape( 'if provided a valid unsigned integer string, the constructor stores the lower 64 bits', function test( t ) { + var x; + + x = new Uint64( '18446744073709551616' ); + t.strictEqual( x.toString(), '0', 'returns expected value' ); + + x = new Uint64( '18446744073709551615' ); + t.strictEqual( x.toString(), '18446744073709551615', 'returns expected value' ); + + x = new Uint64( '5' ); + t.strictEqual( x.toString(), '5', 'returns expected value' ); + + // TODO: Should empty string be supported? + x = new Uint64( '' ); + t.strictEqual( x.toString(), '0', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an invalid unsigned integer string, the constructor throws an error', function test( t ) { + var values; + var i; + + values = [ + 'beep boop', + '-5', + '0x', + '0b2', + '4a5' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var x = new Uint64( value ); + }; + } +}); + +tape( 'if provided a valid number, the constructor stores the expected numeric value', function test( t ) { + var x; + + x = new Uint64( 4294967301 ); + t.strictEqual( x.toString(), '4294967301', 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an invalid number, the constructor throws an error', function test( t ) { + var values; + var i; + + values = [ + -1, + 3.14, + Number.MAX_SAFE_INTEGER + 1 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var x = new Uint64( value ); + }; + } +}); + +tape( 'instances support serializing to string for supported radices', function test( t ) { + var x; + + x = new Uint64( 5 ); + t.strictEqual( x.toString(), '5', 'returns expected value' ); + t.strictEqual( x.toString( 16 ), '5', 'returns expected value' ); + + x = new Uint64( [ 1, 0 ] ); + t.strictEqual( x.toString( 16 ), '100000000', 'returns expected value' ); + t.end(); +}); + +tape( 'instances throw when provided an invalid radix', function test( t ) { + var values; + var i; + var x = new Uint64( 5 ); + + values = [ + 1, + -1, + 2.5, + 37 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var y = x.toString( value ); + }; + } +}); + +tape( 'the constructor returns an instance which supports serializing an instance as a JSON object', function test( t ) { + var expected; + var x; + + x = new Uint64( 5 ); + expected = { + 'type': 'Uint64', + 'high': 0, + 'low': 5 + }; + t.deepEqual( x.toJSON(), expected, 'returns expected value' ); + t.strictEqual( JSON.stringify( x ), JSON.stringify( expected ), 'returns expected value' ); + + x = new Uint64( [ 1, 2 ] ); + expected = { + 'type': 'Uint64', + 'high': 1, + 'low': 2 + }; + t.deepEqual( x.toJSON(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'instances support converting to a primitive number', function test( t ) { + var x; + + x = new Uint64( 5 ); + t.strictEqual( x.valueOf(), 5, 'returns expected value' ); + + x = new Uint64( [ UINT32_MAX, UINT32_MAX ] ); + t.strictEqual( x.valueOf(), 18446744073709552000, 'returns expected value' ); + + x = new Uint64( Number.MAX_SAFE_INTEGER ); + t.strictEqual( x.valueOf(), Number.MAX_SAFE_INTEGER, 'returns expected value' ); + + t.end(); +}); + +tape( 'instances expose high and low word accessors', function test( t ) { + var x; + + x = new Uint64( [ 1, 2 ] ); + t.strictEqual( x.getHighWord(), 1, 'returns expected value' ); + t.strictEqual( x.getLowWord(), 2, 'returns expected value' ); + t.end(); +}); + +tape( 'if BigInt is supported, instances provide `toBigInt()`', function test( t ) { + var x; + + if ( !hasBigIntSupport() ) { + t.strictEqual( hasOwnProp( Uint64.prototype, 'toBigInt' ), false, 'prototype does not provide `toBigInt`' ); + t.end(); + return; + } + t.strictEqual( hasOwnProp( Uint64.prototype, 'toBigInt' ), true, 'prototype provides `toBigInt`' ); + + x = new Uint64( [ 1, 2 ] ); + t.strictEqual( x.toBigInt().toString(), '4294967298', 'returns expected value' ); + t.end(); +}); + +tape( '`parseUint64dw()` parses prefixed, whitespace-padded, decimal, and invalid strings', function test( t ) { + var out; + + out = parseUint64dw( '0xffffffff0000ffff' ); + t.deepEqual( out, [ 4294967295, 65535 ], 'parses hex word pair' ); + + out = parseUint64dw( '0x1ffffffff0000ffff' ); + t.deepEqual( out, [ 4294967295, 65535 ], 'truncates over-wide hex to 64 bits' ); + + out = parseUint64dw( '0xffff' ); + t.deepEqual( out, [ 0, 65535 ], 'parses short hex string' ); + + out = parseUint64dw( '0o100' ); + t.deepEqual( out, [ 0, 64 ], 'parses octal string' ); + + out = parseUint64dw( '0b100' ); + t.deepEqual( out, [ 0, 4 ], 'parses binary string' ); + + out = parseUint64dw( '4294967296' ); + t.deepEqual( out, [ 1, 0 ], 'parses decimal into high/low words' ); + + out = parseUint64dw( ' 123456 ' ); + t.deepEqual( out, [ 0, 123456 ], 'parses whitespace-padded decimal string' ); + + out = parseUint64dw( 'beep boop' ); + t.ok( isNaN( out[ 0 ] ) && isNaN( out[ 1 ] ), 'returns NaN words for invalid input' ); + t.end(); +}); + +tape( '`toString()` helper supports Uint32Array backed objects', function test( t ) { + var x; + + x = { + '_raw': new Uint32Array( [ 1, 0 ] ) + }; + t.strictEqual( toString.call( x, 16 ), '100000000', 'returns expected hexadecimal string' ); + t.strictEqual( toString.call( x, 10 ), '4294967296', 'returns expected decimal string' ); + + x = { + '_raw': new Uint32Array( [ 0, 100 ] ) + }; + t.strictEqual( toString.call( x, 3 ), '10201', 'returns expected string' ); + t.end(); +});