From 85a55daf49533b9fd558e52f13603a904613e4f2 Mon Sep 17 00:00:00 2001 From: Hridyanshu <124202756+HRIDYANSHU054@users.noreply.github.com> Date: Fri, 20 Dec 2024 21:35:50 +0530 Subject: [PATCH] test: added for Linear Search Algorithm (#1753) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --- Search/LinearSearch.js | 2 ++ Search/test/LinearSearch.test.js | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Search/test/LinearSearch.test.js diff --git a/Search/LinearSearch.js b/Search/LinearSearch.js index 637ebc1589..96a9d1fa34 100644 --- a/Search/LinearSearch.js +++ b/Search/LinearSearch.js @@ -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) diff --git a/Search/test/LinearSearch.test.js b/Search/test/LinearSearch.test.js new file mode 100644 index 0000000000..d593d9aa1c --- /dev/null +++ b/Search/test/LinearSearch.test.js @@ -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) + } + ) +})