diff --git a/Class/Graph.ts b/Class/Graph.ts index 8812f46..183488e 100644 --- a/Class/Graph.ts +++ b/Class/Graph.ts @@ -35,7 +35,7 @@ class DirectedGraph { includes(item: T): boolean { return this.nodes.some((node) => node.value === item); } - getNode(value: T): GraphNode | undefined { + getNode(value: T): GraphNode { return this.nodes.find((node) => node.value === value); } removeEdge(from: GraphNode, to: GraphNode): void { diff --git a/Class/Groups.ts b/Class/Groups.ts index 1d557fd..44f1dc9 100644 --- a/Class/Groups.ts +++ b/Class/Groups.ts @@ -31,7 +31,7 @@ class Groups { return this.groups; } - get(path: string[]): Group | undefined { + get(path: string[]): Group { return this.groups.find((group) => arraysHaveSameValues(group.path, path)); } } diff --git a/Class/PriorityQueue.ts b/Class/PriorityQueue.ts index 94d4f14..6472847 100644 --- a/Class/PriorityQueue.ts +++ b/Class/PriorityQueue.ts @@ -9,9 +9,9 @@ class PriorityQueue { this.heapifyUp(); } - dequeue(): T | undefined { + dequeue(): T { if (this.isEmpty()) { - return undefined; + throw new Error("PriorityQueue is Empty. Can't dequeue."); } const ROOT = this.items[0]; @@ -26,24 +26,16 @@ class PriorityQueue { return ROOT.element; } - peek(): T | undefined { + front(): T | undefined { return this.isEmpty() ? undefined : this.items[0].element; } - peekLast(): T | undefined { + rear(): T | undefined { return this.isEmpty() ? undefined : this.items[this.items.length - 1].element; } - front(): T | undefined { - return this.peek(); - } - - rear(): T | undefined { - return this.peekLast(); - } - isEmpty(): boolean { return this.items.length === 0; } diff --git a/test/Groups.test.ts b/test/Groups.test.ts index e990ff0..d8e729d 100644 --- a/test/Groups.test.ts +++ b/test/Groups.test.ts @@ -7,7 +7,7 @@ describe("Groups class", () => { groups = new Groups(); }); - test("should add a student to an existing group", () => { + it("should add a student to an existing group", () => { const path = ["A", "B", "C"]; const studentId = "123"; @@ -17,7 +17,7 @@ describe("Groups class", () => { expect(groups.getAll()).toEqual([expectedGroup]); }); - test("should create a new group if path doesn't exist", () => { + it("should create a new group if path doesn't exist", () => { const path1 = ["A", "B", "C"]; const path2 = ["X", "Y", "Z"]; const studentId1 = "123"; @@ -33,7 +33,7 @@ describe("Groups class", () => { expect(groups.getAll()).toEqual(expectedGroups); }); - test("should get a group by path", () => { + it("should get a group by path", () => { const path = ["A", "B", "C"]; const studentId = "123"; @@ -45,7 +45,7 @@ describe("Groups class", () => { expect(retrievedGroup).toEqual(expectedGroup); }); - test("should return undefined when getting a non-existing group", () => { + it("should return undefined when getting a non-existing group", () => { const path = ["X", "Y", "Z"]; const retrievedGroup = groups.get(path); diff --git a/test/pq.test.ts b/test/pq.test.ts index 749b0fb..a7aa32d 100644 --- a/test/pq.test.ts +++ b/test/pq.test.ts @@ -13,12 +13,10 @@ describe("PriorityQueue", () => { expect(priorityQueue.dequeue()).toBe("Task 1"); }); - it("should return undefined when dequeuing from an empty queue", () => { + it("should return when dequeuing from an empty queue", () => { const priorityQueue = new PriorityQueue(); - const result = priorityQueue.dequeue(); - - expect(result).toBeUndefined(); + expect(() => priorityQueue.dequeue()).toThrow(); }); it("should return the front element without dequeuing", () => { @@ -51,8 +49,8 @@ describe("PriorityQueue", () => { it("should return undefined when peeking an empty queue", () => { const priorityQueue = new PriorityQueue(); - expect(priorityQueue.peek()).toBeUndefined(); - expect(priorityQueue.peekLast()).toBeUndefined(); + expect(priorityQueue.front()).toBeUndefined(); + expect(priorityQueue.rear()).toBeUndefined(); }); it("should return the correct size of the queue", () => {