Skip to content

Commit d482e35

Browse files
committed
Add assertion utility to test for a cube number
1 parent 497dc2d commit d482e35

File tree

15 files changed

+1269
-0
lines changed

15 files changed

+1269
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# isCubeNumber
22+
23+
> Test if a value is a cube number.
24+
25+
<section class="intro">
26+
27+
A **cube number** is defined as an integer value which is the cube of an integer.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var isCubeNumber = require( '@stdlib/assert/is-cube-number' );
39+
```
40+
41+
#### isCubeNumber( value )
42+
43+
Tests if a `value` is a cube number.
44+
45+
<!-- eslint-disable no-new-wrappers -->
46+
47+
```javascript
48+
var Number = require( '@stdlib/number/ctor' );
49+
50+
var bool = isCubeNumber( 8.0 );
51+
// returns true
52+
53+
bool = isCubeNumber( new Number( 8.0 ) );
54+
// returns true
55+
56+
bool = isCubeNumber( 3.14 );
57+
// returns false
58+
59+
bool = isCubeNumber( -5.0 );
60+
// returns false
61+
62+
bool = isCubeNumber( NaN );
63+
// returns false
64+
65+
bool = isCubeNumber( null );
66+
// returns false
67+
```
68+
69+
#### isCubeNumber.isPrimitive( value )
70+
71+
Tests if a `value` is a primitive cube number.
72+
73+
<!-- eslint-disable no-new-wrappers -->
74+
75+
```javascript
76+
var Number = require( '@stdlib/number/ctor' );
77+
78+
var bool = isCubeNumber.isPrimitive( 8.0 );
79+
// returns true
80+
81+
bool = isCubeNumber.isPrimitive( new Number( 8.0 ) );
82+
// returns false
83+
```
84+
85+
#### isCubeNumber.isObject( value )
86+
87+
Tests if a `value` is a `Number` object having a value which is a cube number.
88+
89+
<!-- eslint-disable no-new-wrappers -->
90+
91+
```javascript
92+
var Number = require( '@stdlib/number/ctor' );
93+
94+
var bool = isCubeNumber.isObject( 8.0 );
95+
// returns false
96+
97+
bool = isCubeNumber.isObject( new Number( 8.0 ) );
98+
// returns true
99+
```
100+
101+
</section>
102+
103+
<!-- /.usage -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint-disable no-new-wrappers -->
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var Number = require( '@stdlib/number/ctor' );
115+
var isCubeNumber = require( '@stdlib/assert/is-cube-number' );
116+
117+
var bool = isCubeNumber( 8.0 );
118+
// returns true
119+
120+
bool = isCubeNumber( new Number( 8.0 ) );
121+
// returns true
122+
123+
bool = isCubeNumber( 0.0 );
124+
// returns true
125+
126+
bool = isCubeNumber( 1.0 );
127+
// returns true
128+
129+
bool = isCubeNumber( 3.14 );
130+
// returns false
131+
132+
bool = isCubeNumber( -5.0 );
133+
// returns false
134+
135+
bool = isCubeNumber( NaN );
136+
// returns false
137+
138+
bool = isCubeNumber( '0.5' );
139+
// returns false
140+
141+
bool = isCubeNumber( null );
142+
// returns false
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<section class="links">
150+
151+
</section>
152+
153+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 no-new-wrappers, no-empty-function */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var bench = require( '@stdlib/bench' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var Number = require( '@stdlib/number/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var isCubeNumber = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+'::primitives', function benchmark( b ) {
35+
var values;
36+
var bool;
37+
var i;
38+
39+
values = [
40+
'5',
41+
5,
42+
4,
43+
0.5,
44+
1.0,
45+
0.0,
46+
3.14,
47+
-5,
48+
-4,
49+
NaN,
50+
true,
51+
false,
52+
null,
53+
void 0
54+
];
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
bool = isCubeNumber( values[ i % values.length ] );
59+
if ( !isBoolean( bool ) ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isBoolean( bool ) ) {
65+
b.fail( 'should return a boolean' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+'::objects', function benchmark( b ) {
72+
var values;
73+
var bool;
74+
var i;
75+
76+
values = [
77+
[],
78+
{},
79+
function noop() {},
80+
new Number( 8.0 ),
81+
new Number( 0.5 ),
82+
new Number( NaN ),
83+
new Number( 3.14 )
84+
];
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
bool = isCubeNumber( values[ i % values.length ] );
89+
if ( !isBoolean( bool ) ) {
90+
b.fail( 'should return a boolean' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isBoolean( bool ) ) {
95+
b.fail( 'should return a boolean' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::primitives:isPrimitive', function benchmark( b ) {
102+
var values;
103+
var bool;
104+
var i;
105+
106+
values = [
107+
'5',
108+
5,
109+
4,
110+
3.14,
111+
0.5,
112+
1.0,
113+
0.0,
114+
-5,
115+
-4,
116+
NaN,
117+
true,
118+
false,
119+
null,
120+
void 0
121+
];
122+
123+
b.tic();
124+
for ( i = 0; i < b.iterations; i++ ) {
125+
bool = isCubeNumber.isPrimitive( values[ i % values.length ] );
126+
if ( !isBoolean( bool ) ) {
127+
b.fail( 'should return a boolean' );
128+
}
129+
}
130+
b.toc();
131+
if ( !isBoolean( bool ) ) {
132+
b.fail( 'should return a boolean' );
133+
}
134+
b.pass( 'benchmark finished' );
135+
b.end();
136+
});
137+
138+
bench( pkg+'::objects:isPrimitive', function benchmark( b ) {
139+
var values;
140+
var bool;
141+
var i;
142+
143+
values = [
144+
[],
145+
{},
146+
function noop() {},
147+
new Number( 8.0 ),
148+
new Number( 0.5 ),
149+
new Number( NaN ),
150+
new Number( 3.14 )
151+
];
152+
153+
b.tic();
154+
for ( i = 0; i < b.iterations; i++ ) {
155+
bool = isCubeNumber.isPrimitive( values[ i % values.length ] );
156+
if ( !isBoolean( bool ) ) {
157+
b.fail( 'should return a boolean' );
158+
}
159+
}
160+
b.toc();
161+
if ( !isBoolean( bool ) ) {
162+
b.fail( 'should return a boolean' );
163+
}
164+
b.pass( 'benchmark finished' );
165+
b.end();
166+
});
167+
168+
bench( pkg+'::primitives:isObject', function benchmark( b ) {
169+
var values;
170+
var bool;
171+
var i;
172+
173+
values = [
174+
'5',
175+
5,
176+
4,
177+
3.14,
178+
0.5,
179+
1.0,
180+
0.0,
181+
-5,
182+
-4,
183+
NaN,
184+
true,
185+
false,
186+
null,
187+
void 0
188+
];
189+
190+
b.tic();
191+
for ( i = 0; i < b.iterations; i++ ) {
192+
bool = isCubeNumber.isObject( values[ i % values.length ] );
193+
if ( !isBoolean( bool ) ) {
194+
b.fail( 'should return a boolean' );
195+
}
196+
}
197+
b.toc();
198+
if ( !isBoolean( bool ) ) {
199+
b.fail( 'should return a boolean' );
200+
}
201+
b.pass( 'benchmark finished' );
202+
b.end();
203+
});
204+
205+
bench( pkg+'::objects:isObject', function benchmark( b ) {
206+
var values;
207+
var bool;
208+
var i;
209+
210+
values = [
211+
[],
212+
{},
213+
function noop() {},
214+
new Number( 8.0 ),
215+
new Number( 0.5 ),
216+
new Number( NaN ),
217+
new Number( 3.14 )
218+
];
219+
220+
b.tic();
221+
for ( i = 0; i < b.iterations; i++ ) {
222+
bool = isCubeNumber.isObject( values[ i % values.length ] );
223+
if ( !isBoolean( bool ) ) {
224+
b.fail( 'should return a boolean' );
225+
}
226+
}
227+
b.toc();
228+
if ( !isBoolean( bool ) ) {
229+
b.fail( 'should return a boolean' );
230+
}
231+
b.pass( 'benchmark finished' );
232+
b.end();
233+
});

0 commit comments

Comments
 (0)