leetcode_unit is a lightweight and offline tool to generate solutions and test cases for the problems in Leetcode. leetcode_unit has the following features:
- Automatic generate template files for a new leetcode problem
- Add customized unit tests for each problem
- Compile and run any solution with one command.
- Debug the code with gdb with one command.
It has been tested on Ubuntu 20.04 and Windows Subsystem for Linux (WSL) v2.
leetcode_unit has been tested based on following versions:
- python3 (>=3.8.10)
- make (>=4.2.1) and g++(>=8.3.0)
- cppunit (>=1.14.0)
- gdb (>=9.2)
you can install them using apt
sudo apt install make g++ libcppunit-dev
On WSL v2, you also must install Ubuntu 20.04 version. Besides the above tools and libraries, it is better to install vscode.
Ideally, leetcode_unit can be used at other Linux platforms.
git clone https://github.com/OctHe/leetcode_unit
cd leetcode_unit
To begin with leetcode_unit, please read the usage with ./leetcode_unit --help or ./leetcode_unit -h
The following content uses the Problem 1 as an example.
The program is a python program named as leetcode_unit.py. To add a new problem, you can directly run the following command
./leetcode_unit add -i 1 -n Two_Sum
where -i is the index of the problem and -n is its name.
After running the command, a new directory will be created in the ./problems/ directory like this:
.
├── problems
│ ├── 1_Two_Sum
│ │ ├── 1_Two_Sum.h
│ │ └── Test_Cases_1.cc
Until now, you can add your own solution to this new problem.
1_Two_Sum.h contains Solution class. Open it with a editor, you need to replace the following contents:
<return_type> <function>
{
}
by your solution, such as:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> sum;
int result;
for(int i = 0; i < nums.size(); i++)
{
result = target - nums[i];
for(int j = i+1; j < nums.size(); j++)
{
if(result == nums[j])
{
sum.push_back(i);
sum.push_back(j);
}
}
}
return sum;
}
leetcode_unit provides a command to run the solution.
./leetcode_unit.py run -i 1
This command will automatically compile (for C and C++) and run the program of Problem 1.
If the program has no error, you can see the output
OK (1 tests)
Note that the results will be always OK because test cases have not been added.
Test cases for Problem 1 can be saved in file Test_Cases_1.cc like this
void test_problem_1::test_case_1()
{
// A simple assert: CPPUNIT_ASSERT
}
The following code can be added in the function
void test_problem_1::test_case_1()
{
vector<int> input = {2, 7, 11, 15};
int target = 9;
vector<int> output = {0, 1};
CPPUNIT_ASSERT(s->twoSum(input, target) == output);
}
Compile and run it with
./leetcode_unit.py run -i 1
- ``open'' sub-command (work with gvim)
- Time and space complexity of each problem
- python style (Google python style)
- problem info in template
- Helper data structures for unit tests of list and binary tree