|
| 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 tryRequire = require( '@stdlib/utils/try-require' ); |
| 26 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 27 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 28 | +var PINF = require( '@stdlib/constants/float64/pinf' ); |
| 29 | +var NINF = require( '@stdlib/constants/float64/ninf' ); |
| 30 | +var EPS = require( '@stdlib/constants/float64/eps' ); |
| 31 | + |
| 32 | + |
| 33 | +// VARIABLES // |
| 34 | + |
| 35 | +var mode = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 36 | +var opts = { |
| 37 | + 'skip': ( mode instanceof Error ) |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +// FIXTURES // |
| 42 | + |
| 43 | +var data = require( './fixtures/julia/data.json' ); |
| 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 mode, 'function', 'main export is a function' ); |
| 51 | + t.end(); |
| 52 | +}); |
| 53 | + |
| 54 | +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) { |
| 55 | + var v = mode( NaN, 0.5 ); |
| 56 | + t.equal( isnan( v ), true, 'returns NaN' ); |
| 57 | + |
| 58 | + v = mode( 10.0, NaN ); |
| 59 | + t.equal( isnan( v ), true, 'returns NaN' ); |
| 60 | + |
| 61 | + t.end(); |
| 62 | +}); |
| 63 | + |
| 64 | +tape( 'if provided `a >= b`, the function returns `NaN`', function test( t ) { |
| 65 | + var y; |
| 66 | + |
| 67 | + y = mode( 3.0, 2.0 ); |
| 68 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 69 | + |
| 70 | + y = mode( 2.0, 2.0 ); |
| 71 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 72 | + |
| 73 | + y = mode( NINF, NINF ); |
| 74 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 75 | + |
| 76 | + y = mode( PINF, NINF ); |
| 77 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 78 | + |
| 79 | + t.end(); |
| 80 | +}); |
| 81 | + |
| 82 | +tape( 'the function returns the mode of an arcsine distribution', function test( t ) { |
| 83 | + var expected; |
| 84 | + var delta; |
| 85 | + var tol; |
| 86 | + var a; |
| 87 | + var b; |
| 88 | + var i; |
| 89 | + var y; |
| 90 | + |
| 91 | + expected = data.expected; |
| 92 | + a = data.a; |
| 93 | + b = data.b; |
| 94 | + for ( i = 0; i < expected.length; i++ ) { |
| 95 | + y = mode( a[i], b[i] ); |
| 96 | + if ( y === expected[i] ) { |
| 97 | + t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', y: '+y+', expected: '+expected[i] ); |
| 98 | + } else { |
| 99 | + delta = abs( y - expected[ i ] ); |
| 100 | + tol = 1.0 * EPS * abs( expected[ i ] ); |
| 101 | + t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); |
| 102 | + } |
| 103 | + } |
| 104 | + t.end(); |
| 105 | +}); |
0 commit comments