|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2026 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 isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); |
| 25 | +var isSameAccessorArray = require( '@stdlib/assert/is-same-accessor-array' ); |
| 26 | +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 27 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 28 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 29 | +var zeros = require( '@stdlib/array/zeros' ); |
| 30 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 31 | +var where = require( './../lib' ); |
| 32 | + |
| 33 | + |
| 34 | +// TESTS // |
| 35 | + |
| 36 | +tape( 'main export is a function', function test( t ) { |
| 37 | + t.ok( true, __filename ); |
| 38 | + t.strictEqual( typeof where, 'function', 'main export is a function' ); |
| 39 | + t.end(); |
| 40 | +}); |
| 41 | + |
| 42 | +tape( 'the function conditionally assigns elements from a 1-dimensional input ndarray to an output ndarray', function test( t ) { |
| 43 | + var condition; |
| 44 | + var expected; |
| 45 | + var cbuf; |
| 46 | + var xbuf; |
| 47 | + var ybuf; |
| 48 | + var out; |
| 49 | + var x; |
| 50 | + var y; |
| 51 | + |
| 52 | + cbuf = new Uint8Array([ |
| 53 | + 1, |
| 54 | + 0, |
| 55 | + 1, |
| 56 | + 1, |
| 57 | + 0, |
| 58 | + 0, |
| 59 | + 1, |
| 60 | + 0 |
| 61 | + ]); |
| 62 | + xbuf = new Float64Array([ |
| 63 | + 0.0, |
| 64 | + 1.0, |
| 65 | + 0.0, |
| 66 | + 2.0, |
| 67 | + 0.0, |
| 68 | + 3.0, |
| 69 | + 0.0, |
| 70 | + 4.0 |
| 71 | + ]); |
| 72 | + ybuf = new Float64Array([ |
| 73 | + 0.0, |
| 74 | + -1.0, |
| 75 | + 0.0, |
| 76 | + -2.0, |
| 77 | + 0.0, |
| 78 | + -3.0, |
| 79 | + 0.0, |
| 80 | + -4.0 |
| 81 | + ]); |
| 82 | + condition = ndarray( 'uint8', cbuf, [ 4 ], [ 2 ], 1, 'row-major' ); |
| 83 | + x = ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 1, 'row-major' ); |
| 84 | + y = ndarray( 'float64', ybuf, [ 4 ], [ 2 ], 1, 'row-major' ); |
| 85 | + out = ndarray( 'float64', zeros( 8, 'float64' ), [ 4 ], [ 2 ], 1, 'row-major' ); |
| 86 | + |
| 87 | + where( [ condition, x, y, out ] ); |
| 88 | + |
| 89 | + expected = new Float64Array([ |
| 90 | + 0.0, |
| 91 | + -1.0, |
| 92 | + 0.0, |
| 93 | + 2.0, |
| 94 | + 0.0, |
| 95 | + -3.0, |
| 96 | + 0.0, |
| 97 | + -4.0 |
| 98 | + ]); |
| 99 | + |
| 100 | + t.strictEqual( isSameFloat64Array( out.data, expected ), true, 'returns expected value' ); |
| 101 | + |
| 102 | + t.end(); |
| 103 | +}); |
| 104 | + |
| 105 | +tape( 'the function conditionally assigns elements from a 1-dimensional input ndarray to an output ndarray (empty array)', function test( t ) { |
| 106 | + var condition; |
| 107 | + var expected; |
| 108 | + var cbuf; |
| 109 | + var xbuf; |
| 110 | + var ybuf; |
| 111 | + var out; |
| 112 | + var x; |
| 113 | + var y; |
| 114 | + |
| 115 | + cbuf = new Uint8Array([ |
| 116 | + 1, |
| 117 | + 0, |
| 118 | + 1, |
| 119 | + 1, |
| 120 | + 0, |
| 121 | + 0, |
| 122 | + 1, |
| 123 | + 0 |
| 124 | + ]); |
| 125 | + xbuf = new Float64Array([ |
| 126 | + 1.0, |
| 127 | + 2.0, |
| 128 | + 3.0, |
| 129 | + 4.0, |
| 130 | + 5.0, |
| 131 | + 6.0, |
| 132 | + 7.0, |
| 133 | + 8.0 |
| 134 | + ]); |
| 135 | + ybuf = new Float64Array([ |
| 136 | + -1.0, |
| 137 | + -2.0, |
| 138 | + -3.0, |
| 139 | + -4.0, |
| 140 | + -5.0, |
| 141 | + -6.0, |
| 142 | + -7.0, |
| 143 | + -8.0 |
| 144 | + ]); |
| 145 | + condition = ndarray( 'uint8', cbuf, [ 0 ], [ 1 ], 0, 'row-major' ); |
| 146 | + x = ndarray( 'float64', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); |
| 147 | + y = ndarray( 'float64', ybuf, [ 0 ], [ 1 ], 0, 'row-major' ); |
| 148 | + out = ndarray( 'float64', zeros( 8, 'float64' ), [ 0 ], [ 1 ], 0, 'row-major' ); |
| 149 | + |
| 150 | + where( [ condition, x, y, out ] ); |
| 151 | + |
| 152 | + expected = new Float64Array([ |
| 153 | + 0.0, |
| 154 | + 0.0, |
| 155 | + 0.0, |
| 156 | + 0.0, |
| 157 | + 0.0, |
| 158 | + 0.0, |
| 159 | + 0.0, |
| 160 | + 0.0 |
| 161 | + ]); |
| 162 | + t.strictEqual( isSameFloat64Array( out.data, expected ), true, 'returns expected value' ); |
| 163 | + |
| 164 | + t.end(); |
| 165 | +}); |
| 166 | + |
| 167 | +tape( 'the function conditionally assigns elements from a 1-dimensional input ndarray to an output ndarray (accessors)', function test( t ) { |
| 168 | + var condition; |
| 169 | + var cbuf; |
| 170 | + var xbuf; |
| 171 | + var ybuf; |
| 172 | + var out; |
| 173 | + var x; |
| 174 | + var y; |
| 175 | + |
| 176 | + cbuf = [ |
| 177 | + 0, |
| 178 | + 0, |
| 179 | + 0, |
| 180 | + 0 |
| 181 | + ]; |
| 182 | + xbuf = [ |
| 183 | + 1.0, |
| 184 | + 2.0, |
| 185 | + 3.0, |
| 186 | + 4.0 |
| 187 | + ]; |
| 188 | + ybuf = [ |
| 189 | + -1.0, |
| 190 | + -2.0, |
| 191 | + -3.0, |
| 192 | + -4.0 |
| 193 | + ]; |
| 194 | + condition = ndarray( 'generic', toAccessorArray( cbuf ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 195 | + x = ndarray( 'generic', toAccessorArray( xbuf ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 196 | + y = ndarray( 'generic', toAccessorArray( ybuf ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 197 | + out = ndarray( 'generic', toAccessorArray( zeros( 4, 'generic' ) ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 198 | + |
| 199 | + where( [ condition, x, y, out ] ); |
| 200 | + |
| 201 | + t.strictEqual( isSameAccessorArray( out.data, y.data ), true, 'returns expected value' ); |
| 202 | + |
| 203 | + t.end(); |
| 204 | +}); |
0 commit comments