From 16bc4703bbc9b71815ecb70f43fae87ceabcab37 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 27 Feb 2024 01:37:54 +0000 Subject: [PATCH] Auto-generated commit --- .npmignore | 3 +- CONTRIBUTORS | 8 + iter/sequences/tribonacci/README.md | 215 +++++++++++++++ .../tribonacci/benchmark/benchmark.js | 71 +++++ ...quation_tribonacci_recurrence_relation.svg | 44 +++ .../docs/img/equation_tribonacci_sequence.svg | 84 ++++++ iter/sequences/tribonacci/docs/repl.txt | 48 ++++ .../tribonacci/docs/types/index.d.ts | 74 +++++ iter/sequences/tribonacci/docs/types/test.ts | 44 +++ iter/sequences/tribonacci/examples/index.js | 34 +++ iter/sequences/tribonacci/lib/index.js | 50 ++++ iter/sequences/tribonacci/lib/main.js | 168 ++++++++++++ iter/sequences/tribonacci/lib/validate.js | 69 +++++ iter/sequences/tribonacci/package.json | 65 +++++ iter/sequences/tribonacci/test/test.js | 255 ++++++++++++++++++ .../tribonacci/test/test.validate.js | 147 ++++++++++ 16 files changed, 1378 insertions(+), 1 deletion(-) create mode 100644 iter/sequences/tribonacci/README.md create mode 100644 iter/sequences/tribonacci/benchmark/benchmark.js create mode 100644 iter/sequences/tribonacci/docs/img/equation_tribonacci_recurrence_relation.svg create mode 100644 iter/sequences/tribonacci/docs/img/equation_tribonacci_sequence.svg create mode 100644 iter/sequences/tribonacci/docs/repl.txt create mode 100644 iter/sequences/tribonacci/docs/types/index.d.ts create mode 100644 iter/sequences/tribonacci/docs/types/test.ts create mode 100644 iter/sequences/tribonacci/examples/index.js create mode 100644 iter/sequences/tribonacci/lib/index.js create mode 100644 iter/sequences/tribonacci/lib/main.js create mode 100644 iter/sequences/tribonacci/lib/validate.js create mode 100644 iter/sequences/tribonacci/package.json create mode 100644 iter/sequences/tribonacci/test/test.js create mode 100644 iter/sequences/tribonacci/test/test.validate.js diff --git a/.npmignore b/.npmignore index 5c7f09d7c..8eea7dc24 100644 --- a/.npmignore +++ b/.npmignore @@ -29,8 +29,9 @@ branches.md .postinstall.json Makefile -# Ignore `binding.gyp` file to avoid compilation of native addon when installing package: +# Ignore files to avoid compilation of native addon when installing package: binding.gyp +include.gypi # Directories # ############### diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 4d10e1beb..2f33708df 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -2,6 +2,7 @@ # # Contributors listed in alphabetical order. +Aditya Sapra <110766802+adityacodes30@users.noreply.github.com> Ali Salesi Amit Jimiwal Athan Reines @@ -13,6 +14,8 @@ Daniel Killenberger Dominik Moritz Dorrin Sotoudeh Frank Kovacs +GUNJ JOSHI +Golden <103646877+AuenKr@users.noreply.github.com> Harshita Kalani James Gelok Jaysukh Makvana @@ -22,6 +25,7 @@ Jordan Gallivan <115050475+Jordan-Gallivan@users.noreply.github.com> Joris Labie Justin Dennison Karthik Prakash <116057817+skoriop@users.noreply.github.com> +Khaldon Marcus Fantham Matt Cochrane Milan Raj @@ -30,13 +34,17 @@ Naresh Jagadeesan Nithin Katta <88046362+nithinkatta@users.noreply.github.com> Ognjen Jevremović Philipp Burckhardt +Prajwal Kulkarni Pranav Goswami +Pratik <97464067+Pratik772846@users.noreply.github.com> Ricky Reusser Robert Gislason Roman Stetsyk <25715951+romanstetsyk@users.noreply.github.com> +Rutam <138517416+performant23@users.noreply.github.com> Ryan Seal Seyyed Parsa Neshaei Shraddheya Shendre +Spandan Barve <114365550+marsian83@users.noreply.github.com> Stephannie Jiménez Gacha Yernar Yergaziyev orimiles5 <97595296+orimiles5@users.noreply.github.com> diff --git a/iter/sequences/tribonacci/README.md b/iter/sequences/tribonacci/README.md new file mode 100644 index 000000000..c1cb037c9 --- /dev/null +++ b/iter/sequences/tribonacci/README.md @@ -0,0 +1,215 @@ + + +# iterTribonacciSeq + +> Create an iterator which generates a [tribonacci sequence][tribonacci-number]. + + + +
+ +The [Tribonacci numbers][tribonacci-number] are the integer sequence + + + +```math +0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, \ldots +``` + + + + + +The sequence is defined by the recurrence relation + + + +```math +F_n = F_{n-1} + F_{n-2} + F_{n-3} +``` + + + + + +with seed values `F_0 = 0`, `F_1 = 0`, and `F_2 = 1`. + +
+ + + + + +
+ +## Usage + +```javascript +var iterTribonacciSeq = require( '@stdlib/math/iter/sequences/tribonacci' ); +``` + +#### iterTribonacciSeq( \[options] ) + +Returns an iterator which generates a [Tribonacci sequence][tribonacci-number]. + +```javascript +var it = iterTribonacciSeq(); +// returns + +var v = it.next().value; +// returns 0 + +v = it.next().value; +// returns 0 + +v = it.next().value; +// returns 1 + +// ... +``` + +The returned iterator protocol-compliant object has the following properties: + +- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. +- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. + +The function supports the following `options`: + +- **iter**: number of iterations. Default: `64`. + +The returned iterator can only generate the first `64` [Tribonacci numbers][tribonacci-number], as larger [Tribonacci numbers][tribonacci-number] cannot be safely represented in [double-precision floating-point format][ieee754]. By default, the function returns an iterator which generates all `64` numbers. To limit the number of iterations, set the `iter` option. + +```javascript +var opts = { + 'iter': 3 +}; +var it = iterTribonacciSeq( opts ); +// returns + +var v = it.next().value; +// returns 0 + +v = it.next().value; +// returns 0 + +v = it.next().value; +// returns 1 + +var bool = it.next().done; +// returns true +``` + + + + + + + +
+ +## Notes + +- If an environment supports `Symbol.iterator`, the returned iterator is iterable. + +
+ + + + + +
+ +## Examples + + + +```javascript +var iterTribonacciSeq = require( '@stdlib/math/iter/sequences/tribonacci' ); + +// Create an iterator: +var it = iterTribonacciSeq(); + +// Perform manual iteration... +var v; +while ( true ) { + v = it.next(); + if ( v.done ) { + break; + } + console.log( v.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/iter/sequences/tribonacci/benchmark/benchmark.js b/iter/sequences/tribonacci/benchmark/benchmark.js new file mode 100644 index 000000000..ac8095c6a --- /dev/null +++ b/iter/sequences/tribonacci/benchmark/benchmark.js @@ -0,0 +1,71 @@ +/** +* @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 isnan = require( './../../../../base/assert/is-nan' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var pkg = require( './../package.json' ).name; +var iterTribonacciSeq = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iter; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = iterTribonacciSeq(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( iter ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::iteration', function benchmark( b ) { + var iter; + var z; + var i; + + iter = iterTribonacciSeq(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = iter.next().value; + if ( isnan( z ) ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( isnan( z ) ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/iter/sequences/tribonacci/docs/img/equation_tribonacci_recurrence_relation.svg b/iter/sequences/tribonacci/docs/img/equation_tribonacci_recurrence_relation.svg new file mode 100644 index 000000000..c49388bf2 --- /dev/null +++ b/iter/sequences/tribonacci/docs/img/equation_tribonacci_recurrence_relation.svg @@ -0,0 +1,44 @@ + +upper F Subscript n Baseline equals upper F Subscript n minus 1 Baseline plus upper F Subscript n minus 2 Baseline plus upper F Subscript n minus 3 + + + \ No newline at end of file diff --git a/iter/sequences/tribonacci/docs/img/equation_tribonacci_sequence.svg b/iter/sequences/tribonacci/docs/img/equation_tribonacci_sequence.svg new file mode 100644 index 000000000..4035b9687 --- /dev/null +++ b/iter/sequences/tribonacci/docs/img/equation_tribonacci_sequence.svg @@ -0,0 +1,84 @@ + +0 comma 0 comma 1 comma 1 comma 2 comma 4 comma 7 comma 13 comma 24 comma 44 comma 81 comma 149 comma 274 comma 504 comma 927 comma 1705 comma ellipsis + + + \ No newline at end of file diff --git a/iter/sequences/tribonacci/docs/repl.txt b/iter/sequences/tribonacci/docs/repl.txt new file mode 100644 index 000000000..dcbd8486e --- /dev/null +++ b/iter/sequences/tribonacci/docs/repl.txt @@ -0,0 +1,48 @@ + +{{alias}}( [options] ) + Returns an iterator which generates a Tribonacci sequence. + + The returned iterator can only generate the first 64 Tribonacci numbers, as + larger Tribonacci numbers cannot be safely represented in double-precision + floating-point format. + + If an environment supports Symbol.iterator, the returned iterator is + iterable. + + + Parameters + ---------- + options: Object (optional) + Function options. + + options.iter: integer (optional) + Number of iterations. Default: 64. + + + Returns + ------- + iterator: Object + Iterator. + + iterator.next(): Function + Returns an iterator protocol-compliant + object has the next + iterated value (if one exists) and a + boolean flag indicating whether the + iterator is finished. + + iterator.return( [value] ): Function + Finishes an iterator and returns a provided value. + + + Examples + -------- + > var it = {{alias}}(); + > var v = it.next().value + 0 + > v = it.next().value + 0 + + See Also + -------- + diff --git a/iter/sequences/tribonacci/docs/types/index.d.ts b/iter/sequences/tribonacci/docs/types/index.d.ts new file mode 100644 index 000000000..72cff68e4 --- /dev/null +++ b/iter/sequences/tribonacci/docs/types/index.d.ts @@ -0,0 +1,74 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Number of iterations. + */ + iter?: number; +} + +/** +* Returns an iterator which generates a Tribonacci sequence. +* +* ## Notes +* +* - The returned iterator can only generate the first `64` Tribonacci numbers, as larger Tribonacci numbers cannot be safely represented in double-precision floating-point format. +* - If an environment supports `Symbol.iterator`, the returned iterator is iterable. +* +* @param options - function options +* @param options.iter - number of iterations (default: 64) +* @throws `iter` option must be a nonnegative integer +* @throws `iter` option must be less than or equal to 64 +* @returns iterator +* +* @example +* var iter = iterTribonacciSeq(); +* +* var v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 1 +* +* v = iter.next().value; +* // returns 2 +* +* // ... +*/ +declare function iterTribonacciSeq( options?: Options ): Iterator; + + +// EXPORTS //s + +export = iterTribonacciSeq; diff --git a/iter/sequences/tribonacci/docs/types/test.ts b/iter/sequences/tribonacci/docs/types/test.ts new file mode 100644 index 000000000..2d7b5dfe8 --- /dev/null +++ b/iter/sequences/tribonacci/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @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. +*/ + +import iterTribonacciSeq = require( './../../../../../iter/sequences/tribonacci' ); + + +// TESTS // + +// The function returns an iterator... +{ + iterTribonacciSeq(); // $ExpectType Iterator + iterTribonacciSeq( {} ); // $ExpectType Iterator + iterTribonacciSeq( { 'iter': 10 } ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an options object... +{ + iterTribonacciSeq( null ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `iter` option which is not a number... +{ + iterTribonacciSeq( { 'iter': '5' } ); // $ExpectError + iterTribonacciSeq( { 'iter': true } ); // $ExpectError + iterTribonacciSeq( { 'iter': false } ); // $ExpectError + iterTribonacciSeq( { 'iter': null } ); // $ExpectError + iterTribonacciSeq( { 'iter': [] } ); // $ExpectError + iterTribonacciSeq( { 'iter': {} } ); // $ExpectError +} diff --git a/iter/sequences/tribonacci/examples/index.js b/iter/sequences/tribonacci/examples/index.js new file mode 100644 index 000000000..75a61f73e --- /dev/null +++ b/iter/sequences/tribonacci/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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'; + +var iterTribonacciSeq = require('./../lib'); + +// Create an iterator: +var it = iterTribonacciSeq(); + +// Perform manual iteration... +var v; +while ( true ) { + v = it.next(); + if ( v.done ) { + break; + } + console.log( v.value ); +} diff --git a/iter/sequences/tribonacci/lib/index.js b/iter/sequences/tribonacci/lib/index.js new file mode 100644 index 000000000..757f25afb --- /dev/null +++ b/iter/sequences/tribonacci/lib/index.js @@ -0,0 +1,50 @@ +/** +* @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'; + +/** +* Create an iterator which generates a Tribonacci sequence. +* +* @module @stdlib/math/iter/sequences/tribonacci +* +* @example +* var iterTribonacciSeq = require( '@stdlib/math/iter/sequences/tribonacci' ); +* +* var iter = iterTribonacciSeq(); +* +* var v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 1 +* +* // ... +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/iter/sequences/tribonacci/lib/main.js b/iter/sequences/tribonacci/lib/main.js new file mode 100644 index 000000000..20ab0628f --- /dev/null +++ b/iter/sequences/tribonacci/lib/main.js @@ -0,0 +1,168 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var validate = require( './validate.js' ); + + +// VARIABLES // + +var F0 = 0; +var F1 = 0; +var F2 = 1; + + +// MAIN // + +/** +* Returns an iterator which generates a Tribonacci sequence. +* +* ## Notes +* +* - The returned iterator can only generate the first `63` Tribonacci numbers, as larger Tribonacci numbers cannot be safely represented in double-precision floating-point format. +* - If an environment supports `Symbol.iterator`, the returned iterator is iterable. +* +* @param {Options} [options] - function options +* @param {NonNegativeInteger} [options.iter=64] - number of iterations +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @throws {RangeError} `iter` option must be less than or equal to `64` +* @returns {Iterator} iterator +* +* @example +* var iter = iterTribonacciSeq(); +* +* var v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 1 +* +* // ... +*/ +function iterTribonacciSeq( options ) { + var opts; + var iter; + var FLG; + var err; + var f1; + var f2; + var f3; + var f; + var i; + + opts = { + 'iter': 64 + }; + if ( arguments.length ) { + err = validate( opts, options ); + if ( err ) { + throw err; + } + } + f1 = F0; + f2 = F1; + f3 = F2; + f = 0; + i = 0; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator`, make the iterator iterable: + if ( iteratorSymbol ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG || i > opts.iter ) { + return { + 'done': true + }; + } + if ( i === 1 ) { + f = F0; + } else if ( i === 2 ) { + f = F1; + } else if ( i === 3 ) { + f=F2; + } else { + f = f1 + f2 + f3; + f1 = f2; + f2 = f3; + f3 = f; + } + return { + 'value': f, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterTribonacciSeq( opts ); + } +} + + +// EXPORTS // + +module.exports = iterTribonacciSeq; diff --git a/iter/sequences/tribonacci/lib/validate.js b/iter/sequences/tribonacci/lib/validate.js new file mode 100644 index 000000000..83508855d --- /dev/null +++ b/iter/sequences/tribonacci/lib/validate.js @@ -0,0 +1,69 @@ +/** +* @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 isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination object +* @param {Options} options - function options +* @param {NonNegativeInteger} [options.iter] - number of iterations +* @returns {(Error|null)} null or an error object +* +* @example +* var opts = {}; +* var options = { +* 'iter': 50 +* }; +* var err = validate( opts, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, options ) { + if ( !isPlainObject( options ) ) { + return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'iter' ) ) { + opts.iter = options.iter; + if ( !isNonNegativeInteger( options.iter ) ) { + return new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'iter', options.iter ) ); + } + if ( options.iter > 64 ) { + return new RangeError( format( 'invalid option. `%s` option must be less than or equal to 64. Option: `%u`.', 'iter', options.iter ) ); + } + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/iter/sequences/tribonacci/package.json b/iter/sequences/tribonacci/package.json new file mode 100644 index 000000000..153503620 --- /dev/null +++ b/iter/sequences/tribonacci/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/math/iter/sequences/tribonacci", + "version": "0.0.0", + "description": "Create an iterator which generates a Tribonacci sequence.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "tribonacci", + "tribo", + "sequence", + "iterator", + "iterate", + "iteration", + "iter" + ] + } + \ No newline at end of file diff --git a/iter/sequences/tribonacci/test/test.js b/iter/sequences/tribonacci/test/test.js new file mode 100644 index 000000000..530258d44 --- /dev/null +++ b/iter/sequences/tribonacci/test/test.js @@ -0,0 +1,255 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var tribonacci = require( './../../../../base/special/tribonacci' ); +var iterTribonacciSeq = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterTribonacciSeq, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterTribonacciSeq( value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid option', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 3.14159265, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + iterTribonacciSeq({ + 'iter': value + }); + }; + } +}); + +tape( 'the function returns an iterator protocol-compliant object which generates a Tribonacci sequence', function test( t ) { + var expected; + var actual; + var it; + var i; + + it = iterTribonacciSeq(); + t.equal( it.next.length, 0, 'has zero arity' ); + + for ( i = 0; i < 64; i++ ) { + actual = it.next(); + expected = tribonacci( i ); + t.equal( actual.value, expected, 'returns expected value' ); + t.equal( actual.done, false, 'returns expected value' ); + } + actual = it.next(); + t.equal( actual.value, void 0, 'returns expected value' ); + t.equal( actual.done, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports limiting the number of iterations', function test( t ) { + var expected; + var actual; + var opts; + var it; + var i; + + expected = [ + { + 'value': 0, + 'done': false + }, + { + 'value': 0, + 'done': false + }, + { + 'value': 1, + 'done': false + }, + { + 'value': 1, + 'done': false + }, + { + 'value': 2, + 'done': false + }, + { + 'done': true + } + ]; + + opts = { + 'iter': 5 + }; + it = iterTribonacciSeq( opts ); + t.equal( it.next.length, 0, 'has zero arity' ); + + actual = []; + for ( i = 0; i < opts.iter; i++ ) { + actual.push( it.next() ); + } + actual.push( it.next() ); + + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var it; + var r; + + it = iterTribonacciSeq(); + + r = it.next(); + t.equal( typeof r.value, 'number', 'returns a number' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'number', 'returns a number' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var it; + var r; + + it = iterTribonacciSeq(); + + r = it.next(); + t.equal( typeof r.value, 'number', 'returns a number' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.next(); + t.equal( typeof r.value, 'number', 'returns a number' ); + t.equal( r.done, false, 'returns expected value' ); + + r = it.return( 'finished' ); + t.equal( r.value, 'finished', 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + r = it.next(); + t.equal( r.value, void 0, 'returns expected value' ); + t.equal( r.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the returned iterator is iterable', function test( t ) { + var iterTribonacciSeq; + var it1; + var it2; + var i; + + iterTribonacciSeq = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + it1 = iterTribonacciSeq(); + t.equal( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.equal( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.equal( typeof it2, 'object', 'returns an object' ); + t.equal( typeof it2.next, 'function', 'has method' ); + t.equal( typeof it2.return, 'function', 'has method' ); + + for ( i = 0; i < 10; i++ ) { + t.equal( it2.next().value, it1.next().value, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the returned iterator is not "iterable"', function test( t ) { + var iterTribonacciSeq; + var it; + + iterTribonacciSeq = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + it = iterTribonacciSeq(); + t.equal( it[ iteratorSymbol ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/iter/sequences/tribonacci/test/test.validate.js b/iter/sequences/tribonacci/test/test.validate.js new file mode 100644 index 000000000..0f8d0c3a1 --- /dev/null +++ b/iter/sequences/tribonacci/test/test.validate.js @@ -0,0 +1,147 @@ +/** +* @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 validate = require( './../lib/validate.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof validate, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { + var values; + var err; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + void 0, + null, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + err = validate( {}, values[ i ] ); + t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns an error if provided an `iter` option which is not a nonnegative integer', function test( t ) { + var values; + var opts; + var err; + var i; + + values = [ + '5', + -1, + 3.14159265, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + opts = { + 'iter': values[ i ] + }; + err = validate( {}, opts ); + t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns an error if provided an `iter` option which is greater than `64`', function test( t ) { + var values; + var opts; + var err; + var i; + + values = [ + 65, + 69, + 70, + 82, + 83, + 84 + ]; + + for ( i = 0; i < values.length; i++ ) { + opts = { + 'iter': values[ i ] + }; + err = validate( {}, opts ); + t.strictEqual( err instanceof RangeError, true, 'returns a type error when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `null` if all options are valid', function test( t ) { + var options; + var opts; + var err; + + opts = {}; + options = { + 'iter': 50 + }; + + err = validate( opts, options ); + t.strictEqual( err, null, 'returns null' ); + t.deepEqual( opts, options, 'sets options' ); + + t.end(); +}); + +tape( 'the function will ignore unrecognized options', function test( t ) { + var options; + var opts; + var err; + + opts = {}; + options = { + 'beep': true, + 'ping': 'pong' + }; + + err = validate( opts, options ); + t.strictEqual( err, null, 'returns null' ); + t.deepEqual( opts, {}, 'ignores unrecognized options' ); + + t.end(); +});