Skip to content

Commit 0e90cbc

Browse files
authored
chore: add test.ndarray.js
Signed-off-by: Shabareesh Shetty <[email protected]>
1 parent eb83b7e commit 0e90cbc

File tree

1 file changed

+370
-0
lines changed

1 file changed

+370
-0
lines changed
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
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 dnancusumkbn2 = require( './../lib/ndarray.js' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof dnancusumkbn2, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function has an arity of 8', function test( t ) {
38+
t.strictEqual( dnancusumkbn2.length, 8, 'has expected arity' );
39+
t.end();
40+
});
41+
42+
tape( 'the function calculates the cumulative sum', function test( t ) {
43+
var expected;
44+
var x;
45+
var y;
46+
var i;
47+
48+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, NaN ] );
49+
y = new Float64Array( x.length );
50+
51+
dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
52+
expected = new Float64Array([
53+
1.0,
54+
3.0,
55+
6.0,
56+
10.0,
57+
10.0
58+
]);
59+
t.deepEqual( y, expected, 'returns expected value' );
60+
61+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
62+
y = new Float64Array( x.length );
63+
64+
dnancusumkbn2( x.length, 10.0, x, 1, 0, y, 1, 0 );
65+
expected = new Float64Array([
66+
11.0,
67+
13.0,
68+
16.0,
69+
20.0,
70+
25.0
71+
]);
72+
t.deepEqual( y, expected, 'returns expected value' );
73+
74+
x = new Float64Array( [ NaN, NaN ] );
75+
y = new Float64Array( x.length );
76+
dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
77+
78+
for ( i = 0; i < y.length; i++ ) {
79+
t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i );
80+
}
81+
82+
x = new Float64Array( [ 1.0, NaN, 3.0, NaN ] );
83+
y = new Float64Array( x.length );
84+
dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
85+
86+
expected = new Float64Array([
87+
1.0,
88+
NaN,
89+
NaN,
90+
NaN
91+
]);
92+
for ( i = 0; i < y.length; i++ ) {
93+
if ( isnan( expected[ i ] ) ) {
94+
t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i );
95+
} else {
96+
t.strictEqual( y[ i ], expected[ i ], true, 'returns expected value. i: ' + i );
97+
}
98+
}
99+
100+
x = new Float64Array( [ 1.0, 1.0e100, 1.0, -1.0e100 ] );
101+
y = new Float64Array( x.length );
102+
dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
103+
104+
expected = new Float64Array([
105+
1.0,
106+
1.0e100,
107+
1.0e100,
108+
2.0
109+
]);
110+
t.deepEqual( y, expected, 'returns expected value' );
111+
112+
x = new Float64Array( 1e3 );
113+
y = new Float64Array( x.length );
114+
expected = new Float64Array( x.length );
115+
for ( i = 0; i < x.length; i++ ) {
116+
x[ i ] = i + 1;
117+
if ( i === 0 ) {
118+
expected[ i ] += x[ i ];
119+
} else {
120+
expected[ i ] += expected[ i-1 ] + x[ i ];
121+
}
122+
}
123+
dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
124+
t.deepEqual( y, expected, 'returns expected value' );
125+
126+
t.end();
127+
});
128+
129+
tape( 'the function returns a reference to the output array', function test( t ) {
130+
var out;
131+
var x;
132+
var y;
133+
134+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
135+
y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
136+
137+
out = dnancusumkbn2( x.length, 0.0, x, 1, 0, y, 1, 0 );
138+
139+
t.strictEqual( out, y, 'same reference' );
140+
t.end();
141+
});
142+
143+
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `y` unchanged', function test( t ) {
144+
var expected;
145+
var x;
146+
var y;
147+
148+
x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
149+
y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
150+
151+
expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
152+
153+
dcusumkbn2( -1, 0.0, x, 1, 0, y, 1, 0 );
154+
t.deepEqual( y, expected, 'returns `y` unchanged' );
155+
156+
dcusumkbn2( 0, 0.0, x, 1, 0, y, 1, 0 );
157+
t.deepEqual( y, expected, 'returns `y` unchanged' );
158+
159+
t.end();
160+
});
161+
162+
tape( 'the function supports an `x` stride', function test( t ) {
163+
var expected;
164+
var x;
165+
var y;
166+
var N;
167+
168+
x = new Float64Array([
169+
1.0, // 0
170+
2.0,
171+
3.0, // 1
172+
4.0,
173+
5.0 // 2
174+
]);
175+
y = new Float64Array([
176+
0.0, // 0
177+
0.0, // 1
178+
0.0, // 2
179+
0.0,
180+
0.0
181+
]);
182+
N = 3;
183+
184+
dcusumkbn2( N, 0.0, x, 2, 0, y, 1, 0 );
185+
186+
expected = new Float64Array( [ 1.0, 4.0, 9.0, 0.0, 0.0 ] );
187+
188+
t.deepEqual( y, expected, 'returns expected value' );
189+
t.end();
190+
});
191+
192+
tape( 'the function supports a `y` stride', function test( t ) {
193+
var expected;
194+
var x;
195+
var y;
196+
var N;
197+
198+
x = new Float64Array([
199+
1.0, // 0
200+
2.0, // 1
201+
3.0, // 2
202+
4.0,
203+
5.0
204+
]);
205+
y = new Float64Array([
206+
0.0, // 0
207+
0.0,
208+
0.0, // 1
209+
0.0,
210+
0.0 // 2
211+
]);
212+
N = 3;
213+
214+
dcusumkbn2( N, 0.0, x, 1, 0, y, 2, 0 );
215+
216+
expected = new Float64Array( [ 1.0, 0.0, 3.0, 0.0, 6.0 ] );
217+
218+
t.deepEqual( y, expected, 'returns expected value' );
219+
t.end();
220+
});
221+
222+
tape( 'the function supports negative strides', function test( t ) {
223+
var expected;
224+
var x;
225+
var y;
226+
var N;
227+
var i;
228+
229+
x = new Float64Array([
230+
1.0, // 2
231+
2.0,
232+
3.0, // 1
233+
4.0,
234+
5.0 // 0
235+
]);
236+
y = new Float64Array([
237+
0.0, // 2
238+
0.0, // 1
239+
0.0, // 0
240+
0.0,
241+
0.0
242+
]);
243+
N = 3;
244+
245+
dcusumkbn2( N, 0.0, x, -2, x.length-1, y, -1, 2 );
246+
247+
expected = new Float64Array( [ 9.0, 8.0, 5.0, 0.0, 0.0 ] );
248+
t.deepEqual( y, expected, 'returns expected value' );
249+
250+
x = new Float64Array( 1e3 );
251+
y = new Float64Array( x.length );
252+
expected = new Float64Array( x.length );
253+
for ( i = 0; i < x.length; i++ ) {
254+
x[ i ] = i + 1;
255+
}
256+
for ( i = x.length-1; i >= 0; i-- ) {
257+
if ( i === x.length-1 ) {
258+
expected[ i ] += x[ i ];
259+
} else {
260+
expected[ i ] += expected[ i+1 ] + x[ i ];
261+
}
262+
}
263+
dnancusumkbn2( x.length, 0.0, x, -1, x.length-1, y, -1, y.length-1 );
264+
t.deepEqual( y, expected, 'returns expected value' );
265+
t.end();
266+
});
267+
268+
tape( 'the function supports an `x` offset', function test( t ) {
269+
var expected;
270+
var N;
271+
var x;
272+
var y;
273+
274+
x = new Float64Array([
275+
2.0,
276+
1.0, // 0
277+
2.0,
278+
-2.0, // 1
279+
-2.0,
280+
2.0, // 2
281+
3.0,
282+
4.0 // 3
283+
]);
284+
y = new Float64Array([
285+
0.0, // 0
286+
0.0, // 1
287+
0.0, // 2
288+
0.0, // 3
289+
0.0,
290+
0.0,
291+
0.0,
292+
0.0
293+
]);
294+
N = 4;
295+
296+
dnancusumkbn2( N, 0.0, x, 2, 1, y, 1, 0 );
297+
298+
expected = new Float64Array( [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ] );
299+
300+
t.deepEqual( y, expected, 'returns expected value' );
301+
t.end();
302+
});
303+
304+
tape( 'the function supports a `y` offset', function test( t ) {
305+
var expected;
306+
var N;
307+
var x;
308+
var y;
309+
310+
x = new Float64Array([
311+
2.0, // 0
312+
1.0, // 1
313+
2.0, // 2
314+
-2.0, // 3
315+
-2.0,
316+
2.0,
317+
3.0,
318+
4.0
319+
]);
320+
y = new Float64Array([
321+
0.0,
322+
0.0, // 0
323+
0.0,
324+
0.0, // 1
325+
0.0,
326+
0.0, // 2
327+
0.0,
328+
0.0 // 3
329+
]);
330+
N = 4;
331+
332+
dnancusumkbn2( N, 0.0, x, 1, 0, y, 2, 1 );
333+
334+
expected = new Float64Array( [ 0.0, 2.0, 0.0, 3.0, 0.0, 5.0, 0.0, 3.0 ] );
335+
336+
t.deepEqual( y, expected, 'returns expected value' );
337+
t.end();
338+
});
339+
340+
tape( 'the function supports complex access patterns', function test( t ) {
341+
var expected;
342+
var x;
343+
var y;
344+
var N;
345+
346+
x = new Float64Array([
347+
1.0, // 0
348+
2.0,
349+
3.0, // 1
350+
4.0,
351+
5.0, // 2
352+
6.0
353+
]);
354+
y = new Float64Array([
355+
0.0, // 2
356+
0.0, // 1
357+
0.0, // 0
358+
0.0,
359+
0.0,
360+
0.0
361+
]);
362+
N = 4;
363+
364+
dnancusumkbn2( N, 0.0, x, 2, 0, y, -1, 2 );
365+
366+
expected = new Float64Array( [ 9.0, 4.0, 1.0, 0.0, 0.0, 0.0 ] );
367+
368+
t.deepEqual( y, expected, 'returns expected value' );
369+
t.end();
370+
});

0 commit comments

Comments
 (0)