Skip to content

Commit 67119d0

Browse files
committed
Add benchmarks and fix bounds checks
1 parent b888d12 commit 67119d0

File tree

2 files changed

+164
-3
lines changed

2 files changed

+164
-3
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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 Float32Array = require( '@stdlib/array/float32' );
25+
var Complex64 = require( '@stdlib/complex/float32' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::complex_number:set', function benchmark( b ) {
33+
var values;
34+
var arr;
35+
var N;
36+
var v;
37+
var i;
38+
39+
values = [];
40+
for ( i = 0; i < 10; i++ ) {
41+
values.push( new Complex64( i, i ) );
42+
}
43+
arr = new Complex64Array( values );
44+
N = arr.length;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
v = arr.set( values[ (i+1)%N ] );
49+
if ( typeof v !== 'undefined' ) {
50+
b.fail( 'should return undefined' );
51+
}
52+
}
53+
b.toc();
54+
if ( typeof v !== 'undefined' ) {
55+
b.fail( 'should return undefined' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+'::array:set', function benchmark( b ) {
62+
var values;
63+
var arr;
64+
var N;
65+
var v;
66+
var i;
67+
68+
values = [];
69+
for ( i = 0; i < 10; i++ ) {
70+
values.push( new Complex64( i, i ) );
71+
}
72+
arr = new Complex64Array( values );
73+
N = arr.length;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
v = arr.set( [ values[ (i+1)%N ] ] );
78+
if ( typeof v !== 'undefined' ) {
79+
b.fail( 'should return undefined' );
80+
}
81+
}
82+
b.toc();
83+
if ( typeof v !== 'undefined' ) {
84+
b.fail( 'should return undefined' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
89+
90+
bench( pkg+'::typed_array:set', function benchmark( b ) {
91+
var values;
92+
var arr;
93+
var buf;
94+
var M;
95+
var N;
96+
var v;
97+
var i;
98+
99+
values = new Float32Array( 20 );
100+
M = values.length;
101+
for ( i = 0; i < M; i++ ) {
102+
values[ i ] = i;
103+
}
104+
105+
arr = new Complex64Array( values );
106+
N = arr.length;
107+
108+
buf = new Float32Array( 2 );
109+
110+
b.tic();
111+
for ( i = 0; i < b.iterations; i++ ) {
112+
buf[ 0 ] = values[ i%N ];
113+
buf[ 1 ] = values[ (i+1)%N ];
114+
v = arr.set( buf );
115+
if ( typeof v !== 'undefined' ) {
116+
b.fail( 'should return undefined' );
117+
}
118+
}
119+
b.toc();
120+
if ( typeof v !== 'undefined' ) {
121+
b.fail( 'should return undefined' );
122+
}
123+
b.pass( 'benchmark finished' );
124+
b.end();
125+
});
126+
127+
bench( pkg+'::complex_typed_array:set', function benchmark( b ) {
128+
var values;
129+
var arr;
130+
var buf;
131+
var M;
132+
var N;
133+
var v;
134+
var i;
135+
136+
values = new Float32Array( 20 );
137+
M = values.length;
138+
for ( i = 0; i < M; i++ ) {
139+
values[ i ] = i;
140+
}
141+
142+
arr = new Complex64Array( values );
143+
N = arr.length;
144+
145+
buf = new Complex64Array( 1 );
146+
buf.set( new Complex64( 1.0, -1.0 ) );
147+
148+
b.tic();
149+
for ( i = 0; i < b.iterations; i++ ) {
150+
v = arr.set( buf, i%N );
151+
if ( typeof v !== 'undefined' ) {
152+
b.fail( 'should return undefined' );
153+
}
154+
}
155+
b.toc();
156+
if ( typeof v !== 'undefined' ) {
157+
b.fail( 'should return undefined' );
158+
}
159+
b.pass( 'benchmark finished' );
160+
b.end();
161+
});

lib/node_modules/@stdlib/array/complex64/lib/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ Object.defineProperty( Complex64Array.prototype, 'set', {
906906
}
907907
if ( isComplexArray( value ) ) {
908908
N = value._length;
909-
if ( idx+N >= this._length ) {
909+
if ( idx+N > this._length ) {
910910
throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
911911
}
912912
sbuf = value._buffer;
@@ -951,7 +951,7 @@ Object.defineProperty( Complex64Array.prototype, 'set', {
951951
if ( !isEven( N ) ) {
952952
throw new RangeError( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `'+N+'`.' );
953953
}
954-
if ( idx+(N/2) >= this._length ) {
954+
if ( idx+(N/2) > this._length ) {
955955
throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
956956
}
957957
sbuf = value;
@@ -984,7 +984,7 @@ Object.defineProperty( Complex64Array.prototype, 'set', {
984984
return;
985985
}
986986
// If an array contains only complex numbers, then we need to extract real and imaginary components...
987-
if ( idx+N >= this._length ) {
987+
if ( idx+N > this._length ) {
988988
throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
989989
}
990990
idx *= 2;

0 commit comments

Comments
 (0)