tests: Add tests for `SkipList.atOffset()`

pull/5003/head
Richard Hansen 2021-04-12 03:43:17 -04:00
parent c00031a8d8
commit a58fa4a2c5
1 changed files with 36 additions and 0 deletions

View File

@ -15,4 +15,40 @@ describe('skiplist.js', function () {
skiplist.push({key: 'foo'});
expect(() => skiplist.push({key: 'foo'})).to.throwError();
});
it('atOffset() returns last entry that touches offset', async function () {
const skiplist = new SkipList();
const entries = [];
let nextId = 0;
const makeEntry = (width) => {
const entry = {key: `id${nextId++}`, width};
entries.push(entry);
return entry;
};
skiplist.push(makeEntry(5));
expect(skiplist.atOffset(4)).to.be(entries[0]);
expect(skiplist.atOffset(5)).to.be(entries[0]);
expect(() => skiplist.atOffset(6)).to.throwError();
skiplist.push(makeEntry(0));
expect(skiplist.atOffset(4)).to.be(entries[0]);
expect(skiplist.atOffset(5)).to.be(entries[1]);
expect(() => skiplist.atOffset(6)).to.throwError();
skiplist.push(makeEntry(0));
expect(skiplist.atOffset(4)).to.be(entries[0]);
expect(skiplist.atOffset(5)).to.be(entries[2]);
expect(() => skiplist.atOffset(6)).to.throwError();
skiplist.splice(2, 0, [makeEntry(0)]);
expect(skiplist.atOffset(4)).to.be(entries[0]);
expect(skiplist.atOffset(5)).to.be(entries[2]);
expect(() => skiplist.atOffset(6)).to.throwError();
skiplist.push(makeEntry(3));
expect(skiplist.atOffset(4)).to.be(entries[0]);
expect(skiplist.atOffset(5)).to.be(entries[4]);
expect(skiplist.atOffset(6)).to.be(entries[4]);
});
});