Skip to content

Commit ff636d7

Browse files
author
Kohei Asai
authored
Refactor tests (#94)
* Refactor tests * Refactor tests * Refactor tests * Refactor tests
1 parent 0e4fcde commit ff636d7

File tree

63 files changed

+952
-1073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+952
-1073
lines changed

solutions/addBinary.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import addBinary from "./addBinary";
22

33
describe("67. Add Binary", () => {
4-
test("#1", () => {
5-
expect(addBinary("11", "1")).toBe("100");
6-
});
4+
const TEST_CASES = new Map([
5+
[["11", "1"], "100"],
6+
[["1010", "1011"], "10101"]
7+
]);
78

8-
test("#2", () => {
9-
expect(addBinary("1010", "1011")).toBe("10101");
10-
});
9+
for (const [[a, b], expected] of TEST_CASES) {
10+
it(`returns ${expected} when called with ${a} and ${b}`, () => {
11+
expect(addBinary(a, b)).toBe(expected);
12+
});
13+
}
1114
});
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import backspaceCompare from "./backspaceStringCompare";
22

33
describe("844. Backspace String Compare", () => {
4-
test("#1", () => {
5-
expect(backspaceCompare("ab#c", "ad#c")).toBe(true);
6-
});
4+
const TEST_CASES = new Map([
5+
[["ab#c", "ad#c"], true],
6+
[["ab##", "c#d#"], true],
7+
[["a##c", "#a#c"], true],
8+
[["a#c", "b"], false]
9+
]);
710

8-
test("#2", () => {
9-
expect(backspaceCompare("ab##", "c#d#")).toBe(true);
10-
});
11-
12-
test("#3", () => {
13-
expect(backspaceCompare("a##c", "#a#c")).toBe(true);
14-
});
15-
16-
test("#4", () => {
17-
expect(backspaceCompare("a#c", "b")).toBe(false);
18-
});
11+
for (const [[s, t], expected] of TEST_CASES) {
12+
it(`returns ${expected} when called with ${s} and ${t}`, () => {
13+
expect(backspaceCompare(s, t)).toBe(expected);
14+
});
15+
}
1916
});
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import maxProfit from "./bestTimeToBuyAndSellStock";
22

33
describe("121. Best Time to Buy and Sell Stock", () => {
4-
test("#1", () => {
5-
expect(maxProfit([7, 1, 5, 3, 6, 4])).toBe(5);
6-
});
4+
const TEST_CASES = new Map([
5+
[[7, 1, 5, 3, 6, 4], 5],
6+
[[7, 6, 4, 3, 1], 0],
7+
[[7, 6, 5, 2, 3, 4, 1], 2],
8+
[[7, 3, 8, 1, 2, 5, 4], 5]
9+
]);
710

8-
test("#2", () => {
9-
expect(maxProfit([7, 6, 4, 3, 1])).toBe(0);
10-
});
11-
12-
test("#3", () => {
13-
expect(maxProfit([7, 6, 5, 2, 3, 4, 1])).toBe(2);
14-
});
15-
16-
test("#4", () => {
17-
expect(maxProfit([7, 3, 8, 1, 2, 5, 4])).toBe(5);
18-
});
11+
for (const [prices, expected] of TEST_CASES) {
12+
it(`returns ${expected} when called with [${prices}]`, () => {
13+
expect(maxProfit(prices)).toBe(expected);
14+
});
15+
}
1916
});

solutions/binarySearch.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import search from "./binarySearch";
22

33
describe("704. Binary Search", () => {
4-
test("#1", () => {
5-
expect(search([-1, 0, 3, 5, 9, 12], 9)).toBe(4);
6-
});
4+
const TEST_CASES = new Map<[number[], number], number>([
5+
[[[-1, 0, 3, 5, 9, 12], 9], 4],
6+
[[[-1, 0, 3, 5, 9, 12], 13], -1]
7+
]);
78

8-
test("#2", () => {
9-
expect(search([-1, 0, 3, 5, 9, 12], 13)).toBe(-1);
10-
});
9+
for (const [[nums, target], expected] of TEST_CASES) {
10+
it(`returns ${expected} when called with [${nums}] and ${target}`, () => {
11+
expect(search(nums, target)).toBe(expected);
12+
});
13+
}
1114
});

solutions/binaryTreePreorderTraversal.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("144. Binary Tree Preorder Traversal", () => {
1414
]);
1515

1616
for (const [tree, expected] of TEST_CASES) {
17-
test(`when [${tree.join(", ")}]`, () => {
17+
it(`returns ${expected} when called with [${tree}]`, () => {
1818
expect(preorderTraversal(createBinaryTreeNode(tree))).toEqual(expected);
1919
});
2020
}

solutions/bullsAndCows.test.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
import getHint from "./bullsAndCows";
22

33
describe("299. Bulls and Cows", () => {
4-
test("#1", () => {
5-
expect(getHint("1807", "7810")).toBe("1A3B");
6-
});
4+
const TEST_CASES = new Map([
5+
[["1807", "7810"], "1A3B"],
6+
[["1123", "0111"], "1A1B"],
7+
[["1234", "5678"], "0A0B"],
8+
[["1234", "1234"], "4A0B"],
9+
[["1234", "4123"], "0A4B"],
10+
[["1122", "0001"], "0A1B"],
11+
[["11", "10"], "1A0B"]
12+
]);
713

8-
test("#2", () => {
9-
expect(getHint("1123", "0111")).toBe("1A1B");
10-
});
11-
12-
test("#3", () => {
13-
expect(getHint("1234", "5678")).toBe("0A0B");
14-
});
15-
16-
test("#4", () => {
17-
expect(getHint("1234", "1234")).toBe("4A0B");
18-
});
19-
20-
test("#5", () => {
21-
expect(getHint("1234", "4123")).toBe("0A4B");
22-
});
23-
24-
test("#6", () => {
25-
expect(getHint("1122", "0001")).toBe("0A1B");
26-
});
27-
28-
test("#7", () => {
29-
expect(getHint("11", "10")).toBe("1A0B");
30-
});
14+
for (const [[secret, guess], expected] of TEST_CASES) {
15+
it(`returns ${expected} when called with ${secret} and ${guess}`, () => {
16+
expect(getHint(secret, guess)).toBe(expected);
17+
});
18+
}
3119
});

solutions/climbingStairs.test.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
import climbStairs from "./climbingStairs";
22

33
describe("70. Climbing Stairs", () => {
4-
test("#1", () => {
5-
expect(climbStairs(1)).toBe(1);
6-
});
4+
const TEST_CASES = new Map([[1, 1], [2, 2], [3, 3], [4, 5], [32, 3524578]]);
75

8-
test("#2", () => {
9-
expect(climbStairs(2)).toBe(2);
10-
});
11-
12-
test("#3", () => {
13-
expect(climbStairs(3)).toBe(3);
14-
});
15-
16-
test("#4", () => {
17-
expect(climbStairs(4)).toBe(5);
18-
});
19-
20-
test("#5", () => {
21-
expect(climbStairs(32)).toBe(3524578);
22-
});
6+
for (const [n, expected] of TEST_CASES) {
7+
it(`returns ${expected} when called with ${n}`, () => {
8+
expect(climbStairs(n)).toBe(expected);
9+
});
10+
}
2311
});

solutions/countAndSay.test.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
import countAndSay from "./countAndSay";
22

33
describe("38. Count and Say", () => {
4-
test("#1", () => {
5-
expect(countAndSay(1)).toBe("1");
6-
});
7-
8-
test("#2", () => {
9-
expect(countAndSay(2)).toBe("11");
10-
});
11-
12-
test("#3", () => {
13-
expect(countAndSay(3)).toBe("21");
14-
});
15-
16-
test("#4", () => {
17-
expect(countAndSay(4)).toBe("1211");
18-
});
19-
20-
test("#5", () => {
21-
expect(countAndSay(5)).toBe("111221");
22-
});
23-
24-
test("#6", () => {
25-
expect(countAndSay(10)).toBe("13211311123113112211");
26-
});
27-
28-
test("#7", () => {
29-
expect(countAndSay(30)).toBe(
4+
const TEST_CASES = new Map([
5+
[1, "1"],
6+
[2, "11"],
7+
[3, "21"],
8+
[4, "1211"],
9+
[5, "111221"],
10+
[10, "13211311123113112211"],
11+
[
12+
30,
3013
"3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211"
31-
);
32-
});
14+
]
15+
]);
16+
17+
for (const [n, expected] of TEST_CASES) {
18+
it(`returns ${expected} when called with ${n}`, () => {
19+
expect(countAndSay(n)).toBe(expected);
20+
});
21+
}
3322
});

solutions/countCompleteTreeNodes.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ import { createBinaryTreeNode } from "../utilities/TreeNode";
22
import countNodes from "./countCompleteTreeNodes";
33

44
describe("222. Count Complete Tree Nodes", () => {
5-
test("#1", () => {
6-
expect(countNodes(createBinaryTreeNode([1, 2, 3, 4, 5, 6]))).toBe(6);
7-
});
5+
const TEST_CASES = new Map([
6+
[[1, 2, 3, 4, 5, 6], 6],
7+
[[], 0],
8+
[[1, 2, 3, 4, 5, 6, 7, 8], 8],
9+
[[1, 2, 3, 4, 5, 6, 7], 7]
10+
]);
811

9-
test("#2", () => {
10-
expect(countNodes(null)).toBe(0);
11-
});
12-
13-
test("#3", () => {
14-
expect(countNodes(createBinaryTreeNode([1, 2, 3, 4, 5, 6, 7, 8]))).toBe(8);
15-
});
16-
17-
test("#4", () => {
18-
expect(countNodes(createBinaryTreeNode([1, 2, 3, 4, 5, 6, 7]))).toBe(7);
19-
});
12+
for (const [values, expected] of TEST_CASES) {
13+
it(`returns ${expected} when called with [${values}]`, () => {
14+
expect(countNodes(createBinaryTreeNode(values))).toBe(expected);
15+
});
16+
}
2017
});

solutions/diameterOfBinaryTree.test.ts

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,50 @@ import { createBinaryTreeNode } from "../utilities/TreeNode";
22
import diameterOfBinaryTree from "./diameterOfBinaryTree";
33

44
describe("543. Diameter of Binary Tree", () => {
5-
test("#1", () => {
6-
expect(diameterOfBinaryTree(createBinaryTreeNode([1, 2, 3, 4, 5]))).toBe(3);
7-
});
5+
const TEST_CASES = new Map([
6+
[[1, 2, 3, 4, 5], 3],
7+
[[1], 0],
8+
[[], 0],
9+
[
10+
[
11+
4,
12+
-7,
13+
-3,
14+
null,
15+
null,
16+
-9,
17+
-3,
18+
9,
19+
-7,
20+
-4,
21+
null,
22+
6,
23+
null,
24+
-6,
25+
-6,
26+
null,
27+
null,
28+
0,
29+
6,
30+
5,
31+
null,
32+
9,
33+
null,
34+
null,
35+
-1,
36+
-4,
37+
null,
38+
null,
39+
null,
40+
-2
41+
],
42+
8
43+
]
44+
]);
845

9-
test("#2", () => {
10-
expect(diameterOfBinaryTree(createBinaryTreeNode([1]))).toBe(0);
11-
});
12-
13-
test("#3", () => {
14-
expect(diameterOfBinaryTree(null)).toBe(0);
15-
});
16-
17-
test("#4", () => {
18-
expect(
19-
diameterOfBinaryTree(
20-
createBinaryTreeNode([
21-
4,
22-
-7,
23-
-3,
24-
null,
25-
null,
26-
-9,
27-
-3,
28-
9,
29-
-7,
30-
-4,
31-
null,
32-
6,
33-
null,
34-
-6,
35-
-6,
36-
null,
37-
null,
38-
0,
39-
6,
40-
5,
41-
null,
42-
9,
43-
null,
44-
null,
45-
-1,
46-
-4,
47-
null,
48-
null,
49-
null,
50-
-2
51-
])
52-
)
53-
).toBe(8);
54-
});
46+
for (const [values, expected] of TEST_CASES) {
47+
it(`returns ${expected} when called with [${values}]`, () => {
48+
expect(diameterOfBinaryTree(createBinaryTreeNode(values))).toBe(expected);
49+
});
50+
}
5551
});

0 commit comments

Comments
 (0)