|
| 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 a `reduceRight` method', function test( t ) { |
| 44 | + var ctor = factory( 'float64' ); |
| 45 | + t.strictEqual( hasOwnProp( ctor.prototype, 'reduceRight' ), true, 'returns expected value' ); |
| 46 | + t.strictEqual( isFunction( ctor.prototype.reduceRight ), 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.reduceRight.call( value, reducer ); |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + function reducer( acc, value ) { |
| 83 | + if ( value ) { |
| 84 | + return acc + 1; |
| 85 | + } |
| 86 | + return acc; |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { |
| 91 | + var values; |
| 92 | + var ctor; |
| 93 | + var arr; |
| 94 | + var i; |
| 95 | + |
| 96 | + ctor = factory( 'float64' ); |
| 97 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 98 | + |
| 99 | + values = [ |
| 100 | + '5', |
| 101 | + 3.14, |
| 102 | + NaN, |
| 103 | + true, |
| 104 | + false, |
| 105 | + null, |
| 106 | + void 0, |
| 107 | + {}, |
| 108 | + [] |
| 109 | + ]; |
| 110 | + for ( i = 0; i < values.length; i++ ) { |
| 111 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 112 | + } |
| 113 | + t.end(); |
| 114 | + |
| 115 | + function badValue( value ) { |
| 116 | + return function badValue() { |
| 117 | + return arr.reduceRight( value ); |
| 118 | + }; |
| 119 | + } |
| 120 | +}); |
| 121 | + |
| 122 | +tape( 'the method throws an error if not provided an initial value when operating on an empty array', function test( t ) { |
| 123 | + var ctor; |
| 124 | + var arr; |
| 125 | + |
| 126 | + ctor = factory( 'float64' ); |
| 127 | + arr = new ctor( 'little-endian' ); |
| 128 | + t.throws( foo, Error, 'throws an error' ); |
| 129 | + t.end(); |
| 130 | + |
| 131 | + function foo() { |
| 132 | + arr.reduceRight( reducer ); |
| 133 | + } |
| 134 | + |
| 135 | + function reducer( acc, value ) { |
| 136 | + if ( value ) { |
| 137 | + return acc + 1; |
| 138 | + } |
| 139 | + return acc; |
| 140 | + } |
| 141 | +}); |
| 142 | + |
| 143 | +tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) { |
| 144 | + var valueArray; |
| 145 | + var accArray; |
| 146 | + var expected; |
| 147 | + var actual; |
| 148 | + var ctor; |
| 149 | + var arr; |
| 150 | + var ind; |
| 151 | + var len; |
| 152 | + |
| 153 | + ctor = factory( 'float64' ); |
| 154 | + arr = new ctor( 'little-endian', [ 1.0, 0.0, 1.0, 0.0 ] ); |
| 155 | + len = arr.length; |
| 156 | + accArray = [ 0.0, 0.0, 0.0 ]; |
| 157 | + valueArray = [ 1.0, 0.0, 1.0 ]; |
| 158 | + expected = 0.0; |
| 159 | + actual = arr.reduceRight( reducer ); |
| 160 | + |
| 161 | + t.strictEqual( actual, expected, 'returns expected value' ); |
| 162 | + |
| 163 | + t.end(); |
| 164 | + |
| 165 | + function reducer( acc, value, index ) { |
| 166 | + ind = ( len - index - 2 ); |
| 167 | + t.strictEqual( acc, accArray[ ind ], 'returns expected value' ); |
| 168 | + t.strictEqual( value, valueArray[ ind ], 'returns expected value' ); |
| 169 | + return ( acc && value ); |
| 170 | + } |
| 171 | +}); |
| 172 | + |
| 173 | +tape( 'the method supports providing an initial value as the second argument', function test( t ) { |
| 174 | + var valueArray; |
| 175 | + var accArray; |
| 176 | + var expected; |
| 177 | + var actual; |
| 178 | + var ctor; |
| 179 | + var arr; |
| 180 | + var ind; |
| 181 | + var len; |
| 182 | + |
| 183 | + ctor = factory( 'float64' ); |
| 184 | + arr = new ctor( 'little-endian', [ 1.0, 0.0, 0.0, 1.0 ] ); |
| 185 | + len = arr.length; |
| 186 | + accArray = [ 0.0, 1.0, 1.0, 1.0 ]; |
| 187 | + valueArray = [ 1.0, 0.0, 0.0, 1.0 ]; |
| 188 | + expected = 2; |
| 189 | + actual = arr.reduceRight( reducer, 0 ); |
| 190 | + |
| 191 | + t.strictEqual( actual, expected, 'returns expected value' ); |
| 192 | + t.end(); |
| 193 | + |
| 194 | + function reducer( acc, value, index ) { |
| 195 | + ind = ( len - index - 1 ); |
| 196 | + t.strictEqual( acc, accArray[ ind ], 'returns expected value' ); |
| 197 | + t.strictEqual( value, valueArray[ ind ], 'returns expected value' ); |
| 198 | + if ( value ) { |
| 199 | + return acc + 1; |
| 200 | + } |
| 201 | + return acc; |
| 202 | + } |
| 203 | +}); |
| 204 | + |
| 205 | +tape( 'the method returns the accumulated result', function test( t ) { |
| 206 | + var expected; |
| 207 | + var actual; |
| 208 | + var ctor; |
| 209 | + var arr; |
| 210 | + |
| 211 | + ctor = factory( 'float64' ); |
| 212 | + arr = new ctor( 'little-endian', [ 1.0, 0.0, 1.0, 1.0 ] ); |
| 213 | + expected = 0.0; |
| 214 | + actual = arr.reduceRight( reducer ); |
| 215 | + |
| 216 | + t.strictEqual( actual, expected, 'returns expected value' ); |
| 217 | + t.end(); |
| 218 | + |
| 219 | + function reducer( acc, value ) { |
| 220 | + return ( acc && value ); |
| 221 | + } |
| 222 | +}); |
0 commit comments