Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
313 changes: 312 additions & 1 deletion lib/node_modules/@stdlib/random/tools/unary/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );


Expand All @@ -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();
});