Skip to content

Commit 0411de3

Browse files
committed
Add assertion utility to test for a supported casting mode
1 parent 8591aad commit 0411de3

File tree

8 files changed

+559
-0
lines changed

8 files changed

+559
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isCastingMode
22+
23+
> Test if an input value is a supported ndarray casting mode.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var isCastingMode = require( '@stdlib/ndarray/base/assert/is-casting-mode' );
41+
```
42+
43+
#### isCastingMode( value )
44+
45+
Tests if an input `value` is a supported ndarray casting mode.
46+
47+
```javascript
48+
var bool = isCastingMode( 'safe' );
49+
// returns true
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var isCastingMode = require( '@stdlib/ndarray/base/assert/is-casting-mode' );
74+
75+
var bool = isCastingMode( 'none' );
76+
// returns true
77+
78+
bool = isCastingMode( 'equiv' );
79+
// returns true
80+
81+
bool = isCastingMode( 'safe' );
82+
// returns true
83+
84+
bool = isCastingMode( 'same-kind' );
85+
// returns true
86+
87+
bool = isCastingMode( 'unsafe' );
88+
// returns true
89+
90+
bool = isCastingMode( '' );
91+
// returns false
92+
93+
bool = isCastingMode( 'foo' );
94+
// returns false
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="references">
104+
105+
</section>
106+
107+
<!-- /.references -->
108+
109+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
110+
111+
<section class="links">
112+
113+
</section>
114+
115+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var floor = require( '@stdlib/math/base/special/floor' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var isCastingMode = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var out;
36+
var v;
37+
var i;
38+
39+
values = [
40+
'none',
41+
'equiv',
42+
'safe',
43+
'same-kind',
44+
'unsafe',
45+
'foo',
46+
'bar',
47+
'',
48+
'beep',
49+
'boop'
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
v = values[ floor( randu()*values.length ) ];
55+
out = isCastingMode( v );
56+
if ( typeof out !== 'boolean' ) {
57+
b.fail( 'should return a boolean' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isBoolean( out ) ) {
62+
b.fail( 'should return a boolean' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( value )
3+
Tests if an input value is a supported ndarray casting mode.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating if an input value is a supported ndarray casting
14+
mode.
15+
16+
Examples
17+
--------
18+
> var bool = {{alias}}( 'none' )
19+
true
20+
> bool = {{alias}}( 'equiv' )
21+
true
22+
> bool = {{alias}}( 'safe' )
23+
true
24+
> bool = {{alias}}( 'same-kind' )
25+
true
26+
> bool = {{alias}}( 'unsafe' )
27+
true
28+
> bool = {{alias}}( '' )
29+
false
30+
> bool = {{alias}}( 'beep' )
31+
false
32+
33+
See Also
34+
--------
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
var isCastingMode = require( './../lib' );
22+
23+
var bool = isCastingMode( 'none' );
24+
console.log( bool );
25+
// => true
26+
27+
bool = isCastingMode( 'equiv' );
28+
console.log( bool );
29+
// => true
30+
31+
bool = isCastingMode( 'safe' );
32+
console.log( bool );
33+
// => true
34+
35+
bool = isCastingMode( 'same-kind' );
36+
console.log( bool );
37+
// => true
38+
39+
bool = isCastingMode( 'unsafe' );
40+
console.log( bool );
41+
// => true
42+
43+
bool = isCastingMode( '' );
44+
console.log( bool );
45+
// => false
46+
47+
bool = isCastingMode( 'foo' );
48+
console.log( bool );
49+
// => false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
/**
22+
* Test whether an input value is a supported ndarray casting mode.
23+
*
24+
* @module @stdlib/ndarray/base/assert/is-casting-mode
25+
*
26+
* @example
27+
* var isCastingMode = require( '@stdlib/ndarray/base/assert/is-casting-mode' );
28+
*
29+
* var bool = isCastingMode( 'none' );
30+
* // returns true
31+
*
32+
* bool = isCastingMode( 'equiv' );
33+
* // returns true
34+
*
35+
* bool = isCastingMode( 'safe' );
36+
* // returns true
37+
*
38+
* bool = isCastingMode( 'same-kind' );
39+
* // returns true
40+
*
41+
* bool = isCastingMode( 'unsafe' );
42+
* // returns true
43+
*
44+
* bool = isCastingMode( 'foo' );
45+
* // returns false
46+
*/
47+
48+
// MODULES //
49+
50+
var isCastingMode = require( './main.js' );
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = isCastingMode;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
// MODULES //
22+
23+
var modes = require( '@stdlib/ndarray/casting-modes' );
24+
25+
26+
// VARIABLES //
27+
28+
var MODES = modes();
29+
var len = MODES.length;
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Tests whether an input value is a supported ndarray casting mode.
36+
*
37+
* @param {*} v - value to test
38+
* @returns {boolean} boolean indicating whether an input value is a supported ndarray casting mode
39+
*
40+
* @example
41+
* var bool = isCastingMode( 'none' );
42+
* // returns true
43+
*
44+
* bool = isCastingMode( 'equiv' );
45+
* // returns true
46+
*
47+
* bool = isCastingMode( 'safe' );
48+
* // returns true
49+
*
50+
* bool = isCastingMode( 'same-kind' );
51+
* // returns true
52+
*
53+
* bool = isCastingMode( 'unsafe' );
54+
* // returns true
55+
*
56+
* bool = isCastingMode( 'foo' );
57+
* // returns false
58+
*/
59+
function isCastingMode( v ) {
60+
var i;
61+
for ( i = 0; i < len; i++ ) {
62+
if ( v === MODES[ i ] ) {
63+
return true;
64+
}
65+
}
66+
return false;
67+
}
68+
69+
70+
// EXPORTS //
71+
72+
module.exports = isCastingMode;

0 commit comments

Comments
 (0)