Skip to content

Commit

Permalink
Updated Graph, Group, Pq with more error messages and updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-sth committed Oct 21, 2023
1 parent 3412852 commit 2760265
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Class/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DirectedGraph<T> {
includes(item: T): boolean {
return this.nodes.some((node) => node.value === item);
}
getNode(value: T): GraphNode<T> | undefined {
getNode(value: T): GraphNode<T> {
return this.nodes.find((node) => node.value === value);
}
removeEdge(from: GraphNode<T>, to: GraphNode<T>): void {
Expand Down
2 changes: 1 addition & 1 deletion Class/Groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
16 changes: 4 additions & 12 deletions Class/PriorityQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class PriorityQueue<T> {
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];
Expand All @@ -26,24 +26,16 @@ class PriorityQueue<T> {
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;
}
Expand Down
8 changes: 4 additions & 4 deletions test/Groups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand All @@ -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";

Expand All @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions test/pq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>();

const result = priorityQueue.dequeue();

expect(result).toBeUndefined();
expect(() => priorityQueue.dequeue()).toThrow();
});

it("should return the front element without dequeuing", () => {
Expand Down Expand Up @@ -51,8 +49,8 @@ describe("PriorityQueue", () => {
it("should return undefined when peeking an empty queue", () => {
const priorityQueue = new PriorityQueue<number>();

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", () => {
Expand Down

0 comments on commit 2760265

Please sign in to comment.