|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' ); |
| 27 | +var EPS = require( '@stdlib/constants/float32/eps' ); |
| 28 | +var absf = require( '@stdlib/math/base/special/absf' ); |
| 29 | +var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' ); |
| 30 | +var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' ); |
| 31 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 32 | +var csignumf = require( './../lib' ); |
| 33 | + |
| 34 | + |
| 35 | +// FIXTURES // |
| 36 | + |
| 37 | +var data = require( './fixtures/julia/data.json' ); |
| 38 | + |
| 39 | + |
| 40 | +// TESTS // |
| 41 | + |
| 42 | +tape( 'main export is a function', function test( t ) { |
| 43 | + t.ok( true, __filename ); |
| 44 | + t.strictEqual( typeof csignumf.strided, 'function', 'main export is a function' ); |
| 45 | + t.end(); |
| 46 | +}); |
| 47 | + |
| 48 | +tape( 'the function evaluates the signum function for complex numbers in strided arrays', function test( t ) { |
| 49 | + var delta; |
| 50 | + var out; |
| 51 | + var tol; |
| 52 | + var ere; |
| 53 | + var eim; |
| 54 | + var re; |
| 55 | + var im; |
| 56 | + var z; |
| 57 | + var i; |
| 58 | + |
| 59 | + re = data.re; |
| 60 | + im = data.im; |
| 61 | + ere = data.expected_re; |
| 62 | + eim = data.expected_im; |
| 63 | + |
| 64 | + for ( i = 0; i < re.length; i++ ) { |
| 65 | + z = new Float32Array( 2 ); |
| 66 | + z[ 0 ] = re[ i ]; |
| 67 | + z[ 1 ] = im[ i ]; |
| 68 | + |
| 69 | + out = new Float32Array( 2 ); |
| 70 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 71 | + |
| 72 | + if ( isnanf( ere[ i ] ) || isnanf( eim[ i ] ) ) { |
| 73 | + t.strictEqual( isnanf( out[ 0 ] ), true, 're is NaN' ); |
| 74 | + t.strictEqual( isnanf( out[ 1 ] ), true, 'im is NaN' ); |
| 75 | + continue; |
| 76 | + } |
| 77 | + if ( out[ 0 ] === ere[ i ] && out[ 1 ] === eim[ i ] ) { |
| 78 | + t.strictEqual( out[ 0 ], ere[ i ], 're: '+re[ i ]+'. Expected: '+ere[ i ] ); |
| 79 | + t.strictEqual( out[ 1 ], eim[ i ], 'im: '+im[ i ]+'. Expected: '+eim[ i ] ); |
| 80 | + } else { |
| 81 | + delta = absf( out[ 0 ] - ere[ i ] ); |
| 82 | + tol = EPS * absf( ere[ i ] ); |
| 83 | + t.ok(delta <= tol, 'within tolerance. re: '+re[ i ]+'. Expected: '+ere[ i ]+'. Actual: '+out[ 0 ]+'. Δ: '+delta+'. tol: '+tol+'.'); |
| 84 | + |
| 85 | + delta = absf( out[ 1 ] - eim[ i ] ); |
| 86 | + tol = EPS * absf( eim[ i ] ); |
| 87 | + t.ok(delta <= tol, 'within tolerance. im: '+im[ i ]+'. Expected: '+eim[ i ]+'. Actual: '+out[ 1 ]+'. Δ: '+delta+'. tol: '+tol+'.'); |
| 88 | + } |
| 89 | + } |
| 90 | + t.end(); |
| 91 | +}); |
| 92 | + |
| 93 | +tape( 'the function returns +0 if provided +0 +0i', function test( t ) { |
| 94 | + var out = new Float32Array( 2 ); |
| 95 | + var z = new Float32Array( [ +0.0, +0.0 ] ); |
| 96 | + |
| 97 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 98 | + |
| 99 | + t.strictEqual( isPositiveZerof( out[ 0 ] ), true, 'real is +0' ); |
| 100 | + t.strictEqual( isPositiveZerof( out[ 1 ] ), true, 'imag is +0' ); |
| 101 | + |
| 102 | + t.end(); |
| 103 | +}); |
| 104 | + |
| 105 | +tape( 'the function returns -0 if provided -0 -0i', function test( t ) { |
| 106 | + var out = new Float32Array( 2 ); |
| 107 | + var z = new Float32Array( [ -0.0, -0.0 ] ); |
| 108 | + |
| 109 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 110 | + |
| 111 | + t.strictEqual( isNegativeZerof( out[ 0 ] ), true, 'real is -0' ); |
| 112 | + t.strictEqual( isNegativeZerof( out[ 1 ] ), true, 'imag is -0' ); |
| 113 | + |
| 114 | + t.end(); |
| 115 | +}); |
| 116 | + |
| 117 | +tape( 'the function returns NaNs if provided NaNs', function test( t ) { |
| 118 | + var out = new Float32Array( 2 ); |
| 119 | + var z = new Float32Array( [ NaN, NaN ] ); |
| 120 | + |
| 121 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 122 | + |
| 123 | + t.strictEqual( isnanf( out[ 0 ] ), true, 'real is NaN' ); |
| 124 | + t.strictEqual( isnanf( out[ 1 ] ), true, 'imag is NaN' ); |
| 125 | + |
| 126 | + t.end(); |
| 127 | +}); |
| 128 | + |
| 129 | +tape( 'the function returns NaNs if provided infinities', function test( t ) { |
| 130 | + var out; |
| 131 | + var z; |
| 132 | + |
| 133 | + z = new Float32Array( [ PINF, PINF ] ); |
| 134 | + out = new Float32Array( 2 ); |
| 135 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 136 | + |
| 137 | + t.strictEqual( isnanf( out[ 0 ] ), true, 'real is NaN' ); |
| 138 | + t.strictEqual( isnanf( out[ 1 ] ), true, 'imag is NaN' ); |
| 139 | + |
| 140 | + z = new Float32Array( [ NINF, NINF ] ); |
| 141 | + csignumf.strided( z, 1, 0, out, 1, 0 ); |
| 142 | + |
| 143 | + t.strictEqual( isnanf( out[ 0 ] ), true, 'real is NaN' ); |
| 144 | + t.strictEqual( isnanf( out[ 1 ] ), true, 'imag is NaN' ); |
| 145 | + |
| 146 | + t.end(); |
| 147 | +}); |
0 commit comments