Skip to content

Commit 1797278

Browse files
committed
refactor: update test cases
1 parent 55d09ce commit 1797278

14 files changed

+293
-94
lines changed

easy/concatenation-of-array.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
const getConcatenation = (nums: number[]): number[] => [...nums, ...nums];
44

55
describe('Concatenation of Array', () => {
6-
it('should return the concatenated array', () => {
7-
expect(getConcatenation([1])).toEqual([1, 1]);
8-
expect(getConcatenation([1, 1])).toEqual([1, 1, 1, 1]);
9-
expect(getConcatenation([1, 2, 1])).toEqual([1, 2, 1, 1, 2, 1]);
10-
expect(getConcatenation([1, 3, 2, 1])).toEqual([1, 3, 2, 1, 1, 3, 2, 1]);
6+
it('#1 should return [1, 2, 1, 1, 2, 1]', () => {
7+
const nums = [1, 2, 1];
8+
expect(getConcatenation(nums)).toStrictEqual([1, 2, 1, 1, 2, 1]);
9+
});
10+
11+
it('#2 should return [1, 3, 2, 1, 1, 3, 2, 1]', () => {
12+
const nums = [1, 3, 2, 1];
13+
expect(getConcatenation(nums)).toStrictEqual([1, 3, 2, 1, 1, 3, 2, 1]);
14+
});
15+
16+
it('#3 should return [1, 2, 3, 1, 2, 3]', () => {
17+
const nums = [1, 2, 3];
18+
expect(getConcatenation(nums)).toStrictEqual([1, 2, 3, 1, 2, 3]);
1119
});
1220
});

easy/contains-duplicate.test.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@ const containsDuplicate = (nums: number[]): boolean => {
1414
};
1515

1616
describe('Contains Duplicate', () => {
17-
it('should return true if the array contains duplicates', () => {
18-
expect(containsDuplicate([1, 2, 3, 4, 1])).toBe(true);
19-
expect(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])).toBe(true);
17+
it('#1 should return true', () => {
18+
const nums = [1, 2, 3, 1];
19+
expect(containsDuplicate(nums)).toBe(true);
2020
});
2121

22-
it('should return false if the array does not contain duplicates', () => {
23-
expect(containsDuplicate([])).toBe(false);
24-
expect(containsDuplicate([0])).toBe(false);
25-
expect(containsDuplicate([1, 2, 3, 4])).toBe(false);
22+
it('#2 should return false', () => {
23+
const nums = [1, 2, 3, 4];
24+
expect(containsDuplicate(nums)).toBe(false);
25+
});
26+
27+
it('#3 should return true', () => {
28+
const nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2];
29+
expect(containsDuplicate(nums)).toBe(true);
30+
});
31+
32+
it('#4 should return false', () => {
33+
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
34+
expect(containsDuplicate(nums)).toBe(false);
35+
});
36+
37+
it('#5 should return true', () => {
38+
const nums = [1, 1];
39+
expect(containsDuplicate(nums)).toBe(true);
2640
});
2741
});

easy/crawler-log-folder.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,28 @@ const minOperations = (logs: string[]): number => {
1414
};
1515

1616
describe('Crawler Log Folder', () => {
17-
it('should return the minimum number of operations', () => {
18-
expect(minOperations(['d1/', 'd2/', './', 'd3/', '../', 'd31/'])).toBe(3);
19-
expect(minOperations(['d1/', '../', '../', '../'])).toBe(0);
20-
expect(minOperations(['../'])).toBe(0);
17+
it('#1 should return 2', () => {
18+
const logs = ['d1/', 'd2/', '../', 'd21/', './'];
19+
expect(minOperations(logs)).toBe(2);
20+
});
21+
22+
it('#2 should return 3', () => {
23+
const logs = ['d1/', 'd2/', './', 'd3/', '../', 'd31/'];
24+
expect(minOperations(logs)).toBe(3);
25+
});
26+
27+
it('#3 should return 0', () => {
28+
const logs = ['d1/', '../', '../', '../'];
29+
expect(minOperations(logs)).toBe(0);
30+
});
31+
32+
it('#4 should return 0', () => {
33+
const logs = ['./', './', './'];
34+
expect(minOperations(logs)).toBe(0);
35+
});
36+
37+
it('#5 should return 1', () => {
38+
const logs = ['d1/', './', './', './'];
39+
expect(minOperations(logs)).toBe(1);
2140
});
2241
});

easy/happy-number.test.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,28 @@ const isHappy = (n: number): boolean => {
2020
};
2121

2222
describe('Happy Number', () => {
23-
it('should return true if the number is happy', () => {
24-
expect(isHappy(19)).toBeTruthy();
25-
expect(isHappy(1)).toBeTruthy();
26-
expect(isHappy(7)).toBeTruthy();
23+
it('#1 should return true', () => {
24+
const n = 19;
25+
expect(isHappy(n)).toBe(true);
2726
});
2827

29-
it('should return false if the number is not happy', () => {
30-
expect(isHappy(2)).toBeFalsy();
31-
expect(isHappy(3)).toBeFalsy();
32-
expect(isHappy(4)).toBeFalsy();
28+
it('#2 should return false', () => {
29+
const n = 2;
30+
expect(isHappy(n)).toBe(false);
31+
});
32+
33+
it('#3 should return false', () => {
34+
const n = 3;
35+
expect(isHappy(n)).toBe(false);
36+
});
37+
38+
it('#4 should return true', () => {
39+
const n = 7;
40+
expect(isHappy(n)).toBe(true);
41+
});
42+
43+
it('#5 should return true', () => {
44+
const n = 1111111;
45+
expect(isHappy(n)).toBe(true);
3346
});
3447
});

easy/majority-element.test.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,28 @@ const majorityElement = (nums: number[]): number => {
1818
};
1919

2020
describe('Majority Element', () => {
21-
it('should return the majority element', () => {
22-
expect(majorityElement([3, 2, 3])).toBe(3);
23-
expect(majorityElement([2, 2, 1, 1, 1, 2, 2])).toBe(2);
21+
it('#1 should return 3', () => {
22+
const nums = [3, 2, 3];
23+
expect(majorityElement(nums)).toBe(3);
24+
});
25+
26+
it('#2 should return 2', () => {
27+
const nums = [2, 2, 1, 1, 1, 2, 2];
28+
expect(majorityElement(nums)).toBe(2);
29+
});
30+
31+
it('#3 should return 3', () => {
32+
const nums = [3, 3, 4];
33+
expect(majorityElement(nums)).toBe(3);
34+
});
35+
36+
it('#4 should return 3', () => {
37+
const nums = [3, 3, 3, 3, 3, 3, 3];
38+
expect(majorityElement(nums)).toBe(3);
39+
});
40+
41+
it('#5 should return 1', () => {
42+
const nums = [1];
43+
expect(majorityElement(nums)).toBe(1);
2444
});
2545
});

easy/number-of-good-pairs.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ const numIdenticalPairs = (nums: number[]): number => {
3434
};
3535

3636
describe('Number of Good Pairs', () => {
37-
it('should return 4', () => {
37+
it('#1 should return 4', () => {
3838
const nums = [1, 2, 3, 1, 1, 3];
3939
expect(numIdenticalPairs(nums)).toBe(4);
4040
});
4141

42-
it('should return 6', () => {
42+
it('#2 should return 6', () => {
4343
const nums = [1, 1, 1, 1];
4444
expect(numIdenticalPairs(nums)).toBe(6);
4545
});
4646

47-
it('should return 0', () => {
47+
it('#3 should return 0', () => {
4848
const nums = [1, 2, 3];
4949
expect(numIdenticalPairs(nums)).toBe(0);
5050
});
5151

52-
it('should return 5', () => {
52+
it('#4 should return 5', () => {
5353
const nums = [1, 2, 3, 1, 1, 3, 2];
5454
expect(numIdenticalPairs(nums)).toBe(5);
5555
});

easy/pascals-triangle.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@ const generate = (numRows: number): number[][] => {
2020
};
2121

2222
describe("Pascal's Triangle", () => {
23-
it('should return the pascal triangle', () => {
24-
expect(generate(5)).toEqual([
23+
it('#1 should return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]', () => {
24+
const numRows = 5;
25+
expect(generate(numRows)).toStrictEqual([
2526
[1],
2627
[1, 1],
2728
[1, 2, 1],
2829
[1, 3, 3, 1],
2930
[1, 4, 6, 4, 1],
3031
]);
31-
expect(generate(1)).toEqual([[1]]);
32+
});
33+
34+
it('#2 should return [[1],[1,1],[1,2,1]]', () => {
35+
const numRows = 3;
36+
expect(generate(numRows)).toStrictEqual([[1], [1, 1], [1, 2, 1]]);
37+
});
38+
39+
it('#3 should return [[1],[1,1]]', () => {
40+
const numRows = 2;
41+
expect(generate(numRows)).toStrictEqual([[1], [1, 1]]);
3242
});
3343
});

easy/remove-element.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,35 @@ const removeElement = (nums: number[], val: number): number => {
1414
};
1515

1616
describe('Remove Element', () => {
17-
it('should remove the element from the array', () => {
18-
const arr1 = [3, 2, 2, 3];
19-
expect(removeElement(arr1, 3)).toBe(2);
20-
expect(arr1.slice(0, 2)).toEqual([2, 2]);
17+
it('#1 should return 2 and array should be [2, 2]', () => {
18+
const nums = [3, 2, 2, 3];
19+
const val = 3;
20+
const result = removeElement(nums, val);
21+
expect(result).toBe(2);
22+
expect(nums.slice(0, result)).toEqual([2, 2]);
23+
});
24+
25+
it('#2 should return 5 and array should be [0, 1, 3, 0, 4]', () => {
26+
const nums = [0, 1, 2, 2, 3, 0, 4, 2];
27+
const val = 2;
28+
const result = removeElement(nums, val);
29+
expect(result).toBe(5);
30+
expect(nums.slice(0, result)).toEqual([0, 1, 3, 0, 4]);
31+
});
32+
33+
it('#3 should return 0 and array should be []', () => {
34+
const nums = [];
35+
const val = 0;
36+
const result = removeElement(nums, val);
37+
expect(result).toBe(0);
38+
expect(nums.slice(0, result)).toEqual([]);
39+
});
2140

22-
const arr2 = [0, 1, 2, 2, 3, 0, 4, 2];
23-
expect(removeElement(arr2, 2)).toBe(5);
24-
expect(arr2.slice(0, 5)).toEqual([0, 1, 3, 0, 4]);
41+
it('#4 should return 0 and array should be []', () => {
42+
const nums = [1];
43+
const val = 1;
44+
const result = removeElement(nums, val);
45+
expect(result).toBe(0);
46+
expect(nums.slice(0, result)).toEqual([]);
2547
});
2648
});

easy/sqrtx.test.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,28 @@ const mySqrt = (x: number): number => {
2222
};
2323

2424
describe('Sqrt(x)', () => {
25-
it('should return the square root of x', () => {
26-
expect(mySqrt(0)).toBe(0);
27-
expect(mySqrt(1)).toBe(1);
28-
expect(mySqrt(2)).toBe(1);
29-
expect(mySqrt(4)).toBe(2);
30-
expect(mySqrt(8)).toBe(2);
31-
expect(mySqrt(9)).toBe(3);
32-
expect(mySqrt(16)).toBe(4);
33-
expect(mySqrt(25)).toBe(5);
34-
expect(mySqrt(36)).toBe(6);
35-
expect(mySqrt(49)).toBe(7);
36-
expect(mySqrt(64)).toBe(8);
37-
expect(mySqrt(81)).toBe(9);
38-
expect(mySqrt(100)).toBe(10);
25+
it('#1 should return 2', () => {
26+
const x = 4;
27+
expect(mySqrt(x)).toBe(2);
28+
});
29+
30+
it('#2 should return 2', () => {
31+
const x = 8;
32+
expect(mySqrt(x)).toBe(2);
33+
});
34+
35+
it('#3 should return 1', () => {
36+
const x = 1;
37+
expect(mySqrt(x)).toBe(1);
38+
});
39+
40+
it('#4 should return 46340', () => {
41+
const x = 2147395600;
42+
expect(mySqrt(x)).toBe(46340);
43+
});
44+
45+
it('#5 should return 0', () => {
46+
const x = 0;
47+
expect(mySqrt(x)).toBe(0);
3948
});
4049
});

easy/two-sum.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,27 @@ export const twoSum = (nums: number[], target: number): number[] => {
1818
};
1919

2020
describe('Two Sum', () => {
21-
it('should return the indices of the two numbers that add up to the target', () => {
22-
expect(twoSum([2, 7, 11, 15], 9)).toEqual([0, 1]);
23-
expect(twoSum([3, 3], 6)).toEqual([0, 1]);
24-
expect(twoSum([2, 7, 11, 15], 17)).toEqual([0, 3]);
21+
it('#1 should return [0, 1]', () => {
22+
const nums = [2, 7, 11, 15];
23+
const target = 9;
24+
expect(twoSum(nums, target)).toEqual([0, 1]);
25+
});
26+
27+
it('#2 should return [1, 2]', () => {
28+
const nums = [3, 2, 4];
29+
const target = 6;
30+
expect(twoSum(nums, target)).toEqual([1, 2]);
31+
});
32+
33+
it('#3 should return [0, 1]', () => {
34+
const nums = [3, 3];
35+
const target = 6;
36+
expect(twoSum(nums, target)).toEqual([0, 1]);
37+
});
38+
39+
it('#4 should return [0, 1]', () => {
40+
const nums = [3, 2, 3];
41+
const target = 6;
42+
expect(twoSum(nums, target)).toEqual([0, 2]);
2543
});
2644
});

easy/valid-anagram.test.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,33 @@ const isAnagram = (s: string, t: string): boolean => {
3030
};
3131

3232
describe('Valid Anagram', () => {
33-
it('should return true if the strings are anagrams', () => {
34-
expect(isAnagram('anagram', 'nagrama')).toBe(true);
35-
expect(isAnagram('', '')).toBe(true);
36-
expect(isAnagram('a', 'a')).toBe(true);
37-
expect(isAnagram('kasur', 'rusak')).toBe(true);
33+
it('#1 should return true', () => {
34+
const s = 'anagram';
35+
const t = 'nagaram';
36+
expect(isAnagram(s, t)).toBe(true);
3837
});
3938

40-
it('should return false if the strings are not anagrams', () => {
41-
expect(isAnagram('anagram', 'nagram')).toBe(false);
42-
expect(isAnagram('', 'a')).toBe(false);
43-
expect(isAnagram('a', '')).toBe(false);
44-
expect(isAnagram('kasur', 'sura')).toBe(false);
39+
it('#2 should return false', () => {
40+
const s = 'rat';
41+
const t = 'car';
42+
expect(isAnagram(s, t)).toBe(false);
43+
});
44+
45+
it('#3 should return false', () => {
46+
const s = 'a';
47+
const t = 'ab';
48+
expect(isAnagram(s, t)).toBe(false);
49+
});
50+
51+
it('#4 should return false', () => {
52+
const s = 'a';
53+
const t = 'b';
54+
expect(isAnagram(s, t)).toBe(false);
55+
});
56+
57+
it('#5 should return true', () => {
58+
const s = 'a';
59+
const t = 'a';
60+
expect(isAnagram(s, t)).toBe(true);
4561
});
4662
});

0 commit comments

Comments
 (0)