1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import ind2sub = require( './index' );
// TESTS //
// The function returns an array of numbers...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
ind2sub( shape, idx, { 'mode': 'throw' } ); // $ExpectType number[]
ind2sub( shape, idx, { 'order': 'row-major' } ); // $ExpectType number[]
}
// The function does not compile if provided a first argument which is not an array-like object containing numbers...
{
const idx = 17;
ind2sub( true, idx ); // $ExpectError
ind2sub( false, idx ); // $ExpectError
ind2sub( null, idx ); // $ExpectError
ind2sub( undefined, idx ); // $ExpectError
ind2sub( '5', idx ); // $ExpectError
ind2sub( [ '1', '2' ], idx ); // $ExpectError
ind2sub( {}, idx ); // $ExpectError
ind2sub( ( x: number ): number => x, idx ); // $ExpectError
}
// The function does not compile if provided a second argument which is not a number...
{
const shape = [ 3, 3, 3 ];
ind2sub( shape, 'abc' ); // $ExpectError
ind2sub( shape, true ); // $ExpectError
ind2sub( shape, false ); // $ExpectError
ind2sub( shape, null ); // $ExpectError
ind2sub( shape, undefined ); // $ExpectError
ind2sub( shape, [] ); // $ExpectError
ind2sub( shape, {} ); // $ExpectError
ind2sub( shape, ( x: number ): number => x ); // $ExpectError
}
// The function does not compile if provided a third argument which is not an options object...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
ind2sub( shape, idx, 'abc' ); // $ExpectError
ind2sub( shape, idx, 123 ); // $ExpectError
ind2sub( shape, idx, true ); // $ExpectError
ind2sub( shape, idx, false ); // $ExpectError
ind2sub( shape, idx, null ); // $ExpectError
ind2sub( shape, idx, [] ); // $ExpectError
ind2sub( shape, idx, ( x: number ): number => x ); // $ExpectError
}
// The compiler throws an error if the function is provided an `order` option which is not a recognized order...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
ind2sub( shape, idx, { 'order': 'abc' } ); // $ExpectError
ind2sub( shape, idx, { 'order': 123 } ); // $ExpectError
ind2sub( shape, idx, { 'order': true } ); // $ExpectError
ind2sub( shape, idx, { 'order': false } ); // $ExpectError
ind2sub( shape, idx, { 'order': null } ); // $ExpectError
ind2sub( shape, idx, { 'order': [] } ); // $ExpectError
ind2sub( shape, idx, { 'order': {} } ); // $ExpectError
ind2sub( shape, idx, { 'order': ( x: number ): number => x } ); // $ExpectError
}
// The compiler throws an error if the function is provided a `mode` option which is not a recognized mode...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
ind2sub( shape, idx, { 'mode': 'abc' } ); // $ExpectError
ind2sub( shape, idx, { 'mode': 123 } ); // $ExpectError
ind2sub( shape, idx, { 'mode': true } ); // $ExpectError
ind2sub( shape, idx, { 'mode': false } ); // $ExpectError
ind2sub( shape, idx, { 'mode': null } ); // $ExpectError
ind2sub( shape, idx, { 'mode': [] } ); // $ExpectError
ind2sub( shape, idx, { 'mode': {} } ); // $ExpectError
ind2sub( shape, idx, { 'mode': ( x: number ): number => x } ); // $ExpectError
}
// The function does not compile if provided an invalid number of arguments...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
ind2sub(); // $ExpectError
ind2sub( shape ); // $ExpectError
ind2sub( shape, idx, {}, {} ); // $ExpectError
}
// Attached to main export is a `assign` method which returns an array of numbers...
{
const shape = [ 3, 3, 3 ];
const out = [ 0, 0, 0 ];
ind2sub.assign( shape, 0, out ); // $ExpectType number[]
ind2sub.assign( shape, 0, { 'mode': 'throw' }, out ); // $ExpectType number[]
}
// The `assign` method does not compile if provided a first argument which is not an array-like object containing numbers...
{
const idx = 17;
const out = [ 0, 0, 0 ];
ind2sub.assign( 123, idx, out ); // $ExpectError
ind2sub.assign( true, idx, out ); // $ExpectError
ind2sub.assign( false, idx, out ); // $ExpectError
ind2sub.assign( null, idx, out ); // $ExpectError
ind2sub.assign( undefined, idx, out ); // $ExpectError
ind2sub.assign( '5', idx, out ); // $ExpectError
ind2sub.assign( [ '1', '2' ], idx, out ); // $ExpectError
ind2sub.assign( {}, idx, out ); // $ExpectError
ind2sub.assign( ( x: number ): number => x, idx, out ); // $ExpectError
const opts = {
'order': 'row-major'
};
ind2sub.assign( 123, idx, opts, out ); // $ExpectError
ind2sub.assign( true, idx, opts, out ); // $ExpectError
ind2sub.assign( false, idx, opts, out ); // $ExpectError
ind2sub.assign( null, idx, opts, out ); // $ExpectError
ind2sub.assign( undefined, idx, opts, out ); // $ExpectError
ind2sub.assign( '5', idx, opts, out ); // $ExpectError
ind2sub.assign( [ '1', '2' ], idx, opts, out ); // $ExpectError
ind2sub.assign( {}, idx, opts, out ); // $ExpectError
ind2sub.assign( ( x: number ): number => x, idx, opts, out ); // $ExpectError
}
// The `assign` method does not compile if provided a second argument which is not a number...
{
const shape = [ 3, 3, 3 ];
const out = [ 0, 0, 0 ];
ind2sub.assign( shape, true, out ); // $ExpectError
ind2sub.assign( shape, false, out ); // $ExpectError
ind2sub.assign( shape, null, out ); // $ExpectError
ind2sub.assign( shape, undefined, out ); // $ExpectError
ind2sub.assign( shape, 'abc', out ); // $ExpectError
ind2sub.assign( shape, [ '1', '2' ], out ); // $ExpectError
ind2sub.assign( shape, {}, out ); // $ExpectError
ind2sub.assign( shape, ( x: number ): number => x, out ); // $ExpectError
const opts = {
'order': 'row-major'
};
ind2sub.assign( shape, true opts, out ); // $ExpectError
ind2sub.assign( shape, false opts, out ); // $ExpectError
ind2sub.assign( shape, null opts, out ); // $ExpectError
ind2sub.assign( shape, undefined opts, out ); // $ExpectError
ind2sub.assign( shape, 'abc' opts, out ); // $ExpectError
ind2sub.assign( shape, [ '1', '2' ] opts, out ); // $ExpectError
ind2sub.assign( shape, {} opts, out ); // $ExpectError
ind2sub.assign( shape, ( x: number ): number => x opts, out ); // $ExpectError
}
// The `assign` method does not compile if provided an options argument which is not an options object...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
const out = [ 0, 0, 0 ];
ind2sub.assign( shape, idx, 'abc', out ); // $ExpectError
ind2sub.assign( shape, idx, 123, out ); // $ExpectError
ind2sub.assign( shape, idx, true, out ); // $ExpectError
ind2sub.assign( shape, idx, false, out ); // $ExpectError
ind2sub.assign( shape, idx, null, out ); // $ExpectError
ind2sub.assign( shape, idx, undefined, out ); // $ExpectError
ind2sub.assign( shape, idx, [], out ); // $ExpectError
ind2sub.assign( shape, idx, ( x: number ): number => x, out ); // $ExpectError
}
// The compiler throws an error if the `assign` method is provided an `order` option which is not a recognized order...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
const out = [ 0, 0, 0 ];
ind2sub.assign( shape, idx, { 'order': 'abc' }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': 123 }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': true }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': false }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': null }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': [] }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': {} }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'order': ( x: number ): number => x }, out ); // $ExpectError
}
// The compiler throws an error if the `assign` method is provided a `mode` option which is not a recognized mode...
{
const shape = [ 3, 3, 3 ];
const idx = 17;
const out = [ 0, 0, 0 ];
ind2sub.assign( shape, idx, { 'mode': 'abc' }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': 123 }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': true }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': false }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': null }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': [] }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': {} }, out ); // $ExpectError
ind2sub.assign( shape, idx, { 'mode': ( x: number ): number => x }, out ); // $ExpectError
}
|