Skip to content

Commit f76b11b

Browse files
authored
Add unit tests for LeetCode algorithms using migrated tests from another project. (#27)
* add test file * add test files
1 parent 61c20f0 commit f76b11b

Some content is hidden

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

74 files changed

+2063
-0
lines changed

test/0002-test.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0002-Add-Two-Numbers/Add_Two_Numbers.cpp"
5+
#include "../util/ListNode.h"
6+
#include "LinkedListUtils.cpp"
7+
8+
9+
TEST(test, test1)
10+
{
11+
LinkedListUtils linkedListUtils;
12+
ListNode *l1 = linkedListUtils.contructLinkedList({2, 4, 3});
13+
ListNode *l2 = linkedListUtils.contructLinkedList({5, 6, 4});
14+
ListNode *expected = linkedListUtils.contructLinkedList({7, 0, 8});
15+
ASSERT_EQ(*expected, *Solution().addTwoNumbers(l1, l2));
16+
}
17+
18+
19+
TEST(test, test2)
20+
{
21+
LinkedListUtils linkedListUtils;
22+
ListNode *l1 = linkedListUtils.contructLinkedList({1, 8});
23+
ListNode *l2 = linkedListUtils.contructLinkedList({0});
24+
ListNode *expected = linkedListUtils.contructLinkedList({1, 8});
25+
ASSERT_EQ(*expected, *Solution().addTwoNumbers(l1, l2));
26+
}
27+
28+
29+
TEST(test, test3)
30+
{
31+
LinkedListUtils linkedListUtils;
32+
ListNode *l1 = linkedListUtils.contructLinkedList({5});
33+
ListNode *l2 = linkedListUtils.contructLinkedList({5});
34+
ListNode *expected = linkedListUtils.contructLinkedList({0, 1});
35+
ASSERT_EQ(*expected, *Solution().addTwoNumbers(l1, l2));
36+
}
37+
38+
39+
40+
GTEST_API_ int main(int argc, char **argv)
41+
{
42+
testing::InitGoogleTest(&argc, argv);
43+
return RUN_ALL_TESTS();
44+
}

test/0003-test.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0003-Longest-Substring-Without-Repeating-Characters/0003.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
int expected = 3;
9+
std::string s = "abcabcbb";
10+
11+
ASSERT_EQ(expected, Solution().lengthOfLongestSubstring(s));
12+
}
13+
14+
GTEST_API_ int main(int argc, char **argv)
15+
{
16+
testing::InitGoogleTest(&argc, argv);
17+
return RUN_ALL_TESTS();
18+
}

test/0004-test.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0004-Median-of-Two-Sorted-Arrays/0004.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
std::vector<int> A = {1, 3};
9+
std::vector<int> B = {2};
10+
ASSERT_EQ(2.0, Solution().findMedianSortedArrays(A, B));
11+
}
12+
13+
14+
TEST(test, test2)
15+
{
16+
std::vector<int> A = {1, 2};
17+
std::vector<int> B = {3, 4};
18+
ASSERT_EQ(2.5, Solution().findMedianSortedArrays(A, B));
19+
}
20+
21+
22+
GTEST_API_ int main(int argc, char **argv)
23+
{
24+
testing::InitGoogleTest(&argc, argv);
25+
return RUN_ALL_TESTS();
26+
}

test/0005-test.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0005-Longest-Palindromic-Substring/0005.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
std::string s = "babad";
9+
ASSERT_EQ("bab", Solution().longestPalindrome(s));
10+
}
11+
12+
GTEST_API_ int main(int argc, char **argv)
13+
{
14+
testing::InitGoogleTest(&argc, argv);
15+
return RUN_ALL_TESTS();
16+
}

test/0006-test.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0006-ZigZag-Conversion/0006.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ("PAHNAPLSIIGYIR", Solution().convert("PAYPALISHIRING", 3));
9+
}
10+
11+
GTEST_API_ int main(int argc, char **argv)
12+
{
13+
testing::InitGoogleTest(&argc, argv);
14+
return RUN_ALL_TESTS();
15+
}

test/0007-test.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0007-Reverse-Integer/0007.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ(0, Solution().reverse(1534236469));
9+
}
10+
11+
TEST(test, test2)
12+
{
13+
ASSERT_EQ(-123, Solution().reverse(-321));
14+
}
15+
16+
17+
GTEST_API_ int main(int argc, char **argv)
18+
{
19+
testing::InitGoogleTest(&argc, argv);
20+
return RUN_ALL_TESTS();
21+
}

test/0009-test.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0009-Palindrome-Number/0009.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ(false, Solution().isPalindrome(2147483647));
9+
}
10+
11+
TEST(test, test2)
12+
{
13+
ASSERT_EQ(true, Solution().isPalindrome(0));
14+
}
15+
16+
TEST(test, test3)
17+
{
18+
ASSERT_EQ(true, Solution().isPalindrome(1));
19+
}
20+
21+
TEST(test, test4)
22+
{
23+
ASSERT_EQ(false, Solution().isPalindrome(10));
24+
}
25+
26+
GTEST_API_ int main(int argc, char **argv)
27+
{
28+
testing::InitGoogleTest(&argc, argv);
29+
return RUN_ALL_TESTS();
30+
}

test/0011-test.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0011-Container-With-Most-Water/0011.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
std::vector<int> height = {1, 1};
9+
int expected = 1;
10+
ASSERT_EQ(expected, Solution().maxArea(height));
11+
}
12+
13+
GTEST_API_ int main(int argc, char **argv)
14+
{
15+
testing::InitGoogleTest(&argc, argv);
16+
return RUN_ALL_TESTS();
17+
}

test/0012-test.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0012-Integer-to-Roman/0012.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ("XII", Solution().intToRoman(12));
9+
}
10+
11+
TEST(test, test2)
12+
{
13+
ASSERT_EQ("M", Solution().intToRoman(1000));
14+
}
15+
16+
TEST(test, test3)
17+
{
18+
ASSERT_EQ("MMMCMXCIX", Solution().intToRoman(3999));
19+
}
20+
21+
GTEST_API_ int main(int argc, char **argv)
22+
{
23+
testing::InitGoogleTest(&argc, argv);
24+
return RUN_ALL_TESTS();
25+
}

test/0013-test.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0013-Roman-to-Integer/0013.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ(12, Solution().romanToInt("XII"));
9+
}
10+
11+
TEST(test, test2)
12+
{
13+
ASSERT_EQ(1000, Solution().romanToInt("M"));
14+
}
15+
16+
TEST(test, test3)
17+
{
18+
ASSERT_EQ(3999, Solution().romanToInt("MMMCMXCIX"));
19+
}
20+
21+
GTEST_API_ int main(int argc, char **argv)
22+
{
23+
testing::InitGoogleTest(&argc, argv);
24+
return RUN_ALL_TESTS();
25+
}

test/0014-test.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0014-Longest-Common-Prefix/0014.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
std::vector<std::string> strs = {"a", "b"};
9+
ASSERT_EQ("", Solution().longestCommonPrefix(strs));
10+
}
11+
12+
TEST(test, test2)
13+
{
14+
std::vector<std::string> strs = {"leetcode", "lead"};
15+
ASSERT_EQ("le", Solution().longestCommonPrefix(strs));
16+
}
17+
18+
TEST(test, test3)
19+
{
20+
std::vector<std::string> strs = {"flower", "flow", "flight"};
21+
ASSERT_EQ("fl", Solution().longestCommonPrefix(strs));
22+
}
23+
24+
TEST(test, test4)
25+
{
26+
std::vector<std::string> strs = {};
27+
ASSERT_EQ("", Solution().longestCommonPrefix(strs));
28+
}
29+
30+
TEST(test, test5)
31+
{
32+
std::vector<std::string> strs = {"a"};
33+
ASSERT_EQ("a", Solution().longestCommonPrefix(strs));
34+
}
35+
36+
TEST(test, test6)
37+
{
38+
std::vector<std::string> strs = {"c", "c"};
39+
ASSERT_EQ("c", Solution().longestCommonPrefix(strs));
40+
}
41+
42+
GTEST_API_ int main(int argc, char **argv)
43+
{
44+
testing::InitGoogleTest(&argc, argv);
45+
return RUN_ALL_TESTS();
46+
}

test/0015-test.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0015-3Sum/0015.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
vector<int> nums = {-1, 0, 1, 2, -1, -4};
9+
vector<vector<int>> expected = {{-1, -1, 2}, {-1, 0, 1}};
10+
ASSERT_EQ(expected, Solution().threeSum(nums));
11+
}
12+
13+
GTEST_API_ int main(int argc, char **argv)
14+
{
15+
testing::InitGoogleTest(&argc, argv);
16+
return RUN_ALL_TESTS();
17+
}

test/0016-test.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0016-3Sum-Closest/0016.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
vector<int> nums = {-1, 2, 1, -4};
9+
ASSERT_EQ(2, Solution().threeSumClosest(nums, 1));
10+
}
11+
12+
GTEST_API_ int main(int argc, char **argv)
13+
{
14+
testing::InitGoogleTest(&argc, argv);
15+
return RUN_ALL_TESTS();
16+
}

test/0019-test.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0019-Remove-Nth-Node-From-End-of-List/0019.cpp"
5+
6+
#include "../util/ListNode.h"
7+
#include "LinkedListUtils.cpp"
8+
9+
TEST(test, test1)
10+
{
11+
LinkedListUtils linkedListUtils;
12+
ListNode *head = linkedListUtils.contructLinkedList({1, 2, 3, 4, 5});
13+
ListNode *expected = linkedListUtils.contructLinkedList({1, 2, 3, 5});
14+
ASSERT_EQ(*expected, *Solution().removeNthFromEnd(head, 2));
15+
}
16+
17+
TEST(test, test2)
18+
{
19+
LinkedListUtils linkedListUtils;
20+
ListNode *head = linkedListUtils.contructLinkedList({11});
21+
ListNode *expected = linkedListUtils.contructLinkedList({});
22+
ASSERT_EQ(*expected, *Solution().removeNthFromEnd(head, 1));
23+
}
24+
25+
GTEST_API_ int main(int argc, char **argv)
26+
{
27+
testing::InitGoogleTest(&argc, argv);
28+
return RUN_ALL_TESTS();
29+
}

test/0020-test.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include "../src/0020-Valid-Parentheses/0020.cpp"
5+
6+
TEST(test, test1)
7+
{
8+
ASSERT_EQ(false, Solution().isValid("(]"));
9+
}
10+
11+
TEST(test, test2)
12+
{
13+
ASSERT_EQ(false, Solution().isValid("([)]"));
14+
}
15+
16+
TEST(test, test3)
17+
{
18+
ASSERT_EQ(true, Solution().isValid("()[]{}"));
19+
}
20+
21+
TEST(test, test4)
22+
{
23+
ASSERT_EQ(true, Solution().isValid("()"));
24+
}
25+
26+
GTEST_API_ int main(int argc, char **argv)
27+
{
28+
testing::InitGoogleTest(&argc, argv);
29+
return RUN_ALL_TESTS();
30+
}

0 commit comments

Comments
 (0)