Skip to content

Commit

Permalink
test: added for Linear Search Algorithm (#1753)
Browse files Browse the repository at this point in the history
* test: added for Linear Search Algorithm

* Update Search/LinearSearch.js

Co-authored-by: Lars Müller <[email protected]>

---------

Co-authored-by: Hridyanshu7 <[email protected]>
Co-authored-by: Lars Müller <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent d8588f9 commit 85a55da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Search/LinearSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions Search/test/LinearSearch.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
)
})

0 comments on commit 85a55da

Please sign in to comment.