|
| 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 Float64Array = require( '@stdlib/array/float64' ); |
| 25 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 26 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 27 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 28 | +var assign = require( './../lib/assign.js' ); |
| 29 | + |
| 30 | + |
| 31 | +// TESTS // |
| 32 | + |
| 33 | +tape( 'main export is a function', function test( t ) { |
| 34 | + t.ok( true, __filename ); |
| 35 | + t.strictEqual( typeof assign, 'function', 'main export is a function' ); |
| 36 | + t.end(); |
| 37 | +}); |
| 38 | + |
| 39 | +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { |
| 40 | + var values; |
| 41 | + var y; |
| 42 | + var i; |
| 43 | + |
| 44 | + y = empty( [], { |
| 45 | + 'dtype': 'bool' |
| 46 | + }); |
| 47 | + |
| 48 | + values = [ |
| 49 | + '5', |
| 50 | + 5, |
| 51 | + NaN, |
| 52 | + true, |
| 53 | + false, |
| 54 | + null, |
| 55 | + void 0, |
| 56 | + {}, |
| 57 | + [], |
| 58 | + function noop() {} |
| 59 | + ]; |
| 60 | + |
| 61 | + for ( i = 0; i < values.length; i++ ) { |
| 62 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 63 | + } |
| 64 | + t.end(); |
| 65 | + |
| 66 | + function badValue( value ) { |
| 67 | + return function badValue() { |
| 68 | + assign( value, y ); |
| 69 | + }; |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { |
| 74 | + var values; |
| 75 | + var y; |
| 76 | + var i; |
| 77 | + |
| 78 | + y = empty( [], { |
| 79 | + 'dtype': 'bool' |
| 80 | + }); |
| 81 | + |
| 82 | + values = [ |
| 83 | + '5', |
| 84 | + 5, |
| 85 | + NaN, |
| 86 | + true, |
| 87 | + false, |
| 88 | + null, |
| 89 | + void 0, |
| 90 | + {}, |
| 91 | + [], |
| 92 | + function noop() {} |
| 93 | + ]; |
| 94 | + |
| 95 | + for ( i = 0; i < values.length; i++ ) { |
| 96 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 97 | + } |
| 98 | + t.end(); |
| 99 | + |
| 100 | + function badValue( value ) { |
| 101 | + return function badValue() { |
| 102 | + assign( value, y, {} ); |
| 103 | + }; |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { |
| 108 | + var values; |
| 109 | + var x; |
| 110 | + var i; |
| 111 | + |
| 112 | + x = empty( [ 2, 2 ], { |
| 113 | + 'dtype': 'float64' |
| 114 | + }); |
| 115 | + |
| 116 | + values = [ |
| 117 | + '5', |
| 118 | + 5, |
| 119 | + NaN, |
| 120 | + true, |
| 121 | + false, |
| 122 | + null, |
| 123 | + void 0, |
| 124 | + {}, |
| 125 | + [], |
| 126 | + function noop() {} |
| 127 | + ]; |
| 128 | + |
| 129 | + for ( i = 0; i < values.length; i++ ) { |
| 130 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 131 | + } |
| 132 | + t.end(); |
| 133 | + |
| 134 | + function badValue( value ) { |
| 135 | + return function badValue() { |
| 136 | + assign( x, value ); |
| 137 | + }; |
| 138 | + } |
| 139 | +}); |
| 140 | + |
| 141 | +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { |
| 142 | + var values; |
| 143 | + var x; |
| 144 | + var i; |
| 145 | + |
| 146 | + x = empty( [ 2, 2 ], { |
| 147 | + 'dtype': 'float64' |
| 148 | + }); |
| 149 | + |
| 150 | + values = [ |
| 151 | + '5', |
| 152 | + 5, |
| 153 | + NaN, |
| 154 | + true, |
| 155 | + false, |
| 156 | + null, |
| 157 | + void 0, |
| 158 | + {}, |
| 159 | + [], |
| 160 | + function noop() {} |
| 161 | + ]; |
| 162 | + |
| 163 | + for ( i = 0; i < values.length; i++ ) { |
| 164 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 165 | + } |
| 166 | + t.end(); |
| 167 | + |
| 168 | + function badValue( value ) { |
| 169 | + return function badValue() { |
| 170 | + assign( x, value, {} ); |
| 171 | + }; |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { |
| 176 | + var values; |
| 177 | + var x; |
| 178 | + var y; |
| 179 | + var i; |
| 180 | + |
| 181 | + x = empty( [ 2, 2 ], { |
| 182 | + 'dtype': 'float64' |
| 183 | + }); |
| 184 | + y = empty( [ 2 ], { |
| 185 | + 'dtype': 'bool' |
| 186 | + }); |
| 187 | + |
| 188 | + values = [ |
| 189 | + '5', |
| 190 | + 5, |
| 191 | + NaN, |
| 192 | + true, |
| 193 | + false, |
| 194 | + null, |
| 195 | + void 0, |
| 196 | + [], |
| 197 | + function noop() {} |
| 198 | + ]; |
| 199 | + |
| 200 | + for ( i = 0; i < values.length; i++ ) { |
| 201 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 202 | + } |
| 203 | + t.end(); |
| 204 | + |
| 205 | + function badValue( value ) { |
| 206 | + return function badValue() { |
| 207 | + assign( x, y, value ); |
| 208 | + }; |
| 209 | + } |
| 210 | +}); |
| 211 | + |
| 212 | +tape( 'the function throws an error if provided an options with invalid `dims` property', function test( t ) { |
| 213 | + var values; |
| 214 | + var x; |
| 215 | + var y; |
| 216 | + var i; |
| 217 | + |
| 218 | + x = empty( [ 2, 2 ], { |
| 219 | + 'dtype': 'float64' |
| 220 | + }); |
| 221 | + y = empty( [ 2 ], { |
| 222 | + 'dtype': 'bool' |
| 223 | + }); |
| 224 | + |
| 225 | + values = [ |
| 226 | + '5', |
| 227 | + NaN, |
| 228 | + true, |
| 229 | + false, |
| 230 | + null, |
| 231 | + void 0, |
| 232 | + {} |
| 233 | + ]; |
| 234 | + |
| 235 | + for ( i = 0; i < values.length; i++ ) { |
| 236 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 237 | + } |
| 238 | + t.end(); |
| 239 | + |
| 240 | + function badValue( value ) { |
| 241 | + return function badValue() { |
| 242 | + var opts = { |
| 243 | + 'dims': value |
| 244 | + }; |
| 245 | + assign( x, y, opts ); |
| 246 | + }; |
| 247 | + } |
| 248 | +}); |
| 249 | + |
| 250 | +tape( 'the function tests whether every element along one or more ndarray dimensions is truthy (row-major)', function test( t ) { |
| 251 | + var expected; |
| 252 | + var actual; |
| 253 | + var x; |
| 254 | + var y; |
| 255 | + |
| 256 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 257 | + y = empty( [], { |
| 258 | + 'dtype': 'bool' |
| 259 | + }); |
| 260 | + |
| 261 | + actual = assign( x, y ); |
| 262 | + expected = true; |
| 263 | + |
| 264 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 265 | + t.strictEqual( actual.get(), expected, 'returns expected value' ); |
| 266 | + |
| 267 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 268 | + y = empty( [], { |
| 269 | + 'dtype': 'bool' |
| 270 | + }); |
| 271 | + |
| 272 | + actual = assign( x, y ); |
| 273 | + expected = false; |
| 274 | + |
| 275 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 276 | + t.strictEqual( actual.get(), expected, 'returns expected value' ); |
| 277 | + |
| 278 | + t.end(); |
| 279 | +}); |
| 280 | + |
| 281 | +tape( 'the function tests whether every element along one or more ndarray dimensions is truthy (column-major)', function test( t ) { |
| 282 | + var expected; |
| 283 | + var actual; |
| 284 | + var x; |
| 285 | + var y; |
| 286 | + |
| 287 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); |
| 288 | + y = empty( [], { |
| 289 | + 'dtype': 'bool' |
| 290 | + }); |
| 291 | + |
| 292 | + actual = assign( x, y ); |
| 293 | + expected = true; |
| 294 | + |
| 295 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 296 | + t.strictEqual( actual.get(), expected, 'returns expected value' ); |
| 297 | + |
| 298 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); |
| 299 | + y = empty( [], { |
| 300 | + 'dtype': 'bool' |
| 301 | + }); |
| 302 | + |
| 303 | + actual = assign( x, y ); |
| 304 | + expected = false; |
| 305 | + |
| 306 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 307 | + t.strictEqual( actual.get(), expected, 'returns expected value' ); |
| 308 | + |
| 309 | + t.end(); |
| 310 | +}); |
| 311 | + |
| 312 | +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { |
| 313 | + var expected; |
| 314 | + var actual; |
| 315 | + var opts; |
| 316 | + var x; |
| 317 | + var y; |
| 318 | + |
| 319 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, 8.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); |
| 320 | + |
| 321 | + opts = { |
| 322 | + 'dims': [ 0 ] |
| 323 | + }; |
| 324 | + y = empty( [ 4 ], { |
| 325 | + 'dtype': 'bool' |
| 326 | + }); |
| 327 | + actual = assign( x, y, opts ); |
| 328 | + expected = [ true, true, true, true ]; |
| 329 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 330 | + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); |
| 331 | + |
| 332 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, -5.0, 0.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); |
| 333 | + |
| 334 | + opts = { |
| 335 | + 'dims': [ 0 ] |
| 336 | + }; |
| 337 | + y = empty( [ 4 ], { |
| 338 | + 'dtype': 'bool' |
| 339 | + }); |
| 340 | + actual = assign( x, y, opts ); |
| 341 | + expected = [ true, false, true, false ]; |
| 342 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 343 | + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); |
| 344 | + t.end(); |
| 345 | +}); |
| 346 | + |
| 347 | +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { |
| 348 | + var expected; |
| 349 | + var actual; |
| 350 | + var opts; |
| 351 | + var x; |
| 352 | + var y; |
| 353 | + |
| 354 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); |
| 355 | + |
| 356 | + opts = { |
| 357 | + 'dims': [ 1 ] |
| 358 | + }; |
| 359 | + y = empty( [ 2 ], { |
| 360 | + 'dtype': 'bool' |
| 361 | + }); |
| 362 | + actual = assign( x, y, opts ); |
| 363 | + expected = [ true, true ]; |
| 364 | + |
| 365 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 366 | + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); |
| 367 | + |
| 368 | + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); |
| 369 | + |
| 370 | + opts = { |
| 371 | + 'dims': [ 1 ] |
| 372 | + }; |
| 373 | + y = empty( [ 2 ], { |
| 374 | + 'dtype': 'bool' |
| 375 | + }); |
| 376 | + actual = assign( x, y, opts ); |
| 377 | + expected = [ true, false ]; |
| 378 | + |
| 379 | + t.strictEqual( actual, y, 'returns expected value' ); |
| 380 | + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); |
| 381 | + t.end(); |
| 382 | +}); |
0 commit comments