|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2022 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 unaryBy = require( '@stdlib/strided/base/unary-by' ); |
| 25 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 26 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 27 | +var dispatchBy = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// FIXTURES // |
| 31 | + |
| 32 | +var fill = require( './fixtures/fill.js' ); |
| 33 | + |
| 34 | + |
| 35 | +// TESTS // |
| 36 | + |
| 37 | +tape( 'main export is a function', function test( t ) { |
| 38 | + t.ok( true, __filename ); |
| 39 | + t.strictEqual( typeof dispatchBy, 'function', 'main export is a function' ); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'the function throws an error if not provided either a function or an array of functions as a first argument', function test( t ) { |
| 44 | + var values; |
| 45 | + var i; |
| 46 | + |
| 47 | + values = [ |
| 48 | + '5', |
| 49 | + 5, |
| 50 | + 3.14, |
| 51 | + NaN, |
| 52 | + true, |
| 53 | + false, |
| 54 | + null, |
| 55 | + void 0, |
| 56 | + [], |
| 57 | + {} |
| 58 | + ]; |
| 59 | + |
| 60 | + for ( i = 0; i < values.length; i++ ) { |
| 61 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 62 | + } |
| 63 | + t.end(); |
| 64 | + |
| 65 | + function badValue( value ) { |
| 66 | + return function badValue() { |
| 67 | + var types = [ 'float64', 'float64' ]; |
| 68 | + var data = [ abs ]; |
| 69 | + dispatchBy( value, types, data, 8, 1, 1 ); |
| 70 | + }; |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +tape( 'the function throws an error if not provided an array-like object as a second argument', function test( t ) { |
| 75 | + var values; |
| 76 | + var i; |
| 77 | + |
| 78 | + values = [ |
| 79 | + '5', |
| 80 | + 5, |
| 81 | + 3.14, |
| 82 | + NaN, |
| 83 | + true, |
| 84 | + false, |
| 85 | + null, |
| 86 | + void 0, |
| 87 | + {}, |
| 88 | + function noop() {} |
| 89 | + ]; |
| 90 | + |
| 91 | + for ( i = 0; i < values.length; i++ ) { |
| 92 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 93 | + } |
| 94 | + t.end(); |
| 95 | + |
| 96 | + function badValue( value ) { |
| 97 | + return function badValue() { |
| 98 | + var data = [ abs ]; |
| 99 | + dispatchBy( unaryBy, value, data, 8, 1, 1 ); |
| 100 | + }; |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +tape( 'the function throws an error if not provided either an array-like object or `null` as a third argument', function test( t ) { |
| 105 | + var values; |
| 106 | + var i; |
| 107 | + |
| 108 | + values = [ |
| 109 | + '5', |
| 110 | + 5, |
| 111 | + 3.14, |
| 112 | + NaN, |
| 113 | + true, |
| 114 | + false, |
| 115 | + void 0, |
| 116 | + {}, |
| 117 | + function noop() {} |
| 118 | + ]; |
| 119 | + |
| 120 | + for ( i = 0; i < values.length; i++ ) { |
| 121 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 122 | + } |
| 123 | + t.end(); |
| 124 | + |
| 125 | + function badValue( value ) { |
| 126 | + return function badValue() { |
| 127 | + var types = [ 'float64', 'float64' ]; |
| 128 | + dispatchBy( unaryBy, types, value, 8, 1, 1 ); |
| 129 | + }; |
| 130 | + } |
| 131 | +}); |
| 132 | + |
| 133 | +tape( 'the function throws an error if not provided a positive integer for a fourth argument', function test( t ) { |
| 134 | + var values; |
| 135 | + var i; |
| 136 | + |
| 137 | + values = [ |
| 138 | + '5', |
| 139 | + 0, |
| 140 | + -1, |
| 141 | + 3.14, |
| 142 | + NaN, |
| 143 | + true, |
| 144 | + false, |
| 145 | + null, |
| 146 | + void 0, |
| 147 | + [], |
| 148 | + {}, |
| 149 | + function noop() {} |
| 150 | + ]; |
| 151 | + |
| 152 | + for ( i = 0; i < values.length; i++ ) { |
| 153 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 154 | + } |
| 155 | + t.end(); |
| 156 | + |
| 157 | + function badValue( value ) { |
| 158 | + return function badValue() { |
| 159 | + var types = [ 'float64', 'float64' ]; |
| 160 | + var data = [ abs ]; |
| 161 | + dispatchBy( unaryBy, types, data, value, 1, 1 ); |
| 162 | + }; |
| 163 | + } |
| 164 | +}); |
| 165 | + |
| 166 | +tape( 'the function throws an error if not provided a nonnegative integer for a fifth argument', function test( t ) { |
| 167 | + var values; |
| 168 | + var i; |
| 169 | + |
| 170 | + values = [ |
| 171 | + '5', |
| 172 | + -1, |
| 173 | + 3.14, |
| 174 | + NaN, |
| 175 | + true, |
| 176 | + false, |
| 177 | + null, |
| 178 | + void 0, |
| 179 | + [], |
| 180 | + {}, |
| 181 | + function noop() {} |
| 182 | + ]; |
| 183 | + |
| 184 | + for ( i = 0; i < values.length; i++ ) { |
| 185 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 186 | + } |
| 187 | + t.end(); |
| 188 | + |
| 189 | + function badValue( value ) { |
| 190 | + return function badValue() { |
| 191 | + var types = [ 'float64', 'float64' ]; |
| 192 | + var data = [ abs ]; |
| 193 | + dispatchBy( unaryBy, types, data, 8, value, 1 ); |
| 194 | + }; |
| 195 | + } |
| 196 | +}); |
| 197 | + |
| 198 | +tape( 'the function throws an error if not provided a nonnegative integer for a sixth argument', function test( t ) { |
| 199 | + var values; |
| 200 | + var i; |
| 201 | + |
| 202 | + values = [ |
| 203 | + '5', |
| 204 | + -1, |
| 205 | + 3.14, |
| 206 | + NaN, |
| 207 | + true, |
| 208 | + false, |
| 209 | + null, |
| 210 | + void 0, |
| 211 | + [], |
| 212 | + {}, |
| 213 | + function noop() {} |
| 214 | + ]; |
| 215 | + |
| 216 | + for ( i = 0; i < values.length; i++ ) { |
| 217 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 218 | + } |
| 219 | + t.end(); |
| 220 | + |
| 221 | + function badValue( value ) { |
| 222 | + return function badValue() { |
| 223 | + var types = [ 'float64', 'float64' ]; |
| 224 | + var data = [ abs ]; |
| 225 | + dispatchBy( unaryBy, types, data, 8, 1, value ); |
| 226 | + }; |
| 227 | + } |
| 228 | +}); |
| 229 | + |
| 230 | +tape( 'the function throws an error if the number of types is incompatible with the number of types indicated by other function parameters (function array)', function test( t ) { |
| 231 | + var values; |
| 232 | + var i; |
| 233 | + |
| 234 | + values = [ |
| 235 | + [ 'float64' ], |
| 236 | + [ 'float64', 'float64', 'float64' ], |
| 237 | + [ 'float64', 'float32', 'float32' ] |
| 238 | + ]; |
| 239 | + |
| 240 | + for ( i = 0; i < values.length; i++ ) { |
| 241 | + t.throws( badValue( values[i] ), Error, 'throws an error when provided ' + values[ i ] ); |
| 242 | + } |
| 243 | + t.end(); |
| 244 | + |
| 245 | + function badValue( value ) { |
| 246 | + return function badValue() { |
| 247 | + var fcns = [ unaryBy, unaryBy, unaryBy ]; |
| 248 | + var data = [ abs, abs, abs ]; |
| 249 | + dispatchBy( fcns, value, data, 8, 1, 1 ); |
| 250 | + }; |
| 251 | + } |
| 252 | +}); |
| 253 | + |
| 254 | +tape( 'the function throws an error if the number of types is incompatible with the number of types indicated by other function parameters (function argument)', function test( t ) { |
| 255 | + var values; |
| 256 | + var i; |
| 257 | + |
| 258 | + values = [ |
| 259 | + [ 'float64' ], |
| 260 | + [ 'float64', 'float64', 'float64' ], |
| 261 | + [ 'float64', 'float32', 'float32' ] |
| 262 | + ]; |
| 263 | + |
| 264 | + for ( i = 0; i < values.length; i++ ) { |
| 265 | + t.throws( badValue( values[i] ), Error, 'throws an error when provided ' + values[ i ] ); |
| 266 | + } |
| 267 | + t.end(); |
| 268 | + |
| 269 | + function badValue( value ) { |
| 270 | + return function badValue() { |
| 271 | + var data = [ abs, abs, abs ]; |
| 272 | + dispatchBy( unaryBy, value, data, 8, 1, 1 ); |
| 273 | + }; |
| 274 | + } |
| 275 | +}); |
| 276 | + |
| 277 | +tape( 'the function throws an error if the length of `data` argument does not match the number of strided array functions', function test( t ) { |
| 278 | + var values; |
| 279 | + var i; |
| 280 | + |
| 281 | + values = [ |
| 282 | + [], |
| 283 | + [ abs, abs ], |
| 284 | + [ abs, abs, abs ] |
| 285 | + ]; |
| 286 | + |
| 287 | + for ( i = 0; i < values.length; i++ ) { |
| 288 | + t.throws( badValue( values[i] ), Error, 'throws an error when provided ' + values[ i ] ); |
| 289 | + } |
| 290 | + t.end(); |
| 291 | + |
| 292 | + function badValue( value ) { |
| 293 | + return function badValue() { |
| 294 | + var types = [ 'float64', 'float64' ]; |
| 295 | + var fcns = [ unaryBy ]; |
| 296 | + dispatchBy( fcns, types, value, 8, 1, 1 ); |
| 297 | + }; |
| 298 | + } |
| 299 | +}); |
| 300 | + |
| 301 | +tape( 'the function throws an error if the fourth argument is incompatible with the number of strided input and output arrays', function test( t ) { |
| 302 | + var values; |
| 303 | + var i; |
| 304 | + |
| 305 | + values = [ |
| 306 | + 0, |
| 307 | + 1, |
| 308 | + 2, |
| 309 | + 3, |
| 310 | + 4, |
| 311 | + 5, |
| 312 | + 6, |
| 313 | + 7, |
| 314 | + 9, |
| 315 | + 11 |
| 316 | + ]; |
| 317 | + |
| 318 | + for ( i = 0; i < values.length; i++ ) { |
| 319 | + t.throws( badValue( values[i] ), Error, 'throws an error when provided ' + values[ i ] ); |
| 320 | + } |
| 321 | + t.end(); |
| 322 | + |
| 323 | + function badValue( value ) { |
| 324 | + return function badValue() { |
| 325 | + var types = [ 'float64', 'float64' ]; |
| 326 | + var data = [ abs ]; |
| 327 | + dispatchBy( unaryBy, types, data, value, 1, 1 ); |
| 328 | + }; |
| 329 | + } |
| 330 | +}); |
| 331 | + |
| 332 | +tape( 'the function throws an error if the number of specified strided input and output arrays is zero', function test( t ) { |
| 333 | + t.throws( badValue, Error, 'throws an error' ); |
| 334 | + t.end(); |
| 335 | + |
| 336 | + function badValue() { |
| 337 | + var types = [ 'float64' ]; |
| 338 | + dispatchBy( fill( 3 ), types, null, 2, 0, 0 ); |
| 339 | + } |
| 340 | +}); |
| 341 | + |
| 342 | +tape( 'the function returns a strided array function interface', function test( t ) { |
| 343 | + var types; |
| 344 | + var data; |
| 345 | + var f; |
| 346 | + |
| 347 | + types = [ 'float64', 'float64' ]; |
| 348 | + data = [ abs ]; |
| 349 | + |
| 350 | + f = dispatchBy( unaryBy, types, data, 8, 1, 1 ); |
| 351 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 352 | + |
| 353 | + f = dispatchBy( unaryBy, types, data, 10, 1, 1 ); |
| 354 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 355 | + |
| 356 | + f = dispatchBy( [ unaryBy ], types, data, 8, 1, 1 ); |
| 357 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 358 | + |
| 359 | + f = dispatchBy( [ unaryBy ], types, data, 10, 1, 1 ); |
| 360 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 361 | + |
| 362 | + f = dispatchBy( fill( 3 ), types, null, 8, 1, 1 ); |
| 363 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 364 | + |
| 365 | + f = dispatchBy( fill( 3 ), types, null, 10, 1, 1 ); |
| 366 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 367 | + |
| 368 | + f = dispatchBy( [ fill( 3 ) ], types, null, 8, 1, 1 ); |
| 369 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 370 | + |
| 371 | + f = dispatchBy( [ fill( 3 ) ], types, null, 10, 1, 1 ); |
| 372 | + t.strictEqual( isFunction( f ), true, 'returns expected value' ); |
| 373 | + |
| 374 | + t.end(); |
| 375 | +}); |
0 commit comments