Skip to content

Commit ee5c909

Browse files
committed
test: add 1d tests
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 42a8d57 commit ee5c909

File tree

1 file changed

+219
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/some-by/test

1 file changed

+219
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 ones = require( '@stdlib/array/ones' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var real = require( '@stdlib/complex/float64/real' );
27+
var imag = require( '@stdlib/complex/float64/imag' );
28+
var Complex128Array = require( '@stdlib/array/complex128' );
29+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
30+
var ndarray = require( '@stdlib/ndarray/ctor' );
31+
var someBy = require( './../lib' );
32+
33+
34+
// TESTS //
35+
36+
tape( 'main export is a function', function test( t ) {
37+
t.ok( true, __filename );
38+
t.strictEqual( typeof someBy, 'function', 'main export is a function');
39+
t.end();
40+
});
41+
42+
tape( 'the function tests whether at `n` elements in a 1-dimensional ndarray pass a test implemented by a predicate function', function test( t ) {
43+
var actual;
44+
var x;
45+
var n;
46+
47+
x = ndarray( 'float64', zeros( 8, 'float64' ), [ 4 ], [ 2 ], 1, 'row-major' );
48+
n = scalar2ndarray( 1, {
49+
'dtype': 'generic'
50+
});
51+
52+
actual = someBy( [ x, n ], clbk );
53+
t.strictEqual( actual, false, 'returns expected value' );
54+
55+
x = ndarray( 'float64', ones( 8, 'float64' ), [ 4 ], [ 2 ], 1, 'row-major' );
56+
57+
actual = someBy( [ x, n ], clbk );
58+
t.strictEqual( actual, true, 'returns expected value' );
59+
60+
t.end();
61+
62+
function clbk( v ) {
63+
return v !== 0;
64+
}
65+
});
66+
67+
tape( 'the function tests whether at `n` elements in a 0-dimensional ndarray pass a test implemented by a predicate function (accessors)', function test( t ) {
68+
var actual;
69+
var xbuf;
70+
var x;
71+
var n;
72+
73+
xbuf = zeros( 6*2, 'float64' );
74+
x = ndarray( 'complex128', new Complex128Array( xbuf ), [ 4 ], [ 1 ], 1, 'row-major' );
75+
n = scalar2ndarray( 1, {
76+
'dtype': 'generic'
77+
});
78+
79+
actual = someBy( [ x, n ], clbk );
80+
t.strictEqual( actual, false, 'returns expected value' );
81+
82+
xbuf = ones( 6*2, 'float64' );
83+
x = ndarray( 'complex128', new Complex128Array( xbuf ), [ 4 ], [ 1 ], 1, 'row-major' );
84+
85+
actual = someBy( [ x, n ], clbk );
86+
t.strictEqual( actual, true, 'returns expected value' );
87+
88+
t.end();
89+
90+
function clbk( v ) {
91+
return ( real( v ) !== 0.0 && imag( v ) !== 0.0 );
92+
}
93+
});
94+
95+
tape( 'the function supports specifying the callback execution context', function test( t ) {
96+
var expected;
97+
var indices;
98+
var values;
99+
var arrays;
100+
var actual;
101+
var ctx;
102+
var x;
103+
var n;
104+
105+
x = new ndarray( 'float64', ones( 8, 'float64'), [ 4 ], [ 2 ], 0, 'row-major' );
106+
n = scalar2ndarray( 4, {
107+
'dtype': 'generic'
108+
});
109+
110+
indices = [];
111+
values = [];
112+
arrays = [];
113+
114+
ctx = {
115+
'count': 0
116+
};
117+
actual = someBy( [ x, n ], clbk, ctx );
118+
119+
t.strictEqual( actual, true, 'returns expected value' );
120+
t.strictEqual( ctx.count, 4, 'returns expected value' );
121+
122+
expected = [
123+
1.0,
124+
1.0,
125+
1.0,
126+
1.0
127+
];
128+
t.deepEqual( values, expected, 'returns expected value' );
129+
130+
expected = [
131+
[ 0 ],
132+
[ 1 ],
133+
[ 2 ],
134+
[ 3 ]
135+
];
136+
t.deepEqual( indices, expected, 'returns expected value' );
137+
138+
expected = [
139+
x,
140+
x,
141+
x,
142+
x
143+
];
144+
t.deepEqual( arrays, expected, 'returns expected value' );
145+
146+
t.end();
147+
148+
function clbk( v, idx, arr ) {
149+
this.count += 1; // eslint-disable-line no-invalid-this
150+
values.push( v );
151+
indices.push( idx );
152+
arrays.push( arr );
153+
return v !== 0.0;
154+
}
155+
});
156+
157+
tape( 'the function supports specifying the callback execution context (accessors)', function test( t ) {
158+
var expected;
159+
var indices;
160+
var values;
161+
var arrays;
162+
var actual;
163+
var xbuf;
164+
var ctx;
165+
var x;
166+
var n;
167+
168+
xbuf = ones( 6*2, 'float64' );
169+
x = ndarray( 'complex128', new Complex128Array( xbuf ), [ 4 ], [ 1 ], 1, 'row-major' );
170+
n = scalar2ndarray( 4, {
171+
'dtype': 'generic'
172+
});
173+
174+
indices = [];
175+
values = [];
176+
arrays = [];
177+
178+
ctx = {
179+
'count': 0
180+
};
181+
actual = someBy( [ x, n ], clbk, ctx );
182+
183+
t.strictEqual( actual, true, 'returns expected value' );
184+
t.strictEqual( ctx.count, 4, 'returns expected value' );
185+
186+
expected = [
187+
[ 1.0, 1.0 ],
188+
[ 1.0, 1.0 ],
189+
[ 1.0, 1.0 ],
190+
[ 1.0, 1.0 ]
191+
];
192+
t.deepEqual( values, expected, 'returns expected value' );
193+
194+
expected = [
195+
[ 0 ],
196+
[ 1 ],
197+
[ 2 ],
198+
[ 3 ]
199+
];
200+
t.deepEqual( indices, expected, 'returns expected value' );
201+
202+
expected = [
203+
x,
204+
x,
205+
x,
206+
x
207+
];
208+
t.deepEqual( arrays, expected, 'returns expected value' );
209+
210+
t.end();
211+
212+
function clbk( v, idx, arr ) {
213+
this.count += 1; // eslint-disable-line no-invalid-this
214+
values.push( [ real( v ), imag( v ) ] );
215+
indices.push( idx );
216+
arrays.push( arr );
217+
return ( real( v ) !== 0.0 && imag( v ) !== 0.0 );
218+
}
219+
});

0 commit comments

Comments
 (0)