mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
test: added for Linear Search Algorithm (#1753)
* test: added for Linear Search Algorithm * Update Search/LinearSearch.js Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --------- Co-authored-by: Hridyanshu7 <himank7794@gmail.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -3,6 +3,8 @@
|
||||
* value within a list. It sequentially checks each element of the list
|
||||
* for the target value until a match is found or until all the elements
|
||||
* have been searched.
|
||||
*
|
||||
* @see https://en.wikipedia.org/wiki/Linear_search
|
||||
*/
|
||||
function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
|
||||
const position = Search(ar, searchNum)
|
||||
|
35
Search/test/LinearSearch.test.js
Normal file
35
Search/test/LinearSearch.test.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { Search as linearSearch } from '../LinearSearch'
|
||||
|
||||
const tests = [
|
||||
{
|
||||
test: {
|
||||
arr: [1, 2, 300, 401, 450, 504, 800, 821, 855, 900, 1002],
|
||||
target: 900
|
||||
},
|
||||
expectedValue: 9
|
||||
},
|
||||
{
|
||||
test: {
|
||||
arr: [1, 104, 110, 4, 44, 55, 56, 78],
|
||||
target: 104
|
||||
},
|
||||
expectedValue: 1
|
||||
},
|
||||
{
|
||||
test: {
|
||||
arr: [-4, 5, 50, 77, 821, 85, 99, 100],
|
||||
target: 192
|
||||
},
|
||||
expectedValue: -1
|
||||
}
|
||||
]
|
||||
|
||||
describe('Linear Search', () => {
|
||||
it.each(tests)(
|
||||
'linearSearch($test.arr, $test.target) => $expectedValue',
|
||||
({ test, expectedValue }) => {
|
||||
const { arr, target } = test
|
||||
expect(linearSearch(arr, target)).toBe(expectedValue)
|
||||
}
|
||||
)
|
||||
})
|
Reference in New Issue
Block a user