mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Update tests.
This commit is contained in:
parent
183dade079
commit
00fbba5438
@ -13,13 +13,13 @@ export default class FenwickTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds value to position.
|
* Adds value to existing value at position.
|
||||||
*
|
*
|
||||||
* @param {number} position
|
* @param {number} position
|
||||||
* @param {number} value
|
* @param {number} value
|
||||||
* @return {FenwickTree}
|
* @return {FenwickTree}
|
||||||
*/
|
*/
|
||||||
update(position, value) {
|
increase(position, value) {
|
||||||
if (position < 1 || position > this.arraySize) {
|
if (position < 1 || position > this.arraySize) {
|
||||||
throw new Error('Position is out of allowed range');
|
throw new Error('Position is out of allowed range');
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ describe('FenwickTree', () => {
|
|||||||
expect(tree.treeArray.length).toBe(inputArray.length + 1);
|
expect(tree.treeArray.length).toBe(inputArray.length + 1);
|
||||||
|
|
||||||
inputArray.forEach((value, index) => {
|
inputArray.forEach((value, index) => {
|
||||||
tree.update(index + 1, value);
|
tree.increase(index + 1, value);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(tree.treeArray).toEqual([0, 3, 5, -1, 10, 5, 9, -3, 19, 7, 9, 3]);
|
expect(tree.treeArray).toEqual([0, 3, 5, -1, 10, 5, 9, -3, 19, 7, 9, 3]);
|
||||||
@ -41,23 +41,42 @@ describe('FenwickTree', () => {
|
|||||||
expect(tree.queryRange(1, 2)).toBe(5);
|
expect(tree.queryRange(1, 2)).toBe(5);
|
||||||
expect(tree.queryRange(2, 4)).toBe(7);
|
expect(tree.queryRange(2, 4)).toBe(7);
|
||||||
expect(tree.queryRange(6, 9)).toBe(11);
|
expect(tree.queryRange(6, 9)).toBe(11);
|
||||||
|
|
||||||
|
tree.increase(3, 1);
|
||||||
|
|
||||||
|
expect(tree.query(1)).toBe(3);
|
||||||
|
expect(tree.query(2)).toBe(5);
|
||||||
|
expect(tree.query(3)).toBe(5);
|
||||||
|
expect(tree.query(4)).toBe(11);
|
||||||
|
expect(tree.query(5)).toBe(16);
|
||||||
|
expect(tree.query(6)).toBe(20);
|
||||||
|
expect(tree.query(7)).toBe(17);
|
||||||
|
expect(tree.query(8)).toBe(20);
|
||||||
|
expect(tree.query(9)).toBe(27);
|
||||||
|
expect(tree.query(10)).toBe(29);
|
||||||
|
expect(tree.query(11)).toBe(32);
|
||||||
|
|
||||||
|
expect(tree.queryRange(1, 1)).toBe(3);
|
||||||
|
expect(tree.queryRange(1, 2)).toBe(5);
|
||||||
|
expect(tree.queryRange(2, 4)).toBe(8);
|
||||||
|
expect(tree.queryRange(6, 9)).toBe(11);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should correctly execute queries', () => {
|
it('should correctly execute queries', () => {
|
||||||
const tree = new FenwickTree(5);
|
const tree = new FenwickTree(5);
|
||||||
|
|
||||||
tree.update(1, 4);
|
tree.increase(1, 4);
|
||||||
tree.update(3, 7);
|
tree.increase(3, 7);
|
||||||
|
|
||||||
expect(tree.query(1)).toBe(4);
|
expect(tree.query(1)).toBe(4);
|
||||||
expect(tree.query(3)).toBe(11);
|
expect(tree.query(3)).toBe(11);
|
||||||
expect(tree.query(5)).toBe(11);
|
expect(tree.query(5)).toBe(11);
|
||||||
expect(tree.queryRange(2, 3)).toBe(7);
|
expect(tree.queryRange(2, 3)).toBe(7);
|
||||||
|
|
||||||
tree.update(2, 5);
|
tree.increase(2, 5);
|
||||||
expect(tree.query(5)).toBe(16);
|
expect(tree.query(5)).toBe(16);
|
||||||
|
|
||||||
tree.update(1, 3);
|
tree.increase(1, 3);
|
||||||
expect(tree.queryRange(1, 1)).toBe(7);
|
expect(tree.queryRange(1, 1)).toBe(7);
|
||||||
expect(tree.query(5)).toBe(19);
|
expect(tree.query(5)).toBe(19);
|
||||||
expect(tree.queryRange(1, 5)).toBe(19);
|
expect(tree.queryRange(1, 5)).toBe(19);
|
||||||
@ -66,12 +85,12 @@ describe('FenwickTree', () => {
|
|||||||
it('should throw exceptions', () => {
|
it('should throw exceptions', () => {
|
||||||
const tree = new FenwickTree(5);
|
const tree = new FenwickTree(5);
|
||||||
|
|
||||||
const updateAtInvalidLowIndex = () => {
|
const increaseAtInvalidLowIndex = () => {
|
||||||
tree.update(0, 1);
|
tree.increase(0, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateAtInvalidHighIndex = () => {
|
const increaseAtInvalidHighIndex = () => {
|
||||||
tree.update(10, 1);
|
tree.increase(10, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryInvalidLowIndex = () => {
|
const queryInvalidLowIndex = () => {
|
||||||
@ -86,8 +105,8 @@ describe('FenwickTree', () => {
|
|||||||
tree.queryRange(3, 2);
|
tree.queryRange(3, 2);
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(updateAtInvalidLowIndex).toThrowError();
|
expect(increaseAtInvalidLowIndex).toThrowError();
|
||||||
expect(updateAtInvalidHighIndex).toThrowError();
|
expect(increaseAtInvalidHighIndex).toThrowError();
|
||||||
expect(queryInvalidLowIndex).toThrowError();
|
expect(queryInvalidLowIndex).toThrowError();
|
||||||
expect(queryInvalidHighIndex).toThrowError();
|
expect(queryInvalidHighIndex).toThrowError();
|
||||||
expect(rangeQueryInvalidIndex).toThrowError();
|
expect(rangeQueryInvalidIndex).toThrowError();
|
||||||
|
Loading…
Reference in New Issue
Block a user