|  | 
|  | 1 | +/** | 
|  | 2 | +* @license Apache-2.0 | 
|  | 3 | +* | 
|  | 4 | +* Copyright (c) 2025 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 Float64Array = require( '@stdlib/array/float64' ); | 
|  | 25 | +var dasumpw = require( './../lib' ); | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | +// TESTS // | 
|  | 29 | + | 
|  | 30 | +tape( 'main export is an object', function test( t ) { | 
|  | 31 | +	t.ok( true, __filename ); | 
|  | 32 | +	t.strictEqual( typeof dasumpw, 'object', 'main export is an object' ); | 
|  | 33 | +	t.end(); | 
|  | 34 | +}); | 
|  | 35 | + | 
|  | 36 | +tape( 'the function has an arity of 4', function test( t ) { | 
|  | 37 | +	t.strictEqual( dasumpw.ndarray.length, 4, 'returns expected value' ); | 
|  | 38 | +	t.end(); | 
|  | 39 | +}); | 
|  | 40 | + | 
|  | 41 | +tape( 'the function calculates the sum of absolute values', function test( t ) { | 
|  | 42 | +	var x; | 
|  | 43 | +	var v; | 
|  | 44 | +	var i; | 
|  | 45 | + | 
|  | 46 | +	x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, NaN, 0.0, NaN, 3.0 ] ); | 
|  | 47 | +	v = dasumpw.ndarray( x.length, x, 1, 0 ); | 
|  | 48 | +	t.strictEqual( v, 3.0, 'returns expected value' ); | 
|  | 49 | + | 
|  | 50 | +	x = new Float64Array( [ 1.0, -2.0, NaN, 5.0, 0.0, 3.0 ] ); | 
|  | 51 | +	v = dasumpw.ndarray( x.length, x, 1, 0 ); | 
|  | 52 | +	t.strictEqual( v, 7.0, 'returns expected value' ); | 
|  | 53 | + | 
|  | 54 | +	x = new Float64Array( [ 4.0, NaN ] ); | 
|  | 55 | +	v = dasumpw.ndarray( x.length, x, 1, 0 ); | 
|  | 56 | +	t.strictEqual( v, 4.0, 'returns expected value' ); | 
|  | 57 | + | 
|  | 58 | +	x = new Float64Array( [ 1.0, 1.0e100, NaN, -1.0e100 ] ); | 
|  | 59 | +	v = dasumpw.ndarray( x.length, x, 1, 0 ); | 
|  | 60 | +	t.strictEqual( v, 1.0, 'returns expected value' ); | 
|  | 61 | + | 
|  | 62 | +	x = new Float64Array( 1e3 ); | 
|  | 63 | +	for ( i = 0; i < 1e3; i++ ) { | 
|  | 64 | +		x[ i ] = i + 1; | 
|  | 65 | +	} | 
|  | 66 | +	v = dasumpw.ndarray( x.length, x, 1, 0 ); | 
|  | 67 | +	t.strictEqual( v, 500500.0, 'returns expected value' ); | 
|  | 68 | + | 
|  | 69 | +	t.end(); | 
|  | 70 | +}); | 
|  | 71 | + | 
|  | 72 | +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) { | 
|  | 73 | +	var x; | 
|  | 74 | +	var v; | 
|  | 75 | + | 
|  | 76 | +	x = new Float64Array( [ 1.0, -2.0, NaN, 5.0, 3.0 ] ); | 
|  | 77 | + | 
|  | 78 | +	v = dasumpw.ndarray( 0, x, 1, 0 ); | 
|  | 79 | +	t.strictEqual( v, 0.0, 'returns expected value' ); | 
|  | 80 | + | 
|  | 81 | +	v = dasumpw.ndarray( -1, x, 1, 0 ); | 
|  | 82 | +	t.strictEqual( v, 0.0, 'returns expected value' ); | 
|  | 83 | + | 
|  | 84 | +	t.end(); | 
|  | 85 | +}); | 
|  | 86 | + | 
|  | 87 | +tape( 'if provided an `N` parameter equal to `1`, the function returns the first element', function test( t ) { | 
|  | 88 | +	var x; | 
|  | 89 | +	var v; | 
|  | 90 | + | 
|  | 91 | +	x = new Float64Array( [ 1.0, -2.0, NaN, 5.0, 3.0 ] ); | 
|  | 92 | + | 
|  | 93 | +	v = dasumpw.ndarray( 1, x, 1, 0 ); | 
|  | 94 | +	t.strictEqual( v, 1.0, 'returns expected value' ); | 
|  | 95 | + | 
|  | 96 | +	t.end(); | 
|  | 97 | +}); | 
|  | 98 | + | 
|  | 99 | +tape( 'the function supports a `stride` parameter', function test( t ) { | 
|  | 100 | +	var x; | 
|  | 101 | +	var v; | 
|  | 102 | + | 
|  | 103 | +	x = new Float64Array([ | 
|  | 104 | +		1.0,  // 0 | 
|  | 105 | +		2.0, | 
|  | 106 | +		2.0,  // 1 | 
|  | 107 | +		-7.0, | 
|  | 108 | +		NaN, // 2 | 
|  | 109 | +		3.0, | 
|  | 110 | +		4.0,  // 3 | 
|  | 111 | +		2.0 | 
|  | 112 | +	]); | 
|  | 113 | + | 
|  | 114 | +	v = dasumpw.ndarray( 4, x, 2, 0 ); | 
|  | 115 | + | 
|  | 116 | +	t.strictEqual( v, 7.0, 'returns expected value' ); | 
|  | 117 | +	t.end(); | 
|  | 118 | +}); | 
|  | 119 | + | 
|  | 120 | +tape( 'the function supports a negative `stride` parameter', function test( t ) { | 
|  | 121 | +	var x; | 
|  | 122 | +	var v; | 
|  | 123 | +	var i; | 
|  | 124 | + | 
|  | 125 | +	x = new Float64Array([ | 
|  | 126 | +		1.0,  // 3 | 
|  | 127 | +		2.0, | 
|  | 128 | +		2.0,  // 2 | 
|  | 129 | +		-7.0, | 
|  | 130 | +		NaN, // 1 | 
|  | 131 | +		3.0, | 
|  | 132 | +		4.0,  // 0 | 
|  | 133 | +		2.0 | 
|  | 134 | +	]); | 
|  | 135 | + | 
|  | 136 | +	v = dasumpw.ndarray( 4, x, -2, 6 ); | 
|  | 137 | + | 
|  | 138 | +	t.strictEqual( v, 7.0, 'returns expected value' ); | 
|  | 139 | + | 
|  | 140 | +	x = new Float64Array( 1e3 ); | 
|  | 141 | +	for ( i = 0; i < 1e3; i++ ) { | 
|  | 142 | +		x[ i ] = i + 1; | 
|  | 143 | +	} | 
|  | 144 | +	v = dasumpw.ndarray( x.length, x, -1, 1e3-1 ); | 
|  | 145 | +	t.strictEqual( v, 500500.0, 'returns expected value' ); | 
|  | 146 | + | 
|  | 147 | +	t.end(); | 
|  | 148 | +}); | 
|  | 149 | + | 
|  | 150 | +tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) { | 
|  | 151 | +	var x; | 
|  | 152 | +	var v; | 
|  | 153 | + | 
|  | 154 | +	x = new Float64Array( [ 1.0, -2.0, NaN, 5.0, 3.0 ] ); | 
|  | 155 | + | 
|  | 156 | +	v = dasumpw.ndarray( x.length, x, 0, 0 ); | 
|  | 157 | +	t.strictEqual( v, 5.0, 'returns expected value' ); | 
|  | 158 | + | 
|  | 159 | +	t.end(); | 
|  | 160 | +}); | 
|  | 161 | + | 
|  | 162 | +tape( 'the function supports view offsets', function test( t ) { | 
|  | 163 | +	var x0; | 
|  | 164 | +	var x1; | 
|  | 165 | +	var v; | 
|  | 166 | + | 
|  | 167 | +	x0 = new Float64Array([ | 
|  | 168 | +		2.0, | 
|  | 169 | +		1.0,  // 0 | 
|  | 170 | +		2.0, | 
|  | 171 | +		-2.0, // 1 | 
|  | 172 | +		NaN, | 
|  | 173 | +		2.0,  // 2 | 
|  | 174 | +		3.0, | 
|  | 175 | +		4.0,  // 3 | 
|  | 176 | +		6.0 | 
|  | 177 | +	]); | 
|  | 178 | + | 
|  | 179 | +	x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | 
|  | 180 | + | 
|  | 181 | +	v = dasumpw.ndarray( 4, x1, 2, 0 ); | 
|  | 182 | +	t.strictEqual( v, 5.0, 'returns expected value' ); | 
|  | 183 | + | 
|  | 184 | +	t.end(); | 
|  | 185 | +}); | 
|  | 186 | + | 
|  | 187 | +tape( 'the `ndarray` method supports an `offset` parameter', function test( t ) { | 
|  | 188 | +	var x; | 
|  | 189 | +	var v; | 
|  | 190 | + | 
|  | 191 | +	x = new Float64Array([ | 
|  | 192 | +		2.0, | 
|  | 193 | +		1.0,  // 0 | 
|  | 194 | +		2.0, | 
|  | 195 | +		-2.0, // 1 | 
|  | 196 | +		NaN, | 
|  | 197 | +		2.0,  // 2 | 
|  | 198 | +		3.0, | 
|  | 199 | +		4.0   // 3 | 
|  | 200 | +	]); | 
|  | 201 | + | 
|  | 202 | +	v = dasumpw.ndarray( 4, x, 2, 1 ); | 
|  | 203 | +	t.strictEqual( v, 5.0, 'returns expected value' ); | 
|  | 204 | + | 
|  | 205 | +	t.end(); | 
|  | 206 | +}); | 
0 commit comments