Skip to content

Commit a10caf2

Browse files
committed
feat: tests added
1 parent 644c1fc commit a10caf2

File tree

6 files changed

+317
-0
lines changed

6 files changed

+317
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 1.5
2+
JSON 0.21

lib/node_modules/@stdlib/math/base/special/ahaversinf/test/fixtures/julia/data.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env julia
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2018 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import JSON
20+
21+
"""
22+
gen( domain, name )
23+
24+
Generate fixture data and write to file.
25+
26+
# Arguments
27+
28+
* `domain`: domain
29+
* `name::AbstractString`: output filename
30+
31+
# Examples
32+
33+
``` julia
34+
julia> x = range( 0.0, stop = 1.0, length = 2001 );
35+
julia> gen( x, \"data.json\" );
36+
```
37+
"""
38+
function gen( domain, name )
39+
x = collect( domain );
40+
y = Float32.( 2.0 ) .* asin.( sqrt.( Float32.( x ) ) );
41+
42+
# Store data to be written to file as a collection:
43+
data = Dict([
44+
("x", x),
45+
("expected", y)
46+
]);
47+
48+
# Based on the script directory, create an output filepath:
49+
filepath = joinpath( dir, name );
50+
51+
# Write the data to the output filepath as JSON:
52+
outfile = open( filepath, "w" );
53+
write( outfile, JSON.json(data) );
54+
write( outfile, "\n" );
55+
close( outfile );
56+
end
57+
58+
# Get the filename:
59+
file = @__FILE__;
60+
61+
# Extract the directory in which this file resides:
62+
dir = dirname( file );
63+
64+
# Generate fixture data for decimal values:
65+
x = range( 0.0, stop = 1.0, length = 2003 );
66+
gen( x, "data.json" );
67+
68+
# Generate fixture data for small positive values:
69+
x = range( 1e-200, stop = 1e-208, length = 2003 );
70+
gen( x, "small_positive.json" );

lib/node_modules/@stdlib/math/base/special/ahaversinf/test/fixtures/julia/small_positive.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var absf = require( '@stdlib/math/base/special/absf' );
27+
var EPS = require( '@stdlib/constants/float32/eps' );
28+
var ahaversinf = require( './../lib' );
29+
30+
31+
// FIXTURES //
32+
33+
var data = require( './fixtures/julia/data.json' );
34+
var smallPositive = require( './fixtures/julia/small_positive.json' );
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof ahaversinf, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'the function computes the inverse half-value versed sine', function test( t ) {
46+
var expected;
47+
var delta;
48+
var tol;
49+
var x;
50+
var y;
51+
var i;
52+
53+
x = data.x;
54+
expected = data.expected;
55+
56+
for ( i = 0; i < x.length; i++ ) {
57+
y = ahaversinf( x[i] );
58+
if ( y === expected[ i ] ) {
59+
t.strictEqual( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
60+
} else {
61+
delta = absf( y - expected[i] );
62+
tol = 3 * EPS * absf( expected[i] );
63+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
64+
}
65+
}
66+
t.end();
67+
});
68+
69+
tape( 'the function computes the inverse half-value versed sine (small positive numbers)', function test( t ) {
70+
var expected;
71+
var delta;
72+
var tol;
73+
var x;
74+
var y;
75+
var i;
76+
77+
x = smallPositive.x;
78+
expected = smallPositive.expected;
79+
80+
for ( i = 0; i < x.length; i++ ) {
81+
y = ahaversinf( x[i] );
82+
if ( y === expected[ i ] ) {
83+
t.strictEqual( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
84+
} else {
85+
delta = absf( y - expected[i] );
86+
tol = 3 * EPS * absf( expected[i] );
87+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
88+
}
89+
}
90+
t.end();
91+
});
92+
93+
tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
94+
var v = ahaversinf( NaN );
95+
t.strictEqual( isnanf( v ), true, 'returns NaN' );
96+
t.end();
97+
});
98+
99+
tape( 'the function returns `NaN` if provided a value less than `0`', function test( t ) {
100+
var v;
101+
var i;
102+
for ( i = 0; i < 1e4; i++ ) {
103+
v = -( randu() * 1.0e6 ) - EPS;
104+
t.strictEqual( isnanf( ahaversinf( v ) ), true, 'returns NaN when provided '+v );
105+
}
106+
t.end();
107+
});
108+
109+
tape( 'the function returns `NaN` if provided a value greater than `1`', function test( t ) {
110+
var v;
111+
var i;
112+
for ( i = 0; i < 1e4; i++ ) {
113+
v = ( randu() * 1.0e6 ) + 1.0 + EPS;
114+
t.strictEqual( isnanf( ahaversinf( v ) ), true, 'returns NaN when provided '+v );
115+
}
116+
t.end();
117+
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var tape = require( 'tape' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var absf = require( '@stdlib/math/base/special/absf' );
28+
var EPS = require( '@stdlib/constants/float32/eps' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
31+
32+
// FIXTURES //
33+
34+
var data = require( './fixtures/julia/data.json' );
35+
var smallPositive = require( './fixtures/julia/small_positive.json' );
36+
37+
38+
// VARIABLES //
39+
40+
var ahaversinf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
41+
var opts = {
42+
'skip': ( ahaversinf instanceof Error )
43+
};
44+
45+
46+
// TESTS //
47+
48+
tape( 'main export is a function', opts, function test( t ) {
49+
t.ok( true, __filename );
50+
t.strictEqual( typeof ahaversinf, 'function', 'main export is a function' );
51+
t.end();
52+
});
53+
54+
tape( 'the function computes the inverse half-value versed sine', opts, function test( t ) {
55+
var expected;
56+
var delta;
57+
var tol;
58+
var x;
59+
var y;
60+
var i;
61+
62+
x = data.x;
63+
expected = data.expected;
64+
65+
for ( i = 0; i < x.length; i++ ) {
66+
y = ahaversinf( x[i] );
67+
if ( y === expected[ i ] ) {
68+
t.strictEqual( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
69+
} else {
70+
delta = absf( y - expected[i] );
71+
tol = 3.0 * EPS * absf( expected[i] );
72+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
73+
}
74+
}
75+
t.end();
76+
});
77+
78+
tape( 'the function computes the inverse half-value versed sine (small positive numbers)', opts, function test( t ) {
79+
var expected;
80+
var delta;
81+
var tol;
82+
var x;
83+
var y;
84+
var i;
85+
86+
x = smallPositive.x;
87+
expected = smallPositive.expected;
88+
89+
for ( i = 0; i < x.length; i++ ) {
90+
y = ahaversinf( x[i] );
91+
if ( y === expected[ i ] ) {
92+
t.strictEqual( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
93+
} else {
94+
delta = absf( y - expected[i] );
95+
tol = 3.0 * EPS * absf( expected[i] );
96+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
97+
}
98+
}
99+
t.end();
100+
});
101+
102+
tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
103+
var v = ahaversinf( NaN );
104+
t.strictEqual( isnanf( v ), true, 'returns NaN' );
105+
t.end();
106+
});
107+
108+
tape( 'the function returns `NaN` if provided a value less than `0`', opts, function test( t ) {
109+
var v;
110+
var i;
111+
for ( i = 0; i < 1e4; i++ ) {
112+
v = -( randu() * 1.0e6 ) - EPS;
113+
t.strictEqual( isnanf( ahaversinf( v ) ), true, 'returns NaN when provided '+v );
114+
}
115+
t.end();
116+
});
117+
118+
tape( 'the function returns `NaN` if provided a value greater than `1`', opts, function test( t ) {
119+
var v;
120+
var i;
121+
for ( i = 0; i < 1e4; i++ ) {
122+
v = ( randu() * 1.0e6 ) + 1.0 + EPS;
123+
t.strictEqual( isnanf( ahaversinf( v ) ), true, 'returns NaN when provided '+v );
124+
}
125+
t.end();
126+
});

0 commit comments

Comments
 (0)