|
1 | | -const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search'); |
2 | | - |
3 | | -describe('Binary Search Recursive', () => { |
4 | | - let array; |
5 | | - |
6 | | - beforeEach(() => { |
7 | | - array = [7, 9, 13, 23]; |
8 | | - }); |
9 | | - |
10 | | - it('should find a middle element', () => { |
11 | | - expect(binarySearchRecursive(array, 9)).toEqual(1); |
12 | | - }); |
13 | | - |
14 | | - it('should find an first element', () => { |
15 | | - expect(binarySearchRecursive(array, 7)).toEqual(0); |
16 | | - }); |
17 | | - |
18 | | - it('should find the last element', () => { |
19 | | - expect(binarySearchRecursive(array, 23)).toEqual(3); |
20 | | - }); |
21 | | - |
22 | | - it('should not find an bigger element', () => { |
23 | | - expect(binarySearchRecursive(array, 9000)).toEqual(-1); |
24 | | - }); |
25 | | - |
26 | | - it('should find a smaller element', () => { |
27 | | - expect(binarySearchRecursive(array, -9)).toEqual(-1); |
28 | | - }); |
29 | | -}); |
30 | | - |
31 | | -describe('Binary Search Iterative', () => { |
32 | | - let array; |
33 | | - |
34 | | - beforeEach(() => { |
35 | | - array = [7, 9, 13, 23]; |
36 | | - }); |
37 | | - |
38 | | - it('should find a middle element', () => { |
39 | | - expect(binarySearchIterative(array, 9)).toEqual(1); |
40 | | - }); |
41 | | - |
42 | | - it('should find an first element', () => { |
43 | | - expect(binarySearchIterative(array, 7)).toEqual(0); |
44 | | - }); |
45 | | - |
46 | | - it('should find the last element', () => { |
47 | | - expect(binarySearchIterative(array, 23)).toEqual(3); |
48 | | - }); |
49 | | - |
50 | | - it('should not find an bigger element', () => { |
51 | | - expect(binarySearchIterative(array, 9000)).toEqual(-1); |
52 | | - }); |
53 | | - |
54 | | - it('should find a smaller element', () => { |
55 | | - expect(binarySearchIterative(array, -9)).toEqual(-1); |
| 1 | +const { |
| 2 | + binarySearchRecursive, |
| 3 | + binarySearchIterative, |
| 4 | +} = require("./02-binary-search"); |
| 5 | + |
| 6 | +const binarySearchImplementations = [ |
| 7 | + binarySearchRecursive, |
| 8 | + binarySearchIterative, |
| 9 | +]; |
| 10 | + |
| 11 | +binarySearchImplementations.forEach((binarySearchImpl) => { |
| 12 | + describe(binarySearchImpl.name, () => { |
| 13 | + let array; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + array = [7, 9, 13, 23]; |
| 17 | + }); |
| 18 | + |
| 19 | + it("should find a middle element", () => { |
| 20 | + expect(binarySearchImpl(array, 9)).toEqual(1); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should find an first element", () => { |
| 24 | + expect(binarySearchImpl(array, 7)).toEqual(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should find the last element", () => { |
| 28 | + expect(binarySearchImpl(array, 23)).toEqual(3); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should not find an bigger element", () => { |
| 32 | + expect(binarySearchImpl(array, 9000)).toEqual(-1); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should find a smaller element", () => { |
| 36 | + expect(binarySearchImpl(array, -9)).toEqual(-1); |
| 37 | + }); |
56 | 38 | });
|
57 | 39 | });
|
0 commit comments