Skip to content

Commit 861cd7f

Browse files
refactor: update math/base/assert/is-even to follow latest project conventions
PR-URL: #4183 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 5c1c0fd commit 861cd7f

File tree

5 files changed

+67
-67
lines changed

5 files changed

+67
-67
lines changed

Diff for: lib/node_modules/@stdlib/math/base/assert/is-even/benchmark/benchmark.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
25-
var round = require( '@stdlib/math/base/special/round' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2625
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2726
var pkg = require( './../package.json' ).name;
2827
var isEven = require( './../lib' );
@@ -31,14 +30,17 @@ var isEven = require( './../lib' );
3130
// MAIN //
3231

3332
bench( pkg, function benchmark( b ) {
33+
var len;
3434
var x;
3535
var y;
3636
var i;
3737

38+
len = 100;
39+
x = discreteUniform( len, 0, 1000 );
40+
3841
b.tic();
3942
for ( i = 0; i < b.iterations; i++ ) {
40-
x = round( (randu()*1.0e7) - 5.0e6 );
41-
y = isEven( x );
43+
y = isEven( x[ i % len ] );
4244
if ( typeof y !== 'boolean' ) {
4345
b.fail( 'should return a boolean' );
4446
}

Diff for: lib/node_modules/@stdlib/math/base/assert/is-even/benchmark/benchmark.native.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var round = require( '@stdlib/math/base/special/round' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2827
var tryRequire = require( '@stdlib/utils/try-require' );
2928
var pkg = require( './../package.json' ).name;
@@ -40,14 +39,17 @@ var opts = {
4039
// MAIN //
4140

4241
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var len;
4343
var x;
4444
var y;
4545
var i;
4646

47+
len = 100;
48+
x = discreteUniform( len, 0, 1000 );
49+
4750
b.tic();
4851
for ( i = 0; i < b.iterations; i++ ) {
49-
x = round( (randu()*1.0e7) - 5.0e6 );
50-
y = isEven( x );
52+
y = isEven( x[ i % len ] );
5153
if ( typeof y !== 'boolean' ) {
5254
b.fail( 'should return a boolean' );
5355
}

Diff for: lib/node_modules/@stdlib/math/base/assert/is-even/benchmark/c/native/benchmark.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ static double rand_double( void ) {
9292
*/
9393
static double benchmark( void ) {
9494
double elapsed;
95-
double x;
95+
double x[ 100 ];
9696
double t;
9797
bool b;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = round( (rand_double()*200.0) - 100.0 );
102+
}
103+
100104
t = tic();
101105
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( rand_double() * 200.0 ) - 100.0;
103-
b = stdlib_base_is_even( x );
106+
b = stdlib_base_is_even( x[ i%100 ] );
104107
if ( b != true && b != false ) {
105108
printf( "should return either true or false\n" );
106109
break;

Diff for: lib/node_modules/@stdlib/math/base/assert/is-even/manifest.json

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build"
4+
},
35
"fields": [
46
{
57
"field": "src",
@@ -24,6 +26,43 @@
2426
],
2527
"confs": [
2628
{
29+
"task": "build",
30+
"src": [
31+
"./src/main.c"
32+
],
33+
"include": [
34+
"./include"
35+
],
36+
"libraries": [
37+
"-lm"
38+
],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/math/base/assert/is-integer",
42+
"@stdlib/napi/export",
43+
"@stdlib/napi/argv",
44+
"@stdlib/napi/argv-double",
45+
"@stdlib/napi/create-int32"
46+
]
47+
},
48+
{
49+
"task": "benchmark",
50+
"src": [
51+
"./src/main.c"
52+
],
53+
"include": [
54+
"./include"
55+
],
56+
"libraries": [
57+
"-lm"
58+
],
59+
"libpath": [],
60+
"dependencies": [
61+
"@stdlib/math/base/assert/is-integer"
62+
]
63+
},
64+
{
65+
"task": "examples",
2766
"src": [
2867
"./src/main.c"
2968
],

Diff for: lib/node_modules/@stdlib/math/base/assert/is-even/src/addon.c

+9-55
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818

1919
#include "stdlib/math/base/assert/is_even.h"
20+
#include "stdlib/napi/argv.h"
21+
#include "stdlib/napi/argv_double.h"
22+
#include "stdlib/napi/create_int32.h"
23+
#include "stdlib/napi/export.h"
2024
#include <node_api.h>
2125
#include <stdint.h>
22-
#include <assert.h>
2326

2427
/**
2528
* Receives JavaScript callback invocation data.
@@ -29,60 +32,11 @@
2932
* @return Node-API value
3033
*/
3134
static napi_value addon( napi_env env, napi_callback_info info ) {
32-
napi_status status;
33-
34-
// Get callback arguments:
35-
size_t argc = 1;
36-
napi_value argv[ 1 ];
37-
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
38-
assert( status == napi_ok );
39-
40-
// Check whether we were provided the correct number of arguments:
41-
if ( argc < 1 ) {
42-
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
43-
assert( status == napi_ok );
44-
return NULL;
45-
}
46-
if ( argc > 1 ) {
47-
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
48-
assert( status == napi_ok );
49-
return NULL;
50-
}
51-
52-
napi_valuetype vtype0;
53-
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54-
assert( status == napi_ok );
55-
if ( vtype0 != napi_number ) {
56-
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
57-
assert( status == napi_ok );
58-
return NULL;
59-
}
60-
61-
double x;
62-
status = napi_get_value_double( env, argv[ 0 ], &x );
63-
assert( status == napi_ok );
64-
65-
bool result = stdlib_base_is_even( x );
66-
67-
napi_value v;
68-
status = napi_create_int32( env, (int32_t)result, &v );
69-
assert( status == napi_ok );
70-
71-
return v;
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
36+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
37+
STDLIB_NAPI_CREATE_INT32( env, (int32_t)stdlib_base_is_even( x ), out );
38+
return out;
7239
}
7340

74-
/**
75-
* Initializes a Node-API module.
76-
*
77-
* @param env environment under which the function is invoked
78-
* @param exports exports object
79-
* @return main export
80-
*/
81-
static napi_value init( napi_env env, napi_value exports ) {
82-
napi_value fcn;
83-
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
84-
assert( status == napi_ok );
85-
return fcn;
86-
}
41+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
8742

88-
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )

0 commit comments

Comments
 (0)