|
| 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 randu = require( '@stdlib/random/iter/randu' ); |
| 20 | +import iterator2arrayview = require( './index' ); |
| 21 | + |
| 22 | +/** |
| 23 | +* Multiplies a value by 10. |
| 24 | +* |
| 25 | +* @param v - iterated value |
| 26 | +* @returns new value |
| 27 | +*/ |
| 28 | +function times10( v: number ): number { |
| 29 | + return v * 10.0; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +// TESTS // |
| 34 | + |
| 35 | +// The function returns a collection... |
| 36 | +{ |
| 37 | + const iter = randu( { 'iter': 10 } ); |
| 38 | + const out = new Float64Array( 10 ); |
| 39 | + iterator2arrayview( iter, out ); // $ExpectType Collection |
| 40 | + iterator2arrayview( iter, out, times10 ); // $ExpectType Collection |
| 41 | + iterator2arrayview( iter, out, times10, {} ); // $ExpectType Collection |
| 42 | + iterator2arrayview( iter, out, 0, 10, times10 ); // $ExpectType Collection |
| 43 | + iterator2arrayview( iter, out, 0, 10, times10, {} ); // $ExpectType Collection |
| 44 | +} |
| 45 | + |
| 46 | +// The compiler throws an error if the function is provided a first argument which is not an iterator... |
| 47 | +{ |
| 48 | + iterator2arrayview( 'abc', [] ); // $ExpectError |
| 49 | + iterator2arrayview( 123, [] ); // $ExpectError |
| 50 | + iterator2arrayview( true, [] ); // $ExpectError |
| 51 | + iterator2arrayview( false, [] ); // $ExpectError |
| 52 | + iterator2arrayview( [], [] ); // $ExpectError |
| 53 | + iterator2arrayview( {}, [] ); // $ExpectError |
| 54 | + iterator2arrayview( null, [] ); // $ExpectError |
| 55 | + iterator2arrayview( undefined, [] ); // $ExpectError |
| 56 | +} |
| 57 | + |
| 58 | +// The compiler throws an error if the function is provided a second argument which is not a collection... |
| 59 | +{ |
| 60 | + const iter = randu( { 'iter': 10 } ); |
| 61 | + iterator2arrayview( iter, 123 ); // $ExpectError |
| 62 | + iterator2arrayview( iter, {} ); // $ExpectError |
| 63 | + iterator2arrayview( iter, true ); // $ExpectError |
| 64 | + iterator2arrayview( iter, false ); // $ExpectError |
| 65 | + iterator2arrayview( iter, null ); // $ExpectError |
| 66 | +} |
| 67 | + |
| 68 | +// The compiler throws an error if the function is provided a third argument which is not a number or map function... |
| 69 | +{ |
| 70 | + const iter = randu( { 'iter': 10 } ); |
| 71 | + iterator2arrayview( iter, [], 'abc' ); // $ExpectError |
| 72 | + iterator2arrayview( iter, [], [] ); // $ExpectError |
| 73 | + iterator2arrayview( iter, [], {} ); // $ExpectError |
| 74 | + iterator2arrayview( iter, [], true ); // $ExpectError |
| 75 | + iterator2arrayview( iter, [], false ); // $ExpectError |
| 76 | + iterator2arrayview( iter, [], null ); // $ExpectError |
| 77 | +} |
| 78 | + |
| 79 | +// The compiler throws an error if the function is provided a `begin` argument and a fourth argument which is not a number or map function... |
| 80 | +{ |
| 81 | + const iter = randu( { 'iter': 10 } ); |
| 82 | + iterator2arrayview( iter, [], 2, 'abc' ); // $ExpectError |
| 83 | + iterator2arrayview( iter, [], 2, [] ); // $ExpectError |
| 84 | + iterator2arrayview( iter, [], 2, {} ); // $ExpectError |
| 85 | + iterator2arrayview( iter, [], 2, true ); // $ExpectError |
| 86 | + iterator2arrayview( iter, [], 2, false ); // $ExpectError |
| 87 | + iterator2arrayview( iter, [], 2, null ); // $ExpectError |
| 88 | +} |
| 89 | + |
| 90 | +// The compiler throws an error if the function is provided an unsupported number of arguments... |
| 91 | +{ |
| 92 | + const iter = randu( { 'iter': 10 } ); |
| 93 | + iterator2arrayview(); // $ExpectError |
| 94 | + iterator2arrayview( iter ); // $ExpectError |
| 95 | + iterator2arrayview( iter, [], 0, 10, times10, {}, 123 ); // $ExpectError |
| 96 | +} |
0 commit comments