Skip to content

Commit 2ce1b69

Browse files
committed
feat: tests added
1 parent 31e9467 commit 2ce1b69

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 tape = require( 'tape' );
24+
var PINF = require( '@stdlib/constants/float32/pinf' );
25+
var NINF = require( '@stdlib/constants/float32/ninf' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var roundf = require( '@stdlib/math/base/special/roundf' );
28+
var isOddf = require( './../lib' );
29+
30+
31+
// TESTS //
32+
33+
tape( 'main export is a function', function test( t ) {
34+
t.ok( true, __filename );
35+
t.strictEqual( typeof isOddf, 'function', 'main export is a function' );
36+
t.end();
37+
});
38+
39+
tape( 'the function returns `false` if provided an even number', function test( t ) {
40+
var bool;
41+
var x;
42+
var i;
43+
for ( i = 0; i < 1000; i++ ) {
44+
x = roundf( randu() * 1.0e6 ) - 5.0e5;
45+
x *= 2; // always even
46+
bool = isOddf( x );
47+
t.equal( bool, false, 'returns false when provided '+x );
48+
}
49+
t.end();
50+
});
51+
52+
tape( 'the function returns `true` if provided an odd number', function test( t ) {
53+
var bool;
54+
var x;
55+
var i;
56+
for ( i = 0; i < 1000; i++ ) {
57+
x = roundf( randu() * 1.0e6 ) - 5.0e5;
58+
if ( x%2 === 0 ) {
59+
x += 1;
60+
}
61+
bool = isOddf( x );
62+
t.equal( bool, true, 'returns true when provided '+x );
63+
}
64+
t.end();
65+
});
66+
67+
tape( 'the function returns `false` if provided `+-0`', function test( t ) {
68+
t.equal( isOddf( +0.0 ), false, 'returns false' );
69+
t.equal( isOddf( -0.0 ), false, 'returns false' );
70+
t.end();
71+
});
72+
73+
tape( 'WARNING: the function returns `true` if provided `+infinity`', function test( t ) {
74+
t.equal( isOddf( PINF ), true, 'returns true' );
75+
t.end();
76+
});
77+
78+
tape( 'WARNING: the function returns `true` if provided `-infinity`', function test( t ) {
79+
t.equal( isOddf( NINF ), true, 'returns true' );
80+
t.end();
81+
});
82+
83+
tape( 'the function returns `false` if provided `NaN`', function test( t ) {
84+
t.equal( isOddf( NaN ), false, 'returns false' );
85+
t.equal( isOddf( 0.0/0.0 ), false, 'returns false' );
86+
t.end();
87+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)