Skip to content

Commit ed8e614

Browse files
committed
add unitest for queue and stack DSA
1 parent 481c199 commit ed8e614

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, it, expect, beforeEach } from 'bun:test';
2+
import { Queue } from './queue-implement';
3+
4+
describe('Queue', () => {
5+
let queue: Queue;
6+
7+
beforeEach(() => {
8+
queue = new Queue();
9+
});
10+
11+
it('should initialize an empty queue', () => {
12+
expect(queue.isEmpty()).toBe(true);
13+
expect(queue.peek()).toBeNull();
14+
});
15+
16+
it('should enqueue elements to the queue', () => {
17+
queue.enqueue('first');
18+
expect(queue.isEmpty()).toBe(false);
19+
expect(queue.peek().value).toBe('first');
20+
21+
queue.enqueue('second');
22+
expect(queue.peek().value).toBe('first'); // First element should still be 'first'
23+
});
24+
25+
it('should dequeue elements from the queue', () => {
26+
queue.enqueue('first');
27+
queue.enqueue('second');
28+
29+
const dequeued = queue.dequeue();
30+
expect(dequeued.value).toBe('first'); // First element should be dequeued
31+
expect(queue.peek().value).toBe('second'); // Now the first element should be 'second'
32+
});
33+
34+
it('should return null when dequeuing from an empty queue', () => {
35+
expect(queue.dequeue()).toBeNull();
36+
});
37+
38+
it('should handle special case when dequeuing the last element', () => {
39+
queue.enqueue('only');
40+
expect(queue.dequeue().value).toBe('only');
41+
expect(queue.isEmpty()).toBe(true);
42+
expect(queue.peek()).toBeNull();
43+
});
44+
});

src/data_structure/queue/queue-implement.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Node {
77
}
88
}
99

10-
class Queue {
10+
export class Queue {
1111
// Dùng Single LinkedList là được rồi!
1212
first: any;
1313
last: any;
@@ -21,19 +21,24 @@ class Queue {
2121
return this.first
2222
}
2323
enqueue(value: any) {
24+
const newNode = new Node(value)
2425
if (this.length === 0) {
25-
this.first = new Node(value)
26-
this.last = this.first
26+
this.first = newNode
27+
this.last = newNode
2728
} else {
28-
const newNode = new Node(value)
2929
this.last.next = newNode
3030
this.last = newNode
3131
}
3232
this.length++
3333
return this.last
3434
}
3535
dequeue() {
36+
if (!this.first) return null
3637
const holder = this.first
38+
if (this.first == this.last) {
39+
this.last = null // Special case (When there is only one element)
40+
}
41+
3742
this.first = this.first.next;
3843
this.length--
3944
return holder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, it, expect, beforeEach } from 'bun:test';
2+
import { Stack } from './stack-implement'; // Adjust the import based on your file structure
3+
4+
describe('Stack', () => {
5+
let stack: Stack;
6+
7+
beforeEach(() => {
8+
stack = new Stack();
9+
});
10+
11+
it('should initialize an empty stack', () => {
12+
expect(stack.isEmpty()).toBe(true);
13+
expect(stack.peek()).toBeNull();
14+
});
15+
16+
it('should push elements onto the stack', () => {
17+
stack.push(1);
18+
expect(stack.isEmpty()).toBe(false);
19+
expect(stack.peek().value).toBe(1); // Check the top value
20+
21+
stack.push(3);
22+
expect(stack.peek().value).toBe(3); // Top value should now be 3
23+
24+
stack.push(100);
25+
expect(stack.peek().value).toBe(100); // Top value should now be 100
26+
27+
stack.push(3000);
28+
expect(stack.peek().value).toBe(3000); // Top value should now be 3000
29+
});
30+
31+
it('should pop elements from the stack', () => {
32+
stack.push(1);
33+
stack.push(3);
34+
stack.push(100);
35+
36+
const popped = stack.pop();
37+
expect(popped.value).toBe(100); // Last pushed value should be popped
38+
expect(stack.peek().value).toBe(3); // Now the top value should be 3
39+
40+
stack.pop(); // Pop 3
41+
expect(stack.peek().value).toBe(1); // Now the top value should be 1
42+
43+
stack.pop(); // Pop 1
44+
expect(stack.isEmpty()).toBe(true); // Stack should be empty now
45+
expect(stack.peek()).toBeNull(); // Peek should return null
46+
});
47+
48+
it('should return null when popping from an empty stack', () => {
49+
expect(stack.pop()).toBeNull(); // Popping from an empty stack should return null
50+
});
51+
52+
it('should handle special case when popping the last element', () => {
53+
stack.push(42);
54+
expect(stack.pop().value).toBe(42); // Pop the only element
55+
expect(stack.isEmpty()).toBe(true); // Stack should be empty now
56+
expect(stack.peek()).toBeNull(); // Peek should return null
57+
});
58+
});

src/data_structure/stack/stack-implement.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { LinkedList } from "../linkedList/reverseLinkedList";
21

32
class Node {
43
value: any

0 commit comments

Comments
 (0)