-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add at
method to array/fixed-endian-factory
#3184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.at.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var factory = require( './../lib' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var Float64ArrayFE = factory( 'float64' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+':at:endianness=little-endian', function benchmark( b ) { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var arr; | ||
var N; | ||
var v; | ||
var i; | ||
|
||
arr = []; | ||
for ( i = 0; i < 10; i++ ) { | ||
arr.push( i ); | ||
} | ||
arr = new Float64ArrayFE( 'little-endian', arr ); | ||
N = arr.length; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = arr.at( i % N ); | ||
if ( typeof v !== 'number' ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isNumber( v ) ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':at:endianness=little-endian', function benchmark( b ) { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var arr; | ||
var N; | ||
var v; | ||
var i; | ||
|
||
arr = []; | ||
for ( i = 0; i < 10; i++ ) { | ||
arr.push( i ); | ||
} | ||
arr = new Float64ArrayFE( 'little-endian', arr ); | ||
N = arr.length; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = arr.at( -( i % N ) - 1 ); | ||
if ( typeof v !== 'number' ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isNumber( v ) ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':at:endianness=big-endian', function benchmark( b ) { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var arr; | ||
var N; | ||
var v; | ||
var i; | ||
|
||
arr = []; | ||
for ( i = 0; i < 1000; i++ ) { | ||
arr.push( i ); | ||
} | ||
arr = new Float64ArrayFE( 'big-endian', arr ); | ||
N = arr.length; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = arr.at( i % N ); | ||
if ( typeof v !== 'number' ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isNumber( v ) ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':at:endianness=big-endian', function benchmark( b ) { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var arr; | ||
var N; | ||
var v; | ||
var i; | ||
|
||
arr = []; | ||
for ( i = 0; i < 1000; i++ ) { | ||
arr.push( i ); | ||
} | ||
arr = new Float64ArrayFE( 'big-endian', arr ); | ||
N = arr.length; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = arr.at( -( i % N ) - 1 ); | ||
if ( typeof v !== 'number' ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isNumber( v ) ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.at.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var tape = require( 'tape' ); | ||
var hasOwnProp = require( '@stdlib/assert/has-own-property' ); | ||
var isFunction = require( '@stdlib/assert/is-function' ); | ||
var factory = require( './../lib' ); | ||
|
||
|
||
// TESTS // | ||
|
||
tape( 'main export is a function', function test( t ) { | ||
t.ok( true, __filename ); | ||
t.strictEqual( typeof factory, 'function', 'main export is a function' ); | ||
t.end(); | ||
}); | ||
|
||
tape( 'the function returns a function', function test( t ) { | ||
var ctor = factory( 'float64' ); | ||
t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); | ||
t.end(); | ||
}); | ||
|
||
tape( 'attached to the prototype of the returned function is an `at` method', function test( t ) { | ||
var ctor = factory( 'float64' ); | ||
t.strictEqual( hasOwnProp( ctor.prototype, 'at' ), true, 'returns expected value' ); | ||
t.strictEqual( isFunction( ctor.prototype.at ), true, 'returns expected value' ); | ||
t.end(); | ||
}); | ||
|
||
tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { | ||
var values; | ||
var ctor; | ||
var arr; | ||
var i; | ||
|
||
ctor = factory( 'float64' ); | ||
arr = new ctor( 'little-endian', 5 ); | ||
|
||
values = [ | ||
'5', | ||
5, | ||
NaN, | ||
true, | ||
false, | ||
null, | ||
void 0, | ||
{}, | ||
[], | ||
function noop() {} | ||
]; | ||
for ( i = 0; i < values.length; i++ ) { | ||
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); | ||
} | ||
t.end(); | ||
|
||
function badValue( value ) { | ||
return function badValue() { | ||
return arr.at.call( value, 0 ); | ||
}; | ||
} | ||
}); | ||
|
||
tape( 'the method throws an error if provided an index argument which is not an integer', function test( t ) { | ||
var values; | ||
var ctor; | ||
var arr; | ||
var i; | ||
|
||
ctor = factory( 'float64' ); | ||
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); | ||
|
||
values = [ | ||
'5', | ||
3.14, | ||
NaN, | ||
true, | ||
false, | ||
null, | ||
void 0, | ||
{}, | ||
[], | ||
function noop() {} | ||
]; | ||
for ( i = 0; i < values.length; i++ ) { | ||
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); | ||
} | ||
t.end(); | ||
|
||
function badValue( value ) { | ||
return function badValue() { | ||
return arr.at( value ); | ||
}; | ||
} | ||
}); | ||
|
||
tape( 'the method returns `undefined` if provided an index which exceeds array dimensions', function test( t ) { | ||
var ctor; | ||
var arr; | ||
var v; | ||
var i; | ||
|
||
ctor = factory( 'float64' ); | ||
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] ); | ||
|
||
for ( i = -arr.length; i < arr.length; i++ ) { | ||
if ( i < 0 ) { | ||
v = arr.at( i - arr.length ); | ||
t.strictEqual( v, void 0, 'returns expected value for index '+(i-arr.length) ); | ||
} else { | ||
v = arr.at( arr.length+i ); | ||
t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) ); | ||
} | ||
} | ||
t.end(); | ||
}); | ||
|
||
tape( 'the method returns an array element', function test( t ) { | ||
var expected; | ||
var ctor; | ||
var arr; | ||
var v; | ||
var i; | ||
|
||
ctor = factory( 'float64' ); | ||
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); | ||
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]; | ||
|
||
for ( i = -arr.length; i < arr.length; i++ ) { | ||
v = arr.at( i ); | ||
t.strictEqual( v, expected[ i + arr.length ], 'returns expected value' ); | ||
} | ||
t.end(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.