Skip to content

Commit a615133

Browse files
feat: added tests
--- 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 ---
1 parent c19ab6f commit a615133

File tree

7 files changed

+1068
-0
lines changed

7 files changed

+1068
-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 sapxsumkbn = 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 sapxsumkbn, '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 sapxsumkbn.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 sapxsumkbn.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 sapxsumkbn.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( sapxsumkbn instanceof sapxsumkbn.Module, true, 'returns expected value' );
52+
t.end();
53+
});
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var Float32Array = require( '@stdlib/array/float32' );
26+
var sapxsumkbn = 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 sapxsumkbn, 'object', 'main export is an object' );
34+
t.end();
35+
});
36+
37+
tape( 'the `main` method has an arity of 4', function test( t ) {
38+
t.strictEqual( sapxsumkbn.main.length, 4, 'returns expected value' );
39+
t.end();
40+
});
41+
42+
tape( 'the `main` method adds a constant and calculates the sum of all strided array elements', function test( t ) {
43+
var x;
44+
var v;
45+
46+
x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0, 0.0, -3.0, 3.0 ] );
47+
v = sapxsumkbn.main( x.length, 5.0, x, 1 );
48+
t.strictEqual( v, 48.0, 'returns expected value' );
49+
50+
x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] );
51+
v = sapxsumkbn.main( x.length, 5.0, x, 1 );
52+
t.strictEqual( v, 33.0, 'returns expected value' );
53+
54+
x = new Float32Array( [ -4.0, -4.0 ] );
55+
v = sapxsumkbn.main( x.length, 5.0, x, 1 );
56+
t.strictEqual( v, 2.0, 'returns expected value' );
57+
58+
x = new Float32Array( [ NaN, 4.0 ] );
59+
v = sapxsumkbn.main( x.length, 5.0, x, 1 );
60+
t.strictEqual( isnan( v ), true, 'returns expected value' );
61+
62+
x = new Float32Array( [ 1.0, 1e100, 1.0, -1.0e100 ] );
63+
v = sapxsumkbn.main( x.length, 5.0, x, 1 );
64+
t.strictEqual( v, 12.0, 'returns expected value' );
65+
66+
t.end();
67+
});
68+
69+
tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method returns `0.0`', function test( t ) {
70+
var x;
71+
var v;
72+
73+
x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] );
74+
75+
v = sapxsumkbn.main( 0, 5.0, x, 1 );
76+
t.strictEqual( v, 0.0, 'returns expected value' );
77+
78+
v = sapxsumkbn.main( -1, 5.0, x, 1 );
79+
t.strictEqual( v, 0.0, 'returns expected value' );
80+
81+
t.end();
82+
});
83+
84+
tape( 'if provided an `N` parameter equal to `1`, the `main` method returns the first element plus a constant', function test( t ) {
85+
var x;
86+
var v;
87+
88+
x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] );
89+
90+
v = sapxsumkbn.main( 1, 5.0, x, 1 );
91+
t.strictEqual( v, 6.0, 'returns expected value' );
92+
93+
t.end();
94+
});
95+
96+
tape( 'the `main` method supports a `stride` parameter', function test( t ) {
97+
var x;
98+
var v;
99+
100+
x = new Float32Array([
101+
1.0, // 0
102+
2.0,
103+
2.0, // 1
104+
-7.0,
105+
-2.0, // 2
106+
3.0,
107+
4.0, // 3
108+
2.0
109+
]);
110+
111+
v = sapxsumkbn.main( 4, 5.0, x, 2 );
112+
113+
t.strictEqual( v, 25.0, 'returns expected value' );
114+
t.end();
115+
});
116+
117+
tape( 'the `main` method supports a negative `stride` parameter', function test( t ) {
118+
var x;
119+
var v;
120+
121+
x = new Float32Array([
122+
1.0, // 3
123+
2.0,
124+
2.0, // 2
125+
-7.0,
126+
-2.0, // 1
127+
3.0,
128+
4.0, // 0
129+
2.0
130+
]);
131+
132+
v = sapxsumkbn.main( 4, 5.0, x, -2 );
133+
134+
t.strictEqual( v, 25.0, 'returns expected value' );
135+
t.end();
136+
});
137+
138+
tape( 'if provided a `stride` parameter equal to `0`, the `main` method returns the first element plus a constant repeated N times', function test( t ) {
139+
var x;
140+
var v;
141+
142+
x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] );
143+
144+
v = sapxsumkbn.main( x.length, 5.0, x, 0 );
145+
146+
t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' );
147+
t.end();
148+
});
149+
150+
tape( 'the `main` method supports view offsets', function test( t ) {
151+
var x0;
152+
var x1;
153+
var v;
154+
155+
x0 = new Float32Array([
156+
2.0,
157+
1.0, // 0
158+
2.0,
159+
-2.0, // 1
160+
-2.0,
161+
2.0, // 2
162+
3.0,
163+
4.0, // 3
164+
6.0
165+
]);
166+
167+
x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
168+
v = sapxsumkbn.main( 4, 5.0, x1, 2 );
169+
t.strictEqual( v, 25.0, 'returns expected value' );
170+
171+
t.end();
172+
});
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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 Memory = require( '@stdlib/wasm/memory' );
25+
var ModuleWrapper = require( '@stdlib/wasm/module-wrapper' );
26+
var Module = require( './../lib' ).Module;
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof Module, 'function', 'returns expected value' );
34+
t.end();
35+
});
36+
37+
tape( 'the function is a constructor', function test( t ) {
38+
var mem;
39+
var mod;
40+
41+
mem = new Memory({
42+
'initial': 0
43+
});
44+
mod = new Module( mem );
45+
t.strictEqual( mod instanceof Module, true, 'returns expected value' );
46+
t.end();
47+
});
48+
49+
tape( 'the function is a constructor which does not require `new`', function test( t ) {
50+
var mem;
51+
var mod;
52+
53+
mem = new Memory({
54+
'initial': 0
55+
});
56+
mod = Module( mem ); // eslint-disable-line new-cap
57+
t.strictEqual( mod instanceof Module, true, 'returns expected value' );
58+
t.end();
59+
});
60+
61+
tape( 'the module constructor throws an error if provided a first argument which is not a WebAssembly memory instance (new)', function test( t ) {
62+
var values;
63+
var i;
64+
65+
values = [
66+
'5',
67+
5,
68+
NaN,
69+
true,
70+
false,
71+
null,
72+
void 0,
73+
[],
74+
{},
75+
function noop() {}
76+
];
77+
for ( i = 0; i < values.length; i++ ) {
78+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
79+
}
80+
t.end();
81+
82+
function badValue( value ) {
83+
return function badValue() {
84+
return new Module( value );
85+
};
86+
}
87+
});
88+
89+
tape( 'the module constructor throws an error if provided a first argument which is not a WebAssembly memory instance (no new)', function test( t ) {
90+
var values;
91+
var i;
92+
93+
values = [
94+
'5',
95+
5,
96+
NaN,
97+
true,
98+
false,
99+
null,
100+
void 0,
101+
[],
102+
{},
103+
function noop() {}
104+
];
105+
for ( i = 0; i < values.length; i++ ) {
106+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
107+
}
108+
t.end();
109+
110+
function badValue( value ) {
111+
return function badValue() {
112+
return Module( value ); // eslint-disable-line new-cap
113+
};
114+
}
115+
});
116+
117+
tape( 'the module instance returned by the module constructor inherits from a module wrapper', function test( t ) {
118+
var mem;
119+
var mod;
120+
121+
mem = new Memory({
122+
'initial': 0
123+
});
124+
mod = new Module( mem );
125+
126+
t.strictEqual( mod instanceof ModuleWrapper, true, 'returns expected value' );
127+
t.end();
128+
});
129+
130+
tape( 'attached to a module instance is a `main` method', function test( t ) {
131+
var mem;
132+
var mod;
133+
134+
mem = new Memory({
135+
'initial': 0
136+
});
137+
mod = new Module( mem );
138+
139+
t.strictEqual( typeof mod.main, 'function', 'returns expected value' );
140+
t.end();
141+
});
142+
143+
tape( 'attached to a module instance is an `ndarray` method', function test( t ) {
144+
var mem;
145+
var mod;
146+
147+
mem = new Memory({
148+
'initial': 0
149+
});
150+
mod = new Module( mem );
151+
152+
t.strictEqual( typeof mod.ndarray, 'function', 'returns expected value' );
153+
t.end();
154+
});

0 commit comments

Comments
 (0)