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