Skip to content
Permalink
Browse files
feature: added initial implementation of .add() method
  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent 791b62c commit 1d1bddc6dc120e0d622385b34daf67dfb093f28b
Showing with 36 additions and 0 deletions.
  1. +19 −0 round-linked-queue.js
  2. +17 −0 round-linked-queue.test.js
@@ -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() {
@@ -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;
@@ -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.