|
| 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 26 | +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); |
| 27 | +var PINF = require( '@stdlib/constants/float64/pinf' ); |
| 28 | +var NINF = require( '@stdlib/constants/float64/ninf' ); |
| 29 | +var EPS = require( '@stdlib/constants/float64/eps' ); |
| 30 | +var randu = require( '@stdlib/random/base/randu' ); |
| 31 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 32 | + |
| 33 | + |
| 34 | +// VARIABLES // |
| 35 | + |
| 36 | +var heaviside = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 37 | +var opts = { |
| 38 | + 'skip': ( heaviside 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 heaviside, 'function', 'main export is a function' ); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +tape( 'the function returns `0` if `x` is negative', opts, function test( t ) { |
| 51 | + var x; |
| 52 | + var v; |
| 53 | + var i; |
| 54 | + |
| 55 | + for ( i = 0; i < 1e3; i++ ) { |
| 56 | + x = -( randu()*100.0 ) - EPS; |
| 57 | + v = heaviside( x, 'left-continuous' ); |
| 58 | + t.strictEqual( isPositiveZero( v ), true, 'returns expected value when provided '+x ); |
| 59 | + } |
| 60 | + t.end(); |
| 61 | +}); |
| 62 | + |
| 63 | +tape( 'the function returns `1` if `x` is positive', opts, function test( t ) { |
| 64 | + var x; |
| 65 | + var v; |
| 66 | + var i; |
| 67 | + |
| 68 | + for ( i = 0; i < 1e3; i++ ) { |
| 69 | + x = ( randu()*100.0 ) + EPS; |
| 70 | + v = heaviside( x, 'left-continuous' ); |
| 71 | + t.strictEqual( v, 1.0, 'returns expected value when provided '+x ); |
| 72 | + } |
| 73 | + t.end(); |
| 74 | +}); |
| 75 | + |
| 76 | +tape( 'if the `continuity` option is `discontinuous`, the function returns `NaN` if provided `+-0`', opts, function test( t ) { |
| 77 | + var v; |
| 78 | + |
| 79 | + v = heaviside( -0.0, 'discontinuous' ); |
| 80 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 81 | + |
| 82 | + v = heaviside( +0.0, 'discontinuous' ); |
| 83 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 84 | + |
| 85 | + t.end(); |
| 86 | +}); |
| 87 | + |
| 88 | +tape( 'if the `continuity` option is `half-maximum`, the function returns `0.5` if provided `+-0`', opts, function test( t ) { |
| 89 | + var v; |
| 90 | + |
| 91 | + v = heaviside( -0.0, 'half-maximum' ); |
| 92 | + t.strictEqual( v, 0.5, 'returns expected value' ); |
| 93 | + |
| 94 | + v = heaviside( +0.0, 'half-maximum' ); |
| 95 | + t.strictEqual( v, 0.5, 'returns expected value' ); |
| 96 | + |
| 97 | + t.end(); |
| 98 | +}); |
| 99 | + |
| 100 | +tape( 'if the `continuity` option is `left-continuous`, the function returns `0.0` if provided `+-0`', opts, function test( t ) { |
| 101 | + var v; |
| 102 | + |
| 103 | + v = heaviside( -0.0, 'left-continuous' ); |
| 104 | + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); |
| 105 | + |
| 106 | + v = heaviside( +0.0, 'left-continuous' ); |
| 107 | + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); |
| 108 | + |
| 109 | + t.end(); |
| 110 | +}); |
| 111 | + |
| 112 | +tape( 'if the `continuity` option is `right-continuous`, the function returns `1` if provided `+-0`', opts, function test( t ) { |
| 113 | + var v; |
| 114 | + |
| 115 | + v = heaviside( -0.0, 'right-continuous' ); |
| 116 | + t.strictEqual( v, 1, 'returns expected value' ); |
| 117 | + |
| 118 | + v = heaviside( +0.0, 'right-continuous' ); |
| 119 | + t.strictEqual( v, 1, 'returns expected value' ); |
| 120 | + |
| 121 | + t.end(); |
| 122 | +}); |
| 123 | + |
| 124 | +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { |
| 125 | + var v = heaviside( NaN, 'left-continuous' ); |
| 126 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 127 | + t.end(); |
| 128 | +}); |
| 129 | + |
| 130 | +tape( 'the function returns `0` if provided `-infinity`', opts, function test( t ) { |
| 131 | + var v = heaviside( NINF, 'left-continuous' ); |
| 132 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 133 | + t.end(); |
| 134 | +}); |
| 135 | + |
| 136 | +tape( 'the function returns `+1` if provided `+infinity`', opts, function test( t ) { |
| 137 | + var v = heaviside( PINF, 'left-continuous' ); |
| 138 | + t.strictEqual( v, 1.0, 'returns expected value' ); |
| 139 | + t.end(); |
| 140 | +}); |
0 commit comments