From 5b17c89ff2f264f83f281d0a0009315b0d2a841e Mon Sep 17 00:00:00 2001 From: adityacodes30 Date: Wed, 27 Nov 2024 13:01:05 +0530 Subject: [PATCH 1/6] feat: main logic for indeOf --- .../array/fixed-endian-factory/lib/main.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index d15a80bf7da2..eca7f798f893 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -621,6 +621,53 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return this._buffer[ GETTER ]( idx*BYTES_PER_ELEMENT, this._isLE ); }); + /** + * Returns the index of the first occurrence of a given element. + * + * @name indexOf + * @memberof TypedArray.prototype + * @type {Function} + * @param {*} searchElement - element to search for + * @param {integer} fromIndex - index to start the search from (optional) + * @throws {TypeError} `this` must be a typed array instance + * @returns {integer} index of the first occurrence of the element, or -1 if not found + */ + setReadOnly( TypedArray.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { + var len; + var buf; + var i; + + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + + len = this._length; + + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex < 0 ) { + fromIndex += len; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } + } else { + fromIndex = 0; + } + + buf = this._buffer; + + for ( i = fromIndex; i < len; i++ ) { + if ( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) === searchElement ) { + return i; + } + } + + return -1; + }); + /** * Number of array elements. * From 180937517f3e127914b08da1356bfb0f1569d523 Mon Sep 17 00:00:00 2001 From: adityacodes30 Date: Wed, 27 Nov 2024 13:53:00 +0530 Subject: [PATCH 2/6] feat: indexOf implementation --- .../benchmark/benchmark.index_of.js | 56 +++++ .../benchmark/benchmark.index_of.length.js | 103 +++++++++ .../array/fixed-endian-factory/lib/main.js | 3 + .../test/test.index_of.js | 214 ++++++++++++++++++ 4 files changed, 376 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.length.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js new file mode 100644 index 000000000000..49e9d2f58cf1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js @@ -0,0 +1,56 @@ +/** +* @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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var factory = require( './../lib' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':indexOf', function benchmark( b ) { + var arr; + var idx; + var i; + + arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + idx = arr.indexOf( 5.0, 0 ); // Start searching from index 0 + if ( typeof idx !== 'number' ) { + b.fail( 'should return an integer' ); + } + } + b.toc(); + if ( !isInteger( idx ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.length.js new file mode 100644 index 000000000000..cb0dccc57af1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.length.js @@ -0,0 +1,103 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var factory = require( './../lib' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var idx; + var v; + var i; + + v = len - 1; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + idx = arr.indexOf( v ); + if ( typeof idx !== 'number' ) { + b.fail( 'should return an integer' ); + } + } + b.toc(); + if ( !isInteger( idx ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':indexOf:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index eca7f798f893..f2dccffb49a6 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -653,6 +653,9 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli fromIndex = 0; } } + if ( fromIndex >= len ) { + return -1; + } } else { fromIndex = 0; } diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js new file mode 100644 index 000000000000..b7da906cb4e6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js @@ -0,0 +1,214 @@ +/** +* @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 `indexOf` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'indexOf' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.indexOf ), 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.indexOf.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided `fromIndex` 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.indexOf( 1.0, value ); + }; + } +}); + +tape( 'the method returns `-1` if provided fromIndex argument 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 ] ); + + for ( i = arr.length; i < arr.length + 5; i++ ) { + v = arr.indexOf( 1.0, i ); + t.strictEqual( v, -1, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the method returns `-1` if operating on an empty array', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [] ); + + v = arr.indexOf( 1.0 ); + t.strictEqual( v, -1, 'returns -1 for an empty array' ); + + t.end(); +}); + +tape( 'the method returns `-1` if element not found', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + v = arr.indexOf( 10.0 ); + t.strictEqual( v, -1, 'returns -1 when element is not found' ); + + t.end(); +}); + +tape( 'the method returns the index of the first match if element is found', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0 ); + t.strictEqual( v, 1, 'returns index of the first match when the element is found' ); + + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, 2 ); + t.strictEqual( v, 4, 'returns index of the match starting from the specified index' ); + + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, -3 ); + t.strictEqual( v, 4, 'returns index of the match starting from the resolved negative index' ); + + t.end(); +}); + +tape( 'when provided a starting index which resolves to an index less than zero, the method searches from the first array element', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, -10 ); + t.strictEqual( v, 1, 'returns index of the match starting from the first array element when starting index resolves to less than zero' ); + + t.end(); +}); From 6615add998b3101f72efdfe1cc15f18073685b03 Mon Sep 17 00:00:00 2001 From: adityacodes30 Date: Wed, 27 Nov 2024 14:13:18 +0530 Subject: [PATCH 3/6] feat: indexOf docs --- .../array/fixed-endian-factory/README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 985e4560ca9b..00c3022f2132 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -338,6 +338,27 @@ v = arr.at( -100 ); // returns undefined ``` + + +#### TypedArrayFE.prototype.indexOf( searchElement\[, fromIndex] ) + +Returns the first index at which a given element can be found in the typed array, or `-1` if it is not present. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + +var idx = arr.indexOf( 2.0 ); +// returns 1 + +idx = arr.indexOf( 5.0 ); +// returns -1 + +idx = arr.indexOf( 3.0, 3 ); +// returns -1 +``` + #### TypedArrayFE.prototype.every( predicate\[, thisArg] ) From f996caa86ece00197b6fdf46c0798ebf5e327d90 Mon Sep 17 00:00:00 2001 From: Aditya Sapra <110766802+adityacodes30@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:22:14 +0530 Subject: [PATCH 4/6] Update benchmark.index_of.js Signed-off-by: Aditya Sapra <110766802+adityacodes30@users.noreply.github.com> --- .../array/fixed-endian-factory/benchmark/benchmark.index_of.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js index 49e9d2f58cf1..04944e3cc5b2 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.index_of.js @@ -42,7 +42,7 @@ bench( pkg+':indexOf', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - idx = arr.indexOf( 5.0, 0 ); // Start searching from index 0 + idx = arr.indexOf( 5.0, 0 ); if ( typeof idx !== 'number' ) { b.fail( 'should return an integer' ); } From 1cc23a68615c71ad5915d70d05e30d5be3388683 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 27 Nov 2024 02:52:05 -0800 Subject: [PATCH 5/6] chore: clean-up --- .../array/fixed-endian-factory/README.md | 53 +++++++++++-------- .../array/fixed-endian-factory/lib/main.js | 20 +++---- .../test/test.index_of.js | 34 ++++++++---- 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 00c3022f2132..44d10988d768 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -338,27 +338,6 @@ v = arr.at( -100 ); // returns undefined ``` - - -#### TypedArrayFE.prototype.indexOf( searchElement\[, fromIndex] ) - -Returns the first index at which a given element can be found in the typed array, or `-1` if it is not present. - -```javascript -var Float64ArrayFE = fixedEndianFactory( 'float64' ); - -var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); - -var idx = arr.indexOf( 2.0 ); -// returns 1 - -idx = arr.indexOf( 5.0 ); -// returns -1 - -idx = arr.indexOf( 3.0, 3 ); -// returns -1 -``` - #### TypedArrayFE.prototype.every( predicate\[, thisArg] ) @@ -496,6 +475,38 @@ var v = arr.get( 100 ); // returns undefined ``` + + +#### TypedArrayFE.prototype.indexOf( searchElement\[, fromIndex] ) + +Returns the first index at which a given element can be found. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + +var idx = arr.indexOf( 2.0 ); +// returns 1 + +idx = arr.indexOf( 2.0, 2 ); +// returns 4 + +idx = arr.indexOf( 2.0, -4 ); +// returns 1 +``` + +If `searchElement` is not present in the array, the method returns `-1`. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + +var idx = arr.indexOf( 5.0 ); +// returns -1 +``` + #### TypedArrayFE.prototype.set( arr\[, offset] ) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index f2dccffb49a6..b4755f5b518b 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -624,50 +624,42 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli /** * Returns the index of the first occurrence of a given element. * + * @private * @name indexOf * @memberof TypedArray.prototype * @type {Function} * @param {*} searchElement - element to search for - * @param {integer} fromIndex - index to start the search from (optional) + * @param {integer} [fromIndex=0] - starting index (inclusive) * @throws {TypeError} `this` must be a typed array instance - * @returns {integer} index of the first occurrence of the element, or -1 if not found + * @throws {TypeError} second argument must be an integer + * @returns {integer} index or -1 */ setReadOnly( TypedArray.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { - var len; var buf; var i; if ( !isTypedArray( this ) ) { throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); } - - len = this._length; - if ( arguments.length > 1 ) { if ( !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); } if ( fromIndex < 0 ) { - fromIndex += len; + fromIndex += this._length; if ( fromIndex < 0 ) { fromIndex = 0; } } - if ( fromIndex >= len ) { - return -1; - } } else { fromIndex = 0; } - buf = this._buffer; - - for ( i = fromIndex; i < len; i++ ) { + for ( i = fromIndex; i < this._length; i++ ) { if ( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) === searchElement ) { return i; } } - return -1; }); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js index b7da906cb4e6..edf3b5de2b14 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js @@ -80,7 +80,7 @@ tape( 'the method throws an error if invoked with a `this` context which is not } }); -tape( 'the method throws an error if provided `fromIndex` argument which is not an integer', function test( t ) { +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { var values; var ctor; var arr; @@ -113,7 +113,7 @@ tape( 'the method throws an error if provided `fromIndex` argument which is not } }); -tape( 'the method returns `-1` if provided fromIndex argument which exceeds array dimensions', function test( t ) { +tape( 'the method returns `-1` if provided a second argument which exceeds array dimensions', function test( t ) { var ctor; var arr; var v; @@ -122,7 +122,7 @@ tape( 'the method returns `-1` if provided fromIndex argument which exceeds arra 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 ] ); - for ( i = arr.length; i < arr.length + 5; i++ ) { + for ( i = arr.length; i < arr.length+5; i++ ) { v = arr.indexOf( 1.0, i ); t.strictEqual( v, -1, 'returns expected value' ); } @@ -138,12 +138,12 @@ tape( 'the method returns `-1` if operating on an empty array', function test( t arr = new ctor( 'little-endian', [] ); v = arr.indexOf( 1.0 ); - t.strictEqual( v, -1, 'returns -1 for an empty array' ); + t.strictEqual( v, -1, 'returns expected value' ); t.end(); }); -tape( 'the method returns `-1` if element not found', function test( t ) { +tape( 'the method returns `-1` if a search element is not found', function test( t ) { var ctor; var arr; var v; @@ -152,12 +152,12 @@ tape( 'the method returns `-1` if element not found', function test( t ) { arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); v = arr.indexOf( 10.0 ); - t.strictEqual( v, -1, 'returns -1 when element is not found' ); + t.strictEqual( v, -1, 'returns expected value' ); t.end(); }); -tape( 'the method returns the index of the first match if element is found', function test( t ) { +tape( 'the method returns the index of the first match if an element is found', function test( t ) { var ctor; var arr; var v; @@ -166,7 +166,7 @@ tape( 'the method returns the index of the first match if element is found', fun arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); v = arr.indexOf( 2.0 ); - t.strictEqual( v, 1, 'returns index of the first match when the element is found' ); + t.strictEqual( v, 1, 'returns expected value' ); t.end(); }); @@ -179,8 +179,14 @@ tape( 'the method supports specifying a starting search index', function test( t ctor = factory( 'float64' ); arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + v = arr.indexOf( 2.0, 0 ); + t.strictEqual( v, 2, 'returns expected value' ); + v = arr.indexOf( 2.0, 2 ); - t.strictEqual( v, 4, 'returns index of the match starting from the specified index' ); + t.strictEqual( v, 4, 'returns expected value' ); + + v = arr.indexOf( 1.0, 4 ); + t.strictEqual( v, -1, 'returns expected value' ); t.end(); }); @@ -194,7 +200,10 @@ tape( 'the method supports specifying a starting search index (negative)', funct arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); v = arr.indexOf( 2.0, -3 ); - t.strictEqual( v, 4, 'returns index of the match starting from the resolved negative index' ); + t.strictEqual( v, 4, 'returns expected value' ); + + v = arr.indexOf( 1.0, -3 ); + t.strictEqual( v, -1, 'returns expected value' ); t.end(); }); @@ -208,7 +217,10 @@ tape( 'when provided a starting index which resolves to an index less than zero, arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); v = arr.indexOf( 2.0, -10 ); - t.strictEqual( v, 1, 'returns index of the match starting from the first array element when starting index resolves to less than zero' ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = arr.indexOf( 1.0, -10 ); + t.strictEqual( v, 0, 'returns expected value' ); t.end(); }); From fd94b2d67f008cb394dd101cb20c1fda13a883c6 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 27 Nov 2024 02:58:16 -0800 Subject: [PATCH 6/6] test: fix expected value Signed-off-by: Athan --- .../@stdlib/array/fixed-endian-factory/test/test.index_of.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js index edf3b5de2b14..ea6b8c2c214a 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.index_of.js @@ -180,7 +180,7 @@ tape( 'the method supports specifying a starting search index', function test( t arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); v = arr.indexOf( 2.0, 0 ); - t.strictEqual( v, 2, 'returns expected value' ); + t.strictEqual( v, 1, 'returns expected value' ); v = arr.indexOf( 2.0, 2 ); t.strictEqual( v, 4, 'returns expected value' );