From 1fc707b7ba3c1d72e3664deeb8c8c809eb08d82e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 20 Feb 2024 03:57:28 +0000 Subject: [PATCH] Auto-generated commit --- .../benchmark/benchmark.assign.length.js | 8 +- base/mskreject/README.md | 40 ++- .../benchmark/benchmark.assign.length.js | 104 +++++++ base/mskreject/docs/repl.txt | 41 +++ base/mskreject/examples/index.js | 5 +- base/mskreject/lib/assign.js | 221 ++++++++++++++ base/mskreject/lib/index.js | 22 +- base/mskreject/package.json | 2 +- base/mskreject/test/test.assign.js | 289 ++++++++++++++++++ base/mskreject/test/test.js | 69 +---- base/mskreject/test/test.main.js | 100 ++++++ 11 files changed, 822 insertions(+), 79 deletions(-) create mode 100644 base/mskreject/benchmark/benchmark.assign.length.js create mode 100644 base/mskreject/lib/assign.js create mode 100644 base/mskreject/test/test.assign.js create mode 100644 base/mskreject/test/test.main.js diff --git a/base/mskfilter/benchmark/benchmark.assign.length.js b/base/mskfilter/benchmark/benchmark.assign.length.js index 421076ae..7646ae13 100644 --- a/base/mskfilter/benchmark/benchmark.assign.length.js +++ b/base/mskfilter/benchmark/benchmark.assign.length.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); -var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var zeroTo = require( './../../../base/zero-to' ); var zeros = require( './../../../zeros' ); var ones = require( './../../../ones' ); var isArray = require( '@stdlib/assert/is-array' ); @@ -45,10 +45,8 @@ function createBenchmark( len ) { var out; var x; - x = ones( len, 'generic' ); - mask = bernoulli( len, 0.5, { - 'dtype': 'generic' - }); + x = zeroTo( len, 'generic' ); + mask = ones( len, 'generic' ); out = zeros( len, 'generic' ); return benchmark; diff --git a/base/mskreject/README.md b/base/mskreject/README.md index dc2e6248..6f2d9e40 100644 --- a/base/mskreject/README.md +++ b/base/mskreject/README.md @@ -20,7 +20,7 @@ limitations under the License. # mskreject -> Return a new array by applying a mask to a provided input array. +> Apply a mask to a provided input array.
@@ -41,6 +41,38 @@ var y = mskreject( x, [ 0, 1, 0, 1 ] ); // returns [ 1, 3 ] ``` +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. + +The function **always** returns a new "generic" array. + +#### mskreject.assign( x, mask, out, stride, offset ) + +Applies a mask to a provided input array and assigns unmasked values to elements in a provided output array. + +```javascript +var x = [ 1, 2, 3, 4 ]; +var mask = [ 1, 0, 1, 0 ]; + +var out = [ 0, 0, 0, 0 ]; + +var arr = mskreject.assign( x, mask, out, -2, out.length-1 ); +// returns [ 0, 4, 0, 2 ] + +var bool = ( arr === out ); +// returns true +``` + +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. +
@@ -49,7 +81,6 @@ var y = mskreject( x, [ 0, 1, 0, 1 ] ); ## Notes -- The function **always** returns a new "generic" array. - If a `mask` array element is falsy, the corresponding element in `x` is **included** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array. @@ -69,17 +100,16 @@ var mskreject = require( '@stdlib/array/base/mskreject' ); // Generate a linearly spaced array: var x = zeroTo( 20 ); +console.log( x ); // Generate a random mask: var mask = bernoulli( x.length, 0.5, { 'dtype': 'generic' }); +console.log( mask ); // Filter an array using the mask: var y = mskreject( x, mask ); - -console.log( x ); -console.log( mask ); console.log( y ); ``` diff --git a/base/mskreject/benchmark/benchmark.assign.length.js b/base/mskreject/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..b62341ab --- /dev/null +++ b/base/mskreject/benchmark/benchmark.assign.length.js @@ -0,0 +1,104 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../../base/zero-to' ); +var zeros = require( './../../../zeros' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var mskreject = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var mask; + var out; + var x; + + x = zeroTo( len, 'generic' ); + mask = zeros( len, 'generic' ); + out = zeros( len, 'generic' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskreject.assign( x, mask, out, 1, 0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) || isnan( v[ i%len ] ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':assign:len='+len, f ); + } +} + +main(); diff --git a/base/mskreject/docs/repl.txt b/base/mskreject/docs/repl.txt index 2dea2f36..2c6eac24 100644 --- a/base/mskreject/docs/repl.txt +++ b/base/mskreject/docs/repl.txt @@ -25,6 +25,47 @@ > var y = {{alias}}( x, [ 0, 1, 0, 1 ] ) [ 1, 3 ] + +{{alias}}.assign( x, mask, out, stride, offset ) + Applies a mask to a provided input array and assigns unmasked values to + elements in a provided output array. + + If a mask array element is falsy, the corresponding element in `x` is + included in the output array; otherwise, the corresponding element in `x` is + "masked" and thus excluded from the output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + mask: ArrayLikeObject + Mask array. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > var m = [ 0, 1, 0, 1 ]; + > var out = [ 0, 0, 0, 0 ]; + > var arr = {{alias}}.assign( x, m, out, 2, 0 ) + [ 1, 0, 3, 0 ] + > var bool = ( arr === out ) + true + See Also -------- diff --git a/base/mskreject/examples/index.js b/base/mskreject/examples/index.js index 9c08d661..b9af6a26 100644 --- a/base/mskreject/examples/index.js +++ b/base/mskreject/examples/index.js @@ -24,15 +24,14 @@ var mskreject = require( './../lib' ); // Generate a linearly spaced array: var x = zeroTo( 20 ); +console.log( x ); // Generate a random mask: var mask = bernoulli( x.length, 0.5, { 'dtype': 'generic' }); +console.log( mask ); // Filter an array using the mask: var y = mskreject( x, mask ); - -console.log( x ); -console.log( mask ); console.log( y ); diff --git a/base/mskreject/lib/assign.js b/base/mskreject/lib/assign.js new file mode 100644 index 00000000..f0cbcb15 --- /dev/null +++ b/base/mskreject/lib/assign.js @@ -0,0 +1,221 @@ +/** +* @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 isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var arraylike2object = require( './../../../base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); + + +// FUNCTIONS // + +/** +* Applies a mask to an indexed array and assigns unmasked values to elements in an indexed output array. +* +* @private +* @param {Collection} x - input array +* @param {IntegerArray} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* +* var arr = indexed( x, mask, out, 1, 0 ); +* // returns [ 1, 3, 0, 0 ] +*/ +function indexed( x, mask, out, stride, offset ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + if ( !mask[ i ] ) { + out[ io ] = x[ i ]; + io += stride; + } + } + return out; +} + +/** +* Applies a mask to an accessor array and assigns unmasked values to elements in an accessor output array. +* +* @private +* @param {Object} x - input array object +* @param {Object} mask - mask array object +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1, 2, 3, 4 ] ); +* var mask = toAccessorArray( [ 0, 1, 0, 1 ] ); +* +* var out = toAccessorArray( [ 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0 ); +* +* var v = arr.get( 0 ); +* // returns 1 +* +* v = arr.get( 1 ); +* // returns 3 +*/ +function accessors( x, mask, out, stride, offset ) { + var xdata; + var mdata; + var odata; + var xget; + var mget; + var oset; + var io; + var i; + + xdata = x.data; + mdata = mask.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + mget = mask.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + if ( !mget( mdata, i ) ) { + oset( odata, io, xget( xdata, i ) ); + io += stride; + } + } + return odata; +} + +/** +* Applies a mask to a complex array and assigns unmasked values to elements in a complex output array. +* +* @private +* @param {Collection} x - real-valued floating-point input array view +* @param {Object} mask - mask array object +* @param {Collection} out - real-valued floating-point output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array view +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = new Float64Array( 8 ); +* +* var arr = complex( x, arraylike2object( mask ), out, 1, 0 ); +* // returns [ 1.0, 2.0, 5.0, 6.0, 0.0, 0.0, 0.0, 0.0 ] +*/ +function complex( x, mask, out, stride, offset ) { + var mdata; + var mget; + var io; + var so; + var i; + var j; + + mdata = mask.data; + mget = mask.accessors[ 0 ]; + + so = stride * 2; // note: multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < mdata.length; i++ ) { + if ( !mget( mdata, i ) ) { + j = i * 2; + out[ io ] = x[ j ]; + out[ io+1 ] = x[ j+1 ]; + io += so; + } + } + return out; +} + + +// MAIN // + +/** +* Applies a mask to a provided input array and assigns unmasked values to elements in a provided output array. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0 ]; +* var arr = assign( x, mask, out, 1, 0 ); +* // returns [ 1, 3 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function assign( x, mask, out, stride, offset ) { + var xo; + var mo; + var oo; + + xo = arraylike2object( x ); + mo = arraylike2object( mask ); + oo = arraylike2object( out ); + if ( + xo.accessorProtocol || + mo.accessorProtocol || + oo.accessorProtocol + ) { + // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) + ) { + complex( reinterpret( x, 0 ), mo, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } + accessors( xo, mo, oo, stride, offset ); + return out; + } + indexed( x, mask, out, stride, offset ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/base/mskreject/lib/index.js b/base/mskreject/lib/index.js index 11319318..02eed372 100644 --- a/base/mskreject/lib/index.js +++ b/base/mskreject/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Return a new array by applying a mask to a provided input array. +* Apply a mask to a provided input array. * * @module @stdlib/array/base/mskreject * @@ -31,11 +31,31 @@ * * var y = mskreject( x, mask ); * // returns [ 1, 3 ] +* +* @example +* var mskreject = require( '@stdlib/array/base/mskreject' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0 ]; +* var arr = mskreject.assign( x, mask, out, 1, 0 ); +* // returns [ 1, 3 ] +* +* var bool = ( arr === out ); +* // returns true */ // MODULES // +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); // EXPORTS // diff --git a/base/mskreject/package.json b/base/mskreject/package.json index efae94f9..1c85a8a2 100644 --- a/base/mskreject/package.json +++ b/base/mskreject/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/base/mskreject", "version": "0.0.0", - "description": "Return a new array by applying a mask to a provided input array.", + "description": "Apply a mask to a provided input array.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/base/mskreject/test/test.assign.js b/base/mskreject/test/test.assign.js new file mode 100644 index 00000000..b72e1001 --- /dev/null +++ b/base/mskreject/test/test.assign.js @@ -0,0 +1,289 @@ +/** +* @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 Complex64Array = require( './../../../complex64' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var Int32Array = require( './../../../int32' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var zeros = require( './../../../zeros' ); +var mskreject = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskreject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function rejects array elements (generic)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = [ 1, 2, 3, 4 ]; + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'generic' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 2, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'generic' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'generic' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'generic' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 1, 2, 3, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'generic' ); + actual = mskreject( x, mask, out, -2, out.length-1 ); + expected = [ 0, 4, 0, 2 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (real typed array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'int32' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Int32Array( [ 2, 4 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'int32' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Int32Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'int32' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Int32Array( [ 4 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'int32' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Int32Array( [ 1, 2, 3, 4 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'int32' ); + actual = mskreject( x, mask, out, -2, out.length-1 ); + expected = new Int32Array( [ 0, 4, 0, 2 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (complex typed array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'complex64' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Complex64Array( [ 3.0, 4.0, 7.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'complex64' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Complex64Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'complex64' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Complex64Array( [ 7.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'complex64' ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'complex64' ); + actual = mskreject( x, mask, out, -2, out.length-1 ); + expected = new Complex64Array( [ 0.0, 0.0, 7.0, 8.0, 0.0, 0.0, 3.0, 4.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + + mask = toAccessorArray( [ 1, 0, 1, 0 ] ); + out = toAccessorArray( zeros( 2, 'generic' ) ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 2, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 1, 1, 1 ] ); + out = toAccessorArray( zeros( 0, 'generic' ) ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 1, 1, 0 ] ); + out = toAccessorArray( zeros( 1, 'generic' ) ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 0, 0, 0, 0 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskreject( x, mask, out, 1, 0 ); + expected = [ 1, 2, 3, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 0, 1, 0 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskreject( x, mask, out, -2, out.length-1 ); + expected = [ 0, 4, 0, 2 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } +}); + +tape( 'the function returns leaves an output array unchanged if provided a second argument which masks all input array values', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + mask = [ 1, 1, 1, 1 ]; + + x = [ 1, 2, 3, 4 ]; + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskreject( x, mask, out, 1, 0 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskreject( x, mask, out, 1, 0 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Int32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskreject( x, mask, out, 1, 0 ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + actual = mskreject( x, mask, out, 1, 0 ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/base/mskreject/test/test.js b/base/mskreject/test/test.js index c548d8bd..5d39f33c 100644 --- a/base/mskreject/test/test.js +++ b/base/mskreject/test/test.js @@ -21,9 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var Complex64Array = require( './../../../complex64' ); -var isSameComplex64 = require( '@stdlib/assert/is-same-complex64' ); -var isArray = require( '@stdlib/assert/is-array' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); var mskreject = require( './../lib' ); @@ -35,66 +34,8 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function rejects array elements', function test( t ) { - var expected; - var actual; - var mask; - var x; - - x = [ 1, 2, 3, 4 ]; - - mask = [ 0, 1, 0, 1 ]; - actual = mskreject( x, mask ); - expected = [ 1, 3 ]; - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.notEqual( actual, x, 'returns new array' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - mask = [ 1, 1, 1, 1 ]; - actual = mskreject( x, mask ); - expected = []; - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.notEqual( actual, x, 'returns new array' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - mask = [ 0, 0, 0, 1 ]; - actual = mskreject( x, mask ); - expected = [ 1, 2, 3 ]; - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.notEqual( actual, x, 'returns new array' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - mask = [ 1, 1, 1, 1 ]; - actual = mskreject( x, mask ); - expected = []; - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.notEqual( actual, x, 'returns new array' ); - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function rejects array elements (accessors)', function test( t ) { - var expected; - var actual; - var mask; - var x; - var i; - - x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - mask = [ 0, 1, 0, 1 ]; - actual = mskreject( x, mask ); - expected = [ x.get( 0 ), x.get( 2 ) ]; - - t.strictEqual( isArray( actual ), true, 'returns expected value' ); - t.notEqual( actual, x, 'returns different reference' ); - for ( i = 0; i < expected.length; i++ ) { - t.strictEqual( isSameComplex64( actual[ i ], expected[ i ] ), true, 'returns expected value' ); - } - t.end(); -}); - -tape( 'the function returns an empty array if provided empty arrays', function test( t ) { - t.deepEqual( mskreject( [], [] ), [], 'returns expected value' ); +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( mskreject, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( mskreject, 'assign' ), true, 'returns expected value' ); t.end(); }); diff --git a/base/mskreject/test/test.main.js b/base/mskreject/test/test.main.js new file mode 100644 index 00000000..c548d8bd --- /dev/null +++ b/base/mskreject/test/test.main.js @@ -0,0 +1,100 @@ +/** +* @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 Complex64Array = require( './../../../complex64' ); +var isSameComplex64 = require( '@stdlib/assert/is-same-complex64' ); +var isArray = require( '@stdlib/assert/is-array' ); +var mskreject = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskreject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function rejects array elements', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = [ 1, 2, 3, 4 ]; + + mask = [ 0, 1, 0, 1 ]; + actual = mskreject( x, mask ); + expected = [ 1, 3 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskreject( x, mask ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskreject( x, mask ); + expected = [ 1, 2, 3 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskreject( x, mask ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var x; + var i; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + mask = [ 0, 1, 0, 1 ]; + actual = mskreject( x, mask ); + expected = [ x.get( 0 ), x.get( 2 ) ]; + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns different reference' ); + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( isSameComplex64( actual[ i ], expected[ i ] ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns an empty array if provided empty arrays', function test( t ) { + t.deepEqual( mskreject( [], [] ), [], 'returns expected value' ); + t.end(); +});