|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
| 25 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 26 | +var factory = require( './../lib' ); |
| 27 | + |
| 28 | + |
| 29 | +// TESTS // |
| 30 | + |
| 31 | +tape( 'main export is a function', function test( t ) { |
| 32 | + t.ok( true, __filename ); |
| 33 | + t.strictEqual( typeof factory, 'function', 'main export is a function' ); |
| 34 | + t.end(); |
| 35 | +}); |
| 36 | + |
| 37 | +tape( 'the function returns a function', function test( t ) { |
| 38 | + var ctor = factory( 'float64' ); |
| 39 | + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'attached to the prototype of the returned function is an `every` method', function test( t ) { |
| 44 | + var ctor = factory( 'float64' ); |
| 45 | + t.strictEqual( hasOwnProp( ctor.prototype, 'every' ), true, 'returns expected value' ); |
| 46 | + t.strictEqual( isFunction( ctor.prototype.every ), true, 'returns expected value' ); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { |
| 51 | + var values; |
| 52 | + var ctor; |
| 53 | + var arr; |
| 54 | + var i; |
| 55 | + |
| 56 | + ctor = factory( 'float64' ); |
| 57 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 58 | + |
| 59 | + values = [ |
| 60 | + '5', |
| 61 | + 5, |
| 62 | + NaN, |
| 63 | + true, |
| 64 | + false, |
| 65 | + null, |
| 66 | + void 0, |
| 67 | + {}, |
| 68 | + [], |
| 69 | + function noop() {} |
| 70 | + ]; |
| 71 | + for ( i = 0; i < values.length; i++ ) { |
| 72 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 73 | + } |
| 74 | + t.end(); |
| 75 | + |
| 76 | + function badValue( value ) { |
| 77 | + return function badValue() { |
| 78 | + return arr.every.call( value, predicate ); |
| 79 | + }; |
| 80 | + } |
| 81 | + function predicate( v ) { |
| 82 | + return v < 0; |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +tape( 'the method throws an error if provided a first arguement which is not a function', function test( t ) { |
| 87 | + var values; |
| 88 | + var ctor; |
| 89 | + var arr; |
| 90 | + var i; |
| 91 | + |
| 92 | + ctor = factory( 'float64' ); |
| 93 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 94 | + |
| 95 | + values = [ |
| 96 | + '5', |
| 97 | + 3.14, |
| 98 | + NaN, |
| 99 | + true, |
| 100 | + false, |
| 101 | + null, |
| 102 | + void 0, |
| 103 | + {}, |
| 104 | + [] |
| 105 | + ]; |
| 106 | + for ( i = 0; i < values.length; i++ ) { |
| 107 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 108 | + } |
| 109 | + t.end(); |
| 110 | + |
| 111 | + function badValue( value ) { |
| 112 | + return function badValue() { |
| 113 | + return arr.every( value ); |
| 114 | + }; |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +tape( 'the method returns `true` if operating on an empty boolean array', function test( t ) { |
| 119 | + var bool; |
| 120 | + var ctor; |
| 121 | + var arr; |
| 122 | + |
| 123 | + ctor = factory( 'float64' ); |
| 124 | + arr = new ctor( 'little-endian' ); |
| 125 | + bool = arr.every( predicate ); |
| 126 | + |
| 127 | + t.strictEqual( bool, true, 'returns expected value' ); |
| 128 | + t.end(); |
| 129 | + |
| 130 | + function predicate() { |
| 131 | + t.fail( 'should not be invoked' ); |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +tape( 'the method returns `true` if all elements pass a test', function test( t ) { |
| 136 | + var bool; |
| 137 | + var ctor; |
| 138 | + var arr; |
| 139 | + |
| 140 | + ctor = factory( 'float64' ); |
| 141 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 142 | + bool = arr.every( predicate ); |
| 143 | + |
| 144 | + t.strictEqual( bool, true, 'returns expected value' ); |
| 145 | + t.end(); |
| 146 | + |
| 147 | + function predicate( v ) { |
| 148 | + return v > 0; |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +tape( 'the method returns `false` if one or more elements fail a test', function test( t ) { |
| 153 | + var bool; |
| 154 | + var ctor; |
| 155 | + var arr; |
| 156 | + |
| 157 | + ctor = factory( 'float64' ); |
| 158 | + arr = new ctor( 'little-endian', [ -1.0, 2.0, -3.0, 4.0, -5.0 ] ); |
| 159 | + bool = arr.every( predicate ); |
| 160 | + |
| 161 | + t.strictEqual( bool, false, 'returns expected value' ); |
| 162 | + t.end(); |
| 163 | + |
| 164 | + function predicate( v ) { |
| 165 | + return v > 0; |
| 166 | + } |
| 167 | +}); |
| 168 | + |
| 169 | +tape( 'the method supports providing an execution context', function test( t ) { |
| 170 | + var bool; |
| 171 | + var ctor; |
| 172 | + var arr; |
| 173 | + var ctx; |
| 174 | + |
| 175 | + ctor = factory( 'float64' ); |
| 176 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 177 | + ctx = { |
| 178 | + 'count': 0 |
| 179 | + }; |
| 180 | + bool = arr.every( predicate, ctx ); |
| 181 | + |
| 182 | + t.strictEqual( bool, true, 'returns expected value' ); |
| 183 | + t.strictEqual( ctx.count, 5, 'returns expected value' ); |
| 184 | + |
| 185 | + t.end(); |
| 186 | + |
| 187 | + function predicate( v ) { |
| 188 | + this.count += 1; // eslint-disable-line no-invalid-this |
| 189 | + return v > 0; |
| 190 | + } |
| 191 | +}); |
0 commit comments