Skip to content

Commit 298ab33

Browse files
authored
merge: Add proper tests for binary search (TheAlgorithms#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
1 parent 8fc5390 commit 298ab33

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

Search/BinarySearch.js

-39
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,3 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
5050
}
5151

5252
export { binarySearchIterative, binarySearchRecursive }
53-
54-
/* ---------------------------------- Test ---------------------------------- */
55-
56-
// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
57-
// const stringArr = [
58-
// 'Alpha',
59-
// 'Bravo',
60-
// 'Charlie',
61-
// 'Delta',
62-
// 'Echo',
63-
// 'Foxtrot',
64-
// 'Golf',
65-
// 'Hotel',
66-
// 'India',
67-
// 'Juliet',
68-
// 'Kilo',
69-
// 'Lima',
70-
// 'Mike',
71-
// 'November',
72-
// 'Oscar',
73-
// 'Papa',
74-
// 'Quebec',
75-
// 'Romeo',
76-
// 'Sierra',
77-
// 'Tango',
78-
// 'Uniform',
79-
// 'Victor',
80-
// 'Whiskey',
81-
// 'X-Ray',
82-
// 'Yankee',
83-
// 'Zulu'
84-
// ]
85-
86-
// binarySearchRecursive(arr, 3)
87-
// binarySearchIterative(arr, 7)
88-
// binarySearchRecursive(arr, 13)
89-
// binarySearchIterative(stringArr, 'Charlie')
90-
// binarySearchRecursive(stringArr, 'Zulu')
91-
// binarySearchIterative(stringArr, 'Sierra')

Search/test/BinarySearch.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { binarySearchIterative, binarySearchRecursive } from '../BinarySearch'
2+
3+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
4+
const stringArr = [
5+
'Alpha',
6+
'Bravo',
7+
'Charlie',
8+
'Delta',
9+
'Echo',
10+
'Foxtrot',
11+
'Golf',
12+
'Hotel',
13+
'India',
14+
'Juliet',
15+
'Kilo',
16+
'Lima',
17+
'Mike',
18+
'November',
19+
'Oscar',
20+
'Papa',
21+
'Quebec',
22+
'Romeo',
23+
'Sierra',
24+
'Tango',
25+
'Uniform',
26+
'Victor',
27+
'Whiskey',
28+
'X-Ray',
29+
'Yankee',
30+
'Zulu'
31+
]
32+
33+
describe('Binary Search', () => {
34+
const funcs = [binarySearchIterative, binarySearchRecursive]
35+
for (const func of funcs) {
36+
test('expect to return the index of the item in the array', () => {
37+
expect(func(arr, 3)).toBe(2)
38+
})
39+
test('expect to return -1 if not in array', () => {
40+
expect(func(arr, 11)).toBe(-1)
41+
})
42+
test('expect to return the index of the item in the array', () => {
43+
expect(func(stringArr, 'Charlie')).toBe(2)
44+
})
45+
test('expect to return -1 if not in array', () => {
46+
expect(func(stringArr, 'Zoft')).toBe(-1)
47+
})
48+
}
49+
})

0 commit comments

Comments
 (0)