Skip to content

Commit 818e011

Browse files
kgrytesahil20021008
authored andcommitted
refactor: update to enforce mostly safe casts
This commit aligns `ndarray/base/fill` behavior with `ndarray/base/slice-assign` where we also enforce mostly safe casts. --- 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: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - 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 e83694a commit 818e011

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

lib/node_modules/@stdlib/ndarray/base/fill/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ A provided ndarray should be an object with the following properties:
9696
## Notes
9797

9898
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input ndarray with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
99+
- A `value` must be able to safely cast to the input ndarray [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
99100
- The function **mutates** the input ndarray.
100101

101102
</section>

lib/node_modules/@stdlib/ndarray/base/fill/docs/repl.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
Input ndarray-like object.
1515

1616
value: any
17-
Scalar value.
17+
Scalar value. Must be able to safely cast to the input ndarray data
18+
type. Scalar values having floating-point data types (both real and
19+
complex) are allowed to downcast to a lower precision data type of the
20+
same kind (e.g., a scalar double-precision floating-point number can be
21+
used to fill a 'float32' input ndarray).
1822

1923
Examples
2024
--------

lib/node_modules/@stdlib/ndarray/base/fill/docs/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ComplexLike } from '@stdlib/types/complex';
2929
* ## Notes
3030
*
3131
* - If `value` is a number, the function fills an input ndarray with a complex number whose real component equals the provided scalar value and whose imaginary component is zero.
32+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
3233
*
3334
* @param x - input ndarray
3435
* @param value - scalar value
@@ -68,6 +69,10 @@ declare function fill( x: complexndarray, value: number | ComplexLike ): void;
6869
/**
6970
* Fills an input ndarray with a specified value.
7071
*
72+
* ## Notes
73+
*
74+
* - A `value` must be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a 'float32' input ndarray).
75+
*
7176
* @param x - input ndarray
7277
* @param value - scalar value
7378
*

lib/node_modules/@stdlib/ndarray/base/fill/lib/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
// MODULES //
2222

23+
var isScalarMostlySafeCompatible = require( '@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible' ); // eslint-disable-line id-length
2324
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
2425
var getDtype = require( '@stdlib/ndarray/base/dtype' );
2526
var getShape = require( '@stdlib/ndarray/base/shape' );
2627
var getOrder = require( '@stdlib/ndarray/base/order' );
2728
var assign = require( '@stdlib/ndarray/base/assign' );
29+
var format = require( '@stdlib/string/format' );
2830

2931

3032
// MAIN //
@@ -40,6 +42,7 @@ var assign = require( '@stdlib/ndarray/base/assign' );
4042
* @param {NonNegativeInteger} x.offset - index offset
4143
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
4244
* @param {*} value - scalar value
45+
* @throws {TypeError} second argument cannot be safely cast to the input array data type
4346
* @returns {void}
4447
*
4548
* @example
@@ -73,10 +76,17 @@ var assign = require( '@stdlib/ndarray/base/assign' );
7376
* // => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
7477
*/
7578
function fill( x, value ) {
79+
var dt;
7680
var v;
7781

82+
dt = getDtype( x );
83+
84+
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the output data type is floating-point...
85+
if ( !isScalarMostlySafeCompatible( value, dt ) ) {
86+
throw new TypeError( format( 'invalid argument. The second argument cannot be safely cast to the input array data type. Data type: %s. Value: `%s`.', dt, value ) );
87+
}
7888
// Broadcast the fill value to an ndarray of same shape and data type as the input ndarray:
79-
v = broadcastScalar( value, getDtype( x ), getShape( x ), getOrder( x ) );
89+
v = broadcastScalar( value, dt, getShape( x ), getOrder( x ) );
8090

8191
// Assign the fill value to each element of the input ndarray:
8292
assign( [ v, x ] ); // TODO: consider replacing with ndarray/base/assign-scalar in order to avoid zero-dimensional ndarray creation and subsequent broadcasting

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ tape( 'main export is a function', function test( t ) {
4343
t.end();
4444
});
4545

46+
tape( 'the function throws an error if provided a second argument which cannot be safely cast to the input ndarray data type', function test( t ) {
47+
var values;
48+
var x;
49+
var i;
50+
51+
x = scalar2ndarray( 0.0, {
52+
'dtype': 'int32'
53+
});
54+
55+
values = [
56+
'5',
57+
3.14,
58+
NaN,
59+
true,
60+
false,
61+
null,
62+
void 0,
63+
[],
64+
{},
65+
function noop() {}
66+
];
67+
for ( i = 0; i < values.length; i++ ) {
68+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
69+
}
70+
t.end();
71+
72+
function badValue( value ) {
73+
return function badValue() {
74+
fill( x, value );
75+
};
76+
}
77+
});
78+
4679
tape( 'the function fills a 0-dimensional input ndarray with a specified value', function test( t ) {
4780
var expected;
4881
var x;

0 commit comments

Comments
 (0)