Skip to content

Commit 2f7e474

Browse files
authored
test: add tests to ndarray/count-falsy
PR-URL: #9670 Closes: stdlib-js/metr-issue-tracker#147 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 141399c commit 2f7e474

File tree

2 files changed

+710
-0
lines changed

2 files changed

+710
-0
lines changed
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 ndarray = require( '@stdlib/ndarray/ctor' );
26+
var empty = require( '@stdlib/ndarray/empty' );
27+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
28+
var countFalsy = require( './../lib/assign.js' );
29+
30+
31+
// TESTS //
32+
33+
tape( 'main export is a function', function test( t ) {
34+
t.ok( true, __filename );
35+
t.strictEqual( typeof countFalsy, 'function', 'main export is a function' );
36+
t.end();
37+
});
38+
39+
tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) {
40+
var values;
41+
var y;
42+
var i;
43+
44+
y = empty( [], {
45+
'dtype': 'int32'
46+
});
47+
48+
values = [
49+
'5',
50+
5,
51+
NaN,
52+
true,
53+
false,
54+
null,
55+
void 0,
56+
{},
57+
[],
58+
function noop() {}
59+
];
60+
61+
for ( i = 0; i < values.length; i++ ) {
62+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
63+
}
64+
t.end();
65+
66+
function badValue( value ) {
67+
return function badValue() {
68+
countFalsy( value, y );
69+
};
70+
}
71+
});
72+
73+
tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) {
74+
var values;
75+
var y;
76+
var i;
77+
78+
y = empty( [], {
79+
'dtype': 'int32'
80+
});
81+
82+
values = [
83+
'5',
84+
5,
85+
NaN,
86+
true,
87+
false,
88+
null,
89+
void 0,
90+
{},
91+
[],
92+
function noop() {}
93+
];
94+
95+
for ( i = 0; i < values.length; i++ ) {
96+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
97+
}
98+
t.end();
99+
100+
function badValue( value ) {
101+
return function badValue() {
102+
countFalsy( value, y, {} );
103+
};
104+
}
105+
});
106+
107+
tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) {
108+
var values;
109+
var x;
110+
var i;
111+
112+
x = empty( [ 2, 2 ], {
113+
'dtype': 'float64'
114+
});
115+
116+
values = [
117+
'5',
118+
5,
119+
NaN,
120+
true,
121+
false,
122+
null,
123+
void 0,
124+
{},
125+
[],
126+
function noop() {}
127+
];
128+
129+
for ( i = 0; i < values.length; i++ ) {
130+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
131+
}
132+
t.end();
133+
134+
function badValue( value ) {
135+
return function badValue() {
136+
countFalsy( x, value );
137+
};
138+
}
139+
});
140+
141+
tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) {
142+
var values;
143+
var x;
144+
var i;
145+
146+
x = empty( [ 2, 2 ], {
147+
'dtype': 'float64'
148+
});
149+
150+
values = [
151+
'5',
152+
5,
153+
NaN,
154+
true,
155+
false,
156+
null,
157+
void 0,
158+
{},
159+
[],
160+
function noop() {}
161+
];
162+
163+
for ( i = 0; i < values.length; i++ ) {
164+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
165+
}
166+
t.end();
167+
168+
function badValue( value ) {
169+
return function badValue() {
170+
countFalsy( x, value, {} );
171+
};
172+
}
173+
});
174+
175+
tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) {
176+
var values;
177+
var x;
178+
var y;
179+
var i;
180+
181+
x = empty( [ 2, 2 ], {
182+
'dtype': 'float64'
183+
});
184+
y = empty( [ 2 ], {
185+
'dtype': 'int32'
186+
});
187+
188+
values = [
189+
'5',
190+
5,
191+
NaN,
192+
true,
193+
false,
194+
null,
195+
void 0,
196+
[],
197+
function noop() {}
198+
];
199+
200+
for ( i = 0; i < values.length; i++ ) {
201+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
202+
}
203+
t.end();
204+
205+
function badValue( value ) {
206+
return function badValue() {
207+
countFalsy( x, y, value );
208+
};
209+
}
210+
});
211+
212+
tape( 'the function throws an error if provided an options argument with an invalid `dims` property', function test( t ) {
213+
var values;
214+
var x;
215+
var y;
216+
var i;
217+
218+
x = empty( [ 2, 2 ], {
219+
'dtype': 'float64'
220+
});
221+
y = empty( [ 2 ], {
222+
'dtype': 'int32'
223+
});
224+
225+
values = [
226+
'5',
227+
NaN,
228+
true,
229+
false,
230+
null,
231+
void 0,
232+
{}
233+
];
234+
235+
for ( i = 0; i < values.length; i++ ) {
236+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
237+
}
238+
t.end();
239+
240+
function badValue( value ) {
241+
return function badValue() {
242+
var opts = {
243+
'dims': value
244+
};
245+
countFalsy( x, y, opts );
246+
};
247+
}
248+
});
249+
250+
tape( 'the function counts the number of falsy elements along one or more ndarray dimensions (row-major)', function test( t ) {
251+
var expected;
252+
var actual;
253+
var x;
254+
var y;
255+
256+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' );
257+
y = empty( [], {
258+
'dtype': 'int32'
259+
});
260+
261+
actual = countFalsy( x, y );
262+
expected = 2;
263+
264+
t.strictEqual( actual, y, 'returns expected value' );
265+
t.strictEqual( actual.get(), expected, 'returns expected value' );
266+
t.end();
267+
});
268+
269+
tape( 'the function counts the number of falsy elements along one or more ndarray dimensions (column-major)', function test( t ) {
270+
var expected;
271+
var actual;
272+
var x;
273+
var y;
274+
275+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' );
276+
y = empty( [], {
277+
'dtype': 'int32'
278+
});
279+
280+
actual = countFalsy( x, y );
281+
expected = 2;
282+
283+
t.strictEqual( actual, y, 'returns expected value' );
284+
t.strictEqual( actual.get(), expected, 'returns expected value' );
285+
t.end();
286+
});
287+
288+
tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) {
289+
var expected;
290+
var actual;
291+
var opts;
292+
var x;
293+
var y;
294+
295+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 3.0, 0.0, 0.0, 0.0, -5.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' );
296+
297+
opts = {
298+
'dims': [ 0 ]
299+
};
300+
y = empty( [ 4 ], {
301+
'dtype': 'int32'
302+
});
303+
actual = countFalsy( x, y, opts );
304+
expected = [ 1, 0, 1, 2 ];
305+
306+
t.strictEqual( actual, y, 'returns expected value' );
307+
t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' );
308+
t.end();
309+
});
310+
311+
tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) {
312+
var expected;
313+
var actual;
314+
var opts;
315+
var x;
316+
var y;
317+
318+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 3.0, 0.0, 0.0, 0.0, -5.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' );
319+
320+
opts = {
321+
'dims': [ 1 ]
322+
};
323+
y = empty( [ 2 ], {
324+
'dtype': 'int32'
325+
});
326+
actual = countFalsy( x, y, opts );
327+
expected = [ 2, 2 ];
328+
329+
t.strictEqual( actual, y, 'returns expected value' );
330+
t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' );
331+
t.end();
332+
});

0 commit comments

Comments
 (0)