dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
57 lines (52 loc) • 2.14 kB
JavaScript
"use strict";
var _jumpSearch = _interopRequireDefault(require("../jumpSearch"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('jumpSearch', () => {
it('should search for an element in sorted array', () => {
expect((0, _jumpSearch.default)([], 1)).toBe(-1);
expect((0, _jumpSearch.default)([1], 2)).toBe(-1);
expect((0, _jumpSearch.default)([1], 1)).toBe(0);
expect((0, _jumpSearch.default)([1, 2], 1)).toBe(0);
expect((0, _jumpSearch.default)([1, 2], 1)).toBe(0);
expect((0, _jumpSearch.default)([1, 1, 1], 1)).toBe(0);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 2)).toBe(1);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 0)).toBe(-1);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 0)).toBe(-1);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 7)).toBe(-1);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 5)).toBe(2);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 20)).toBe(4);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 30)).toBe(7);
expect((0, _jumpSearch.default)([1, 2, 5, 10, 20, 21, 24, 30, 48], 48)).toBe(8);
});
it('should search object in sorted array', () => {
const sortedArrayOfObjects = [{
key: 1,
value: 'value1'
}, {
key: 2,
value: 'value2'
}, {
key: 3,
value: 'value3'
}];
const comparator = (a, b) => {
if (a.key === b.key) return 0;
return a.key < b.key ? -1 : 1;
};
expect((0, _jumpSearch.default)([], {
key: 1
}, comparator)).toBe(-1);
expect((0, _jumpSearch.default)(sortedArrayOfObjects, {
key: 4
}, comparator)).toBe(-1);
expect((0, _jumpSearch.default)(sortedArrayOfObjects, {
key: 1
}, comparator)).toBe(0);
expect((0, _jumpSearch.default)(sortedArrayOfObjects, {
key: 2
}, comparator)).toBe(1);
expect((0, _jumpSearch.default)(sortedArrayOfObjects, {
key: 3
}, comparator)).toBe(2);
});
});