Skip to content

Commit 2defdab

Browse files
aman-095kgryte
andauthored
test: add tests for blas/base/cscal-wasm
PR-URL: #3062 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent cef4c74 commit 2defdab

File tree

4 files changed

+1325
-0
lines changed

4 files changed

+1325
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
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 Complex64Array = require( '@stdlib/array/complex64' );
25+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
26+
var Float32Array = require( '@stdlib/array/float32' );
27+
var EPS = require( '@stdlib/constants/float32/eps' );
28+
var abs = require( '@stdlib/math/base/special/abs' );
29+
var cscal = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Tests for element-wise approximate equality.
36+
*
37+
* @private
38+
* @param {Object} t - test object
39+
* @param {Collection} actual - actual values
40+
* @param {Collection} expected - expected values
41+
* @param {number} rtol - relative tolerance
42+
*/
43+
function isApprox( t, actual, expected, rtol ) {
44+
var delta;
45+
var tol;
46+
var i;
47+
48+
t.strictEqual( actual.length, expected.length, 'returns expected value' );
49+
for ( i = 0; i < expected.length; i++ ) {
50+
if ( actual[ i ] === expected[ i ] ) {
51+
t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' );
52+
} else {
53+
delta = abs( actual[ i ] - expected[ i ] );
54+
tol = rtol * EPS * abs( expected[ i ] );
55+
t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' );
56+
}
57+
}
58+
}
59+
60+
61+
// TESTS //
62+
63+
tape( 'main export is an object', function test( t ) {
64+
t.ok( true, __filename );
65+
t.strictEqual( typeof cscal, 'object', 'main export is an object' );
66+
t.end();
67+
});
68+
69+
tape( 'the `main` method has an arity of 4', function test( t ) {
70+
t.strictEqual( cscal.main.length, 4, 'arity of 4' );
71+
t.end();
72+
});
73+
74+
tape( 'the `main` method scales elements from `cx` by `ca`', function test( t ) {
75+
var expected;
76+
var viewX;
77+
var ca;
78+
var cx;
79+
80+
cx = new Complex64Array( [
81+
0.3, // 1
82+
0.1, // 1
83+
0.5, // 2
84+
0.0, // 2
85+
0.0, // 3
86+
0.5, // 3
87+
0.0, // 4
88+
0.2 // 4
89+
] );
90+
ca = new Complex64( 0.4, -0.7 );
91+
92+
cscal.main( 4, ca, cx, 1 );
93+
94+
viewX = new Float32Array( cx.buffer );
95+
expected = new Float32Array( [
96+
0.19, // 1
97+
-0.17, // 1
98+
0.2, // 2
99+
-0.35, // 2
100+
0.35, // 3
101+
0.2, // 3
102+
0.14, // 4
103+
0.08 // 4
104+
] );
105+
isApprox( t, viewX, expected, 1.0 );
106+
107+
t.end();
108+
});
109+
110+
tape( 'the `main` method supports specifying a `cx` stride', function test( t ) {
111+
var expected;
112+
var viewX;
113+
var ca;
114+
var cx;
115+
116+
cx = new Complex64Array( [
117+
0.1, // 1
118+
0.1, // 1
119+
3.0,
120+
6.0,
121+
-0.6, // 2
122+
0.1, // 2
123+
4.0,
124+
7.0,
125+
0.1, // 3
126+
-0.3, // 3
127+
7.0,
128+
2.0
129+
] );
130+
ca = new Complex64( 0.4, -0.7 );
131+
132+
cscal.main( 3, ca, cx, 2 );
133+
134+
viewX = new Float32Array( cx.buffer );
135+
expected = new Float32Array( [
136+
0.11, // 1
137+
-0.03, // 1
138+
3.0,
139+
6.0,
140+
-0.17, // 2
141+
0.46, // 2
142+
4.0,
143+
7.0,
144+
-0.17, // 3
145+
-0.19, // 3
146+
7.0,
147+
2.0
148+
] );
149+
isApprox( t, viewX, expected, 1.0 );
150+
151+
t.end();
152+
});
153+
154+
tape( 'the `main` method supports specifying a negative `cx` stride', function test( t ) {
155+
var expected;
156+
var viewX;
157+
var ca;
158+
var cx;
159+
160+
cx = new Complex64Array( [
161+
0.1, // 3
162+
0.1, // 3
163+
3.0,
164+
6.0,
165+
-0.6, // 2
166+
0.1, // 2
167+
4.0,
168+
7.0,
169+
0.1, // 1
170+
-0.3, // 1
171+
7.0,
172+
2.0
173+
] );
174+
ca = new Complex64( 0.4, -0.7 );
175+
176+
cscal.main( 3, ca, cx, -2 );
177+
178+
viewX = new Float32Array( cx.buffer );
179+
expected = new Float32Array( [
180+
0.11, // 3
181+
-0.03, // 3
182+
3.0,
183+
6.0,
184+
-0.17, // 2
185+
0.46, // 2
186+
4.0,
187+
7.0,
188+
-0.17, // 1
189+
-0.19, // 1
190+
7.0,
191+
2.0
192+
] );
193+
isApprox( t, viewX, expected, 1.0 );
194+
195+
t.end();
196+
});
197+
198+
tape( 'the `main` method returns a reference to the input array', function test( t ) {
199+
var out;
200+
var ca;
201+
var cx;
202+
203+
cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
204+
ca = new Complex64( 2.0, 2.0 );
205+
206+
out = cscal.main( 4, ca, cx, 1 );
207+
208+
t.strictEqual( out, cx, 'same reference' );
209+
t.end();
210+
});
211+
212+
tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method returns the input array unchanged', function test( t ) {
213+
var expected;
214+
var viewX;
215+
var ca;
216+
var cx;
217+
218+
cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
219+
ca = new Complex64( 2.0, 2.0 );
220+
221+
viewX = new Float32Array( cx.buffer );
222+
expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
223+
224+
cscal.main( -1, ca, cx, 1 );
225+
t.deepEqual( viewX, expected, 'returns expected value' );
226+
227+
cscal.main( 0, ca, cx, 1 );
228+
t.deepEqual( viewX, expected, 'returns expected value' );
229+
230+
t.end();
231+
});
232+
233+
tape( 'the `main` method supports view offsets', function test( t ) {
234+
var expected;
235+
var viewX;
236+
var cx0;
237+
var cx1;
238+
var ca;
239+
240+
// Initial arrays...
241+
cx0 = new Complex64Array( [
242+
0.1,
243+
-0.3,
244+
8.0, // 1
245+
9.0, // 1
246+
0.5, // 2
247+
-0.1, // 2
248+
2.0, // 3
249+
5.0, // 3
250+
2.0,
251+
3.0
252+
] );
253+
ca = new Complex64( 0.4, -0.7 );
254+
255+
// Create offset views...
256+
cx1 = new Complex64Array( cx0.buffer, cx0.BYTES_PER_ELEMENT*1 ); // begin at 2nd element
257+
258+
cscal.main( 3, ca, cx1, 1 );
259+
260+
viewX = new Float32Array( cx0.buffer );
261+
expected = new Float32Array( [
262+
0.1,
263+
-0.3,
264+
9.5, // 1
265+
-2.0, // 1
266+
0.13, // 2
267+
-0.39, // 2
268+
4.3, // 3
269+
0.6, // 3
270+
2.0,
271+
3.0
272+
] );
273+
isApprox( t, viewX, expected, 1.0 );
274+
275+
t.end();
276+
});

0 commit comments

Comments
 (0)