|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2026 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 hasProp = require( '@stdlib/assert/has-property' ); |
| 25 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 26 | +var isArray = require( '@stdlib/assert/is-array' ); |
| 27 | +var Float32Array = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// TESTS // |
| 31 | + |
| 32 | +tape( 'main export is a function', function test( t ) { |
| 33 | + t.ok( true, __filename ); |
| 34 | + t.strictEqual( typeof Float32Array, 'function', 'main export is a function' ); |
| 35 | + t.end(); |
| 36 | +}); |
| 37 | + |
| 38 | +tape( 'a typed array instance has an `entries` method for returning an iterator for iterating over array key-value pairs', function test( t ) { |
| 39 | + var arr = new Float32Array( 2 ); |
| 40 | + t.strictEqual( hasProp( arr, 'entries' ), true, 'has property' ); |
| 41 | + t.strictEqual( isFunction( arr.entries ), true, 'has method' ); |
| 42 | + t.end(); |
| 43 | +}); |
| 44 | + |
| 45 | +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { |
| 46 | + var values; |
| 47 | + var arr; |
| 48 | + var i; |
| 49 | + |
| 50 | + arr = new Float32Array( 5 ); |
| 51 | + |
| 52 | + values = [ |
| 53 | + '5', |
| 54 | + 5, |
| 55 | + NaN, |
| 56 | + true, |
| 57 | + false, |
| 58 | + null, |
| 59 | + void 0, |
| 60 | + {}, |
| 61 | + [], |
| 62 | + function noop() {} |
| 63 | + ]; |
| 64 | + for ( i = 0; i < values.length; i++ ) { |
| 65 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 66 | + } |
| 67 | + t.end(); |
| 68 | + |
| 69 | + function badValue( value ) { |
| 70 | + return function badValue() { |
| 71 | + return arr.entries.call( value ); |
| 72 | + }; |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { |
| 77 | + var arr; |
| 78 | + var it; |
| 79 | + var v; |
| 80 | + var i; |
| 81 | + |
| 82 | + arr = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 83 | + |
| 84 | + it = arr.entries(); |
| 85 | + t.strictEqual( typeof it.next, 'function', 'has next method' ); |
| 86 | + |
| 87 | + for ( i = 0; i < arr.length; i++ ) { |
| 88 | + v = it.next(); |
| 89 | + t.strictEqual( isArray( v.value ), true, 'returns expected value' ); |
| 90 | + t.strictEqual( v.value[ 0 ], i, 'returns expected index' ); |
| 91 | + t.strictEqual( v.value[ 1 ], arr[ i ], 'returns expected value' ); |
| 92 | + t.strictEqual( typeof v.done, 'boolean', 'returns expected value' ); |
| 93 | + } |
| 94 | + t.end(); |
| 95 | +}); |
| 96 | + |
| 97 | +tape( 'the method returns an iterator which indicates completion once all elements have been iterated', function test( t ) { |
| 98 | + var arr; |
| 99 | + var it; |
| 100 | + var v; |
| 101 | + |
| 102 | + arr = new Float32Array( [ 1.0, 2.0 ] ); |
| 103 | + it = arr.entries(); |
| 104 | + |
| 105 | + v = it.next(); |
| 106 | + t.strictEqual( v.done, false, 'returns expected value' ); |
| 107 | + |
| 108 | + v = it.next(); |
| 109 | + t.strictEqual( v.done, false, 'returns expected value' ); |
| 110 | + |
| 111 | + v = it.next(); |
| 112 | + t.strictEqual( v.done, true, 'returns expected value' ); |
| 113 | + t.strictEqual( v.value, void 0, 'returns expected value' ); |
| 114 | + |
| 115 | + t.end(); |
| 116 | +}); |
0 commit comments