Skip to content

Commit f74e989

Browse files
committed
test: add tests for the main method
1 parent faeede1 commit f74e989

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
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+
/* eslint-disable node/no-sync */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var tape = require( 'tape' );
26+
var Memory = require( '@stdlib/wasm/memory' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var daxpy = require( './../lib' );
29+
30+
31+
// TESTS //
32+
33+
tape( 'main export is an object', function test( t ) {
34+
t.ok( true, __filename );
35+
t.strictEqual( typeof daxpy, 'object', 'main export is an object' );
36+
t.end();
37+
});
38+
39+
tape( 'a module instance has a `main` method which has an arity of 6', function test( t ) {
40+
var mem;
41+
var mod;
42+
43+
mem = new Memory({
44+
'initial': 0
45+
});
46+
mod = new daxpy.Module( mem );
47+
t.strictEqual( mod.main.length, 6, 'returns expected value' );
48+
t.end();
49+
});
50+
51+
tape( 'a module instance has a `main` method which multiplies `x` by a constant and adds the result to `y`', function test( t ) {
52+
var expected;
53+
var actual;
54+
var mem;
55+
var mod;
56+
57+
mem = new Memory({
58+
'initial': 1
59+
});
60+
mod = new daxpy.Module( mem );
61+
mod.initializeSync();
62+
63+
mod.write( 0, new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) );
64+
mod.write( 40, new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ) );
65+
66+
expected = new Float64Array( [ 3.0, 5.0, 7.0, 9.0, 11.0 ] );
67+
68+
mod.main( 5, 2.0, 0, 1, 40, 1 );
69+
70+
actual = new Float64Array( 5 );
71+
mod.read( 40, actual );
72+
t.deepEqual( actual, expected, 'returns expected value' );
73+
74+
// Short datasets:
75+
mod.write( 0, new Float64Array( [ 1.0, 2.0 ] ) );
76+
mod.write( 16, new Float64Array( [ 1.0, 1.0 ] ) );
77+
78+
expected = new Float64Array( [ 3.0, 5.0 ] );
79+
80+
mod.main( 2, 2.0, 0, 1, 16, 1 );
81+
82+
actual = new Float64Array( 2 );
83+
mod.read( 16, actual );
84+
t.deepEqual( actual, expected, 'returns expected value' );
85+
86+
t.end();
87+
});
88+
89+
tape( 'a module instance has a `main` method which efficiently handles the case where `alpha` is `0`', function test( t ) {
90+
var expected;
91+
var actual;
92+
var mem;
93+
var mod;
94+
95+
mem = new Memory({
96+
'initial': 1
97+
});
98+
mod = new daxpy.Module( mem );
99+
mod.initializeSync();
100+
101+
mod.write( 0, new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) );
102+
mod.write( 40, new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ) );
103+
104+
expected = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
105+
106+
mod.main( 5, 0.0, 0, 1, 40, 1 );
107+
108+
actual = new Float64Array( 5 );
109+
mod.read( 40, actual );
110+
t.deepEqual( actual, expected, 'returns expected value' );
111+
112+
t.end();
113+
});
114+
115+
tape( 'a module instance has a `main` method which supports an `x` stride', function test( t ) {
116+
var expected;
117+
var actual;
118+
var mem;
119+
var mod;
120+
var N;
121+
122+
mem = new Memory({
123+
'initial': 1
124+
});
125+
mod = new daxpy.Module( mem );
126+
mod.initializeSync();
127+
128+
mod.write( 0, new Float64Array([
129+
1.0, // 0
130+
2.0,
131+
3.0, // 1
132+
4.0,
133+
5.0 // 2
134+
]));
135+
mod.write( 40, new Float64Array([
136+
1.0, // 0
137+
1.0, // 1
138+
1.0, // 2
139+
1.0,
140+
1.0
141+
]));
142+
N = 3;
143+
144+
mod.main( N, 2.0, 0, 2, 40, 1 );
145+
146+
expected = new Float64Array( [ 3.0, 7.0, 11.0, 1.0, 1.0 ] );
147+
148+
actual = new Float64Array( 5 );
149+
mod.read( 40, actual );
150+
t.deepEqual( actual, expected, 'returns expected value' );
151+
152+
t.end();
153+
});
154+
155+
tape( 'a module instance has a `main` method which supports a `y` stride', function test( t ) {
156+
var expected;
157+
var actual;
158+
var mem;
159+
var mod;
160+
var N;
161+
162+
mem = new Memory({
163+
'initial': 1
164+
});
165+
mod = new daxpy.Module( mem );
166+
mod.initializeSync();
167+
168+
mod.write( 0, new Float64Array([
169+
1.0, // 0
170+
2.0, // 1
171+
3.0, // 2
172+
4.0,
173+
5.0
174+
]));
175+
mod.write( 40, new Float64Array([
176+
1.0, // 0
177+
1.0,
178+
1.0, // 1
179+
1.0,
180+
1.0 // 2
181+
]));
182+
N = 3;
183+
184+
mod.main( N, 2.0, 0, 1, 40, 2 );
185+
186+
expected = new Float64Array( [ 3.0, 1.0, 5.0, 1.0, 7.0 ] );
187+
188+
actual = new Float64Array( 5 );
189+
mod.read( 40, actual );
190+
t.deepEqual( actual, expected, 'returns expected value' );
191+
192+
t.end();
193+
});
194+
195+
tape( 'a module instance has a `main` method which returns a pointer to the output array', function test( t ) {
196+
var out;
197+
var mem;
198+
var mod;
199+
200+
mem = new Memory({
201+
'initial': 1
202+
});
203+
mod = new daxpy.Module( mem );
204+
mod.initializeSync();
205+
206+
mod.write( 0, new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) );
207+
mod.write( 40, new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ) );
208+
209+
out = mod.main( 5, 3.0, 0, 1, 40, 1 );
210+
211+
t.strictEqual( out, 40, 'returns expected value' );
212+
t.end();
213+
});
214+
215+
tape( 'if provided an `N` parameter less than or equal to `0`, a module instance has a `main` method which leaves the output array unchanged', function test( t ) {
216+
var expected;
217+
var actual;
218+
var mem;
219+
var mod;
220+
221+
mem = new Memory({
222+
'initial': 1
223+
});
224+
mod = new daxpy.Module( mem );
225+
mod.initializeSync();
226+
227+
mod.write( 0, new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) );
228+
mod.write( 40, new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ) );
229+
230+
expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
231+
232+
mod.main( -1, 3.0, 0, 1, 40, 1 );
233+
actual = new Float64Array( 5 );
234+
mod.read( 40, actual );
235+
t.deepEqual( actual, expected, 'returns expected value' );
236+
237+
mod.main( 0, 3.0, 0, 1, 40, 1 );
238+
actual = new Float64Array( 5 );
239+
mod.read( 40, actual );
240+
t.deepEqual( actual, expected, 'returns expected value' );
241+
242+
t.end();
243+
});
244+
245+
tape( 'a module instance has a `main` method which supports negative strides', function test( t ) {
246+
var expected;
247+
var actual;
248+
var mem;
249+
var mod;
250+
var N;
251+
252+
mem = new Memory({
253+
'initial': 1
254+
});
255+
mod = new daxpy.Module( mem );
256+
mod.initializeSync();
257+
258+
mod.write( 0, new Float64Array([
259+
1.0, // 2
260+
2.0,
261+
3.0, // 1
262+
4.0,
263+
5.0 // 0
264+
]));
265+
mod.write( 40, new Float64Array([
266+
6.0, // 2
267+
7.0, // 1
268+
8.0, // 0
269+
9.0,
270+
10.0
271+
]));
272+
N = 3;
273+
274+
mod.main( N, 3.0, 0, -2, 40, -1 );
275+
276+
expected = new Float64Array( [ 9.0, 16.0, 23.0, 9.0, 10.0 ] );
277+
278+
actual = new Float64Array( 5 );
279+
mod.read( 40, actual );
280+
t.deepEqual( actual, expected, 'returns expected value' );
281+
282+
t.end();
283+
});
284+
285+
tape( 'a module instance has a `main` method which supports complex access patterns', function test( t ) {
286+
var expected;
287+
var actual;
288+
var mem;
289+
var mod;
290+
var N;
291+
292+
mem = new Memory({
293+
'initial': 1
294+
});
295+
mod = new daxpy.Module( mem );
296+
mod.initializeSync();
297+
298+
mod.write( 0, new Float64Array([
299+
1.0, // 0
300+
2.0,
301+
3.0, // 1
302+
4.0,
303+
5.0, // 2
304+
6.0
305+
]));
306+
mod.write( 48, new Float64Array([
307+
7.0, // 2
308+
8.0, // 1
309+
9.0, // 0
310+
10.0,
311+
11.0,
312+
12.0
313+
]));
314+
N = 3;
315+
316+
mod.main( N, 3.0, 0, 2, 48, -1 );
317+
318+
expected = new Float64Array( [ 22.0, 17.0, 12.0, 10.0, 11.0, 12.0 ] );
319+
320+
actual = new Float64Array( 6 );
321+
mod.read( 48, actual );
322+
t.deepEqual( actual, expected, 'returns expected value' );
323+
324+
t.end();
325+
});

0 commit comments

Comments
 (0)