Skip to content

Commit 0ac4dcf

Browse files
committed
test: add main and ndarray method tests
1 parent 64f9d94 commit 0ac4dcf

File tree

3 files changed

+589
-1
lines changed

3 files changed

+589
-1
lines changed

lib/node_modules/@stdlib/blas/base/daxpy-wasm/test/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ tape( 'attached to the main export is an `ndarray` method', function test( t ) {
4242
t.end();
4343
});
4444

45-
// TODO: add tests
45+
tape( 'attached to the main export is a `Module` constructor', function test( t ) {
46+
t.strictEqual( typeof daxpy.Module, 'function', 'returns expected value' );
47+
t.end();
48+
});
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
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 Float64Array = require( '@stdlib/array/float64' );
25+
var daxpy = 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 daxpy, 'object', 'main export is an object' );
33+
t.end();
34+
});
35+
36+
tape( 'the `main` method has an arity of 6', function test( t ) {
37+
t.strictEqual( daxpy.main.length, 6, 'returns expected value' );
38+
t.end();
39+
});
40+
41+
tape( 'the `main` method multiplies `x` by a constant and adds the result to `y`', function test( t ) {
42+
var expected;
43+
var alpha;
44+
var x;
45+
var y;
46+
47+
alpha = 2.0;
48+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
49+
y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
50+
51+
expected = new Float64Array( [ 3.0, 5.0, 7.0, 9.0, 11.0 ] );
52+
53+
daxpy.main( x.length, alpha, x, 1, y, 1 );
54+
55+
t.deepEqual( y, expected, 'returns expected value' );
56+
57+
// Short datasets:
58+
x = new Float64Array( [ 1.0, 2.0 ] );
59+
y = new Float64Array( [ 1.0, 1.0 ] );
60+
61+
expected = new Float64Array( [ 3.0, 5.0 ] );
62+
63+
daxpy.main( x.length, alpha, x, 1, y, 1 );
64+
65+
t.deepEqual( y, expected, 'returns expected value' );
66+
67+
t.end();
68+
});
69+
70+
tape( 'the `main` method efficiently handles the case where `alpha` is `0`', function test( t ) {
71+
var expected;
72+
var alpha;
73+
var x;
74+
var y;
75+
76+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
77+
y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
78+
alpha = 0.0;
79+
80+
expected = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
81+
82+
daxpy.main( x.length, alpha, x, 1, y, 1 );
83+
84+
t.deepEqual( y, expected, 'returns expected value' );
85+
t.end();
86+
});
87+
88+
tape( 'the `main` method supports an `x` stride', function test( t ) {
89+
var expected;
90+
var x;
91+
var y;
92+
var N;
93+
94+
x = new Float64Array([
95+
1.0, // 0
96+
2.0,
97+
3.0, // 1
98+
4.0,
99+
5.0 // 2
100+
]);
101+
y = new Float64Array([
102+
1.0, // 0
103+
1.0, // 1
104+
1.0, // 2
105+
1.0,
106+
1.0
107+
]);
108+
N = 3;
109+
110+
daxpy.main( N, 2.0, x, 2, y, 1 );
111+
112+
expected = new Float64Array( [ 3.0, 7.0, 11.0, 1.0, 1.0 ] );
113+
114+
t.deepEqual( y, expected, 'returns expected value' );
115+
t.end();
116+
});
117+
118+
tape( 'the `main` method supports a `y` stride', function test( t ) {
119+
var expected;
120+
var x;
121+
var y;
122+
var N;
123+
124+
x = new Float64Array([
125+
1.0, // 0
126+
2.0, // 1
127+
3.0, // 2
128+
4.0,
129+
5.0
130+
]);
131+
y = new Float64Array([
132+
1.0, // 0
133+
1.0,
134+
1.0, // 1
135+
1.0,
136+
1.0 // 2
137+
]);
138+
N = 3;
139+
140+
daxpy.main( N, 2.0, x, 1, y, 2 );
141+
142+
expected = new Float64Array( [ 3.0, 1.0, 5.0, 1.0, 7.0 ] );
143+
144+
t.deepEqual( y, expected, 'returns expected value' );
145+
t.end();
146+
});
147+
148+
tape( 'the `main` method returns a reference to the output array', function test( t ) {
149+
var out;
150+
var x;
151+
var y;
152+
153+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
154+
y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
155+
156+
out = daxpy.main( x.length, 3.0, x, 1, y, 1 );
157+
158+
t.strictEqual( out, y, 'same reference' );
159+
t.end();
160+
});
161+
162+
tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method returns the output array unchanged', function test( t ) {
163+
var expected;
164+
var x;
165+
var y;
166+
167+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
168+
y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
169+
170+
expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
171+
172+
daxpy.main( -1, 3.0, x, 1, y, 1 );
173+
t.deepEqual( y, expected, 'returns expected value' );
174+
175+
daxpy.main( 0, 3.0, x, 1, y, 1 );
176+
t.deepEqual( y, expected, 'returns expected value' );
177+
178+
t.end();
179+
});
180+
181+
tape( 'the `main` method supports negative strides', function test( t ) {
182+
var expected;
183+
var x;
184+
var y;
185+
var N;
186+
187+
x = new Float64Array([
188+
1.0, // 2
189+
2.0,
190+
3.0, // 1
191+
4.0,
192+
5.0 // 0
193+
]);
194+
y = new Float64Array([
195+
6.0, // 2
196+
7.0, // 1
197+
8.0, // 0
198+
9.0,
199+
10.0
200+
]);
201+
N = 3;
202+
203+
daxpy.main( N, 3.0, x, -2, y, -1 );
204+
205+
expected = new Float64Array( [ 9.0, 16.0, 23.0, 9.0, 10.0 ] );
206+
207+
t.deepEqual( y, expected, 'returns expected value' );
208+
t.end();
209+
});
210+
211+
tape( 'the `main` method supports complex access patterns', function test( t ) {
212+
var expected;
213+
var x;
214+
var y;
215+
var N;
216+
217+
x = new Float64Array([
218+
1.0, // 0
219+
2.0,
220+
3.0, // 1
221+
4.0,
222+
5.0, // 2
223+
6.0
224+
]);
225+
y = new Float64Array([
226+
7.0, // 2
227+
8.0, // 1
228+
9.0, // 0
229+
10.0,
230+
11.0,
231+
12.0
232+
]);
233+
N = 3;
234+
235+
daxpy.main( N, 3.0, x, 2, y, -1 );
236+
237+
expected = new Float64Array( [ 22.0, 17.0, 12.0, 10.0, 11.0, 12.0 ] );
238+
239+
t.deepEqual( y, expected, 'returns expected value' );
240+
t.end();
241+
});
242+
243+
tape( 'the `main` method supports view offsets', function test( t ) {
244+
var expected;
245+
var x0;
246+
var y0;
247+
var x1;
248+
var y1;
249+
250+
// Initial arrays...
251+
x0 = new Float64Array([
252+
1.0,
253+
2.0, // 2
254+
3.0,
255+
4.0, // 1
256+
5.0,
257+
6.0 // 0
258+
]);
259+
y0 = new Float64Array([
260+
7.0,
261+
8.0,
262+
9.0,
263+
10.0, // 0
264+
11.0, // 1
265+
12.0 // 2
266+
]);
267+
268+
// Create offset views...
269+
x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // begin at 2nd element
270+
y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // begin at the 4th element
271+
272+
daxpy.main( 3, 3.0, x1, -2, y1, 1 );
273+
expected = new Float64Array([
274+
7.0,
275+
8.0,
276+
9.0,
277+
28.0,
278+
23.0,
279+
18.0
280+
]);
281+
282+
t.deepEqual( y0, expected, 'returns expected value' );
283+
t.end();
284+
});

0 commit comments

Comments
 (0)