Skip to content

test: add tests to achieve 100% code coverage #5636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
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

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ dir = dirname( file );
# Generate fixtures:
a = rand( 500 ) .* 10.0;
b = ( rand( 500 ) .* 10.0 ) .+ a;
c = a .+ ( b .- a ) .* rand();
gen( a, b, c, "data.json" );
# Case: c < (a + b) / 2
c1 = a .+ ( b .- a ) .* ( 0.5 .* rand( 500 ) );
gen( a, b, c1, "data1.json" );
# Case: c >= (a + b) / 2
c2 = ( a .+ b ) ./ 2 .+ ( b .- a ) .* ( 0.5 .* rand( 500 ) );
gen( a, b, c2, "data2.json" );

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var median = require( './../lib' );

// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var data1 = require( './fixtures/julia/data1.json' );
var data2 = require( './fixtures/julia/data2.json' );


// TESTS //
Expand All @@ -42,13 +43,13 @@ tape( 'main export is a function', function test( t ) {

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
var v = median( NaN, 1.0, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = median( 0.0, NaN, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = median( 0.0, 10.0, NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand All @@ -57,21 +58,21 @@ tape( 'if provided parameters not satisfying `a <= c <= b`, the function returns
var y;

y = median( -1.0, -1.1, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 3.0, 2.0, 2.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 0.0, 1.0, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 0.0, 1.0, 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});

tape( 'the function returns the median of a triangular distribution', function test( t ) {
tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c < (a + b) / 2`', function test( t ) {
var expected;
var delta;
var tol;
Expand All @@ -81,10 +82,37 @@ tape( 'the function returns the median of a triangular distribution', function t
var i;
var y;

expected = data.expected;
a = data.a;
b = data.b;
c = data.c;
expected = data1.expected;
a = data1.a;
b = data1.b;
c = data1.c;
for ( i = 0; i < expected.length; i++ ) {
y = median( a[i], b[i], c[i] );
if ( y === expected[i] ) {
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', c: '+c[i]+', y: '+y+', expected: '+expected[i] );
} else {
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
}
t.end();
});

tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c >= (a + b) / 2`', function test( t ) {
var expected;
var delta;
var tol;
var a;
var b;
var c;
var i;
var y;

expected = data2.expected;
a = data2.a;
b = data2.b;
c = data2.c;
for ( i = 0; i < expected.length; i++ ) {
y = median( a[i], b[i], c[i] );
if ( y === expected[i] ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var EPS = require( '@stdlib/constants/float64/eps' );

// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var data1 = require( './fixtures/julia/data1.json' );
var data2 = require( './fixtures/julia/data2.json' );


// VARIABLES //
Expand All @@ -51,13 +52,13 @@ tape( 'main export is a function', opts, function test( t ) {

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
var v = median( NaN, 1.0, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = median( 0.0, NaN, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = median( 0.0, 10.0, NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand All @@ -66,21 +67,21 @@ tape( 'if provided parameters not satisfying `a <= c <= b`, the function returns
var y;

y = median( -1.0, -1.1, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 3.0, 2.0, 2.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 0.0, 1.0, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = median( 0.0, 1.0, 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});

tape( 'the function returns the median of a triangular distribution', opts, function test( t ) {
tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c < (a + b) / 2`', opts, function test( t ) {
var expected;
var delta;
var tol;
Expand All @@ -90,10 +91,37 @@ tape( 'the function returns the median of a triangular distribution', opts, func
var i;
var y;

expected = data.expected;
a = data.a;
b = data.b;
c = data.c;
expected = data1.expected;
a = data1.a;
b = data1.b;
c = data1.c;
for ( i = 0; i < expected.length; i++ ) {
y = median( a[i], b[i], c[i] );
if ( y === expected[i] ) {
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', c: '+c[i]+', y: '+y+', expected: '+expected[i] );
} else {
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
}
t.end();
});

tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c >= (a + b) / 2`', opts, function test( t ) {
var expected;
var delta;
var tol;
var a;
var b;
var c;
var i;
var y;

expected = data2.expected;
a = data2.a;
b = data2.b;
c = data2.c;
for ( i = 0; i < expected.length; i++ ) {
y = median( a[i], b[i], c[i] );
if ( y === expected[i] ) {
Expand Down