|
| 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 | +/* eslint-disable stdlib/no-redeclare */ |
| 22 | + |
| 23 | +// MODULES // |
| 24 | + |
| 25 | +var tape = require( 'tape' ); |
| 26 | +var oneTo = require( '@stdlib/array/one-to' ); |
| 27 | +var real = require( '@stdlib/complex/float64/real' ); |
| 28 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 29 | +var Complex128 = require( '@stdlib/complex/float64/ctor' ); |
| 30 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 31 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 32 | +var find = require( './../lib' ); |
| 33 | + |
| 34 | + |
| 35 | +// TESTS // |
| 36 | + |
| 37 | +tape( 'main export is a function', function test( t ) { |
| 38 | + t.ok( true, __filename ); |
| 39 | + t.strictEqual( typeof find, 'function', 'main export is a function'); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'the function returns the first element in a 1-dimensional ndarray which passes a test implemented by a predicate function', function test( t ) { |
| 44 | + var actual; |
| 45 | + var sv; |
| 46 | + var x; |
| 47 | + |
| 48 | + sv = scalar2ndarray( -1.0, { |
| 49 | + 'dtype': 'float64' |
| 50 | + }); |
| 51 | + |
| 52 | + x = ndarray( 'float64', oneTo( 8, 'float64' ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 53 | + actual = find( [ x, sv ], clbk ); |
| 54 | + t.strictEqual( actual, 2.0, 'returns expected value' ); |
| 55 | + |
| 56 | + x = ndarray( 'float64', oneTo( 8, 'float64' ), [ 4 ], [ 2 ], 0, 'row-major' ); |
| 57 | + actual = find( [ x, sv ], clbk ); |
| 58 | + t.strictEqual( actual, -1.0, 'returns expected value' ); |
| 59 | + |
| 60 | + t.end(); |
| 61 | + |
| 62 | + function clbk( v ) { |
| 63 | + return v % 2.0 === 0.0; |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +tape( 'the function returns the first element in a 1-dimensional ndarray which passes a test implemented by a predicate function (accessors)', function test( t ) { |
| 68 | + var actual; |
| 69 | + var sv; |
| 70 | + var x; |
| 71 | + |
| 72 | + sv = scalar2ndarray( new Complex128( -1.0, 0.0 ), { |
| 73 | + 'dtype': 'complex128' |
| 74 | + }); |
| 75 | + |
| 76 | + x = ndarray( 'complex128', oneTo( 8, 'complex128' ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 77 | + actual = find( [ x, sv ], clbk ); |
| 78 | + t.deepEqual( actual, new Complex128( 2.0, 0.0 ), 'returns expected value' ); |
| 79 | + |
| 80 | + x = ndarray( 'complex128', oneTo( 8, 'complex128' ), [ 4 ], [ 2 ], 0, 'row-major' ); |
| 81 | + actual = find( [ x, sv ], clbk ); |
| 82 | + t.deepEqual( actual, new Complex128( -1.0, 0.0 ), 'returns expected value' ); |
| 83 | + |
| 84 | + t.end(); |
| 85 | + |
| 86 | + function clbk( v ) { |
| 87 | + return real( v ) % 2.0 === 0.0; |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +tape( 'the function supports specifying the callback execution context', function test( t ) { |
| 92 | + var expected; |
| 93 | + var indices; |
| 94 | + var values; |
| 95 | + var arrays; |
| 96 | + var actual; |
| 97 | + var ctx; |
| 98 | + var sv; |
| 99 | + var x; |
| 100 | + |
| 101 | + x = new ndarray( 'float64', oneTo( 8, 'float64'), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 102 | + sv = scalar2ndarray( -1.0, { |
| 103 | + 'dtype': 'float64' |
| 104 | + }); |
| 105 | + |
| 106 | + indices = []; |
| 107 | + values = []; |
| 108 | + arrays = []; |
| 109 | + |
| 110 | + ctx = { |
| 111 | + 'count': 0 |
| 112 | + }; |
| 113 | + actual = find( [ x, sv ], clbk, ctx ); |
| 114 | + |
| 115 | + t.strictEqual( actual, 2.0, 'returns expected value' ); |
| 116 | + t.strictEqual( ctx.count, 2, 'returns expected value' ); |
| 117 | + |
| 118 | + expected = [ |
| 119 | + 1.0, |
| 120 | + 2.0 |
| 121 | + ]; |
| 122 | + t.deepEqual( values, expected, 'returns expected value' ); |
| 123 | + |
| 124 | + expected = [ |
| 125 | + [ 0 ], |
| 126 | + [ 1 ] |
| 127 | + ]; |
| 128 | + t.deepEqual( indices, expected, 'returns expected value' ); |
| 129 | + |
| 130 | + expected = [ |
| 131 | + x, |
| 132 | + x |
| 133 | + ]; |
| 134 | + t.deepEqual( arrays, expected, 'returns expected value' ); |
| 135 | + |
| 136 | + t.end(); |
| 137 | + |
| 138 | + function clbk( v, idx, arr ) { |
| 139 | + this.count += 1; // eslint-disable-line no-invalid-this |
| 140 | + values.push( v ); |
| 141 | + indices.push( idx ); |
| 142 | + arrays.push( arr ); |
| 143 | + return v % 2.0 === 0.0; |
| 144 | + } |
| 145 | +}); |
| 146 | + |
| 147 | +tape( 'the function supports specifying the callback execution context (accessors)', function test( t ) { |
| 148 | + var expected; |
| 149 | + var indices; |
| 150 | + var values; |
| 151 | + var arrays; |
| 152 | + var actual; |
| 153 | + var ctx; |
| 154 | + var sv; |
| 155 | + var x; |
| 156 | + |
| 157 | + x = ndarray( 'complex128', oneTo( 8, 'complex128' ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 158 | + sv = scalar2ndarray( new Complex128( -1.0, 0.0 ), { |
| 159 | + 'dtype': 'complex128' |
| 160 | + }); |
| 161 | + |
| 162 | + indices = []; |
| 163 | + values = []; |
| 164 | + arrays = []; |
| 165 | + |
| 166 | + ctx = { |
| 167 | + 'count': 0 |
| 168 | + }; |
| 169 | + actual = find( [ x, sv ], clbk, ctx ); |
| 170 | + |
| 171 | + t.deepEqual( actual, new Complex128( 2.0, 0.0 ), 'returns expected value' ); |
| 172 | + t.strictEqual( ctx.count, 2, 'returns expected value' ); |
| 173 | + |
| 174 | + expected = [ |
| 175 | + [ 1.0, 0.0 ], |
| 176 | + [ 2.0, 0.0 ] |
| 177 | + ]; |
| 178 | + t.deepEqual( values, expected, 'returns expected value' ); |
| 179 | + |
| 180 | + expected = [ |
| 181 | + [ 0 ], |
| 182 | + [ 1 ] |
| 183 | + ]; |
| 184 | + t.deepEqual( indices, expected, 'returns expected value' ); |
| 185 | + |
| 186 | + expected = [ |
| 187 | + x, |
| 188 | + x |
| 189 | + ]; |
| 190 | + t.deepEqual( arrays, expected, 'returns expected value' ); |
| 191 | + |
| 192 | + t.end(); |
| 193 | + |
| 194 | + function clbk( v, idx, arr ) { |
| 195 | + this.count += 1; // eslint-disable-line no-invalid-this |
| 196 | + values.push( [ real( v ), imag( v ) ] ); |
| 197 | + indices.push( idx ); |
| 198 | + arrays.push( arr ); |
| 199 | + return real( v ) % 2.0 === 0.0; |
| 200 | + } |
| 201 | +}); |
0 commit comments