|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2021 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 | +import ndarray2array = require( './index' ); |
| 20 | + |
| 21 | + |
| 22 | +// TESTS // |
| 23 | + |
| 24 | +// The function returns an array... |
| 25 | +{ |
| 26 | + ndarray2array( [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); // $ExpectType any[] |
| 27 | + ndarray2array( [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); // $ExpectType any[] |
| 28 | +} |
| 29 | + |
| 30 | +// The function does not compile if provided a first argument which is not an array-like object or buffer... |
| 31 | +{ |
| 32 | + const shape = [ 2, 2 ]; |
| 33 | + const strides = [ 2, 1 ]; |
| 34 | + const offset = 0; |
| 35 | + const order = 'row-major'; |
| 36 | + ndarray2array( 123, shape, strides, offset, order ); // $ExpectError |
| 37 | + ndarray2array( true, shape, strides, offset, order ); // $ExpectError |
| 38 | + ndarray2array( false, shape, strides, offset, order ); // $ExpectError |
| 39 | + ndarray2array( null, shape, strides, offset, order ); // $ExpectError |
| 40 | + ndarray2array( undefined, shape, strides, offset, order ); // $ExpectError |
| 41 | +} |
| 42 | + |
| 43 | +// The function does not compile if provided a second argument which is not an array-like object containing numbers... |
| 44 | +{ |
| 45 | + const buffer = [ 1, 2, 3, 4 ]; |
| 46 | + const strides = [ 2, 1 ]; |
| 47 | + const offset = 0; |
| 48 | + const order = 'row-major'; |
| 49 | + ndarray2array( buffer, true, strides, offset, order ); // $ExpectError |
| 50 | + ndarray2array( buffer, false, strides, offset, order ); // $ExpectError |
| 51 | + ndarray2array( buffer, null, strides, offset, order ); // $ExpectError |
| 52 | + ndarray2array( buffer, undefined, strides, offset, order ); // $ExpectError |
| 53 | + ndarray2array( buffer, '5', strides, offset, order ); // $ExpectError |
| 54 | + ndarray2array( buffer, [ '1', '2' ], strides, offset, order ); // $ExpectError |
| 55 | + ndarray2array( buffer, {}, strides, offset, order ); // $ExpectError |
| 56 | + ndarray2array( buffer, ( x: number ): number => x, strides, offset, order ); // $ExpectError |
| 57 | +} |
| 58 | + |
| 59 | +// The function does not compile if provided a third argument which is not an array-like object containing numbers... |
| 60 | +{ |
| 61 | + const buffer = [ 1, 2, 3, 4 ]; |
| 62 | + const shape = [ 2, 2 ]; |
| 63 | + const offset = 0; |
| 64 | + const order = 'row-major'; |
| 65 | + ndarray2array( buffer, shape, true, offset, order ); // $ExpectError |
| 66 | + ndarray2array( buffer, shape, false, offset, order ); // $ExpectError |
| 67 | + ndarray2array( buffer, shape, null, offset, order ); // $ExpectError |
| 68 | + ndarray2array( buffer, shape, undefined, offset, order ); // $ExpectError |
| 69 | + ndarray2array( buffer, shape, '5', offset, order ); // $ExpectError |
| 70 | + ndarray2array( buffer, shape, [ '1', '2' ], offset, order ); // $ExpectError |
| 71 | + ndarray2array( buffer, shape, {}, offset, order ); // $ExpectError |
| 72 | + ndarray2array( buffer, shape, ( x: number ): number => x, offset, order ); // $ExpectError |
| 73 | +} |
| 74 | + |
| 75 | +// The function does not compile if provided a fourth argument which is not a number... |
| 76 | +{ |
| 77 | + const buffer = [ 1, 2, 3, 4 ]; |
| 78 | + const shape = [ 2, 2 ]; |
| 79 | + const strides = [ 2, 1 ]; |
| 80 | + const order = 'row-major'; |
| 81 | + ndarray2array( buffer, shape, strides, true, order ); // $ExpectError |
| 82 | + ndarray2array( buffer, shape, strides, false, order ); // $ExpectError |
| 83 | + ndarray2array( buffer, shape, strides, null, order ); // $ExpectError |
| 84 | + ndarray2array( buffer, shape, strides, undefined, order ); // $ExpectError |
| 85 | + ndarray2array( buffer, shape, strides, '5', order ); // $ExpectError |
| 86 | + ndarray2array( buffer, shape, strides, [ '1', '2' ], order ); // $ExpectError |
| 87 | + ndarray2array( buffer, shape, strides, {}, order ); // $ExpectError |
| 88 | + ndarray2array( buffer, shape, strides, ( x: number ): number => x, order ); // $ExpectError |
| 89 | +} |
| 90 | + |
| 91 | +// The function does not compile if provided a fifth argument which is not a known array order... |
| 92 | +{ |
| 93 | + const buffer = [ 1, 2, 3, 4 ]; |
| 94 | + const shape = [ 2, 2 ]; |
| 95 | + const strides = [ 2, 1 ]; |
| 96 | + const offset = 0; |
| 97 | + ndarray2array( buffer, shape, strides, offset, true ); // $ExpectError |
| 98 | + ndarray2array( buffer, shape, strides, offset, false ); // $ExpectError |
| 99 | + ndarray2array( buffer, shape, strides, offset, null ); // $ExpectError |
| 100 | + ndarray2array( buffer, shape, strides, offset, undefined ); // $ExpectError |
| 101 | + ndarray2array( buffer, shape, strides, offset, '5' ); // $ExpectError |
| 102 | + ndarray2array( buffer, shape, strides, offset, [ '1', '2' ] ); // $ExpectError |
| 103 | + ndarray2array( buffer, shape, strides, offset, {} ); // $ExpectError |
| 104 | + ndarray2array( buffer, shape, strides, offset, ( x: number ): number => x ); // $ExpectError |
| 105 | +} |
| 106 | + |
| 107 | +// The function does not compile if provided insufficient arguments... |
| 108 | +{ |
| 109 | + const buffer = [ 1, 2, 3, 4 ]; |
| 110 | + const shape = [ 2, 2 ]; |
| 111 | + const strides = [ 2, 1 ]; |
| 112 | + const offset = 0; |
| 113 | + ndarray2array(); // $ExpectError |
| 114 | + ndarray2array( buffer ); // $ExpectError |
| 115 | + ndarray2array( buffer, shape ); // $ExpectError |
| 116 | + ndarray2array( buffer, shape, strides ); // $ExpectError |
| 117 | + ndarray2array( buffer, shape, strides, offset ); // $ExpectError |
| 118 | +} |
0 commit comments