Skip to content

Commit

Permalink
feature: added initial implementation of .add() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent 791b62c commit 1d1bddc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions round-linked-queue.js
Expand Up @@ -4,6 +4,9 @@ class RoundLinkedQueue {
constructor(maxLength) {
this._maxLength = maxLength;
this._length = 0;
this._root = 0;
this._first = null;
this._last = null;
}

get maxLength() {
Expand All @@ -13,6 +16,22 @@ class RoundLinkedQueue {
get length() {
return this._length;
}

get first() {
return this._first;
}

get last() {
return this._last;
}

add(element) {
this._root = element;
this._first = element;
this._last = element;

this._length += 1;
}
}

module.exports = RoundLinkedQueue;
17 changes: 17 additions & 0 deletions round-linked-queue.test.js
Expand Up @@ -21,4 +21,21 @@ describe("Round-Queue", () => {
expect(queue.length).to.equal(0);
});
});

describe("When adding elements", () => {
it("Should add an element to an empty queue", () => {
const queue = new RoundQueue(3);
const originalLength = queue.length;
const elementToAdd = 1;

queue.add(elementToAdd);

// Element should've been added to the end of the queue
expect(queue.last).to.equal(elementToAdd);
// But since it is now the only element, it should also be the at beginning as well
expect(queue.first).to.equal(elementToAdd);
// Length should've been increased by 1
expect(queue.length).to.equal(originalLength + 1);
});
});
});

0 comments on commit 1d1bddc

Please sign in to comment.