Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(binary-search): consolidate test for multiple implementations
  • Loading branch information
amejiarosario committed Dec 10, 2022
commit c9249d38cc3809bc2c0457f35494772cb57b28b9
6 changes: 4 additions & 2 deletions src/runtimes/02-binary-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) {

if (current === search) {
return offset + half;
} if (array.length === 1) {
}
if (array.length === 1) {
return -1;
} if (search > current) {
}
if (search > current) {
const right = array.slice(half);
return binarySearchRecursive(right, search, offset + half);
}
Expand Down
92 changes: 37 additions & 55 deletions src/runtimes/02-binary-search.spec.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search');

describe('Binary Search Recursive', () => {
let array;

beforeEach(() => {
array = [7, 9, 13, 23];
});

it('should find a middle element', () => {
expect(binarySearchRecursive(array, 9)).toEqual(1);
});

it('should find an first element', () => {
expect(binarySearchRecursive(array, 7)).toEqual(0);
});

it('should find the last element', () => {
expect(binarySearchRecursive(array, 23)).toEqual(3);
});

it('should not find an bigger element', () => {
expect(binarySearchRecursive(array, 9000)).toEqual(-1);
});

it('should find a smaller element', () => {
expect(binarySearchRecursive(array, -9)).toEqual(-1);
});
});

describe('Binary Search Iterative', () => {
let array;

beforeEach(() => {
array = [7, 9, 13, 23];
});

it('should find a middle element', () => {
expect(binarySearchIterative(array, 9)).toEqual(1);
});

it('should find an first element', () => {
expect(binarySearchIterative(array, 7)).toEqual(0);
});

it('should find the last element', () => {
expect(binarySearchIterative(array, 23)).toEqual(3);
});

it('should not find an bigger element', () => {
expect(binarySearchIterative(array, 9000)).toEqual(-1);
});

it('should find a smaller element', () => {
expect(binarySearchIterative(array, -9)).toEqual(-1);
const {
binarySearchRecursive,
binarySearchIterative,
} = require("./02-binary-search");

const binarySearchImplementations = [
binarySearchRecursive,
binarySearchIterative,
];

binarySearchImplementations.forEach((binarySearchImpl) => {
describe(binarySearchImpl.name, () => {
let array;

beforeEach(() => {
array = [7, 9, 13, 23];
});

it("should find a middle element", () => {
expect(binarySearchImpl(array, 9)).toEqual(1);
});

it("should find an first element", () => {
expect(binarySearchImpl(array, 7)).toEqual(0);
});

it("should find the last element", () => {
expect(binarySearchImpl(array, 23)).toEqual(3);
});

it("should not find an bigger element", () => {
expect(binarySearchImpl(array, 9000)).toEqual(-1);
});

it("should find a smaller element", () => {
expect(binarySearchImpl(array, -9)).toEqual(-1);
});
});
});