|
| 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 isNumber = require( '@stdlib/assert/is-number' ); |
| 27 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 28 | +var randu = require( '@stdlib/random/base/randu' ); |
| 29 | +var PINF = require( '@stdlib/constants/float64/pinf' ); |
| 30 | +var NINF = require( '@stdlib/constants/float64/ninf' ); |
| 31 | +var EPS = require( '@stdlib/constants/float64/eps' ); |
| 32 | + |
| 33 | + |
| 34 | +// VARIABLES // |
| 35 | + |
| 36 | +var mgf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 37 | +var opts = { |
| 38 | + 'skip': ( mgf instanceof Error ) |
| 39 | +}; |
| 40 | + |
| 41 | + |
| 42 | +// TESTS // |
| 43 | + |
| 44 | +tape( 'main export is a function', opts, function test( t ) { |
| 45 | + t.ok( true, __filename ); |
| 46 | + t.strictEqual( typeof mgf, 'function', 'main export is a function' ); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) { |
| 51 | + var y = mgf( NaN, 0.0, 1.0 ); |
| 52 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 53 | + y = mgf( 0.0, NaN, 1.0 ); |
| 54 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 55 | + y = mgf( 0.0, 1.0, NaN ); |
| 56 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 57 | + t.end(); |
| 58 | +}); |
| 59 | + |
| 60 | +tape( 'if provided a nonpositive `k`, the function returns `NaN`', opts, function test( t ) { |
| 61 | + var y; |
| 62 | + |
| 63 | + y = mgf( 2.0, 0.0, 2.0 ); |
| 64 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 65 | + |
| 66 | + y = mgf( 2.0, -1.0, 2.0 ); |
| 67 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 68 | + |
| 69 | + y = mgf( 0.0, -1.0, 2.0 ); |
| 70 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 71 | + |
| 72 | + y = mgf( 2.0, NINF, 1.0 ); |
| 73 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 74 | + |
| 75 | + y = mgf( 2.0, NINF, PINF ); |
| 76 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 77 | + |
| 78 | + y = mgf( 2.0, NINF, NINF ); |
| 79 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 80 | + |
| 81 | + y = mgf( 2.0, NINF, NaN ); |
| 82 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 83 | + |
| 84 | + t.end(); |
| 85 | +}); |
| 86 | + |
| 87 | +tape( 'if provided a nonpositive `k`, the function returns `NaN`', opts, function test( t ) { |
| 88 | + var y; |
| 89 | + |
| 90 | + y = mgf( 2.0, 2.0, 0.0 ); |
| 91 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 92 | + |
| 93 | + y = mgf( 2.0, 2.0, -1.0 ); |
| 94 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 95 | + |
| 96 | + y = mgf( 0.0, 2.0, -1/0 ); |
| 97 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 98 | + |
| 99 | + y = mgf( 2.0, 1.0, NINF ); |
| 100 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 101 | + |
| 102 | + y = mgf( 2.0, PINF, NINF ); |
| 103 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 104 | + |
| 105 | + y = mgf( 2.0, NINF, NINF ); |
| 106 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 107 | + |
| 108 | + y = mgf( 2.0, NaN, NINF ); |
| 109 | + t.equal( isnan( y ), true, 'returns NaN' ); |
| 110 | + |
| 111 | + t.end(); |
| 112 | +}); |
| 113 | + |
| 114 | +tape( 'the function evaluates the mgf', opts, function test( t ) { |
| 115 | + var lambda; |
| 116 | + var k; |
| 117 | + var i; |
| 118 | + var x; |
| 119 | + var y; |
| 120 | + |
| 121 | + // TODO: Add test fixtures (function is not available in R and Julia) |
| 122 | + for ( i = 0; i < 100; i++ ) { |
| 123 | + x = randu() * 5.0; |
| 124 | + lambda = (randu() * 10.0) + EPS; |
| 125 | + k = (randu() * 10.0) + EPS; |
| 126 | + y = mgf( x, lambda, k ); |
| 127 | + t.strictEqual( !isnan( y ) && isNumber( y ), true, 'returns a number' ); |
| 128 | + } |
| 129 | + t.end(); |
| 130 | +}); |
0 commit comments