|
| 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 ind2sub = require( './index' ); |
| 20 | + |
| 21 | + |
| 22 | +// TESTS // |
| 23 | + |
| 24 | +// The function returns an array of numbers... |
| 25 | +{ |
| 26 | + const shape = [ 3, 3, 3 ]; |
| 27 | + const idx = 17; |
| 28 | + ind2sub( shape, idx, { 'mode': 'throw' } ); // $ExpectType number[] |
| 29 | + ind2sub( shape, idx, { 'order': 'row-major' } ); // $ExpectType number[] |
| 30 | +} |
| 31 | + |
| 32 | +// The function does not compile if provided a first argument which is not an array-like object containing numbers... |
| 33 | +{ |
| 34 | + const idx = 17; |
| 35 | + ind2sub( true, idx ); // $ExpectError |
| 36 | + ind2sub( false, idx ); // $ExpectError |
| 37 | + ind2sub( null, idx ); // $ExpectError |
| 38 | + ind2sub( undefined, idx ); // $ExpectError |
| 39 | + ind2sub( '5', idx ); // $ExpectError |
| 40 | + ind2sub( [ '1', '2' ], idx ); // $ExpectError |
| 41 | + ind2sub( {}, idx ); // $ExpectError |
| 42 | + ind2sub( ( x: number ): number => x, idx ); // $ExpectError |
| 43 | +} |
| 44 | + |
| 45 | +// The function does not compile if provided a second argument which is not a number... |
| 46 | +{ |
| 47 | + const shape = [ 3, 3, 3 ]; |
| 48 | + ind2sub( shape, 'abc' ); // $ExpectError |
| 49 | + ind2sub( shape, true ); // $ExpectError |
| 50 | + ind2sub( shape, false ); // $ExpectError |
| 51 | + ind2sub( shape, null ); // $ExpectError |
| 52 | + ind2sub( shape, undefined ); // $ExpectError |
| 53 | + ind2sub( shape, [] ); // $ExpectError |
| 54 | + ind2sub( shape, {} ); // $ExpectError |
| 55 | + ind2sub( shape, ( x: number ): number => x ); // $ExpectError |
| 56 | +} |
| 57 | + |
| 58 | +// The function does not compile if provided a third argument which is not an options object... |
| 59 | +{ |
| 60 | + const shape = [ 3, 3, 3 ]; |
| 61 | + const idx = 17; |
| 62 | + ind2sub( shape, idx, 'abc' ); // $ExpectError |
| 63 | + ind2sub( shape, idx, 123 ); // $ExpectError |
| 64 | + ind2sub( shape, idx, true ); // $ExpectError |
| 65 | + ind2sub( shape, idx, false ); // $ExpectError |
| 66 | + ind2sub( shape, idx, null ); // $ExpectError |
| 67 | + ind2sub( shape, idx, [] ); // $ExpectError |
| 68 | + ind2sub( shape, idx, ( x: number ): number => x ); // $ExpectError |
| 69 | +} |
| 70 | + |
| 71 | +// The compiler throws an error if the function is provided an `order` option which is not a recognized order... |
| 72 | +{ |
| 73 | + const shape = [ 3, 3, 3 ]; |
| 74 | + const idx = 17; |
| 75 | + ind2sub( shape, idx, { 'order': 'abc' } ); // $ExpectError |
| 76 | + ind2sub( shape, idx, { 'order': 123 } ); // $ExpectError |
| 77 | + ind2sub( shape, idx, { 'order': true } ); // $ExpectError |
| 78 | + ind2sub( shape, idx, { 'order': false } ); // $ExpectError |
| 79 | + ind2sub( shape, idx, { 'order': null } ); // $ExpectError |
| 80 | + ind2sub( shape, idx, { 'order': [] } ); // $ExpectError |
| 81 | + ind2sub( shape, idx, { 'order': {} } ); // $ExpectError |
| 82 | + ind2sub( shape, idx, { 'order': ( x: number ): number => x } ); // $ExpectError |
| 83 | +} |
| 84 | + |
| 85 | +// The compiler throws an error if the function is provided a `mode` option which is not a recognized mode... |
| 86 | +{ |
| 87 | + const shape = [ 3, 3, 3 ]; |
| 88 | + const idx = 17; |
| 89 | + ind2sub( shape, idx, { 'mode': 'abc' } ); // $ExpectError |
| 90 | + ind2sub( shape, idx, { 'mode': 123 } ); // $ExpectError |
| 91 | + ind2sub( shape, idx, { 'mode': true } ); // $ExpectError |
| 92 | + ind2sub( shape, idx, { 'mode': false } ); // $ExpectError |
| 93 | + ind2sub( shape, idx, { 'mode': null } ); // $ExpectError |
| 94 | + ind2sub( shape, idx, { 'mode': [] } ); // $ExpectError |
| 95 | + ind2sub( shape, idx, { 'mode': {} } ); // $ExpectError |
| 96 | + ind2sub( shape, idx, { 'mode': ( x: number ): number => x } ); // $ExpectError |
| 97 | +} |
| 98 | + |
| 99 | +// The function does not compile if provided an invalid number of arguments... |
| 100 | +{ |
| 101 | + const shape = [ 3, 3, 3 ]; |
| 102 | + const idx = 17; |
| 103 | + ind2sub(); // $ExpectError |
| 104 | + ind2sub( shape ); // $ExpectError |
| 105 | + ind2sub( shape, idx, {}, {} ); // $ExpectError |
| 106 | +} |
| 107 | + |
| 108 | +// Attached to main export is a `assign` method which returns an array of numbers... |
| 109 | +{ |
| 110 | + const shape = [ 3, 3, 3 ]; |
| 111 | + const out = [ 0, 0, 0 ]; |
| 112 | + ind2sub.assign( shape, 0, out ); // $ExpectType number[] |
| 113 | + ind2sub.assign( shape, 0, { 'mode': 'throw' }, out ); // $ExpectType number[] |
| 114 | +} |
| 115 | + |
| 116 | +// The `assign` method does not compile if provided a first argument which is not an array-like object containing numbers... |
| 117 | +{ |
| 118 | + const idx = 17; |
| 119 | + const out = [ 0, 0, 0 ]; |
| 120 | + ind2sub.assign( 123, idx, out ); // $ExpectError |
| 121 | + ind2sub.assign( true, idx, out ); // $ExpectError |
| 122 | + ind2sub.assign( false, idx, out ); // $ExpectError |
| 123 | + ind2sub.assign( null, idx, out ); // $ExpectError |
| 124 | + ind2sub.assign( undefined, idx, out ); // $ExpectError |
| 125 | + ind2sub.assign( '5', idx, out ); // $ExpectError |
| 126 | + ind2sub.assign( [ '1', '2' ], idx, out ); // $ExpectError |
| 127 | + ind2sub.assign( {}, idx, out ); // $ExpectError |
| 128 | + ind2sub.assign( ( x: number ): number => x, idx, out ); // $ExpectError |
| 129 | + |
| 130 | + const opts = { |
| 131 | + 'order': 'row-major' |
| 132 | + }; |
| 133 | + ind2sub.assign( 123, idx, opts, out ); // $ExpectError |
| 134 | + ind2sub.assign( true, idx, opts, out ); // $ExpectError |
| 135 | + ind2sub.assign( false, idx, opts, out ); // $ExpectError |
| 136 | + ind2sub.assign( null, idx, opts, out ); // $ExpectError |
| 137 | + ind2sub.assign( undefined, idx, opts, out ); // $ExpectError |
| 138 | + ind2sub.assign( '5', idx, opts, out ); // $ExpectError |
| 139 | + ind2sub.assign( [ '1', '2' ], idx, opts, out ); // $ExpectError |
| 140 | + ind2sub.assign( {}, idx, opts, out ); // $ExpectError |
| 141 | + ind2sub.assign( ( x: number ): number => x, idx, opts, out ); // $ExpectError |
| 142 | +} |
| 143 | + |
| 144 | +// The `assign` method does not compile if provided a second argument which is not a number... |
| 145 | +{ |
| 146 | + const shape = [ 3, 3, 3 ]; |
| 147 | + const out = [ 0, 0, 0 ]; |
| 148 | + ind2sub.assign( shape, true, out ); // $ExpectError |
| 149 | + ind2sub.assign( shape, false, out ); // $ExpectError |
| 150 | + ind2sub.assign( shape, null, out ); // $ExpectError |
| 151 | + ind2sub.assign( shape, undefined, out ); // $ExpectError |
| 152 | + ind2sub.assign( shape, 'abc', out ); // $ExpectError |
| 153 | + ind2sub.assign( shape, [ '1', '2' ], out ); // $ExpectError |
| 154 | + ind2sub.assign( shape, {}, out ); // $ExpectError |
| 155 | + ind2sub.assign( shape, ( x: number ): number => x, out ); // $ExpectError |
| 156 | + |
| 157 | + const opts = { |
| 158 | + 'order': 'row-major' |
| 159 | + }; |
| 160 | + ind2sub.assign( shape, true opts, out ); // $ExpectError |
| 161 | + ind2sub.assign( shape, false opts, out ); // $ExpectError |
| 162 | + ind2sub.assign( shape, null opts, out ); // $ExpectError |
| 163 | + ind2sub.assign( shape, undefined opts, out ); // $ExpectError |
| 164 | + ind2sub.assign( shape, 'abc' opts, out ); // $ExpectError |
| 165 | + ind2sub.assign( shape, [ '1', '2' ] opts, out ); // $ExpectError |
| 166 | + ind2sub.assign( shape, {} opts, out ); // $ExpectError |
| 167 | + ind2sub.assign( shape, ( x: number ): number => x opts, out ); // $ExpectError |
| 168 | +} |
| 169 | + |
| 170 | +// The `assign` method does not compile if provided an options argument which is not an options object... |
| 171 | +{ |
| 172 | + const shape = [ 3, 3, 3 ]; |
| 173 | + const idx = 17; |
| 174 | + const out = [ 0, 0, 0 ]; |
| 175 | + ind2sub.assign( shape, idx, 'abc', out ); // $ExpectError |
| 176 | + ind2sub.assign( shape, idx, 123, out ); // $ExpectError |
| 177 | + ind2sub.assign( shape, idx, true, out ); // $ExpectError |
| 178 | + ind2sub.assign( shape, idx, false, out ); // $ExpectError |
| 179 | + ind2sub.assign( shape, idx, null, out ); // $ExpectError |
| 180 | + ind2sub.assign( shape, idx, undefined, out ); // $ExpectError |
| 181 | + ind2sub.assign( shape, idx, [], out ); // $ExpectError |
| 182 | + ind2sub.assign( shape, idx, ( x: number ): number => x, out ); // $ExpectError |
| 183 | +} |
| 184 | + |
| 185 | +// The compiler throws an error if the `assign` method is provided an `order` option which is not a recognized order... |
| 186 | +{ |
| 187 | + const shape = [ 3, 3, 3 ]; |
| 188 | + const idx = 17; |
| 189 | + const out = [ 0, 0, 0 ]; |
| 190 | + ind2sub.assign( shape, idx, { 'order': 'abc' }, out ); // $ExpectError |
| 191 | + ind2sub.assign( shape, idx, { 'order': 123 }, out ); // $ExpectError |
| 192 | + ind2sub.assign( shape, idx, { 'order': true }, out ); // $ExpectError |
| 193 | + ind2sub.assign( shape, idx, { 'order': false }, out ); // $ExpectError |
| 194 | + ind2sub.assign( shape, idx, { 'order': null }, out ); // $ExpectError |
| 195 | + ind2sub.assign( shape, idx, { 'order': [] }, out ); // $ExpectError |
| 196 | + ind2sub.assign( shape, idx, { 'order': {} }, out ); // $ExpectError |
| 197 | + ind2sub.assign( shape, idx, { 'order': ( x: number ): number => x }, out ); // $ExpectError |
| 198 | +} |
| 199 | + |
| 200 | +// The compiler throws an error if the `assign` method is provided a `mode` option which is not a recognized mode... |
| 201 | +{ |
| 202 | + const shape = [ 3, 3, 3 ]; |
| 203 | + const idx = 17; |
| 204 | + const out = [ 0, 0, 0 ]; |
| 205 | + ind2sub.assign( shape, idx, { 'mode': 'abc' }, out ); // $ExpectError |
| 206 | + ind2sub.assign( shape, idx, { 'mode': 123 }, out ); // $ExpectError |
| 207 | + ind2sub.assign( shape, idx, { 'mode': true }, out ); // $ExpectError |
| 208 | + ind2sub.assign( shape, idx, { 'mode': false }, out ); // $ExpectError |
| 209 | + ind2sub.assign( shape, idx, { 'mode': null }, out ); // $ExpectError |
| 210 | + ind2sub.assign( shape, idx, { 'mode': [] }, out ); // $ExpectError |
| 211 | + ind2sub.assign( shape, idx, { 'mode': {} }, out ); // $ExpectError |
| 212 | + ind2sub.assign( shape, idx, { 'mode': ( x: number ): number => x }, out ); // $ExpectError |
| 213 | +} |
0 commit comments