|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2023 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 caddf = require( '@stdlib/math-base-ops-caddf' ); |
| 27 | +var instanceOf = require( '@stdlib/assert-instance-of' ); |
| 28 | +var realf = require( '@stdlib/complex-realf' ); |
| 29 | +var imagf = require( '@stdlib/complex-imagf' ); |
| 30 | +var Complex64 = require('@stdlib/complex-float32'); |
| 31 | +var Complex64Array = require( './../lib' ); |
| 32 | + |
| 33 | + |
| 34 | +// TESTS // |
| 35 | + |
| 36 | +tape( 'main export is a function', function test( t ) { |
| 37 | + t.ok( true, __filename ); |
| 38 | + t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' ); |
| 39 | + t.end(); |
| 40 | +}); |
| 41 | + |
| 42 | +tape( 'attached to the prototype of the main export is a `reduce` method', function test( t ) { |
| 43 | + t.strictEqual( hasOwnProp( Complex64Array.prototype, 'reduce' ), true, 'has property' ); |
| 44 | + t.strictEqual( isFunction( Complex64Array.prototype.reduce ), true, 'has method' ); |
| 45 | + t.end(); |
| 46 | +}); |
| 47 | + |
| 48 | +tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) { |
| 49 | + var values; |
| 50 | + var arr; |
| 51 | + var i; |
| 52 | + |
| 53 | + arr = new Complex64Array( 5 ); |
| 54 | + |
| 55 | + values = [ |
| 56 | + '5', |
| 57 | + 5, |
| 58 | + NaN, |
| 59 | + true, |
| 60 | + false, |
| 61 | + null, |
| 62 | + void 0, |
| 63 | + {}, |
| 64 | + [], |
| 65 | + function noop() {} |
| 66 | + ]; |
| 67 | + for ( i = 0; i < values.length; i++ ) { |
| 68 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 69 | + } |
| 70 | + t.end(); |
| 71 | + |
| 72 | + function badValue( value ) { |
| 73 | + return function badValue() { |
| 74 | + return arr.map.reduce( value, caddf ); |
| 75 | + }; |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { |
| 80 | + var values; |
| 81 | + var arr; |
| 82 | + var i; |
| 83 | + |
| 84 | + arr = new Complex64Array( 10 ); |
| 85 | + |
| 86 | + values = [ |
| 87 | + '5', |
| 88 | + 3.14, |
| 89 | + NaN, |
| 90 | + true, |
| 91 | + false, |
| 92 | + null, |
| 93 | + void 0, |
| 94 | + {}, |
| 95 | + [] |
| 96 | + ]; |
| 97 | + for ( i = 0; i < values.length; i++ ) { |
| 98 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 99 | + } |
| 100 | + t.end(); |
| 101 | + |
| 102 | + function badValue( value ) { |
| 103 | + return function badValue() { |
| 104 | + return arr.reduce( value ); |
| 105 | + }; |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +tape( 'the method throws an error if not provided an initial value when operating on an empty complex number array', function test( t ) { |
| 110 | + var arr; |
| 111 | + |
| 112 | + arr = new Complex64Array( 0 ); |
| 113 | + t.throws( foo, Error, 'throws an error' ); |
| 114 | + t.end(); |
| 115 | + |
| 116 | + function foo() { |
| 117 | + arr.reduce( caddf ); |
| 118 | + } |
| 119 | +}); |
| 120 | + |
| 121 | +tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) { |
| 122 | + var valueArray; |
| 123 | + var accArray; |
| 124 | + var expected; |
| 125 | + var actual; |
| 126 | + var arr; |
| 127 | + |
| 128 | + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); |
| 129 | + accArray = [ 1.0, -1.0, 3.0, -3.0 ]; |
| 130 | + valueArray = [ 2.0, -2.0, 3.0, -3.0 ]; |
| 131 | + expected = new Complex64( 6.0, -6.0 ); |
| 132 | + actual = arr.reduce( reducer ); |
| 133 | + |
| 134 | + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); |
| 135 | + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); |
| 136 | + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); |
| 137 | + |
| 138 | + t.end(); |
| 139 | + |
| 140 | + function reducer( acc, value, index ) { |
| 141 | + var ind = 2*(index-1); |
| 142 | + t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); |
| 143 | + t.strictEqual( realf( acc ), accArray[ ind ], 'returns expected value' ); |
| 144 | + t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'returns expected value' ); |
| 145 | + t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' ); |
| 146 | + t.strictEqual( realf( value ), valueArray[ ind ], 'returns expected value' ); |
| 147 | + t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'returns expected value' ); |
| 148 | + return caddf( acc, value ); |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +tape( 'the method supports providing an initial value as the second argument', function test( t ) { |
| 153 | + var valueArray; |
| 154 | + var accArray; |
| 155 | + var expected; |
| 156 | + var actual; |
| 157 | + var arr; |
| 158 | + |
| 159 | + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); |
| 160 | + accArray = [ 2.0, -2.0, 3.0, -3.0, 5.0, -5.0 ]; |
| 161 | + valueArray = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ]; |
| 162 | + expected = new Complex64( 8.0, -8.0 ); |
| 163 | + actual = arr.reduce( reducer, new Complex64( 2.0, -2.0 ) ); |
| 164 | + |
| 165 | + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); |
| 166 | + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); |
| 167 | + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); |
| 168 | + |
| 169 | + t.end(); |
| 170 | + |
| 171 | + function reducer( acc, value, index ) { |
| 172 | + t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); |
| 173 | + t.strictEqual( realf( acc ), accArray[ 2*index ], 'returns expected value' ); |
| 174 | + t.strictEqual( imagf( acc ), accArray[ (2*index)+1 ], 'returns expected value' ); |
| 175 | + t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' ); |
| 176 | + t.strictEqual( realf( value ), valueArray[ 2*index ], 'returns expected value' ); |
| 177 | + t.strictEqual( imagf( value ), valueArray[ (2*index)+1 ], 'returns expected value' ); |
| 178 | + return caddf( acc, value ); |
| 179 | + } |
| 180 | +}); |
| 181 | + |
| 182 | +tape( 'the method returns the accumulated result', function test( t ) { |
| 183 | + var expected; |
| 184 | + var actual; |
| 185 | + var arr; |
| 186 | + |
| 187 | + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); |
| 188 | + expected = new Complex64( 6.0, -6.0 ); |
| 189 | + actual = arr.reduce( caddf ); |
| 190 | + |
| 191 | + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); |
| 192 | + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); |
| 193 | + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); |
| 194 | + t.end(); |
| 195 | +}); |
| 196 | + |
| 197 | +tape( 'the method supports returning real-valued results', function test( t ) { |
| 198 | + var expected; |
| 199 | + var actual; |
| 200 | + var arr; |
| 201 | + |
| 202 | + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); |
| 203 | + expected = 6.0; |
| 204 | + actual = arr.reduce( reducer, 0.0 ); |
| 205 | + |
| 206 | + t.strictEqual( actual, expected, 'returns expected value' ); |
| 207 | + t.end(); |
| 208 | + |
| 209 | + function reducer( acc, value ) { |
| 210 | + return acc + realf( value ); |
| 211 | + } |
| 212 | +}); |
0 commit comments