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:
Hridyanshu
2024-12-20 21:35:50 +05:30
committed by GitHub
parent d8588f9de1
commit 85a55daf49
2 changed files with 37 additions and 0 deletions

View File

@ -3,6 +3,8 @@
* value within a list. It sequentially checks each element of the list * 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 * for the target value until a match is found or until all the elements
* have been searched. * have been searched.
*
* @see https://en.wikipedia.org/wiki/Linear_search
*/ */
function SearchArray(searchNum, ar, output = (v) => console.log(v)) { function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
const position = Search(ar, searchNum) const position = Search(ar, searchNum)

View 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)
}
)
})