From 915ab8bdd324e03c7b1190985e4227cacfdfc8f5 Mon Sep 17 00:00:00 2001 From: aman-095 Date: Wed, 6 Nov 2024 10:44:10 +0530 Subject: [PATCH 1/4] test: add test suite for cscal-wasm --- .../blas/base/cscal-wasm/test/test.main.js | 276 ++++++++++++ .../base/cscal-wasm/test/test.module.main.js | 295 ++++++++++++ .../cscal-wasm/test/test.module.ndarray.js | 423 ++++++++++++++++++ .../blas/base/cscal-wasm/test/test.ndarray.js | 331 ++++++++++++++ 4 files changed, 1325 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js create mode 100644 lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js new file mode 100644 index 000000000000..c3e6aa9ec16f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js @@ -0,0 +1,276 @@ +/** +* @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( '@stdlib/array/complex64' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var cscal = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cscal, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the `main` method has an arity of 4', function test( t ) { + t.strictEqual( cscal.main.length, 4, 'arity of 4' ); + t.end(); +}); + +tape( 'the `main` method scales elements from `cx` by `ca`', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.3, // 1 + 0.1, // 1 + 0.5, // 2 + 0.0, // 2 + 0.0, // 3 + 0.5, // 3 + 0.0, // 4 + 0.2 // 4 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.main( 4, ca, cx, 1 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.19, // 1 + -0.17, // 1 + 0.2, // 2 + -0.35, // 2 + 0.35, // 3 + 0.2, // 3 + 0.14, // 4 + 0.08 // 4 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `main` method supports specifying a `cx` stride', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, // 1 + 0.1, // 1 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 3 + -0.3, // 3 + 7.0, + 2.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.main( 3, ca, cx, 2 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.11, // 1 + -0.03, // 1 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 3 + -0.19, // 3 + 7.0, + 2.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `main` method supports specifying a negative `cx` stride', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, // 3 + 0.1, // 3 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 1 + -0.3, // 1 + 7.0, + 2.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.main( 3, ca, cx, -2 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.11, // 3 + -0.03, // 3 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 1 + -0.19, // 1 + 7.0, + 2.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `main` method returns a reference to the input array', function test( t ) { + var out; + var ca; + var cx; + + cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + ca = new Complex64( 2.0, 2.0 ); + + out = cscal.main( 4, ca, cx, 1 ); + + t.strictEqual( out, cx, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method returns the input array unchanged', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + ca = new Complex64( 2.0, 2.0 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + // cscal.main( -1, ca, cx, 1 ); + // t.deepEqual( viewX, expected, 'returns expected value' ); + + cscal.main( 0, ca, cx, 1 ); + t.deepEqual( viewX, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the `main` method supports view offsets', function test( t ) { + var expected; + var viewX; + var cx0; + var cx1; + var ca; + + // Initial arrays... + cx0 = new Complex64Array( [ + 0.1, + -0.3, + 8.0, // 1 + 9.0, // 1 + 0.5, // 2 + -0.1, // 2 + 2.0, // 3 + 5.0, // 3 + 2.0, + 3.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + // Create offset views... + cx1 = new Complex64Array( cx0.buffer, cx0.BYTES_PER_ELEMENT*1 ); // begin at 2nd element + + cscal.main( 3, ca, cx1, 1 ); + + viewX = new Float32Array( cx0.buffer ); + expected = new Float32Array( [ + 0.1, + -0.3, + 9.5, // 1 + -2.0, // 1 + 0.13, // 2 + -0.39, // 2 + 4.3, // 3 + 0.6, // 3 + 2.0, + 3.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js new file mode 100644 index 000000000000..374ff5ab0d44 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js @@ -0,0 +1,295 @@ +/** +* @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. +*/ + +/* eslint-disable node/no-sync */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Memory = require( '@stdlib/wasm/memory' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var Module = require( './../lib' ).Module; + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Module, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'a module instance has a `main` method which has an arity of 4', function test( t ) { + var mem; + var mod; + + mem = new Memory({ + 'initial': 0 + }); + mod = new Module( mem ); + t.strictEqual( mod.main.length, 4, 'returns expected value' ); + t.end(); +}); + +tape( 'a module instance has a `main` method which scales elements from `cx` by `ca`', function test( t ) { + var actualX; + var viewX; + var cap; + var cxe; + var cxp; + var mem; + var mod; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ 0.19, -0.17, 0.2, -0.35, 0.35, 0.2 ] ); + + mod.main( 3, cap, cxp, 1 ); + + actualX = new Complex64Array( 3 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has a `main` method which supports specifying a `cx` stride', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, // 1 + 0.1, // 1 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 3 + -0.3, // 3 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.11, // 1 + -0.03, // 1 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 3 + -0.19, // 3 + 7.0, + 2.0 + ] ); + + mod.main( 3, cap, cxp, 2 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has a `main` method which supports specifying a negative `cx` stride', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, // 3 + 0.1, // 3 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 1 + -0.3, // 1 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.11, // 3 + -0.03, // 3 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 1 + -0.19, // 1 + 7.0, + 2.0 + ] ); + + mod.main( 3, cap, cxp, -2 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has a `main` method which returns a reference to the input array', function test( t ) { + var cap; + var cxp; + var mem; + var mod; + var out; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + out = mod.main( 3, cap, cxp, 1 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, a module instance has a `main` method which leaves the input array unchanged', function test( t ) { + var cap; + var cxp; + var mem; + var mod; + var out; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + out = mod.main( 0, cap, cxp, 1 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + out = mod.main( -1, cap, cxp, 1 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js new file mode 100644 index 000000000000..b1d2a16fc938 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js @@ -0,0 +1,423 @@ +/** +* @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. +*/ + +/* eslint-disable node/no-sync */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Memory = require( '@stdlib/wasm/memory' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var Module = require( './../lib' ).Module; + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Module, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which has an arity of 5', function test( t ) { + var mem; + var mod; + + mem = new Memory({ + 'initial': 0 + }); + mod = new Module( mem ); + t.strictEqual( mod.ndarray.length, 5, 'returns expected value' ); + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which scales elements from `cx` by `ca`', function test( t ) { + var actualX; + var viewX; + var cap; + var cxe; + var cxp; + var mem; + var mod; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ 0.19, -0.17, 0.2, -0.35, 0.35, 0.2 ] ); + + mod.ndarray( 3, cap, cxp, 1, 0 ); + + actualX = new Complex64Array( 3 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which supports a `cx` stride', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, // 1 + 0.1, // 1 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 3 + -0.3, // 3 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.11, // 1 + -0.03, // 1 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 3 + -0.19, // 3 + 7.0, + 2.0 + ] ); + + mod.ndarray( 3, cap, cxp, 2, 0 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which supports a negative `cx` stride', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, // 3 + 0.1, // 3 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 1 + -0.3, // 1 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.11, // 3 + -0.03, // 3 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 1 + -0.19, // 1 + 7.0, + 2.0 + ] ); + + mod.ndarray( 3, cap, cxp, -2, 4 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which supports a `cx` offset', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, // 1 + 0.1, // 1 + 4.0, + 7.0, + 0.1, // 2 + -0.3, // 2 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.17, // 1 + 0.46, // 1 + 4.0, + 7.0, + -0.17, // 2 + -0.19, // 2 + 7.0, + 2.0 + ] ); + + mod.ndarray( 2, cap, cxp, 2, 2 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which returns a reference to the input array', function test( t ) { + var cap; + var cxp; + var mem; + var mod; + var out; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + out = mod.ndarray( 3, cap, cxp, 1, 0 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, a module instance has an `ndarray` method which leaves the input array unchanged', function test( t ) { + var cap; + var cxp; + var mem; + var mod; + var out; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 48; + + mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + out = mod.ndarray( 0, cap, cxp, 1, 0 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + out = mod.ndarray( -1, cap, cxp, 1, 0 ); + t.strictEqual( out, cxp, 'returns expected value' ); + + t.end(); +}); + +tape( 'a module instance has an `ndarray` method which supports complex access patterns', function test( t ) { + var actualX; + var viewX; + var xbuf; + var cap; + var cxe; + var cxp; + var mem; + var mod; + var cx; + + mem = new Memory({ + 'initial': 1 + }); + mod = new Module( mem ); + mod.initializeSync(); + + cxp = 0; + cap = 96; + + xbuf = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 1 + -0.3, // 1 + 7.0, + 2.0 + ] ); + cx = new Complex64Array( xbuf.buffer ); + + mod.write( cxp, cx ); + mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); + + cxe = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 1 + -0.19, // 1 + 7.0, + 2.0 + ] ); + + mod.ndarray( 2, cap, cxp, -2, 4 ); + + actualX = new Complex64Array( 6 ); + mod.read( cxp, actualX ); + viewX = new Float32Array( actualX.buffer ); + isApprox( t, viewX, cxe, 1.0 ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js new file mode 100644 index 000000000000..edf34327f553 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js @@ -0,0 +1,331 @@ +/** +* @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( '@stdlib/array/complex64' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var cscal = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cscal, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the `ndarray` method has an arity of 5', function test( t ) { + t.strictEqual( cscal.ndarray.length, 5, 'arity of 5' ); + t.end(); +}); + +tape( 'the `ndarray` method scales elements from `cx` by `ca`', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.3, // 1 + 0.1, // 1 + 0.5, // 2 + 0.0, // 2 + 0.0, // 3 + 0.5, // 3 + 0.0, // 4 + 0.2, // 4 + 2.0, + 3.0, + 2.0, + 3.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.ndarray( 4, ca, cx, 1, 0 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.19, // 1 + -0.17, // 1 + 0.2, // 2 + -0.35, // 2 + 0.35, // 3 + 0.2, // 3 + 0.14, // 4 + 0.08, // 4 + 2.0, + 3.0, + 2.0, + 3.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `ndarray` method supports specifying a `cx` stride', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, // 1 + 0.1, // 1 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 3 + -0.3, // 3 + 7.0, + 2.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.ndarray( 3, ca, cx, 2, 0 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.11, // 1 + -0.03, // 1 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 3 + -0.19, // 3 + 7.0, + 2.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `ndarray` method supports specifying a negative `cx` stride', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, // 3 + 0.1, // 3 + 3.0, + 6.0, + -0.6, // 2 + 0.1, // 2 + 4.0, + 7.0, + 0.1, // 1 + -0.3, // 1 + 7.0, + 2.0 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.ndarray( 3, ca, cx, -2, 4 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.11, // 3 + -0.03, // 3 + 3.0, + 6.0, + -0.17, // 2 + 0.46, // 2 + 4.0, + 7.0, + -0.17, // 1 + -0.19, // 1 + 7.0, + 2.0 + ] ); + isApprox( t, viewX, expected, 1.0 ); + + t.end(); +}); + +tape( 'the `ndarray` method supports a `cx` offset', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, + 0.1, + 4.0, // 1 + 6.0, // 1 + 0.1, // 2 + -0.3, // 2 + 7.0, // 3 + 2.0 // 3 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.ndarray( 3, ca, cx, 1, 3 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, + 0.1, + 5.8, // 1 + -0.4, // 1 + -0.17, // 2 + -0.19, // 2 + 4.2, // 3 + -4.1 // 3 + ] ); + isApprox( t, viewX, expected, 5.0 ); + + t.end(); +}); + +tape( 'the `ndarray` method returns a reference to the input array', function test( t ) { + var out; + var ca; + var cx; + + cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + ca = new Complex64( 2.0, 2.0 ); + + out = cscal.ndarray( 4, ca, cx, 1, 0 ); + + t.strictEqual( out, cx, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the `ndarray` method returns the input array unchanged', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + ca = new Complex64( 2.0, 2.0 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + // cscal.ndarray( -1, ca, cx, 1, 0 ); + // t.deepEqual( viewX, expected, 'returns expected value' ); + + cscal.ndarray( 0, ca, cx, 1, 0 ); + t.deepEqual( viewX, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the `ndarray` method supports complex access patterns', function test( t ) { + var expected; + var viewX; + var ca; + var cx; + + cx = new Complex64Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, + 0.1, + 4.0, // 1 + 6.0, // 1 + 0.1, + -0.3, + 7.0, + 2.0, + 2.0, // 2 + 3.0 // 2 + ] ); + ca = new Complex64( 0.4, -0.7 ); + + cscal.ndarray( 2, ca, cx, 3, 3 ); + + viewX = new Float32Array( cx.buffer ); + expected = new Float32Array( [ + 0.1, + 0.1, + 3.0, + 6.0, + -0.6, + 0.1, + 5.8, // 1 + -0.4, // 1 + 0.1, + -0.3, + 7.0, + 2.0, + 2.9, // 2 + -0.2 // 2 + ] ); + isApprox( t, viewX, expected, 5.0 ); + + t.end(); +}); From 15a7d77e7c5dc9e0b101f4b9019f4f4d97dfe11f Mon Sep 17 00:00:00 2001 From: aman-095 Date: Wed, 6 Nov 2024 11:22:50 +0530 Subject: [PATCH 2/4] test: refactor memory size --- .../blas/base/cscal-wasm/test/test.module.main.js | 10 +++++----- .../base/cscal-wasm/test/test.module.ndarray.js | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js index 374ff5ab0d44..4c47508712b3 100644 --- a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.main.js @@ -96,7 +96,7 @@ tape( 'a module instance has a `main` method which scales elements from `cx` by mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); @@ -131,7 +131,7 @@ tape( 'a module instance has a `main` method which supports specifying a `cx` st mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, // 1 @@ -195,7 +195,7 @@ tape( 'a module instance has a `main` method which supports specifying a negativ mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, // 3 @@ -255,7 +255,7 @@ tape( 'a module instance has a `main` method which returns a reference to the in mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); @@ -280,7 +280,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, a module instance mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js index b1d2a16fc938..134327a740e4 100644 --- a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.module.ndarray.js @@ -96,7 +96,7 @@ tape( 'a module instance has an `ndarray` method which scales elements from `cx` mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); @@ -131,7 +131,7 @@ tape( 'a module instance has an `ndarray` method which supports a `cx` stride', mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, // 1 @@ -195,7 +195,7 @@ tape( 'a module instance has an `ndarray` method which supports a negative `cx` mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, // 3 @@ -259,7 +259,7 @@ tape( 'a module instance has an `ndarray` method which supports a `cx` offset', mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, @@ -319,7 +319,7 @@ tape( 'a module instance has an `ndarray` method which returns a reference to th mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); @@ -344,7 +344,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, a module instance mod.initializeSync(); cxp = 0; - cap = 48; + cap = 24; mod.write( cxp, new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ) ); mod.write( cap, new Float32Array( [ 0.4, -0.7 ] ) ); @@ -376,7 +376,7 @@ tape( 'a module instance has an `ndarray` method which supports complex access p mod.initializeSync(); cxp = 0; - cap = 96; + cap = 48; xbuf = new Float32Array( [ 0.1, From d8fca2b45be9e790261970548478677dc76a73c6 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 17 Nov 2024 15:42:14 -0800 Subject: [PATCH 3/4] test: uncomment test Signed-off-by: Athan --- .../@stdlib/blas/base/cscal-wasm/test/test.main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js index c3e6aa9ec16f..62f519fa5161 100644 --- a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.main.js @@ -221,8 +221,8 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method viewX = new Float32Array( cx.buffer ); expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - // cscal.main( -1, ca, cx, 1 ); - // t.deepEqual( viewX, expected, 'returns expected value' ); + cscal.main( -1, ca, cx, 1 ); + t.deepEqual( viewX, expected, 'returns expected value' ); cscal.main( 0, ca, cx, 1 ); t.deepEqual( viewX, expected, 'returns expected value' ); From 683165dd743e4802422674207e9e54764b187da2 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 17 Nov 2024 15:43:17 -0800 Subject: [PATCH 4/4] test: uncomment test Signed-off-by: Athan --- .../@stdlib/blas/base/cscal-wasm/test/test.ndarray.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js index edf34327f553..1216ac2345ce 100644 --- a/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/base/cscal-wasm/test/test.ndarray.js @@ -273,8 +273,8 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the `ndarray` met viewX = new Float32Array( cx.buffer ); expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); - // cscal.ndarray( -1, ca, cx, 1, 0 ); - // t.deepEqual( viewX, expected, 'returns expected value' ); + cscal.ndarray( -1, ca, cx, 1, 0 ); + t.deepEqual( viewX, expected, 'returns expected value' ); cscal.ndarray( 0, ca, cx, 1, 0 ); t.deepEqual( viewX, expected, 'returns expected value' );