Files
hls.js/tests/unit/utils/binary-search.js
renovate[bot]GitHubrenovate[bot] <29139614+renovate[bot]@users.noreply.github.com>Tom Jenkinson
359197f20b chore(deps): update dependency prettier to v3 (#5646)
* chore(deps): update dependency prettier to v3

* run prettier

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tom Jenkinson <tom@tjenkinson.me>
2023-08-22 11:24:59 +01:00

41 lines
1.1 KiB
JavaScript

import BinarySearch from '../../../src/utils/binary-search';
describe('binary search util', function () {
describe('search helper', function () {
let list = null;
const buildComparisonFunction = function (itemToSearchFor) {
return function (candidate) {
if (candidate < itemToSearchFor) {
return 1;
} else if (candidate > itemToSearchFor) {
return -1;
}
return 0;
};
};
beforeEach(function () {
list = [4, 8, 15, 16, 23, 42];
});
it('finds the element if it is present', function () {
for (let i = 0; i < list.length; i++) {
const item = list[i];
const foundItem = BinarySearch.search(
list,
buildComparisonFunction(item),
);
expect(foundItem).to.equal(item);
}
});
it('does not find the element if it is not present', function () {
const item = 1000;
const foundItem = BinarySearch.search(
list,
buildComparisonFunction(item),
);
expect(foundItem).to.not.exist;
});
});
});