|
| 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 randu = require( '@stdlib/random/base/randu' ); |
| 26 | +var roundf = require( '@stdlib/math/base/special/roundf' ); |
| 27 | +var PINF = require( '@stdlib/constants/float32/pinf' ); |
| 28 | +var NINF = require( '@stdlib/constants/float32/ninf' ); |
| 29 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 30 | + |
| 31 | + |
| 32 | +// VARIABLES // |
| 33 | + |
| 34 | +var isOddf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 35 | +var opts = { |
| 36 | + 'skip': ( isOddf 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.strictEqual( typeof isOddf, 'function', 'main export is a function' ); |
| 45 | + t.end(); |
| 46 | +}); |
| 47 | + |
| 48 | +tape( 'the function returns `false` if provided an even number', opts, function test( t ) { |
| 49 | + var bool; |
| 50 | + var x; |
| 51 | + var i; |
| 52 | + for ( i = 0; i < 1000; i++ ) { |
| 53 | + x = roundf( randu() * 1.0e6 ) - 5.0e5; |
| 54 | + x *= 2; // always even |
| 55 | + bool = isOddf( x ); |
| 56 | + t.equal( bool, false, 'returns false when provided '+x ); |
| 57 | + } |
| 58 | + t.end(); |
| 59 | +}); |
| 60 | + |
| 61 | +tape( 'the function returns `true` if provided an odd number', opts, function test( t ) { |
| 62 | + var bool; |
| 63 | + var x; |
| 64 | + var i; |
| 65 | + for ( i = 0; i < 1000; i++ ) { |
| 66 | + x = roundf( randu() * 1.0e6 ) - 5.0e5; |
| 67 | + if ( x%2 === 0 ) { |
| 68 | + x += 1; |
| 69 | + } |
| 70 | + bool = isOddf( x ); |
| 71 | + t.equal( bool, true, 'returns true when provided '+x ); |
| 72 | + } |
| 73 | + t.end(); |
| 74 | +}); |
| 75 | + |
| 76 | +tape( 'the function returns `false` if provided `+-0`', opts, function test( t ) { |
| 77 | + t.equal( isOddf( +0.0 ), false, 'returns false' ); |
| 78 | + t.equal( isOddf( -0.0 ), false, 'returns false' ); |
| 79 | + t.end(); |
| 80 | +}); |
| 81 | + |
| 82 | +tape( 'WARNING: the function returns `true` if provided `+infinity`', opts, function test( t ) { |
| 83 | + t.equal( isOddf( PINF ), true, 'returns true' ); |
| 84 | + t.end(); |
| 85 | +}); |
| 86 | + |
| 87 | +tape( 'WARNING: the function returns `true` if provided `-infinity`', opts, function test( t ) { |
| 88 | + t.equal( isOddf( NINF ), true, 'returns true' ); |
| 89 | + t.end(); |
| 90 | +}); |
| 91 | + |
| 92 | +tape( 'the function returns `false` if provided `NaN`', opts, function test( t ) { |
| 93 | + t.equal( isOddf( NaN ), false, 'returns false' ); |
| 94 | + t.equal( isOddf( 0.0/0.0 ), false, 'returns false' ); |
| 95 | + t.end(); |
| 96 | +}); |
0 commit comments