Skip to content

Commit 8abdc55

Browse files
test: add tests to achieve 100% code coverage
PR-URL: #5636 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 276f077 commit 8abdc55

File tree

6 files changed

+90
-29
lines changed

6 files changed

+90
-29
lines changed

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/fixtures/julia/data.json

-1
This file was deleted.

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/fixtures/julia/data1.json

+1
Large diffs are not rendered by default.

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/fixtures/julia/data2.json

+1
Large diffs are not rendered by default.

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/fixtures/julia/runner.jl

100644100755
+6-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ dir = dirname( file );
7474
# Generate fixtures:
7575
a = rand( 500 ) .* 10.0;
7676
b = ( rand( 500 ) .* 10.0 ) .+ a;
77-
c = a .+ ( b .- a ) .* rand();
78-
gen( a, b, c, "data.json" );
77+
# Case: c < (a + b) / 2
78+
c1 = a .+ ( b .- a ) .* ( 0.5 .* rand( 500 ) );
79+
gen( a, b, c1, "data1.json" );
80+
# Case: c >= (a + b) / 2
81+
c2 = ( a .+ b ) ./ 2 .+ ( b .- a ) .* ( 0.5 .* rand( 500 ) );
82+
gen( a, b, c2, "data2.json" );
7983

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/test.js

+41-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var median = require( './../lib' );
2929

3030
// FIXTURES //
3131

32-
var data = require( './fixtures/julia/data.json' );
32+
var data1 = require( './fixtures/julia/data1.json' );
33+
var data2 = require( './fixtures/julia/data2.json' );
3334

3435

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

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

4748
v = median( 0.0, NaN, 0.5 );
48-
t.equal( isnan( v ), true, 'returns NaN' );
49+
t.equal( isnan( v ), true, 'returns expected value' );
4950

5051
v = median( 0.0, 10.0, NaN );
51-
t.equal( isnan( v ), true, 'returns NaN' );
52+
t.equal( isnan( v ), true, 'returns expected value' );
5253

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

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

6263
y = median( 3.0, 2.0, 2.5 );
63-
t.equal( isnan( y ), true, 'returns NaN' );
64+
t.equal( isnan( y ), true, 'returns expected value' );
6465

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

6869
y = median( 0.0, 1.0, 2.0 );
69-
t.equal( isnan( y ), true, 'returns NaN' );
70+
t.equal( isnan( y ), true, 'returns expected value' );
7071

7172
t.end();
7273
});
7374

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

84-
expected = data.expected;
85-
a = data.a;
86-
b = data.b;
87-
c = data.c;
85+
expected = data1.expected;
86+
a = data1.a;
87+
b = data1.b;
88+
c = data1.c;
89+
for ( i = 0; i < expected.length; i++ ) {
90+
y = median( a[i], b[i], c[i] );
91+
if ( y === expected[i] ) {
92+
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', c: '+c[i]+', y: '+y+', expected: '+expected[i] );
93+
} else {
94+
delta = abs( y - expected[ i ] );
95+
tol = 1.0 * EPS * abs( expected[ i ] );
96+
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
97+
}
98+
}
99+
t.end();
100+
});
101+
102+
tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c >= (a + b) / 2`', function test( t ) {
103+
var expected;
104+
var delta;
105+
var tol;
106+
var a;
107+
var b;
108+
var c;
109+
var i;
110+
var y;
111+
112+
expected = data2.expected;
113+
a = data2.a;
114+
b = data2.b;
115+
c = data2.c;
88116
for ( i = 0; i < expected.length; i++ ) {
89117
y = median( a[i], b[i], c[i] );
90118
if ( y === expected[i] ) {

lib/node_modules/@stdlib/stats/base/dists/triangular/median/test/test.native.js

+41-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ var EPS = require( '@stdlib/constants/float64/eps' );
3030

3131
// FIXTURES //
3232

33-
var data = require( './fixtures/julia/data.json' );
33+
var data1 = require( './fixtures/julia/data1.json' );
34+
var data2 = require( './fixtures/julia/data2.json' );
3435

3536

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

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

5657
v = median( 0.0, NaN, 0.5 );
57-
t.equal( isnan( v ), true, 'returns NaN' );
58+
t.equal( isnan( v ), true, 'returns expected value' );
5859

5960
v = median( 0.0, 10.0, NaN );
60-
t.equal( isnan( v ), true, 'returns NaN' );
61+
t.equal( isnan( v ), true, 'returns expected value' );
6162

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

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

7172
y = median( 3.0, 2.0, 2.5 );
72-
t.equal( isnan( y ), true, 'returns NaN' );
73+
t.equal( isnan( y ), true, 'returns expected value' );
7374

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

7778
y = median( 0.0, 1.0, 2.0 );
78-
t.equal( isnan( y ), true, 'returns NaN' );
79+
t.equal( isnan( y ), true, 'returns expected value' );
7980

8081
t.end();
8182
});
8283

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

93-
expected = data.expected;
94-
a = data.a;
95-
b = data.b;
96-
c = data.c;
94+
expected = data1.expected;
95+
a = data1.a;
96+
b = data1.b;
97+
c = data1.c;
98+
for ( i = 0; i < expected.length; i++ ) {
99+
y = median( a[i], b[i], c[i] );
100+
if ( y === expected[i] ) {
101+
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', c: '+c[i]+', y: '+y+', expected: '+expected[i] );
102+
} else {
103+
delta = abs( y - expected[ i ] );
104+
tol = 1.0 * EPS * abs( expected[ i ] );
105+
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
106+
}
107+
}
108+
t.end();
109+
});
110+
111+
tape( 'the function returns the median of a triangular distribution if provided parameters satisfy `c >= (a + b) / 2`', opts, function test( t ) {
112+
var expected;
113+
var delta;
114+
var tol;
115+
var a;
116+
var b;
117+
var c;
118+
var i;
119+
var y;
120+
121+
expected = data2.expected;
122+
a = data2.a;
123+
b = data2.b;
124+
c = data2.c;
97125
for ( i = 0; i < expected.length; i++ ) {
98126
y = median( a[i], b[i], c[i] );
99127
if ( y === expected[i] ) {

0 commit comments

Comments
 (0)