Skip to content

Commit 8a3d4d6

Browse files
committed
test: add edge cases
--- 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 ff48ed3 commit 8a3d4d6

File tree

1 file changed

+104
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary/test

1 file changed

+104
-0
lines changed

lib/node_modules/@stdlib/ndarray/base/unary/test/test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var ndarray = require( '@stdlib/ndarray/ctor' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var ones = require( '@stdlib/array/ones' );
27+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
28+
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
29+
var numel = require( '@stdlib/ndarray/base/numel' );
2430
var unary = require( './../lib' );
2531

2632

@@ -31,3 +37,101 @@ tape( 'main export is a function', function test( t ) {
3137
t.strictEqual( typeof unary, 'function', 'main export is a function' );
3238
t.end();
3339
});
40+
41+
tape( 'the function throws an error if provided input and output ndarrays which do not have the same number of dimensions', function test( t ) {
42+
var shapes;
43+
var i;
44+
45+
shapes = [
46+
[ [ 4, 2, 1 ], [ 4, 2 ] ],
47+
[ [ 2, 2 ], [ 2, 2, 2 ] ],
48+
[ [ 1, 1, 1, 1 ], [ 1, 1, 1 ] ],
49+
[ [ 2, 2, 1, 2 ], [ 2, 1, 2 ] ],
50+
[ [ 1, 1, 4, 2, 2, 2 ], [ 10, 2 ] ],
51+
[ [ 1, 1, 1, 1 ], [ 1, 1, 1 ] ]
52+
];
53+
54+
for ( i = 0; i < shapes.length; i++ ) {
55+
t.throws( badValue( shapes[i][0], shapes[i][1] ), Error, 'throws an error when input shape is ' + shapes[i][0] + ' and output shape is ' + shapes[i][1] );
56+
}
57+
t.end();
58+
59+
function scale( z ) {
60+
return z * 10.0;
61+
}
62+
63+
function badValue( sh1, sh2 ) {
64+
return function badValue() {
65+
var dtype;
66+
var ord;
67+
var st1;
68+
var st2;
69+
var o1;
70+
var o2;
71+
var x;
72+
var y;
73+
74+
ord = 'row-major';
75+
dtype = 'float64';
76+
77+
st1 = shape2strides( sh1, ord );
78+
st2 = shape2strides( sh2, ord );
79+
o1 = strides2offset( sh1, st1 );
80+
o2 = strides2offset( sh2, st2 );
81+
82+
x = ndarray( dtype, ones( numel( sh1 ), dtype ), sh1, st1, o1, ord );
83+
y = ndarray( dtype, zeros( numel( sh2 ), dtype ), sh2, st2, o2, ord );
84+
85+
unary( [ x, y ], scale );
86+
};
87+
}
88+
});
89+
90+
tape( 'the function throws an error if provided input and output ndarrays which do not have the same shape', function test( t ) {
91+
var shapes;
92+
var i;
93+
94+
shapes = [
95+
[ [ 4, 2, 1 ], [ 4, 2, 2 ] ],
96+
[ [ 3, 3 ], [ 2, 2 ] ],
97+
[ [ 5, 5, 5 ], [ 5, 5, 4 ] ],
98+
[ [ 1, 1, 1 ], [ 2, 2, 2 ] ],
99+
[ [ 1, 4, 1, 2, 2 ], [ 3, 8 ] ],
100+
[ [ 10, 2, 1 ], [ 1, 2, 10 ] ]
101+
];
102+
103+
for ( i = 0; i < shapes.length; i++ ) {
104+
t.throws( badValue( shapes[i][0], shapes[i][1] ), Error, 'throws an error when input shape is ' + shapes[i][0] + ' and output shape is ' + shapes[i][1] );
105+
}
106+
t.end();
107+
108+
function scale( z ) {
109+
return z * 10.0;
110+
}
111+
112+
function badValue( sh1, sh2 ) {
113+
return function badValue() {
114+
var dtype;
115+
var ord;
116+
var st1;
117+
var st2;
118+
var o1;
119+
var o2;
120+
var x;
121+
var y;
122+
123+
ord = 'row-major';
124+
dtype = 'float64';
125+
126+
st1 = shape2strides( sh1, ord );
127+
st2 = shape2strides( sh2, ord );
128+
o1 = strides2offset( sh1, st1 );
129+
o2 = strides2offset( sh2, st2 );
130+
131+
x = ndarray( dtype, ones( numel( sh1 ), dtype ), sh1, st1, o1, ord );
132+
y = ndarray( dtype, zeros( numel( sh2 ), dtype ), sh2, st2, o2, ord );
133+
134+
unary( [ x, y ], scale );
135+
};
136+
}
137+
});

0 commit comments

Comments
 (0)