This is a project for solving Leetcode problems with C++ and verifying multiple solutions with unit tests (GoogleTest). All solved questions are listed at include/questions/SOLVED.md.
./setup_env.sh
./config.sh
First, create a question by running command below, then the corresponding template of interfaces and implementations will be created and placed to proper folders:
./create_question.sh "Remove Duplicates from Sorted Array"
Second, declare pure virtual function according to question in include/questions/[questionname]/[QuestionName].hpp
, redefine/override that pure virtual function in include/questions/[question name]/Solution.hpp
and implement the function in src/[questionname]/Solution1.cpp
. For example:
// include/questions/mergeintervals/MergeIntervals.hpp
...
class MergeIntervals : public Question
{
public:
virtual vector<vector<int>> merge(vector<vector<int>>& intervals) = 0;
};
...
// include/questions/mergeintervals/Solution.hpp
...
class Solution1 : MergeIntervals
{
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) override;
};
...
// src/mergeintervals/Solution.cpp
...
vector<vector<int>> Solution1::merge(vector<vector<int>>& intervals) {
}
...
Then, you can start to write your own unit tests in tests/[questionname]/[questionName]Test.cpp
. For example, tests/mergeintervals/MergeIntervalsTest.cpp
.
./build.sh
./clean.sh
Notice: Always build before testing
./test.sh
cd /build
make test MergeTwoSortedListsTest
cd build/tests
./MergeTwoSortedListsTest --gtest_list_tests
cd build/tests
./MergeTwoSortedListsTest --gtest_filter=MergeTwoSortedListsTest/MergeTwoSortedListsTestBase/Solution1.test11
cd build/tests
./MergeTwoSortedListsTest --gtest_filter=MergeTwoSortedListsTest/MergeTwoSortedListsTestBase/Solution1.test1
Log locates at build/Testing/Temporary/LastTest.log