Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break potential infinite loop #71

Merged
merged 4 commits into from
Jun 28, 2021
Merged
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
2 changes: 1 addition & 1 deletion packages/red-black-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ava": "^0.24.0",
"rimraf": "^2.6.2",
"tslint": "^5.9.1",
"typescript": "^2.6.2"
"typescript": "3.5.1"
},
"ava": {
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/red-black-tree/src/internals/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class PathNode<K, V> {
release (): PathNode<K, V> {
var p = this.parent;
this.node = NONE;
this.next = BRANCH.NONE;
this.parent = PathNode.cache;
PathNode.cache = this;
return p;
Expand Down
77 changes: 77 additions & 0 deletions packages/red-black-tree/tests/internals/rebalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import test from 'ava';
import { empty, set } from '../../src';
import { RedBlackTreeStructure, isNone } from '../../src/internals';

let tree: RedBlackTreeStructure<string, User>;

type User = {
name: string,
id: string
};

const testUser1: User = {
name: 'Luke',
id: '298'
};

const testUser2: User = {
name: 'Leia',
id: '299'
};

const testUser3: User = {
name: 'Han',
id: '300'
};

const testUser4: User = {
name: 'Chewbacca',
id: '301'
};

test.beforeEach(() => {
tree = empty<string, User>((a, b) => a < b ? -1 : (a > b ? 1 : 0), true);
});

test('should keep the tree balanced', t => {
t.plan(13);

// Set 298 and make sure it's the root and no other elements are there
tree = set(testUser1.id, testUser1, tree);
t.is(tree._root.key, testUser1.id);
t.is(isNone(tree._root._left), true);
t.is(isNone(tree._root._right), true);

// Set 299, no rebalance. 299 should be on the right
tree = set(testUser2.id, testUser2, tree);
t.is(tree._root.key, testUser1.id);
t.is(isNone(tree._root._left), true);
t.is(tree._root._right.key, testUser2.id);

// Set 300, this should cause a rebalance that puts 299 at the top
tree = set(testUser3.id, testUser3, tree);
t.is(tree._root.key, testUser2.id); // 299 is root
t.is(tree._root._left.key, testUser1.id); // 298 goes left
t.is(tree._root._right.key, testUser3.id); // 300 goes right

// Set 301, no rebalance - will go on the right of 300
tree = set(testUser4.id, testUser4, tree);
t.is(tree._root.key, testUser2.id); // 299 is root
t.is(tree._root._left.key, testUser1.id); // 298 goes left
t.is(tree._root._right.key, testUser3.id); // 300 goes right
t.is(tree._root._right._right.key, testUser4.id); // 300 goes right
});

test('should keep the tree balanced - many items synchronously', t => {
// Add accounts to the tree which will trigger lots of rebalancing
for (let i = 0; i < 1000; i++) {
const user: User = {
name: i.toString(),
id: i.toString()
};
tree = set(user.id, user, tree);
}

t.is(tree._root.key, '487');
t.is(tree._size, 1000);
});