|
| 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 resolve = require( 'path' ).resolve; |
| 24 | +var tape = require( 'tape' ); |
| 25 | +var randu = require( '@stdlib/random/base/randu' ); |
| 26 | +var round = require( '@stdlib/math/base/special/round' ); |
| 27 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 28 | +var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); |
| 29 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 30 | + |
| 31 | + |
| 32 | +// VARIABLES // |
| 33 | + |
| 34 | +var signbitf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 35 | +var opts = { |
| 36 | + 'skip': ( signbitf instanceof Error ) |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +// TESTS // |
| 41 | + |
| 42 | +tape( 'main export is a function', opts, function test( t ) { |
| 43 | + t.ok( true, __filename ); |
| 44 | + t.equal( typeof signbitf, 'function', 'main export is a function' ); |
| 45 | + t.end(); |
| 46 | +}); |
| 47 | + |
| 48 | +tape( 'the function returns a boolean', opts, function test( t ) { |
| 49 | + t.equal( typeof signbitf(5.0), 'boolean', 'returns a boolean' ); |
| 50 | + t.end(); |
| 51 | +}); |
| 52 | + |
| 53 | +tape( 'the function returns a boolean indicating if a sign bit is on (true) or off (false)', opts, function test( t ) { |
| 54 | + var bool; |
| 55 | + var sign; |
| 56 | + var frac; |
| 57 | + var exp; |
| 58 | + var x; |
| 59 | + var i; |
| 60 | + |
| 61 | + for ( i = 0; i < 5000; i++ ) { |
| 62 | + if ( randu() < 0.5 ) { |
| 63 | + sign = -1.0; |
| 64 | + } else { |
| 65 | + sign = 1.0; |
| 66 | + } |
| 67 | + frac = randu() * 10.0; |
| 68 | + exp = round( randu()*44.0 ) - 22; |
| 69 | + x = sign * frac * pow( 10.0, exp ); |
| 70 | + x = toFloat32( x ); |
| 71 | + bool = signbitf( x ); |
| 72 | + if ( sign < 0.0 ) { |
| 73 | + t.equal( bool, true, 'returns true for ' + x ); |
| 74 | + } else { |
| 75 | + t.equal( bool, false, 'returns false for ' + x ); |
| 76 | + } |
| 77 | + } |
| 78 | + t.end(); |
| 79 | +}); |
0 commit comments