Skip to content

Commit 5fe826e

Browse files
committed
fix: allow 0d output arrays and address failing 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: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f5517c1 commit 5fe826e

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

  • lib/node_modules/@stdlib/ndarray/broadcast-scalar

lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2424
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
25+
var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' );
2526
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
2627
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
2728
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
@@ -95,7 +96,7 @@ function broadcastScalar( value, shape, options ) {
9596
'order': ORDER,
9697
'readonly': false
9798
};
98-
if ( !isNonNegativeIntegerArray( shape ) ) {
99+
if ( !isNonNegativeIntegerArray( shape ) && !isEmptyCollection( shape ) ) {
99100
throw new TypeError( format( 'invalid argument. First argument must be an array of nonnegative integers. Value: `%s`.', shape ) );
100101
}
101102
if ( arguments.length > 2 ) {

lib/node_modules/@stdlib/ndarray/broadcast-scalar/test/test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ tape( 'the function throws an error if provided a second argument which is not a
6969
false,
7070
null,
7171
void 0,
72-
[],
72+
[ 'foo' ],
7373
function noop() {}
7474
];
7575

@@ -98,7 +98,7 @@ tape( 'the function throws an error if provided a second argument which is not a
9898
false,
9999
null,
100100
void 0,
101-
[],
101+
[ 'foo' ],
102102
function noop() {}
103103
];
104104

@@ -311,7 +311,7 @@ tape( 'the function broadcasts a scalar value to an ndarray of a specified shape
311311
t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' );
312312
t.strictEqual( numel( arr ), 4, 'returns expected value' );
313313

314-
expected = new Uint8Array( [ 0, 0, 0, 0 ] );
314+
expected = new Uint8Array( [ 0 ] );
315315

316316
arr = broadcastScalar( false, [ 2, 2 ] );
317317

@@ -663,7 +663,7 @@ tape( 'the function broadcasts a scalar value to an ndarray of a specified shape
663663
t.end();
664664
});
665665

666-
tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (empty)', function test( t ) {
666+
tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (0d)', function test( t ) {
667667
var expected;
668668
var arr;
669669

@@ -676,6 +676,24 @@ tape( 'the function broadcasts a scalar value to an ndarray of a specified shape
676676
t.strictEqual( instanceOf( getData( arr ), Float64Array ), true, 'returns expected value' );
677677
t.deepEqual( getData( arr ), expected, 'returns expected value' );
678678
t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' );
679+
t.strictEqual( numel( arr ), 1, 'returns expected value' );
680+
681+
t.end();
682+
});
683+
684+
tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (empty)', function test( t ) {
685+
var expected;
686+
var arr;
687+
688+
expected = new Float64Array( [ 1.0 ] );
689+
arr = broadcastScalar( 1.0, [ 2, 0, 2 ] );
690+
691+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
692+
t.strictEqual( String( getDType( arr ) ), 'float64', 'returns expected value' );
693+
t.deepEqual( getShape( arr ), [ 2, 0, 2 ], 'returns expected value' );
694+
t.strictEqual( instanceOf( getData( arr ), Float64Array ), true, 'returns expected value' );
695+
t.deepEqual( getData( arr ), expected, 'returns expected value' );
696+
t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' );
679697
t.strictEqual( numel( arr ), 0, 'returns expected value' );
680698

681699
t.end();

0 commit comments

Comments
 (0)