From 664d00f9a75b0e8e3d6f96157f14ed8052e53de3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 20 Feb 2024 23:29:21 +0000 Subject: [PATCH] Auto-generated commit --- to-fancy/benchmark/benchmark.get.js | 68 +++ to-fancy/docs/repl.txt | 7 +- to-fancy/lib/error_message.js | 2 +- to-fancy/lib/get_elements.js | 4 +- to-fancy/test/test.get.boolean_array.js | 430 +++++++++++++++++ to-fancy/test/test.get.integer_array.js | 596 ++++++++++++++++++++++++ to-fancy/test/test.get.invalid.js | 131 ++++++ to-fancy/test/test.get.mask_array.js | 431 +++++++++++++++++ 8 files changed, 1665 insertions(+), 4 deletions(-) create mode 100644 to-fancy/test/test.get.boolean_array.js create mode 100644 to-fancy/test/test.get.integer_array.js create mode 100644 to-fancy/test/test.get.mask_array.js diff --git a/to-fancy/benchmark/benchmark.get.js b/to-fancy/benchmark/benchmark.get.js index 14388ae8..412d65f4 100644 --- a/to-fancy/benchmark/benchmark.get.js +++ b/to-fancy/benchmark/benchmark.get.js @@ -25,6 +25,8 @@ var isCollection = require( '@stdlib/assert/is-collection' ); var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; var zeroTo = require( './../../zero-to' ); +var zeros = require( './../../zeros' ); +var filledarray = require( './../../filled' ); var Slice = require( '@stdlib/slice/ctor' ); var pkg = require( './../package.json' ).name; var array2fancy = require( './../lib' ); @@ -171,3 +173,69 @@ bench( pkg+'::get,integer_array:len=1', opts, function benchmark( b ) { b.pass( 'benchmark finished' ); b.end(); }); + +bench( pkg+'::get,mask_array:len=1', opts, function benchmark( b ) { + var values; + var opts; + var x; + var v; + var i; + + x = array2fancy( zeros( 100, 'uint8' ) ); + + opts = { + 'persist': true + }; + values = [ + array2fancy.idx( [ 1 ], opts ), + array2fancy.idx( [ 2 ], opts ), + array2fancy.idx( [ 3 ], opts ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = x[ values[ i%values.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isCollection( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get,boolean_array:len=1', opts, function benchmark( b ) { + var values; + var opts; + var x; + var v; + var i; + + x = array2fancy( filledarray( true, 100, 'generic' ) ); + + opts = { + 'persist': true + }; + values = [ + array2fancy.idx( [ 1 ], opts ), + array2fancy.idx( [ 2 ], opts ), + array2fancy.idx( [ 3 ], opts ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = x[ values[ i%values.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isCollection( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/to-fancy/docs/repl.txt b/to-fancy/docs/repl.txt index f22e537f..ee34eda5 100644 --- a/to-fancy/docs/repl.txt +++ b/to-fancy/docs/repl.txt @@ -14,7 +14,8 @@ configuration as specified by provided options. A fancy array supports indexing using positive and negative integers (both - numeric literals and strings), Slice instances, and subsequence expressions. + numeric literals and strings), Slice instances, subsequence expressions, + mask arrays, boolean arrays, and integer arrays. A fancy array supports all properties and methods of the input array, and, thus, a fancy array can be consumed by any API which supports array-like @@ -77,6 +78,8 @@ If an array index is not associated with a provided identifier, the 'get' method should return `null`. + Default: `{{alias:@stdlib/array/index}}`. + Returns ------- out: Array|TypedArray|Object @@ -118,6 +121,8 @@ If an array index is not associated with a provided identifier, the 'get' method should return `null`. + Default: `{{alias:@stdlib/array/index}}`. + Returns ------- fcn: Function diff --git a/to-fancy/lib/error_message.js b/to-fancy/lib/error_message.js index ba5120ae..09d59519 100644 --- a/to-fancy/lib/error_message.js +++ b/to-fancy/lib/error_message.js @@ -33,7 +33,7 @@ var replace = require( '@stdlib/string/base/replace' ); * @returns {string} updated message */ function errMessage( msg ) { - return replace( msg, /^invalid argument/, 'invalid operation' ); + return replace( msg, /^invalid arguments?/, 'invalid operation' ); } diff --git a/to-fancy/lib/get_elements.js b/to-fancy/lib/get_elements.js index 11a5f4f3..8e8e3252 100644 --- a/to-fancy/lib/get_elements.js +++ b/to-fancy/lib/get_elements.js @@ -21,8 +21,8 @@ // MODULES // var take = require( './../../take' ); -var mskfilter = require( './../../base/mskfilter' ); -var mskreject = require( './../../base/mskreject' ); +var mskfilter = require( './../../mskfilter' ); +var mskreject = require( './../../mskreject' ); var format = require( '@stdlib/string/format' ); var prop2array = require( './prop2array.js' ); diff --git a/to-fancy/test/test.get.boolean_array.js b/to-fancy/test/test.get.boolean_array.js new file mode 100644 index 00000000..bbf34b93 --- /dev/null +++ b/to-fancy/test/test.get.boolean_array.js @@ -0,0 +1,430 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +var Int32Array = require( './../../int32' ); +var array2fancy = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasProxySupport() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof array2fancy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an array-like object supporting boolean array indexing (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ] ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ false, false, false, false ] ); + expected = []; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ true, true, false, false ] ); + expected = [ 1, 2 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ false, true, true, false ] ); + expected = [ 2, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ true, false, true, false ] ); + expected = [ 1, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting boolean array indexing (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ] ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ false, false, false, false ] ); + expected = new Int32Array( [] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ true, true, false, false ] ); + expected = new Int32Array( [ 1, 2 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ false, true, true, false ] ); + expected = new Int32Array( [ 2, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ true, false, true, false ] ); + expected = new Int32Array( [ 1, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting boolean array indexing and returning arrays which can themselves support boolean arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ] ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ true, false, true, false ] ); + expected = [ 1, 3 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ false, true ] ); + expected = [ 3 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting boolean array indexing and returning arrays which can themselves support boolean arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ] ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ true, false, true, false ] ); + expected = new Int32Array( [ 1, 3 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ false, true ] ); + expected = new Int32Array( [ 3 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object which throws an error if provided a boolean array index having an incompatible number of elements (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + values = [ + array2fancy.idx( [ true, false ] ), + array2fancy.idx( [ true, true, true ] ), + array2fancy.idx( [ true, false, true, false, true ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error if provided a boolean array index having an incompatible number of elements (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + values = [ + array2fancy.idx( [ true, false ] ), + array2fancy.idx( [ true, true, true ] ), + array2fancy.idx( [ true, false, true, false, true ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ], { + 'persist': false + }); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ], { + 'persist': false + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ], { + 'persist': true + }); + expected = [ 1, 2, 3, 4 ]; + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ true, true, true, true ], { + 'persist': true + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +}); diff --git a/to-fancy/test/test.get.integer_array.js b/to-fancy/test/test.get.integer_array.js new file mode 100644 index 00000000..0b42cbec --- /dev/null +++ b/to-fancy/test/test.get.integer_array.js @@ -0,0 +1,596 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +var Int32Array = require( './../../int32' ); +var array2fancy = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasProxySupport() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof array2fancy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an array-like object supporting integer array indexing (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ] ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ 3, 1, 2, 0, 0, 1 ] ); + expected = [ 4, 2, 3, 1, 1, 2 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [] ); + expected = []; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [] ) ); + expected = []; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ 0, 1 ] ); + expected = [ 1, 2 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [ 1, 2 ] ) ); + expected = [ 2, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [ 0, 2 ] ) ); + expected = [ 1, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [ -1, -3 ] ) ); + expected = [ 4, 2 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting integer array indexing (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ] ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ 3, 1, 2, 0, 0, 1 ] ); + expected = new Int32Array( [ 4, 2, 3, 1, 1, 2 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [] ); + expected = new Int32Array( [] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [] ) ); + expected = new Int32Array( [] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ 0, 1 ] ); + expected = new Int32Array( [ 1, 2 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( [ 1, 2 ] ); + expected = new Int32Array( [ 2, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [ 0, 2 ] ) ); + expected = new Int32Array( [ 1, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Int32Array( [ -1, -3 ] ) ); + expected = new Int32Array( [ 4, 2 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'by default, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'by default, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'when `strict` is `false`, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x, { + 'strict': false + }); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'when `strict` is `false`, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x, { + 'strict': false + }); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'when `strict` is `true`, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x, { + 'strict': true + }); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'when `strict` is `true`, the function returns an array-like object which throws an error when provided an index which is out-of-bounds (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x, { + 'strict': true + }); + + values = [ + array2fancy.idx( [ 10, 20 ] ), + array2fancy.idx( [ -20, -10 ] ), + array2fancy.idx( new Int32Array( [ -20] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object supporting integer array indexing and returning arrays which themselves support integer arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ] ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ -4, -2 ] ); + expected = [ 1, 3 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Int32Array( [ 1, 0 ] ) ); + expected = [ 3, 1 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting integer array indexing and returning arrays which themselves support integer arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ] ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( [ -4, -2 ] ); + expected = new Int32Array( [ 1, 3 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Int32Array( [ 1, 0 ] ) ); + expected = new Int32Array( [ 3, 1 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ], { + 'persist': false + }); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ], { + 'persist': false + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ], { + 'persist': true + }); + expected = [ 1, 2, 3, 4 ]; + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( [ 0, 1, 2, 3 ], { + 'persist': true + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +}); diff --git a/to-fancy/test/test.get.invalid.js b/to-fancy/test/test.get.invalid.js index 32751f2c..ccaa5217 100644 --- a/to-fancy/test/test.get.invalid.js +++ b/to-fancy/test/test.get.invalid.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); var Int32Array = require( './../../int32' ); +var Uint8Array = require( './../../uint8' ); var array2fancy = require( './../lib' ); @@ -152,3 +153,133 @@ tape( 'the function returns an array-like object which throws an error when prov }; } }); + +tape( 'the function returns an array-like object which throws an error when provided an invalid serialized array index (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + values = [ + 'ArrayIndex<9999999>', + 'ArrayIndex<9999999999>' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when provided an invalid serialized array index (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + values = [ + 'ArrayIndex<9999999>', + 'ArrayIndex<9999999999>' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when provided an unsupported array index (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x, { + 'cache': { + 'get': get + } + }); + + values = [ + array2fancy.idx( [ 0, 1, 2, 3 ] ), + array2fancy.idx( [ true, false, true, false ] ), + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } + + function get() { + return { + 'data': [], + 'type': 'beep', + 'dtype': 'generic' + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when provided an unsupported array index (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x, { + 'cache': { + 'get': get + } + }); + + values = [ + array2fancy.idx( [ 0, 1, 2, 3 ] ), + array2fancy.idx( [ true, false, true, false ] ), + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } + + function get() { + return { + 'data': [], + 'type': 'beep', + 'dtype': 'generic' + }; + } +}); diff --git a/to-fancy/test/test.get.mask_array.js b/to-fancy/test/test.get.mask_array.js new file mode 100644 index 00000000..ff7f6e3b --- /dev/null +++ b/to-fancy/test/test.get.mask_array.js @@ -0,0 +1,431 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +var Int32Array = require( './../../int32' ); +var Uint8Array = require( './../../uint8' ); +var array2fancy = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasProxySupport() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof array2fancy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an array-like object supporting mask array indexing (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 1, 1, 1, 1 ] ) ); + expected = []; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 1, 1 ] ) ); + expected = [ 1, 2 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 1, 0, 0, 1 ] ) ); + expected = [ 2, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); + expected = [ 1, 3 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting mask array indexing (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 1, 1, 1, 1 ] ) ); + expected = new Int32Array( [] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 1, 1 ] ) ); + expected = new Int32Array( [ 1, 2 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 1, 0, 0, 1 ] ) ); + expected = new Int32Array( [ 2, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); + expected = new Int32Array( [ 1, 3 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting mask array indexing and returning arrays which can themselves support mask arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); + expected = [ 1, 3 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Uint8Array( [ 1, 0 ] ) ); + expected = [ 3 ]; + actual = z[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object supporting mask array indexing and returning arrays which can themselves support mask arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + var z; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); + expected = new Int32Array( [ 1, 3 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + z = actual; + + idx = array2fancy.idx( new Uint8Array( [ 1, 0 ] ) ); + expected = new Int32Array( [ 3 ] ); + actual = z[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.notEqual( actual, z, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array-like object which throws an error if provided a mask array index having an incompatible number of elements (generic)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + values = [ + array2fancy.idx( new Uint8Array( [ 0, 1 ] ) ), + array2fancy.idx( new Uint8Array( [ 0, 0, 0 ] ) ), + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1, 0 ] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error if provided a mask array index having an incompatible number of elements (typed)', opts, function test( t ) { + var values; + var x; + var y; + var i; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + values = [ + array2fancy.idx( new Uint8Array( [ 0, 1 ] ) ), + array2fancy.idx( new Uint8Array( [ 0, 0, 0 ] ) ), + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1, 0 ] ) ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ), { + 'persist': false + }); + expected = [ 1, 2, 3, 4 ]; + actual = y[ idx ]; + + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which throws an error when unable to resolve an index array (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ), { + 'persist': false + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + actual = y[ idx ]; + + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.throws( badValue( idx ), Error, 'throws an error' ); + t.end(); + + function badValue( value ) { + return function badValue() { + var v = y[ value ]; // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (generic)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ), { + 'persist': true + }); + expected = [ 1, 2, 3, 4 ]; + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +}); + +tape( 'the function returns an array-like object which supports persisted index arrays (typed)', opts, function test( t ) { + var expected; + var actual; + var idx; + var x; + var y; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + y = array2fancy( x ); + + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ), { + 'persist': true + }); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = y[ idx ]; + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); + t.notEqual( actual, x, 'different reference' ); + t.notEqual( actual, y, 'different reference' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + array2fancy.idx.free( idx ); + + t.end(); +});