From cd7169019a1a681c62181d7311406ecfa933f5ba Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 18 Jan 2024 21:24:26 +0000 Subject: [PATCH] Auto-generated commit --- .github/.keepalive | 1 - NOTICE | 2 +- README.md | 14 ++ benchmark/benchmark.assign.length.js | 95 ++++++++ benchmark/benchmark.js | 23 ++ dist/index.js | 9 +- dist/index.js.map | 8 +- docs/repl.txt | 28 +++ docs/types/index.d.ts | 327 ++++++++++++++++++++++++++- docs/types/test.ts | 66 ++++++ lib/assign.js | 198 ++++++++++++++++ lib/index.js | 17 ++ package.json | 19 +- test/test.assign.js | 211 +++++++++++++++++ test/test.js | 39 +--- test/test.main.js | 69 ++++++ 16 files changed, 1081 insertions(+), 45 deletions(-) delete mode 100644 .github/.keepalive create mode 100644 benchmark/benchmark.assign.length.js create mode 100644 lib/assign.js create mode 100644 test/test.assign.js create mode 100644 test/test.main.js diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 4d03851..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-01-01T04:11:21.987Z diff --git a/NOTICE b/NOTICE index f7aca1b..e6e7482 100644 --- a/NOTICE +++ b/NOTICE @@ -1 +1 @@ -Copyright (c) 2016-2023 The Stdlib Authors. +Copyright (c) 2016-2024 The Stdlib Authors. diff --git a/README.md b/README.md index 0323bfd..c9252c5 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,20 @@ var arr = zeroTo( 5.1 ); // returns [ 0, 1, 2, 3, 4, 5 ] ``` +#### zeroTo.assign( out, stride, offset ) + +Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + +```javascript +var out = [ 0, 0, 0, 0, 0, 0 ]; + +var arr = zeroTo.assign( out, -1, out.length-1 ); +// returns [ 5, 4, 3, 2, 1, 0 ] + +var bool = ( arr === out ); +// returns true +``` + diff --git a/benchmark/benchmark.assign.length.js b/benchmark/benchmark.assign.length.js new file mode 100644 index 0000000..79d6c45 --- /dev/null +++ b/benchmark/benchmark.assign.length.js @@ -0,0 +1,95 @@ +/** +* @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-harness' ); +var pow = require( '@stdlib/math-base-special-pow' ); +var isArray = require( '@stdlib/assert-is-array' ); +var zeros = require( '@stdlib/array-base-zeros' ); +var pkg = require( './../package.json' ).name; +var zeroTo = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out = zeros( len ); + 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 = zeroTo.assign( out, 1, 0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + 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/benchmark/benchmark.js b/benchmark/benchmark.js index 1ebbe0f..c1d8f3c 100644 --- a/benchmark/benchmark.js +++ b/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench-harness' ); var isArray = require( '@stdlib/assert-is-array' ); +var zeros = require( '@stdlib/array-base-zeros' ); var pkg = require( './../package.json' ).name; var zeroTo = require( './../lib' ); @@ -46,3 +47,25 @@ bench( pkg, function benchmark( b ) { b.pass( 'benchmark finished' ); b.end(); }); + +bench( pkg+':assign', function benchmark( b ) { + var out; + var i; + var v; + + out = zeros( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = zeroTo.assign( out, 1, 0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/dist/index.js b/dist/index.js index 8286342..7bb8c76 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,8 @@ -"use strict";var u=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var s=u(function(c,i){ -function o(e){var r,t;if(r=[],e<=0)return r;for(t=0;t=0&&n=0&&s=0&&s} linearly spaced numeric array\n*\n* @example\n* var arr = zeroTo( 6 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*/\nfunction zeroTo( n ) {\n\tvar arr;\n\tvar i;\n\n\tarr = [];\n\tif ( n <= 0 ) {\n\t\treturn arr;\n\t}\n\tfor ( i = 0; i < n; i++ ) {\n\t\tarr.push( i );\n\t}\n\treturn arr;\n}\n\n\n// EXPORTS //\n\nmodule.exports = zeroTo;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Generate a linearly spaced numeric array whose elements increment by 1 starting from zero.\n*\n* @module @stdlib/array-base-zero-to\n*\n* @example\n* var zeroTo = require( '@stdlib/array-base-zero-to' );\n*\n* var arr = zeroTo( 6 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAgCA,SAASC,EAAQC,EAAI,CACpB,IAAIC,EACAC,EAGJ,GADAD,EAAM,CAAC,EACFD,GAAK,EACT,OAAOC,EAER,IAAMC,EAAI,EAAGA,EAAIF,EAAGE,IACnBD,EAAI,KAAMC,CAAE,EAEb,OAAOD,CACR,CAKAH,EAAO,QAAUC,ICfjB,IAAII,EAAO,IAKX,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "zeroTo", "n", "arr", "i", "main"] + "sources": ["../lib/main.js", "../lib/assign.js", "../lib/index.js"], + "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MAIN //\n\n/**\n* Generates a linearly spaced numeric array whose elements increment by 1 starting from zero.\n*\n* @param {number} n - number of elements\n* @returns {Array} linearly spaced numeric array\n*\n* @example\n* var arr = zeroTo( 6 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*/\nfunction zeroTo( n ) {\n\tvar arr;\n\tvar i;\n\n\tarr = [];\n\tif ( n <= 0 ) {\n\t\treturn arr;\n\t}\n\tfor ( i = 0; i < n; i++ ) {\n\t\tarr.push( i );\n\t}\n\treturn arr;\n}\n\n\n// EXPORTS //\n\nmodule.exports = zeroTo;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isComplex128Array = require( '@stdlib/array-base-assert-is-complex128array' );\nvar isComplex64Array = require( '@stdlib/array-base-assert-is-complex64array' );\nvar arraylike2object = require( '@stdlib/array-base-arraylike2object' );\nvar reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );\nvar reinterpret64 = require( '@stdlib/strided-base-reinterpret-complex64' );\n\n\n// FUNCTIONS //\n\n/**\n* Fills an indexed array with linearly spaced numeric elements which increment by 1 starting from zero.\n*\n* @private\n* @param {Collection} out - output array\n* @param {integer} stride - output array stride\n* @param {NonNegativeInteger} offset - output array index offset\n* @returns {Collection} output array\n*\n* @example\n* var out = [ 0, 0, 0, 0, 0, 0 ];\n*\n* var arr = indexed( out, 1, 0 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*\n* @example\n* var out = [ 0, 0, 0, 0, 0, 0 ];\n*\n* var arr = indexed( out, -1, out.length-1 );\n* // returns [ 5, 4, 3, 2, 1, 0 ]\n*/\nfunction indexed( out, stride, offset ) {\n\tvar v;\n\tvar i;\n\n\ti = offset;\n\tv = 0;\n\twhile ( i >= 0 && i < out.length ) {\n\t\tout[ i ] = v;\n\t\ti += stride;\n\t\tv += 1;\n\t}\n\treturn out;\n}\n\n/**\n* Fills a complex number array with linearly spaced numeric elements which increment by 1 starting from zero.\n*\n* @private\n* @param {(Complex128Array|Complex64Array)} out - output complex number array\n* @param {(Float64Array|Float32Array)} data - output array data\n* @param {integer} stride - output array stride\n* @param {NonNegativeInteger} offset - output array index offset\n* @returns {(Complex128Array|Complex64Array)} output array\n*\n* @example\n* var Complex128Array = require( '@stdlib/array-complex128' );\n* var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );\n*\n* var out = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );\n* // returns \n*\n* var data = reinterpret128( out, 0 );\n* // returns [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]\n*\n* var arr = complex( out, data, 1, 0 );\n* // returns \n*\n* var bool = ( arr === out );\n* // returns true\n*\n* data = reinterpret128( out, 0 );\n* returns [ 0.0, 0.0, 1.0, 0.0, 2.0, 0.0 ]\n*/\nfunction complex( out, data, stride, offset ) {\n\tvar v;\n\tvar s;\n\tvar i;\n\n\ts = stride * 2;\n\ti = offset * 2;\n\tv = 0.0;\n\twhile ( i >= 0 && i < data.length ) {\n\t\tdata[ i ] = v; // real component\n\t\tdata[ i+1 ] = 0.0; // imaginary component\n\t\ti += s;\n\t\tv += 1.0;\n\t}\n\treturn out;\n}\n\n/**\n* Fills an accessor array with linearly spaced numeric elements which increment by 1 starting from zero.\n*\n* @private\n* @param {Object} out - output array object\n* @param {integer} stride - output array stride\n* @param {NonNegativeInteger} offset - output array index offset\n* @returns {Collection} output array\n*\n* @example\n* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );\n* var arraylike2object = require( '@stdlib/array-base-arraylike2object' );\n\n* var out = toAccessorArray( [ 0, 0, 0, 0, 0, 0 ] );\n* var arr = accessors( arraylike2object( out ), 1, 0 );\n*\n* var bool = ( arr === out );\n* // returns true\n*\n* var v = out.get( 0 );\n* // returns 0\n*\n* v = out.get( out.length-1 );\n* // returns 5\n*/\nfunction accessors( out, stride, offset ) {\n\tvar data;\n\tvar set;\n\tvar v;\n\tvar i;\n\n\tdata = out.data;\n\tset = out.accessors[ 1 ];\n\n\ti = offset;\n\tv = 0;\n\twhile ( i >= 0 && i < data.length ) {\n\t\tset( data, i, v );\n\t\ti += stride;\n\t\tv += 1;\n\t}\n\treturn data;\n}\n\n\n// MAIN //\n\n/**\n* Fills an array with linearly spaced numeric elements which increment by 1 starting from zero.\n*\n* @param {Collection} out - output array\n* @param {integer} stride - output array stride\n* @param {NonNegativeInteger} offset - output array index offset\n* @returns {Collection} output array\n*\n* @example\n* var out = [ 0, 0, 0, 0, 0, 0 ];\n*\n* var arr = assign( out, 1, 0 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*\n* @example\n* var out = [ 0, 0, 0, 0, 0, 0 ];\n*\n* var arr = assign( out, -1, out.length-1 );\n* // returns [ 5, 4, 3, 2, 1, 0 ]\n*/\nfunction assign( out, stride, offset ) {\n\tvar obj = arraylike2object( out );\n\tif ( obj.accessorProtocol ) {\n\t\t// If provided a complex number array, reinterpret as a real typed array and only set the real components...\n\t\tif ( isComplex128Array( out ) ) {\n\t\t\treturn complex( out, reinterpret128( out, 0 ), stride, offset );\n\t\t}\n\t\tif ( isComplex64Array( out ) ) {\n\t\t\treturn complex( out, reinterpret64( out, 0 ), stride, offset );\n\t\t}\n\t\treturn accessors( obj, stride, offset );\n\t}\n\treturn indexed( out, stride, offset );\n}\n\n\n// EXPORTS //\n\nmodule.exports = assign;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Generate a linearly spaced numeric array whose elements increment by 1 starting from zero.\n*\n* @module @stdlib/array-base-zero-to\n*\n* @example\n* var zeroTo = require( '@stdlib/array-base-zero-to' );\n*\n* var arr = zeroTo( 6 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*\n* @example\n* var zeroTo = require( '@stdlib/array-base-zero-to' );\n*\n* var out = [ 0, 0, 0, 0, 0, 0 ];\n* var arr = zeroTo.assign( out, 1, 0 );\n* // returns [ 0, 1, 2, 3, 4, 5 ]\n*\n* var bool = ( out === arr );\n* // returns true\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar assign = require( './assign.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'assign', assign );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], + "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAgCA,SAASC,EAAQC,EAAI,CACpB,IAAIC,EACA,EAGJ,GADAA,EAAM,CAAC,EACFD,GAAK,EACT,OAAOC,EAER,IAAM,EAAI,EAAG,EAAID,EAAG,IACnBC,EAAI,KAAM,CAAE,EAEb,OAAOA,CACR,CAKAH,EAAO,QAAUC,ICjDjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAoB,QAAS,8CAA+C,EAC5EC,EAAmB,QAAS,6CAA8C,EAC1EC,EAAmB,QAAS,qCAAsC,EAClEC,EAAiB,QAAS,6CAA8C,EACxEC,EAAgB,QAAS,4CAA6C,EA0B1E,SAASC,EAASC,EAAKC,EAAQC,EAAS,CACvC,IAAIC,EACAC,EAIJ,IAFAA,EAAIF,EACJC,EAAI,EACIC,GAAK,GAAKA,EAAIJ,EAAI,QACzBA,EAAKI,CAAE,EAAID,EACXC,GAAKH,EACLE,GAAK,EAEN,OAAOH,CACR,CA+BA,SAASK,EAASL,EAAKM,EAAML,EAAQC,EAAS,CAC7C,IAAIC,EACAI,EACAH,EAKJ,IAHAG,EAAIN,EAAS,EACbG,EAAIF,EAAS,EACbC,EAAI,EACIC,GAAK,GAAKA,EAAIE,EAAK,QAC1BA,EAAMF,CAAE,EAAID,EACZG,EAAMF,EAAE,CAAE,EAAI,EACdA,GAAKG,EACLJ,GAAK,EAEN,OAAOH,CACR,CA2BA,SAASQ,EAAWR,EAAKC,EAAQC,EAAS,CACzC,IAAII,EACAG,EACA,EACAL,EAOJ,IALAE,EAAON,EAAI,KACXS,EAAMT,EAAI,UAAW,CAAE,EAEvBI,EAAIF,EACJ,EAAI,EACIE,GAAK,GAAKA,EAAIE,EAAK,QAC1BG,EAAKH,EAAMF,EAAG,CAAE,EAChBA,GAAKH,EACL,GAAK,EAEN,OAAOK,CACR,CAyBA,SAASI,EAAQV,EAAKC,EAAQC,EAAS,CACtC,IAAIS,EAAMf,EAAkBI,CAAI,EAChC,OAAKW,EAAI,iBAEHjB,EAAmBM,CAAI,EACpBK,EAASL,EAAKH,EAAgBG,EAAK,CAAE,EAAGC,EAAQC,CAAO,EAE1DP,EAAkBK,CAAI,EACnBK,EAASL,EAAKF,EAAeE,EAAK,CAAE,EAAGC,EAAQC,CAAO,EAEvDM,EAAWG,EAAKV,EAAQC,CAAO,EAEhCH,EAASC,EAAKC,EAAQC,CAAO,CACrC,CAKAT,EAAO,QAAUiB,ICzJjB,IAAIE,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAS,IAKbF,EAAaC,EAAM,SAAUC,CAAO,EAKpC,OAAO,QAAUD", + "names": ["require_main", "__commonJSMin", "exports", "module", "zeroTo", "n", "arr", "require_assign", "__commonJSMin", "exports", "module", "isComplex128Array", "isComplex64Array", "arraylike2object", "reinterpret128", "reinterpret64", "indexed", "out", "stride", "offset", "v", "i", "complex", "data", "s", "accessors", "set", "assign", "obj", "setReadOnly", "main", "assign"] } diff --git a/docs/repl.txt b/docs/repl.txt index d7e3a70..a25337b 100644 --- a/docs/repl.txt +++ b/docs/repl.txt @@ -23,6 +23,34 @@ > var arr = {{alias}}( 6 ) [ 0, 1, 2, 3, 4, 5 ] + +{{alias}}.assign( out, stride, offset ) + Fills an array with linearly spaced numeric elements which increment by 1 + starting from zero. + + Parameters + ---------- + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array index offset. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var out = [ 0, 0, 0, 0, 0, 0 ]; + > {{alias}}.assign( out, -1, out.length-1 ); + > out + [ 5, 4, 3, 2, 1, 0 ] + See Also -------- diff --git a/docs/types/index.d.ts b/docs/types/index.d.ts index 60f0060..b0e05e8 100644 --- a/docs/types/index.d.ts +++ b/docs/types/index.d.ts @@ -18,6 +18,319 @@ // TypeScript Version: 4.1 +/// + +import { Collection, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array'; + +/** +* Interface describing `zeroTo`. +*/ +interface ZeroTo { + /** + * Generates a linearly spaced numeric array whose elements increment by 1 starting from zero. + * + * @param n - number of elements + * @returns linearly spaced numeric array + * + * @example + * var arr = zeroTo( 6 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + ( n: number ): Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Float64Array = require( '@stdlib/array-float64' ); + * + * var out = new Float64Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] + */ + assign( out: Float64Array, stride: number, offset: number ): Float64Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Float32Array = require( '@stdlib/array-float32' ); + * + * var out = new Float32Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] + */ + assign( out: Float32Array, stride: number, offset: number ): Float32Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Int32Array = require( '@stdlib/array-int32' ); + * + * var out = new Int32Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Int32Array, stride: number, offset: number ): Int32Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Int16Array = require( '@stdlib/array-int16' ); + * + * var out = new Int16Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Int16Array, stride: number, offset: number ): Int16Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Int8Array = require( '@stdlib/array-int8' ); + * + * var out = new Int8Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Int8Array, stride: number, offset: number ): Int8Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Uint32Array = require( '@stdlib/array-uint32' ); + * + * var out = new Uint32Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Uint32Array, stride: number, offset: number ): Uint32Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Uint16Array = require( '@stdlib/array-uint16' ); + * + * var out = new Uint16Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Uint16Array, stride: number, offset: number ): Uint16Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Uint8Array = require( '@stdlib/array-uint8' ); + * + * var out = new Uint8Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Uint8Array, stride: number, offset: number ): Uint8Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Uint8ClampedArray = require( '@stdlib/array-uint8c' ); + * + * var out = new Uint8ClampedArray( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + */ + assign( out: Uint8ClampedArray, stride: number, offset: number ): Uint8ClampedArray; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Complex128Array = require( '@stdlib/array-complex128' ); + * var real = require( '@stdlib/complex-real' ); + * var imag = require( '@stdlib/complex-imag' ); + * + * var out = new Complex128Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns + * + * var bool = ( arr === out ); + * // returns true + * + * var v = out.get( out.length-1 ); + * // returns + * + * var re = real( v ); + * // returns 5.0 + * + * var im = imag( v ); + * // returns 0.0 + */ + assign( out: Complex128Array, stride: number, offset: number ): Complex128Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Complex64Array = require( '@stdlib/array-complex64' ); + * var realf = require( '@stdlib/complex-realf' ); + * var imagf = require( '@stdlib/complex-imagf' ); + * + * var out = new Complex64Array( 6 ); + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns + * + * var bool = ( arr === out ); + * // returns true + * + * var v = out.get( out.length-1 ); + * // returns + * + * var re = realf( v ); + * // returns 5.0 + * + * var im = imagf( v ); + * // returns 0.0 + */ + assign( out: Complex64Array, stride: number, offset: number ): Complex64Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' ); + * + * var out = toAccessorArray( [ 0, 0, 0, 0, 0, 0 ] ); + * var arr = zeroTo.assign( out, 1, 0 ); + * + * var bool = ( arr === out ); + * // returns true + * + * var v = out.get( out.length-1 ); + * // returns 5 + */ + assign( out: AccessorArrayLike, stride: number, offset: number ): AccessorArrayLike; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var out = [ 0, 0, 0, 0, 0, 0 ]; + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + * + * @example + * var out = [ 0, 0, 0, 0, 0, 0 ]; + * + * var arr = zeroTo.assign( out, -1, out.length-1 ); + * // returns [ 5, 4, 3, 2, 1, 0 ] + */ + assign( out: Array, stride: number, offset: number ): Array; + + /** + * Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. + * + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var out = [ 0, 0, 0, 0, 0, 0 ]; + * + * var arr = zeroTo.assign( out, 1, 0 ); + * // returns [ 0, 1, 2, 3, 4, 5 ] + * + * @example + * var out = [ 0, 0, 0, 0, 0, 0 ]; + * + * var arr = zeroTo.assign( out, -1, out.length-1 ); + * // returns [ 5, 4, 3, 2, 1, 0 ] + */ + assign( out: Collection, stride: number, offset: number ): Collection; +} + /** * Generates a linearly spaced numeric array whose elements increment by 1 starting from zero. * @@ -27,8 +340,20 @@ * @example * var arr = zeroTo( 6 ); * // returns [ 0, 1, 2, 3, 4, 5 ] +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = zeroTo.assign( out, 1, 0 ); +* // returns [ 0, 1, 2, 3, 4, 5 ] +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = zeroTo.assign( out, -1, out.length-1 ); +* // returns [ 5, 4, 3, 2, 1, 0 ] */ -declare function zeroTo( n: number ): Array; +declare var zeroTo: ZeroTo; // EXPORTS // diff --git a/docs/types/test.ts b/docs/types/test.ts index d521695..6b43ab6 100644 --- a/docs/types/test.ts +++ b/docs/types/test.ts @@ -16,6 +16,8 @@ * limitations under the License. */ +import Complex128Array = require( '@stdlib/array-complex128' ); +import Complex64Array = require( '@stdlib/array-complex64' ); import zeroTo = require( './index' ); @@ -42,3 +44,67 @@ import zeroTo = require( './index' ); zeroTo( 3, 4 ); // $ExpectError zeroTo( 3, 4, 1 ); // $ExpectError } + +// Attached to the main export is an `assign` method which returns a collection... +{ + zeroTo.assign( [ 0, 0, 0, 0 ], 1, 0 ); // $ExpectType number[] + zeroTo.assign( new Float64Array( 4 ), 1, 0 ); // $ExpectType Float64Array + zeroTo.assign( new Float32Array( 4 ), 1, 0 ); // $ExpectType Float32Array + zeroTo.assign( new Int32Array( 4 ), 1, 0 ); // $ExpectType Int32Array + zeroTo.assign( new Int16Array( 4 ), 1, 0 ); // $ExpectType Int16Array + zeroTo.assign( new Int8Array( 4 ), 1, 0 ); // $ExpectType Int8Array + zeroTo.assign( new Uint32Array( 4 ), 1, 0 ); // $ExpectType Uint32Array + zeroTo.assign( new Uint16Array( 4 ), 1, 0 ); // $ExpectType Uint16Array + zeroTo.assign( new Uint8Array( 4 ), 1, 0 ); // $ExpectType Uint8Array + zeroTo.assign( new Uint8ClampedArray( 4 ), 1, 0 ); // $ExpectType Uint8ClampedArray + zeroTo.assign( new Complex128Array( 4 ), 1, 0 ); // $ExpectType Complex128Array + zeroTo.assign( new Complex64Array( 4 ), 1, 0 ); // $ExpectType Complex64Array +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + zeroTo.assign( 1, 1, 0 ); // $ExpectError + zeroTo.assign( true, 1, 0 ); // $ExpectError + zeroTo.assign( false, 1, 0 ); // $ExpectError + zeroTo.assign( null, 1, 0 ); // $ExpectError + zeroTo.assign( void 0, 1, 0 ); // $ExpectError + zeroTo.assign( {}, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a number... +{ + const out = [ 0, 0, 0, 0 ]; + + zeroTo.assign( out, '1', 0 ); // $ExpectError + zeroTo.assign( out, true, 0 ); // $ExpectError + zeroTo.assign( out, false, 0 ); // $ExpectError + zeroTo.assign( out, null, 0 ); // $ExpectError + zeroTo.assign( out, void 0, 0 ); // $ExpectError + zeroTo.assign( out, {}, 0 ); // $ExpectError + zeroTo.assign( out, [], 0 ); // $ExpectError + zeroTo.assign( out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not a number... +{ + const out = [ 0, 0, 0, 0 ]; + + zeroTo.assign( out, 1, '1' ); // $ExpectError + zeroTo.assign( out, 1, true ); // $ExpectError + zeroTo.assign( out, 1, false ); // $ExpectError + zeroTo.assign( out, 1, null ); // $ExpectError + zeroTo.assign( out, 1, void 0 ); // $ExpectError + zeroTo.assign( out, 1, {} ); // $ExpectError + zeroTo.assign( out, 1, [] ); // $ExpectError + zeroTo.assign( out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const out = [ 0, 0, 0, 0 ]; + + zeroTo.assign(); // $ExpectError + zeroTo.assign( out ); // $ExpectError + zeroTo.assign( out, 1 ); // $ExpectError + zeroTo.assign( out, 1, 0, {} ); // $ExpectError +} diff --git a/lib/assign.js b/lib/assign.js new file mode 100644 index 0000000..1a52b4c --- /dev/null +++ b/lib/assign.js @@ -0,0 +1,198 @@ +/** +* @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 isComplex128Array = require( '@stdlib/array-base-assert-is-complex128array' ); +var isComplex64Array = require( '@stdlib/array-base-assert-is-complex64array' ); +var arraylike2object = require( '@stdlib/array-base-arraylike2object' ); +var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/strided-base-reinterpret-complex64' ); + + +// FUNCTIONS // + +/** +* Fills an indexed array with linearly spaced numeric elements which increment by 1 starting from zero. +* +* @private +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @returns {Collection} output array +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = indexed( out, 1, 0 ); +* // returns [ 0, 1, 2, 3, 4, 5 ] +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = indexed( out, -1, out.length-1 ); +* // returns [ 5, 4, 3, 2, 1, 0 ] +*/ +function indexed( out, stride, offset ) { + var v; + var i; + + i = offset; + v = 0; + while ( i >= 0 && i < out.length ) { + out[ i ] = v; + i += stride; + v += 1; + } + return out; +} + +/** +* Fills a complex number array with linearly spaced numeric elements which increment by 1 starting from zero. +* +* @private +* @param {(Complex128Array|Complex64Array)} out - output complex number array +* @param {(Float64Array|Float32Array)} data - output array data +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @returns {(Complex128Array|Complex64Array)} output array +* +* @example +* var Complex128Array = require( '@stdlib/array-complex128' ); +* var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' ); +* +* var out = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); +* // returns +* +* var data = reinterpret128( out, 0 ); +* // returns [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] +* +* var arr = complex( out, data, 1, 0 ); +* // returns +* +* var bool = ( arr === out ); +* // returns true +* +* data = reinterpret128( out, 0 ); +* returns [ 0.0, 0.0, 1.0, 0.0, 2.0, 0.0 ] +*/ +function complex( out, data, stride, offset ) { + var v; + var s; + var i; + + s = stride * 2; + i = offset * 2; + v = 0.0; + while ( i >= 0 && i < data.length ) { + data[ i ] = v; // real component + data[ i+1 ] = 0.0; // imaginary component + i += s; + v += 1.0; + } + return out; +} + +/** +* Fills an accessor array with linearly spaced numeric elements which increment by 1 starting from zero. +* +* @private +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array-base-arraylike2object' ); + +* var out = toAccessorArray( [ 0, 0, 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( out ), 1, 0 ); +* +* var bool = ( arr === out ); +* // returns true +* +* var v = out.get( 0 ); +* // returns 0 +* +* v = out.get( out.length-1 ); +* // returns 5 +*/ +function accessors( out, stride, offset ) { + var data; + var set; + var v; + var i; + + data = out.data; + set = out.accessors[ 1 ]; + + i = offset; + v = 0; + while ( i >= 0 && i < data.length ) { + set( data, i, v ); + i += stride; + v += 1; + } + return data; +} + + +// MAIN // + +/** +* Fills an array with linearly spaced numeric elements which increment by 1 starting from zero. +* +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @returns {Collection} output array +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = assign( out, 1, 0 ); +* // returns [ 0, 1, 2, 3, 4, 5 ] +* +* @example +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* +* var arr = assign( out, -1, out.length-1 ); +* // returns [ 5, 4, 3, 2, 1, 0 ] +*/ +function assign( out, stride, offset ) { + var obj = arraylike2object( out ); + if ( obj.accessorProtocol ) { + // If provided a complex number array, reinterpret as a real typed array and only set the real components... + if ( isComplex128Array( out ) ) { + return complex( out, reinterpret128( out, 0 ), stride, offset ); + } + if ( isComplex64Array( out ) ) { + return complex( out, reinterpret64( out, 0 ), stride, offset ); + } + return accessors( obj, stride, offset ); + } + return indexed( out, stride, offset ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/index.js b/lib/index.js index 18e4811..8607216 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,11 +28,28 @@ * * var arr = zeroTo( 6 ); * // returns [ 0, 1, 2, 3, 4, 5 ] +* +* @example +* var zeroTo = require( '@stdlib/array-base-zero-to' ); +* +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* var arr = zeroTo.assign( out, 1, 0 ); +* // returns [ 0, 1, 2, 3, 4, 5 ] +* +* var bool = ( out === arr ); +* // 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/package.json b/package.json index 8e02309..3d7eebb 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,27 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": {}, + "dependencies": { + "@stdlib/array-base-arraylike2object": "^0.1.0", + "@stdlib/array-base-assert-is-complex128array": "^0.1.0", + "@stdlib/array-base-assert-is-complex64array": "^0.1.0", + "@stdlib/strided-base-reinterpret-complex128": "^0.1.1", + "@stdlib/strided-base-reinterpret-complex64": "^0.1.1", + "@stdlib/types": "^0.2.0", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1" + }, "devDependencies": { "@stdlib/array-base-filled-by": "^0.1.0", + "@stdlib/array-base-to-accessor-array": "^0.1.0", + "@stdlib/array-base-zeros": "^0.1.1", + "@stdlib/array-complex128": "^0.1.0", + "@stdlib/array-complex64": "^0.1.0", + "@stdlib/array-float64": "^0.1.1", + "@stdlib/assert-has-own-property": "^0.1.1", "@stdlib/assert-is-array": "^0.1.1", + "@stdlib/assert-is-same-complex128array": "github:stdlib-js/assert-is-same-complex128array#main", + "@stdlib/assert-is-same-complex64array": "github:stdlib-js/assert-is-same-complex64array#main", + "@stdlib/assert-is-same-float64array": "github:stdlib-js/assert-is-same-float64array#main", "@stdlib/blas-ext-base-gsort2hp": "^0.1.0", "@stdlib/math-base-special-pow": "^0.1.0", "@stdlib/random-base-randu": "^0.1.0", diff --git a/test/test.assign.js b/test/test.assign.js new file mode 100644 index 0000000..23bbe16 --- /dev/null +++ b/test/test.assign.js @@ -0,0 +1,211 @@ +/** +* @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 Complex128Array = require( '@stdlib/array-complex128' ); +var Complex64Array = require( '@stdlib/array-complex64' ); +var Float64Array = require( '@stdlib/array-float64' ); +var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' ); +var isSameFloat64Array = require( '@stdlib/assert-is-same-float64array' ); +var isSameComplex128Array = require( '@stdlib/assert-is-same-complex128array' ); +var isSameComplex64Array = require( '@stdlib/assert-is-same-complex64array' ); +var zeroTo = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof zeroTo, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function fills an array with linearly spaced numbers (generic)', function test( t ) { + var expected; + var actual; + var out; + + out = [ 0, 0, 0, 0, 0 ]; + actual = zeroTo( out, 1, 0 ); + expected = [ 0, 1, 2, 3, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = [ 0, 0, 0 ]; + actual = zeroTo( out, -1, out.length-1 ); + expected = [ 2, 1, 0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = [ -1, -1, -1, -1, -1 ]; + actual = zeroTo( out, 2, 1 ); + expected = [ -1, 0, -1, 1, -1 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + out = [ -1, -1, -1, -1, -1, -1 ]; + actual = zeroTo( out, -2, out.length-2 ); + expected = [ 2, -1, 1, -1, 0, -1 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an array with linearly spaced numbers (real typed array)', function test( t ) { + var expected; + var actual; + var out; + + out = new Float64Array( [ 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, 1, 0 ); + expected = new Float64Array( [ 0, 1, 2, 3, 4 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual, expected ), true, 'returns expected value' ); + + out = new Float64Array( [ 0, 0, 0 ] ); + actual = zeroTo( out, -1, out.length-1 ); + expected = new Float64Array( [ 2, 1, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual, expected ), true, 'returns expected value' ); + + out = new Float64Array( [ -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, 2, 1 ); + expected = new Float64Array( [ -1, 0, -1, 1, -1 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual, expected ), true, 'returns expected value' ); + + out = new Float64Array( [ -1, -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, -2, out.length-2 ); + expected = new Float64Array( [ 2, -1, 1, -1, 0, -1 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an array with linearly spaced numbers (complex typed array)', function test( t ) { + var expected; + var actual; + var out; + + out = new Complex128Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, 1, 0 ); + expected = new Complex128Array( [ 0, 0, 1, 0, 2, 0, 3, 0, 4, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = new Complex128Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, -1, out.length-1 ); + expected = new Complex128Array( [ 4, 0, 3, 0, 2, 0, 1, 0, 0, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = new Complex128Array( [ -1, -1, -1, -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, 2, 1 ); + expected = new Complex128Array( [ -1, -1, 0, 0, -1, -1, 1, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + out = new Complex64Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, 1, 0 ); + expected = new Complex64Array( [ 0, 0, 1, 0, 2, 0, 3, 0, 4, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + out = new Complex64Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, -1, out.length-1 ); + expected = new Complex64Array( [ 4, 0, 3, 0, 2, 0, 1, 0, 0, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + out = new Complex64Array( [ -1, -1, -1, -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, 2, 1 ); + expected = new Complex64Array( [ -1, -1, 0, 0, -1, -1, 1, 0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an array with linearly spaced numbers (accessor)', function test( t ) { + var expected; + var actual; + var out; + + out = toAccessorArray( [ 0, 0, 0, 0, 0 ] ); + actual = zeroTo( out, 1, 0 ); + expected = [ 0, 1, 2, 3, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = toAccessorArray( [ 0, 0, 0 ] ); + actual = zeroTo( out, -1, out.length-1 ); + expected = [ 2, 1, 0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = toAccessorArray( [ -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, 2, 1 ); + expected = [ -1, 0, -1, 1, -1 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + out = toAccessorArray( [ -1, -1, -1, -1, -1, -1 ] ); + actual = zeroTo( out, -2, out.length-2 ); + expected = [ 2, -1, 1, -1, 0, -1 ]; + + 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 an empty array if provided an empty array', function test( t ) { + t.deepEqual( zeroTo( [], 1, 0 ), [], 'returns expected value' ); + t.deepEqual( zeroTo( [], 1, 0 ), [], 'returns expected value' ); + t.end(); +}); diff --git a/test/test.js b/test/test.js index 37e82a1..2c4dc0e 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2022 The Stdlib Authors. +* 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. @@ -21,6 +21,7 @@ // MODULES // var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var zeroTo = require( './../lib' ); @@ -32,38 +33,8 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function returns a linearly spaced array', function test( t ) { - var expected; - var actual; - - actual = zeroTo( 10 ); - expected = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; - t.deepEqual( actual, expected, 'returns expected value' ); - - actual = zeroTo( 5 ); - expected = [ 0, 1, 2, 3, 4 ]; - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns a linearly spaced array (fractional argument)', function test( t ) { - var expected; - var actual; - - actual = zeroTo( 10.5 ); - expected = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; - t.deepEqual( actual, expected, 'returns expected value' ); - - actual = zeroTo( 5.0000001 ); - expected = [ 0, 1, 2, 3, 4, 5 ]; - t.deepEqual( actual, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function returns an empty array if provided a value which is less than or equal to zero', function test( t ) { - t.deepEqual( zeroTo( -1 ), [], 'returns expected value' ); - t.deepEqual( zeroTo( 0 ), [], 'returns expected value' ); +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( zeroTo, 'assign' ), true, 'has property' ); + t.strictEqual( typeof zeroTo.assign, 'function', 'has method' ); t.end(); }); diff --git a/test/test.main.js b/test/test.main.js new file mode 100644 index 0000000..37e82a1 --- /dev/null +++ b/test/test.main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 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 zeroTo = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof zeroTo, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a linearly spaced array', function test( t ) { + var expected; + var actual; + + actual = zeroTo( 10 ); + expected = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = zeroTo( 5 ); + expected = [ 0, 1, 2, 3, 4 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a linearly spaced array (fractional argument)', function test( t ) { + var expected; + var actual; + + actual = zeroTo( 10.5 ); + expected = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = zeroTo( 5.0000001 ); + expected = [ 0, 1, 2, 3, 4, 5 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty array if provided a value which is less than or equal to zero', function test( t ) { + t.deepEqual( zeroTo( -1 ), [], 'returns expected value' ); + t.deepEqual( zeroTo( 0 ), [], 'returns expected value' ); + t.end(); +});