Skip to content

Commit a4fe592

Browse files
committed
feat: add tests
1 parent 7fa2851 commit a4fe592

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, test } from '@jest/globals';
2+
import decimalToBinary from '../decimal-to-binary';
3+
4+
describe('decimalToBinary', () => {
5+
test('should convert decimal number 0 to binary', () => {
6+
expect(decimalToBinary(0)).toBe('0');
7+
});
8+
9+
test('should convert decimal number 1 to binary', () => {
10+
expect(decimalToBinary(1)).toBe('1');
11+
});
12+
13+
test('should convert decimal number 10 to binary', () => {
14+
expect(decimalToBinary(10)).toBe('1010');
15+
});
16+
17+
test('should convert decimal number 15 to binary', () => {
18+
expect(decimalToBinary(15)).toBe('1111');
19+
});
20+
21+
test('should convert decimal number 100 to binary', () => {
22+
expect(decimalToBinary(100)).toBe('1100100');
23+
});
24+
25+
test('should convert decimal number 255 to binary', () => {
26+
expect(decimalToBinary(255)).toBe('11111111');
27+
});
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, test } from '@jest/globals';
2+
import decimalToBinary from '../decimal-to-binary';
3+
4+
describe('decimalToBinary', () => {
5+
test('should convert decimal number 0 to binary', () => {
6+
expect(decimalToBinary(0)).toBe('0');
7+
});
8+
9+
test('should convert decimal number 1 to binary', () => {
10+
expect(decimalToBinary(1)).toBe('1');
11+
});
12+
13+
test('should convert decimal number 10 to binary', () => {
14+
expect(decimalToBinary(10)).toBe('1010');
15+
});
16+
17+
test('should convert decimal number 15 to binary', () => {
18+
expect(decimalToBinary(15)).toBe('1111');
19+
});
20+
21+
test('should convert decimal number 100 to binary', () => {
22+
expect(decimalToBinary(100)).toBe('1100100');
23+
});
24+
25+
test('should convert decimal number 255 to binary', () => {
26+
expect(decimalToBinary(255)).toBe('11111111');
27+
});
28+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { describe, expect, test, beforeEach } from '@jest/globals';
2+
import Stack from '../stack-object';
3+
4+
describe('Stack', () => {
5+
let stack: Stack<number>;
6+
7+
beforeEach(() => {
8+
stack = new Stack<number>();
9+
});
10+
11+
test('push should add an item to the stack', () => {
12+
stack.push(1);
13+
stack.push(2);
14+
stack.push(3);
15+
16+
expect(stack.size).toBe(3);
17+
});
18+
19+
test('pop should remove and return the top item from the stack', () => {
20+
stack.push(1);
21+
stack.push(2);
22+
stack.push(3);
23+
24+
const poppedItem = stack.pop();
25+
26+
expect(poppedItem).toBe(3);
27+
expect(stack.size).toBe(2);
28+
});
29+
30+
test('pop should return undefined if the stack is empty', () => {
31+
expect(stack.pop()).toBeUndefined();
32+
});
33+
34+
test('peek should return the top item from the stack without removing it', () => {
35+
stack.push(1);
36+
stack.push(2);
37+
stack.push(3);
38+
39+
const topItem = stack.peek();
40+
41+
expect(topItem).toBe(3);
42+
expect(stack.size).toBe(3);
43+
});
44+
45+
test('isEmpty should return true if the stack is empty', () => {
46+
expect(stack.isEmpty()).toBe(true);
47+
48+
stack.push(1);
49+
50+
expect(stack.isEmpty()).toBe(false);
51+
});
52+
53+
test('size should return the number of items in the stack', () => {
54+
expect(stack.size).toBe(0);
55+
56+
stack.push(1);
57+
stack.push(2);
58+
stack.push(3);
59+
60+
expect(stack.size).toBe(3);
61+
});
62+
63+
test('clear should remove all items from the stack', () => {
64+
stack.push(1);
65+
stack.push(2);
66+
stack.push(3);
67+
68+
stack.clear();
69+
70+
expect(stack.size).toBe(0);
71+
expect(stack.isEmpty()).toBe(true);
72+
});
73+
74+
test('toString should return a string representation of the stack', () => {
75+
stack.push(1);
76+
stack.push(2);
77+
stack.push(3);
78+
79+
expect(stack.toString()).toBe('1, 2, 3');
80+
});
81+
82+
test('toString should return an empty string if the stack is empty', () => {
83+
expect(stack.toString()).toBe('Empty Stack');
84+
});
85+
86+
test('toString should return a string representation of the stack with custom object', () => {
87+
const stack = new Stack<{ key: string; value: number }>();
88+
89+
stack.push({ key: 'a', value: 1 });
90+
stack.push({ key: 'b', value: 2 });
91+
stack.push({ key: 'c', value: 3 });
92+
93+
expect(stack.toString()).toBe(
94+
'{"key":"a","value":1}, {"key":"b","value":2}, {"key":"c","value":3}'
95+
);
96+
});
97+
});

src/04-stack/_test_/stack.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {describe, expect, test, beforeEach} from '@jest/globals';
2+
import Stack from '../stack';
3+
4+
describe('Stack', () => {
5+
let stack: Stack<number>;
6+
7+
beforeEach(() => {
8+
stack = new Stack<number>();
9+
});
10+
11+
test('push should add an item to the stack', () => {
12+
stack.push(1);
13+
stack.push(2);
14+
stack.push(3);
15+
16+
expect(stack.size).toBe(3);
17+
});
18+
19+
test('pop should remove and return the top item from the stack', () => {
20+
stack.push(1);
21+
stack.push(2);
22+
stack.push(3);
23+
24+
const poppedItem = stack.pop();
25+
26+
expect(poppedItem).toBe(3);
27+
expect(stack.size).toBe(2);
28+
});
29+
30+
test('peek should return the top item from the stack without removing it', () => {
31+
stack.push(1);
32+
stack.push(2);
33+
stack.push(3);
34+
35+
const topItem = stack.peek();
36+
37+
expect(topItem).toBe(3);
38+
expect(stack.size).toBe(3);
39+
});
40+
41+
test('isEmpty should return true if the stack is empty', () => {
42+
expect(stack.isEmpty()).toBe(true);
43+
44+
stack.push(1);
45+
46+
expect(stack.isEmpty()).toBe(false);
47+
});
48+
49+
test('size should return the number of items in the stack', () => {
50+
expect(stack.size).toBe(0);
51+
52+
stack.push(1);
53+
stack.push(2);
54+
stack.push(3);
55+
56+
expect(stack.size).toBe(3);
57+
});
58+
59+
test('clear should remove all items from the stack', () => {
60+
stack.push(1);
61+
stack.push(2);
62+
stack.push(3);
63+
64+
stack.clear();
65+
66+
expect(stack.size).toBe(0);
67+
expect(stack.isEmpty()).toBe(true);
68+
});
69+
70+
test('toString should return a string representation of the stack', () => {
71+
stack.push(1);
72+
stack.push(2);
73+
stack.push(3);
74+
75+
expect(stack.toString()).toBe('1, 2, 3');
76+
});
77+
78+
test('toString should return an empty string if the stack is empty', () => {
79+
expect(stack.toString()).toBe('Empty Stack');
80+
});
81+
82+
test('toString should return a string representation of the stack with custom object', () => {
83+
const stack = new Stack<{key: string, value: number}>();
84+
85+
stack.push({key: 'a', value: 1});
86+
stack.push({key: 'b', value: 2});
87+
stack.push({key: 'c', value: 3});
88+
89+
expect(stack.toString()).toBe('{"key":"a","value":1}, {"key":"b","value":2}, {"key":"c","value":3}');
90+
});
91+
92+
});

0 commit comments

Comments
 (0)