forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-algorithms-tests.ts
57 lines (47 loc) · 1.48 KB
/
search-algorithms-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'mocha';
import { expect } from 'chai';
import { IEqualsFunction } from '../../../../src/ts/util';
interface CustomObject {
key: number;
}
const customEquals: IEqualsFunction<CustomObject> = (a: CustomObject, b: CustomObject) =>
a.key === b.key;
export function testSearchAlgorithm(
searchAlgorithm: Function,
algorithmName: string,
config = { customEquals: true }
) {
describe(algorithmName, () => {
const SIZE = 10;
function createSortedArray() {
const array: number[] = [];
for (let i = 1; i <= SIZE; i++) {
array.push(i);
}
return array;
}
it('works with empty arrays', () => {
expect(searchAlgorithm([], 1)).to.equal(-1);
});
it('finds value at the first position', () => {
const array = createSortedArray();
expect(searchAlgorithm(array, 1)).to.equal(0);
});
it('finds value at the last position', () => {
const array = createSortedArray();
expect(searchAlgorithm(array, SIZE)).to.equal(SIZE - 1);
});
it('finds value at different positions', () => {
const array = createSortedArray();
for (let value = 1; value <= SIZE; value++) {
expect(searchAlgorithm(array, value)).to.equal(value - 1);
}
});
if (config.customEquals) {
it('finds value with custom equals function', () => {
const array = [{ key: 1 }, { key: 2 }, { key: 3 }];
expect(searchAlgorithm(array, { key: 2 }, customEquals)).to.equal(1);
});
}
});
}