Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/data-structures/tree/fenwick-tree/FenwickTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class FenwickTree {
* @return {FenwickTree}
*/
increase(position, value) {
if (position < 1 || position > this.arraySize) {
if (!Number.isInteger(position) || position < 1 || position > this.arraySize) {
throw new Error('Position is out of allowed range');
}

Expand All @@ -38,7 +38,7 @@ export default class FenwickTree {
* @return {number}
*/
query(position) {
if (position < 1 || position > this.arraySize) {
if (!Number.isInteger(position) || position < 1 || position > this.arraySize) {
throw new Error('Position is out of allowed range');
}

Expand Down
22 changes: 22 additions & 0 deletions src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { runInNewContext } from 'vm';
import FenwickTree from '../FenwickTree';

describe('FenwickTree', () => {
it('should reject fractional queries without entering a nonterminating loop', () => {
const tree = new FenwickTree(5);
// Bound synchronous execution so a regression cannot hang the test runner.
expect(() => runInNewContext('tree.query(1.5)', { tree }, { timeout: 500 }))
.toThrow('Position is out of allowed range');
});

it.each([1.5, NaN])('should reject invalid update position %s without changing the tree', (position) => {
const tree = new FenwickTree(5);
expect(() => tree.increase(position, 7)).toThrow('Position is out of allowed range');
expect(tree.treeArray).toEqual([0, 0, 0, 0, 0, 0]);
});

it('should reject NaN query bounds instead of returning a misleading sum', () => {
const tree = new FenwickTree(5);
tree.increase(1, 7);
expect(() => tree.query(NaN)).toThrow('Position is out of allowed range');
expect(() => tree.queryRange(NaN, 3)).toThrow('Position is out of allowed range');
expect(() => tree.queryRange(1, NaN)).toThrow('Position is out of allowed range');
});

it('should create empty fenwick tree of correct size', () => {
const tree1 = new FenwickTree(5);
expect(tree1.treeArray.length).toBe(5 + 1);
Expand Down