-
-
Notifications
You must be signed in to change notification settings - Fork 38
feat: implement cpp local test #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@w43322 Hi, thank you for your hard work and contributions! Your work is truly impressive and I'm looking forward to reviewing it in detail over the weekend. |
Thank you for your kind words! I really enjoyed working on this project. I'm fairly new to golang, so there might be some imperfections in the code, and many things may be simplified, or could be done in a better way. I'm going to write some comments and documents so other developers can add support for more languages in a similar manner.
Of course it's okay, that's what open source is all about. :-) Still have some work left to do, but everything should be ready by Sunday. |
eb63e3b to
f8ea073
Compare
see j178#90 for more information
f8ea073 to
9b982ea
Compare
see j178#90 for more information
9b982ea to
cde2d5c
Compare
see j178#90 for more information
cde2d5c to
9d39558
Compare
see j178#90 for more information
9d39558 to
6a8253a
Compare
see j178#90 for more information
6a8253a to
77a108c
Compare
see j178#90 for more information
|
Regarding the generated I also want to place the compiled binary in a temporary directory similar to what |
I actually thought about this before implementing everything, but decided on the I/O approach because this approach has some serious drawbacks.
I think this is okay. Or we can delete the files after testing, which more or less yields the same result. It might become a problem if the user wants to use |
Good point, then I suggest we clarify things a bit:
|
|
I agree with this approach. There is one issue though: I think we still need a temporary file to store the serialized output, instead of using stdout directly. Because on LeetCode the stdout is independent from the judging process, and many users use stdout for debugging. So I think we should to respect this behavior and leave stdout to the user. |
|
I made some code movements and minor adjustments, hoping they won't conflict with your code :) I tested the newly generated code with some abnormal code, here are two strage cases I don't know why: Case 1: no return statement, it will hang forever click to expand#include <bits/stdc++.h>
#include "LC_IO.h"
using namespace std;
// @lc code=begin
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};
// @lc code=end
int main() {
ios_base::sync_with_stdio(false);
stringstream out_stream;
vector<int> nums; cin >> nums;
int target; cin >> target;
Solution *obj = new Solution();
auto &&res = obj->twoSum(nums, target);
out_stream << res;
cout << "output: " << out_stream.rdbuf();
delete obj;
return 0;
}Case 2: return an empty list, output contains a single click to expand#include <bits/stdc++.h>
#include "LC_IO.h"
using namespace std;
// @lc code=begin
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
return {};
}
};
// @lc code=end
int main() {
ios_base::sync_with_stdio(false);
stringstream out_stream;
vector<int> nums; cin >> nums;
int target; cin >> target;
Solution *obj = new Solution();
auto &&res = obj->twoSum(nums, target);
out_stream << res;
cout << "output: " << out_stream.rdbuf();
delete obj;
return 0;
} |
This is to be expected since it's undefined behavior. The compiler should issue a warning. However on my machine it compiles and says runtime error instead of hanging. (x86-64, linux, gcc12)
This is due to a bug in my I/O library implementation, which I will fix just in a minute. Basically I output a list and overwrite the last character (expected to be ',') with ']'. When the list is empty, it just overwrites the starting bracket. |
|
If it hangs during the compilation phase we probably should add a timer to the compilation process. |
|
Interesting, for case 1, It compiles with a warning on my machine (x86-64, Windows 11, gcc 11.2.0): but when it runs, it will hang. Adding some printf reveals that it hangs on this line: auto &&res = obj->twoSum(nums, target);whatever, since it's an UB, I thinks it's ok to ignore it. |
add serialization of boolean value TODO: add serialization of char value add scan / print code for boolean / char value in cpp.go
|
Everything should be functional now. :) |
|
Thank you very much. I will play with it for some time before merging it. However, since I have absolutely no knowledge of C++, I may need another C++ pro to review it. I will give you feedback as soon as possible. |
|
Thanks, I might start working on C when I have more free time. It's going to be a lot harder due to the lack of templates and dynamically allocated containers though. |
|
Wouldn't it be a bit difficult to use C to solve algorithmic problems? I'm not sure how many people have this need, and I don't know if it's worth the effort to support C. But it's okay, if you want to do it, just do it. I will try my best to provide help. |
|
Yes, I doubt that many people will benefit from this. I used to use C on leetcode when I don't need STL or strings, but later switched to all C++ because it's too much hassle with C as soon as you step into nd arrays or strings. Everything needs to be manually allocated. 🤣 |
|
haha, I know that pain. |
|
merged, thanks @w43322 ! 🎉 |
Info
This pull request adds preliminary cpp local test support. #11
Tested on linux / gcc 12.2.1
How it works
After
pickcommandIt creates new folders for each new question. The code that you write is in an automatically created file
solution.h.It automatically generates a file
solution.cpp, which contains the testing code, and includessolution.h.After
testcommand with-LparameterIt executes command
g++ -O2 -std=c++17 solution.cpp -o solution.execThen it writes the test case into
input.txt, and executes commandsolution.exec input.txt output.txtAfter the testing program finishes, it checks the content of output.txt against expected output, and shows the result.
If multiple test cases exist, the previously mentioned process is repeated.
Note:
solution.exectakes exactly two arguments, which are paths for input and output files. It will exit prematurely if an incorrect number of arguments are provided, or if one of the file fails to open.Prerequisites
Completion Status
Implement I/O for basic data types
Implement I/O for LC DS (Linked List, Binary Tree)
Implement Local Testing
Implement Support for System Design Problems
Execution time output
Check for CE, RE, TLE,
MLEand automatically stop the program if Time/ Memoryexceeds a certain valueExample of a generated testing code
Click to Expand
Future plans for improvement
Tested problems
Click to Expand