Skip to content

Commit 8063c8c

Browse files
committed
added test.native.js
1 parent 8f43f70 commit 8063c8c

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 PINF = require( '@stdlib/constants/float64/pinf' );
27+
var NINF = require( '@stdlib/constants/float64/ninf' );
28+
var EPS = require( '@stdlib/constants/float64/eps' );
29+
var abs = require( '@stdlib/math/base/special/abs' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
32+
33+
// FIXTURES //
34+
35+
var data = require( './fixtures/julia/data.json' );
36+
var largeNegative = require( './fixtures/julia/large_negative.json' );
37+
var largePositive = require( './fixtures/julia/large_positive.json' );
38+
var tinyNegative = require( './fixtures/julia/tiny_negative.json' );
39+
var tinyPositive = require( './fixtures/julia/tiny_positive.json' );
40+
41+
42+
// VARIABLES //
43+
44+
var cosc = tryRequire( resolve( __dirname, './../lib/native.js' ) );
45+
var opts = {
46+
'skip': ( cosc instanceof Error )
47+
};
48+
49+
50+
// TESTS //
51+
52+
tape( 'main export is a function', opts, function test( t ) {
53+
t.ok( true, __filename );
54+
t.strictEqual( typeof cosc, 'function', 'main export is a function' );
55+
t.end();
56+
});
57+
58+
tape( 'the function computes the derivative of cardinal sine', opts, function test( t ) {
59+
var expected;
60+
var delta;
61+
var tol;
62+
var x;
63+
var y;
64+
var i;
65+
66+
x = data.x;
67+
expected = data.expected;
68+
69+
for ( i = 0; i < x.length; i++ ) {
70+
y = cosc( x[i] );
71+
if ( y === expected[ i ] ) {
72+
t.equal( y, expected[ i ], 'x: '+x[i]+'. Expected: '+expected[i] );
73+
} else {
74+
delta = abs( y - expected[i] );
75+
tol = 2.0 * EPS * abs( expected[i] );
76+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y+'. Expected: '+expected[i]+'. Tolerance: '+tol+'.' );
77+
}
78+
}
79+
t.end();
80+
});
81+
82+
tape( 'the function computes the cardinal cose (large negative)', opts, function test( t ) {
83+
var expected;
84+
var delta;
85+
var tol;
86+
var x;
87+
var y;
88+
var i;
89+
90+
x = largeNegative.x;
91+
expected = largeNegative.expected;
92+
93+
for ( i = 0; i < x.length; i++ ) {
94+
y = cosc( x[i] );
95+
if ( y === expected[ i ] ) {
96+
t.equal( y, expected[ i ], 'x: '+x[i]+'. Expected: '+expected[i] );
97+
} else {
98+
delta = abs( y - expected[i] );
99+
tol = 2.0 * EPS * abs( expected[i] );
100+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y+'. Expected: '+expected[i]+'. Tolerance: '+tol+'.' );
101+
}
102+
}
103+
t.end();
104+
});
105+
106+
tape( 'the function computes the derivative of cardinal sine (large positive)', opts, function test( t ) {
107+
var expected;
108+
var delta;
109+
var tol;
110+
var x;
111+
var y;
112+
var i;
113+
114+
x = largePositive.x;
115+
expected = largePositive.expected;
116+
117+
for ( i = 0; i < x.length; i++ ) {
118+
y = cosc( x[i] );
119+
if ( y === expected[ i ] ) {
120+
t.equal( y, expected[ i ], 'x: '+x[i]+'. Expected: '+expected[i] );
121+
} else {
122+
delta = abs( y - expected[i] );
123+
tol = 2.0 * EPS * abs( expected[i] );
124+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y+'. Expected: '+expected[i]+'. Tolerance: '+tol+'.' );
125+
}
126+
}
127+
t.end();
128+
});
129+
130+
tape( 'the function computes the derivative of cardinal sine (tiny negative)', opts, function test( t ) {
131+
var expected;
132+
var delta;
133+
var tol;
134+
var x;
135+
var y;
136+
var i;
137+
138+
x = tinyNegative.x;
139+
expected = tinyNegative.expected;
140+
141+
for ( i = 0; i < x.length; i++ ) {
142+
y = cosc( x[i] );
143+
if ( y === expected[ i ] ) {
144+
t.equal( y, expected[ i ], 'x: '+x[i]+'. Expected: '+expected[i] );
145+
} else {
146+
delta = abs( y - expected[i] );
147+
tol = EPS * abs( expected[i] );
148+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y+'. Expected: '+expected[i]+'. Tolerance: '+tol+'.' );
149+
}
150+
}
151+
t.end();
152+
});
153+
154+
tape( 'the function computes the derivative of cardinal sine (tiny positive)', opts, function test( t ) {
155+
var expected;
156+
var delta;
157+
var tol;
158+
var x;
159+
var y;
160+
var i;
161+
162+
x = tinyPositive.x;
163+
expected = tinyPositive.expected;
164+
165+
for ( i = 0; i < x.length; i++ ) {
166+
y = cosc( x[i] );
167+
if ( y === expected[ i ] ) {
168+
t.equal( y, expected[ i ], 'x: '+x[i]+'. Expected: '+expected[i] );
169+
} else {
170+
delta = abs( y - expected[i] );
171+
tol = EPS * abs( expected[i] );
172+
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y+'. Expected: '+expected[i]+'. Tolerance: '+tol+'.' );
173+
}
174+
}
175+
t.end();
176+
});
177+
178+
tape( 'the function returns `NaN` if provided a `NaN`', opts, function test( t ) {
179+
var v = cosc( NaN );
180+
t.equal( isnan( v ), true, 'returns expected value' );
181+
t.end();
182+
});
183+
184+
tape( 'the function returns `0.0` if provided `0.0`', opts, function test( t ) {
185+
var v = cosc( 0.0 );
186+
t.equal( v, 0.0, 'returns 0.0' );
187+
t.end();
188+
});
189+
190+
tape( 'the function returns `0.0` if provided positive or negative infinity', opts, function test( t ) {
191+
var v = cosc( PINF );
192+
t.equal( v, 0.0, 'returns 0.0' );
193+
194+
v = cosc( NINF );
195+
t.equal( v, 0.0, 'returns 0.0' );
196+
t.end();
197+
});

0 commit comments

Comments
 (0)