Skip to content

Commit b403898

Browse files
committed
test: add initial 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 590a7d9 commit b403898

File tree

1 file changed

+151
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/binary/test

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
/* eslint-disable max-len */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var tape = require( 'tape' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var ones = require( '@stdlib/array/ones' );
29+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
30+
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
31+
var numel = require( '@stdlib/ndarray/base/numel' );
32+
var binary = 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 binary, 'function', 'main export is a function' );
40+
t.end();
41+
});
42+
43+
tape( 'the function throws an error if provided input and output ndarrays which do not have the same number of dimensions', function test( t ) {
44+
var shapes;
45+
var i;
46+
47+
shapes = [
48+
[ [ 4, 2, 1 ], [ 4, 2, 1 ], [ 4, 2 ] ],
49+
[ [ 2, 2 ], [ 2, 2, 2 ], [ 2, 2, 2 ] ],
50+
[ [ 1, 1, 1, 1 ], [ 1, 1, 1 ], [ 1, 1 ] ],
51+
[ [ 2, 2, 1, 2 ], [ 2, 2, 1 ], [ 2, 1, 2 ] ],
52+
[ [ 1, 1, 4, 2, 2, 2 ], [ 0 ], [ 10, 2 ] ],
53+
[ [ 1, 1, 1, 1 ], [ 1, 1, 1 ], [ 1, 1, 1, 1, 1 ] ]
54+
];
55+
56+
for ( i = 0; i < shapes.length; i++ ) {
57+
t.throws( badValue( shapes[i][0], shapes[i][1], shapes[i][2] ), Error, 'throws an error for index ' + i );
58+
}
59+
t.end();
60+
61+
function add( a, b ) {
62+
return a + b;
63+
}
64+
65+
function badValue( sh1, sh2, sh3 ) {
66+
return function badValue() {
67+
var dtype;
68+
var ord;
69+
var st1;
70+
var st2;
71+
var st3;
72+
var o1;
73+
var o2;
74+
var o3;
75+
var x;
76+
var y;
77+
var z;
78+
79+
ord = 'row-major';
80+
dtype = 'float64';
81+
82+
st1 = shape2strides( sh1, ord );
83+
st2 = shape2strides( sh2, ord );
84+
st3 = shape2strides( sh3, ord );
85+
o1 = strides2offset( sh1, st1 );
86+
o2 = strides2offset( sh2, st2 );
87+
o3 = strides2offset( sh3, st3 );
88+
89+
x = ndarray( dtype, ones( numel( sh1 ), dtype ), sh1, st1, o1, ord );
90+
y = ndarray( dtype, ones( numel( sh2 ), dtype ), sh2, st2, o2, ord );
91+
z = ndarray( dtype, zeros( numel( sh3 ), dtype ), sh3, st3, o3, ord );
92+
93+
binary( [ x, y, z ], add );
94+
};
95+
}
96+
});
97+
98+
tape( 'the function throws an error if provided input and output ndarrays which do not have the same shape', function test( t ) {
99+
var shapes;
100+
var i;
101+
102+
shapes = [
103+
[ [ 4, 2, 1 ], [ 4, 2, 2 ], [ 4, 2, 1 ] ],
104+
[ [ 3, 3 ], [ 2, 2 ], [ 3, 3 ] ],
105+
[ [ 5, 5, 5 ], [ 5, 5, 4 ], [ 5, 5, 4 ] ],
106+
[ [ 1, 1, 1 ], [ 2, 2, 2 ], [ 1, 1, 2 ] ],
107+
[ [ 1, 4 ], [ 3, 8 ], [ 4, 4 ] ],
108+
[ [ 10, 2, 1 ], [ 1, 2, 10 ], [ 2, 1, 10 ] ]
109+
];
110+
111+
for ( i = 0; i < shapes.length; i++ ) {
112+
t.throws( badValue( shapes[i][0], shapes[i][1], shapes[i][2] ), Error, 'throws an error for index ' + i );
113+
}
114+
t.end();
115+
116+
function add( a, b ) {
117+
return a + b;
118+
}
119+
120+
function badValue( sh1, sh2, sh3 ) {
121+
return function badValue() {
122+
var dtype;
123+
var ord;
124+
var st1;
125+
var st2;
126+
var st3;
127+
var o1;
128+
var o2;
129+
var o3;
130+
var x;
131+
var y;
132+
var z;
133+
134+
ord = 'row-major';
135+
dtype = 'float64';
136+
137+
st1 = shape2strides( sh1, ord );
138+
st2 = shape2strides( sh2, ord );
139+
st3 = shape2strides( sh3, ord );
140+
o1 = strides2offset( sh1, st1 );
141+
o2 = strides2offset( sh2, st2 );
142+
o3 = strides2offset( sh3, st3 );
143+
144+
x = ndarray( dtype, ones( numel( sh1 ), dtype ), sh1, st1, o1, ord );
145+
y = ndarray( dtype, ones( numel( sh2 ), dtype ), sh2, st2, o2, ord );
146+
z = ndarray( dtype, zeros( numel( sh3 ), dtype ), sh3, st3, o3, ord );
147+
148+
binary( [ x, y, z ], add );
149+
};
150+
}
151+
});

0 commit comments

Comments
 (0)