Skip to content

Commit f7fe883

Browse files
committed
test: add tests upto 2d
--- 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 2ca2802 commit f7fe883

File tree

3 files changed

+1532
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)