From 12ca9ae0ccf018a9d42c63cf77a52c132c719822 Mon Sep 17 00:00:00 2001 From: kaushal-kumar-it Date: Wed, 31 Dec 2025 10:33:43 +0530 Subject: [PATCH] test: add tests for random/tools/unary --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/random/tools/unary/test/test.js | 313 +++++++++++++++++- 1 file changed, 312 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/random/tools/unary/test/test.js b/lib/node_modules/@stdlib/random/tools/unary/test/test.js index d13b7092da9e..7ec399ecf654 100644 --- a/lib/node_modules/@stdlib/random/tools/unary/test/test.js +++ b/lib/node_modules/@stdlib/random/tools/unary/test/test.js @@ -21,6 +21,8 @@ // MODULES // var tape = require( 'tape' ); +var exponential = require( '@stdlib/random/base/exponential' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); var Random = require( './../lib' ); @@ -32,4 +34,313 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -// FIXME: add tests +tape( 'the function throws an error if the first argument is not a function', function test( t ) { + var values; + var idt; + var odt; + var i; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + 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() { + return new Random( value, idt, odt, { + 'output': 'real_floating_point_and_generic' + }); + }; + } +}); + +tape( 'the function throws an error if the second argument is not an array of data types', function test( t ) { + var values; + var odt; + var i; + + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + [ 'foo' ], + [ 1, 2, 3 ], + [ 'float64', 1 ], + exponential + ]; + + 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() { + return new Random( exponential, value, odt, { + 'output': 'real_floating_point_and_generic' + }); + }; + } +}); + +tape( 'the function throws an error if the third argument is not an array of data types', function test( t ) { + var values; + var idt; + var i; + + idt = dtypes( 'real_and_generic' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + [ 'foo' ], + [ 1, 2, 3 ], + [ 'float64', 1 ], + exponential + ]; + + 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() { + return new Random( exponential, idt, value, { + 'output': 'real_floating_point_and_generic' + }); + }; + } +}); + +tape( 'the function throws an error if the fourth argument is not an object', function test( t ) { + var values; + var idt; + var odt; + var i; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + exponential + ]; + + 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() { + return new Random( exponential, idt, odt, value ); + }; + } +}); + +tape( 'the function throws an error if the fourth argument does not have a valid output data type policy', function test( t ) { + var values; + var idt; + var odt; + var i; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + 'foo', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + exponential + ]; + + 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() { + return new Random( exponential, idt, odt, { + 'output': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var idt; + var odt; + var i; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + exponential + ]; + + 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() { + return new Random( exponential, idt, odt, { + 'output': 'real_floating_point_and_generic' + }, value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var idt; + var odt; + var i; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + values = [ + 'foo', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + exponential + ]; + + 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() { + return new Random( exponential, idt, odt, { + 'output': 'real_floating_point_and_generic' + }, { + 'order': value + }); + }; + } +}); + +tape( 'the function returns an instance', function test( t ) { + var idt; + var odt; + var r; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + r = new Random( exponential, idt, odt, { + 'output': 'real_floating_point_and_generic' + }); + t.strictEqual( r instanceof Random, true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function returns an instance (w/o new)', function test( t ) { + var idt; + var odt; + var r; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + r = Random( exponential, idt, odt, { // eslint-disable-line new-cap + 'output': 'real_floating_point_and_generic' + }); + t.strictEqual( r instanceof Random, true, 'returns an instance' ); + t.end(); +}); + +tape( 'the instance has a `generate` method', function test( t ) { + var idt; + var odt; + var r; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + r = new Random( exponential, idt, odt, { + 'output': 'real_floating_point_and_generic' + }); + t.strictEqual( typeof r.generate, 'function', 'has a generate method' ); + t.end(); +}); + +tape( 'the instance has an `assign` method', function test( t ) { + var idt; + var odt; + var r; + + idt = dtypes( 'real_and_generic' ); + odt = dtypes( 'real_floating_point_and_generic' ); + + r = new Random( exponential, idt, odt, { + 'output': 'real_floating_point_and_generic' + }); + t.strictEqual( typeof r.assign, 'function', 'has an assign method' ); + t.end(); +});