mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
merge: Add proper tests for binary search (#987)
* feat: added alternative binary search * fix: exchange "dir" for "high" * fix : fixing code style * fix: fixed readability * fix: fixed code smells * fix: remove binary search alternative * feat: added tests of binary search interative and recursive * fix: fixed wrong identation * fix: refactoring duplicated code of tests
This commit is contained in:
@ -50,42 +50,3 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export { binarySearchIterative, binarySearchRecursive }
|
export { binarySearchIterative, binarySearchRecursive }
|
||||||
|
|
||||||
/* ---------------------------------- Test ---------------------------------- */
|
|
||||||
|
|
||||||
// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
||||||
// const stringArr = [
|
|
||||||
// 'Alpha',
|
|
||||||
// 'Bravo',
|
|
||||||
// 'Charlie',
|
|
||||||
// 'Delta',
|
|
||||||
// 'Echo',
|
|
||||||
// 'Foxtrot',
|
|
||||||
// 'Golf',
|
|
||||||
// 'Hotel',
|
|
||||||
// 'India',
|
|
||||||
// 'Juliet',
|
|
||||||
// 'Kilo',
|
|
||||||
// 'Lima',
|
|
||||||
// 'Mike',
|
|
||||||
// 'November',
|
|
||||||
// 'Oscar',
|
|
||||||
// 'Papa',
|
|
||||||
// 'Quebec',
|
|
||||||
// 'Romeo',
|
|
||||||
// 'Sierra',
|
|
||||||
// 'Tango',
|
|
||||||
// 'Uniform',
|
|
||||||
// 'Victor',
|
|
||||||
// 'Whiskey',
|
|
||||||
// 'X-Ray',
|
|
||||||
// 'Yankee',
|
|
||||||
// 'Zulu'
|
|
||||||
// ]
|
|
||||||
|
|
||||||
// binarySearchRecursive(arr, 3)
|
|
||||||
// binarySearchIterative(arr, 7)
|
|
||||||
// binarySearchRecursive(arr, 13)
|
|
||||||
// binarySearchIterative(stringArr, 'Charlie')
|
|
||||||
// binarySearchRecursive(stringArr, 'Zulu')
|
|
||||||
// binarySearchIterative(stringArr, 'Sierra')
|
|
||||||
|
49
Search/test/BinarySearch.test.js
Normal file
49
Search/test/BinarySearch.test.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import { binarySearchIterative, binarySearchRecursive } from '../BinarySearch'
|
||||||
|
|
||||||
|
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
const stringArr = [
|
||||||
|
'Alpha',
|
||||||
|
'Bravo',
|
||||||
|
'Charlie',
|
||||||
|
'Delta',
|
||||||
|
'Echo',
|
||||||
|
'Foxtrot',
|
||||||
|
'Golf',
|
||||||
|
'Hotel',
|
||||||
|
'India',
|
||||||
|
'Juliet',
|
||||||
|
'Kilo',
|
||||||
|
'Lima',
|
||||||
|
'Mike',
|
||||||
|
'November',
|
||||||
|
'Oscar',
|
||||||
|
'Papa',
|
||||||
|
'Quebec',
|
||||||
|
'Romeo',
|
||||||
|
'Sierra',
|
||||||
|
'Tango',
|
||||||
|
'Uniform',
|
||||||
|
'Victor',
|
||||||
|
'Whiskey',
|
||||||
|
'X-Ray',
|
||||||
|
'Yankee',
|
||||||
|
'Zulu'
|
||||||
|
]
|
||||||
|
|
||||||
|
describe('Binary Search', () => {
|
||||||
|
const funcs = [binarySearchIterative, binarySearchRecursive]
|
||||||
|
for (const func of funcs) {
|
||||||
|
test('expect to return the index of the item in the array', () => {
|
||||||
|
expect(func(arr, 3)).toBe(2)
|
||||||
|
})
|
||||||
|
test('expect to return -1 if not in array', () => {
|
||||||
|
expect(func(arr, 11)).toBe(-1)
|
||||||
|
})
|
||||||
|
test('expect to return the index of the item in the array', () => {
|
||||||
|
expect(func(stringArr, 'Charlie')).toBe(2)
|
||||||
|
})
|
||||||
|
test('expect to return -1 if not in array', () => {
|
||||||
|
expect(func(stringArr, 'Zoft')).toBe(-1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
Reference in New Issue
Block a user