Skip to content

Commit 17295e1

Browse files
committed
test: add tests for wasm/dasumpw
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent 487ed91 commit 17295e1

File tree

7 files changed

+1120
-0
lines changed

7 files changed

+1120
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 dasumpw = require( './../lib' );
25+
26+
27+
// TESTS //
28+
29+
tape( 'main export is an object', function test( t ) {
30+
t.ok( true, __filename );
31+
t.strictEqual( typeof dasumpw, 'object', 'returns expected value' );
32+
t.end();
33+
});
34+
35+
tape( 'attached to the main export is a `main` method', function test( t ) {
36+
t.strictEqual( typeof dasumpw.main, 'function', 'returns expected value' );
37+
t.end();
38+
});
39+
40+
tape( 'attached to the main export is an `ndarray` method', function test( t ) {
41+
t.strictEqual( typeof dasumpw.ndarray, 'function', 'returns expected value' );
42+
t.end();
43+
});
44+
45+
tape( 'attached to the main export is a `Module` constructor', function test( t ) {
46+
t.strictEqual( typeof dasumpw.Module, 'function', 'returns expected value' );
47+
t.end();
48+
});
49+
50+
tape( 'the main export is a `Module` instance', function test( t ) {
51+
t.strictEqual( dasumpw instanceof dasumpw.Module, true, 'returns expected value' );
52+
t.end();
53+
});
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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

Comments
 (0)