diff --git a/lib/node_modules/@stdlib/ndarray/to-string/README.md b/lib/node_modules/@stdlib/ndarray/to-string/README.md new file mode 100644 index 000000000000..d2caacb2ad00 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/README.md @@ -0,0 +1,160 @@ + + +# ndarray2string + +> Serialize an [ndarray][@stdlib/ndarray/ctor] as a string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var ndarray2string = require( '@stdlib/ndarray/to-string' ); +``` + +#### ndarray2string( x ) + +Serializes an [ndarray][@stdlib/ndarray/ctor] as a string. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var buffer = [ 1, 2, 3, 4 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +var x = ndarray( 'generic', buffer, shape, strides, offset, order ); +// returns + +var str = ndarray2string( x ); +// returns "ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' )" +``` + +
+ + + + + +
+ +## Notes + +- The function does **not** serialize data outside of the buffer defined by the [ndarray][@stdlib/ndarray/ctor] view. +- For ndarrays with more than `100` elements, the function abbreviates the data, showing only the first and last three values. + +
+ + + + + +
+ +## Examples + + + +```javascript +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2string = require( '@stdlib/ndarray/to-string' ); + +// Create a data buffer: +var buffer = zeroTo( 27 ); + +// Specify array meta data: +var shape = [ 3, 3, 3 ]; +var order = 'column-major'; +var ndims = shape.length; + +// Compute array meta data: +var strides = shape2strides( shape, order ); +var offset = strides2offset( shape, strides ); + +// Print array information: +console.log( '' ); +console.log( 'Dims: %s', shape.join( 'x' ) ); + +// Randomly flip strides and convert an ndarray to a string... +var arr; +var i; +for ( i = 0; i < 20; i++ ) { + strides[ discreteUniform( 0, ndims-1 ) ] *= -1; + offset = strides2offset( shape, strides ); + + console.log( '' ); + console.log( 'Strides: %s', strides.join( ',' ) ); + console.log( 'Offset: %d', offset ); + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + console.log( ndarray2string( arr ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-string/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-string/benchmark/benchmark.js new file mode 100644 index 000000000000..202a4bb2e91e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var format = require( '@stdlib/string/format' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var ndarray2string = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:order=row-major', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var order; + var shape; + var arr; + var out; + var i; + + shape = [ 10, 10, 10 ]; + order = 'row-major'; + buffer = zeroTo( numel( shape ) ); + strides = shape2strides( shape, order ); + offset = strides2offset( shape, strides ); + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ndarray2string( arr ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:order=column-major', pkg ), function benchmark( b ) { + var strides; + var buffer; + var offset; + var order; + var shape; + var arr; + var out; + var i; + + shape = [ 10, 10, 10 ]; + order = 'column-major'; + buffer = zeroTo( numel( shape ) ); + strides = shape2strides( shape, order ); + offset = strides2offset( shape, strides ); + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ndarray2string( arr ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/to-string/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-string/docs/repl.txt new file mode 100644 index 000000000000..33502d6fbaa9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( x ) + Serializes an ndarray as a string. + + This function does *not* serialize data outside of the buffer region + defined by the ndarray view. + + Parameters + ---------- + x: ndarray + Input ndarray. + + Returns + ------- + out: string + String representation. + + Examples + -------- + > var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ); + > var out = {{alias}}( arr ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/to-string/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-string/docs/types/index.d.ts new file mode 100644 index 000000000000..6ed44163ecf7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Serializes an ndarray as a string. +* +* ## Notes +* +* - The function does **not** serialize data outside of the buffer region defined by the ndarray view. +* +* @param x - input ndarray +* @returns string representation +* +* @example +* var array = require( `@stdlib/ndarray/array` ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns +* +* var str = ndarray2string( x ); +* // returns +*/ +declare function ndarray2string( x: typedndarray ): string; + + +// EXPORTS // + +export = ndarray2string; diff --git a/lib/node_modules/@stdlib/ndarray/to-string/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-string/docs/types/test.ts new file mode 100644 index 000000000000..cfae97466960 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/docs/types/test.ts @@ -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. +*/ + +import array = require( '@stdlib/ndarray/array' ); +import ndarray2string = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + ndarray2string( array( [ [ 1, 2 ], [ 3, 4 ] ] ) ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + ndarray2string( 10 ); // $ExpectError + ndarray2string( '10' ); // $ExpectError + ndarray2string( true ); // $ExpectError + ndarray2string( false ); // $ExpectError + ndarray2string( null ); // $ExpectError + ndarray2string( undefined ); // $ExpectError + ndarray2string( [ 1, 2 ] ); // $ExpectError + ndarray2string( {} ); // $ExpectError + ndarray2string( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const arr = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + ndarray2string(); // $ExpectError + ndarray2string( arr, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-string/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-string/examples/index.js new file mode 100644 index 000000000000..274bba388e32 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/examples/index.js @@ -0,0 +1,57 @@ +/** +* @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 shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2string = require( './../lib' ); + +// Create a data buffer: +var buffer = zeroTo( 27 ); + +// Specify array meta data: +var shape = [ 3, 3, 3 ]; +var order = 'column-major'; +var ndims = shape.length; + +// Compute array meta data: +var strides = shape2strides( shape, order ); +var offset = strides2offset( shape, strides ); + +// Print array information: +console.log( '' ); +console.log( 'Dims: %s', shape.join( 'x' ) ); + +// Randomly flip strides and convert an ndarray to a string... +var arr; +var i; +for ( i = 0; i < 20; i++ ) { + strides[ discreteUniform( 0, ndims-1 ) ] *= -1; + offset = strides2offset( shape, strides ); + + console.log( '' ); + console.log( 'Strides: %s', strides.join( ',' ) ); + console.log( 'Offset: %d', offset ); + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + console.log( ndarray2string( arr ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/to-string/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-string/lib/index.js new file mode 100644 index 000000000000..8c8954c9dbf2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Serialize an ndarray as a string. +* +* @module @stdlib/ndarray/to-string +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2string = require( '@stdlib/ndarray/to-string' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns +* +* var str = ndarray2string( x ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/to-string/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-string/lib/main.js new file mode 100644 index 000000000000..dc81011e0c43 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/lib/main.js @@ -0,0 +1,83 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); +var format = require( '@stdlib/string/format' ); + + +// FUNCTIONS // + +/** +* Tests whether an object has a specified method. +* +* @private +* @param {Object} obj - input object +* @param {string} method - method name +* @returns {boolean} boolean indicating whether an object has a specified method +* +* @example +* var bool = hasMethod( [], 'toString' ); +* // returns true +*/ +function hasMethod( obj, method ) { + return ( typeof obj[ method ] === 'function' && obj[ method ] !== Object.prototype[ method ] ); +} + + +// MAIN // + +/** +* Serializes an ndarray as a string. +* +* ## Notes +* +* - The function does **not** serialize data outside of the buffer region defined by the ndarray view. +* +* @param {ndarrayLike} x - input ndarray +* @throws {TypeError} must provide an ndarray-like object +* @returns {string} string representation +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns +* +* var str = ndarray2string( x ); +* // returns +*/ +function ndarray2string( x ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. Must provide an ndarray. Value: `%s`.', x ) ); + } + // Defer to input argument's custom implementation, if already defined... + if ( hasMethod( x, 'toString' ) ) { + return x.toString(); + } + return ndarray2localeString( x ); +} + + +// EXPORTS // + +module.exports = ndarray2string; diff --git a/lib/node_modules/@stdlib/ndarray/to-string/package.json b/lib/node_modules/@stdlib/ndarray/to-string/package.json new file mode 100644 index 000000000000..050b579e5ed5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/ndarray/to-string", + "version": "0.0.0", + "description": "Serialize an ndarray as a string.", + "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", + "ndarray", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util", + "view", + "convert", + "tostring", + "transform", + "string", + "serialize" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/to-string/test/test.js b/lib/node_modules/@stdlib/ndarray/to-string/test/test.js new file mode 100644 index 000000000000..a5124699bcd0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-string/test/test.js @@ -0,0 +1,260 @@ +/** +* @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 tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sub2ind = require( '@stdlib/ndarray/sub2ind' ); +var noop = require( '@stdlib/utils/noop' ); +var ndarray2string = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ndarray2string, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + 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() { + ndarray2string( value ); + }; + } +}); + +tape( 'the function serializes an ndarray as a string (row-major)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 2; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 3, 4, 5, 6 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (row-major, negative strides)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ -2, -1 ]; + offset = 5; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 6, 5, 4, 3 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (column-major)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1, 2, 3, 4 ]; + shape = [ 2, 2 ]; + order = 'column-major'; + strides = [ 1, 2 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (column-major, negative strides)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'column-major'; + strides = [ -1, -2 ]; + offset = 5; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 6, 5, 4, 3 ] ), [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (0d)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1, 2, 3, 4 ]; + shape = []; + order = 'column-major'; + strides = [ 0 ]; + offset = 2; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 3 ], [], [ 0 ], 0, \'column-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (dtype=complex)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'complex64'; + buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'complex64\', new Complex64Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray-like object as a string (no toString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'float64', + 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 2, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': true + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'float64\', new Float64Array( [ 3, 4, 5, 6 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2string( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +});