Skip to content

Commit

Permalink
test: updated some DLL and LL tests (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 13, 2024
1 parent 7846f53 commit ebf1ea3
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 208 deletions.
23 changes: 8 additions & 15 deletions src/DataStructures/LinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ class LinkedList {
* @param {*} value - The value to initialize the list with (optional).
*/
constructor(value) {
if (value !== undefined) {
const newNode = new Node(value);
this.head = newNode; // first node to be added
this.tail = newNode; // last node to be added
this.size = 1;
} else {
this.head = null;
this.tail = null;
this.size = 0;
}
this.head = null; // first node to be added
this.tail = null; // last node to be added
this.size = 0;

if (Array.isArray(value)) value.forEach((v) => this.push(v));
else if (value !== undefined) this.push(value);
}

/**
Expand Down Expand Up @@ -96,15 +92,12 @@ class LinkedList {
get(index, returnNode = false) {
if (index < 0 || index >= this.size) return undefined;

let count = 0;
let temp = this.head;
while (temp !== null) {
if (count === index) return returnNode ? temp : temp.value;
for (let i = 0; i < index; i++) {
temp = temp.next;
count++;
}

return temp; // Should never reach here
return returnNode ? temp : temp.value;
}

/**
Expand Down
22 changes: 11 additions & 11 deletions tests/DataStructures/DoublyLinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,20 @@ describe("DataStructures/DoublyLinkedList.js", () => {
expect(dll.toArray()).toEqual([]);
});
});
});

describe("Node", () => {
it("creates a new Node with a value", () => {
const node = new Node(10);
describe("Node", () => {
it("creates a new Node with a value", () => {
const node = new Node(10);

expect(node.value).toBe(10);
expect(node.next).toBeNull();
expect(node.prev).toBeNull();
expect(node).toMatchSnapshot();
});
expect(node.value).toBe(10);
expect(node.next).toBeNull();
expect(node.prev).toBeNull();
expect(node).toMatchSnapshot();
});

it("should return a error if the value is not provided", () => {
expect(() => new Node()).toThrowError("Node value cannot be undefined.");
});
it("should return a error if the value is not provided", () => {
expect(() => new Node()).toThrowError("Node value cannot be undefined.");
});
});
});

0 comments on commit ebf1ea3

Please sign in to comment.