Is there any way to test code without manually entering numbers?
I was writing code which requires 10^ 6 inputs to be entered to be tested. But, I don’t see any way to enter 10^6 inputs one by one each randomly by hand and check the solution. So, what’s the best way to do it?
Also is there any trick to debug codes in C++? Like they have in Python, another console window which shows the values in each variable in realtime and stuff. Also, breakpoints to stop the program if it reaches a particular line of code? Do we have all those in C++?
On Linux, it’s as simple as generating your test input and re-directing it to a file, and then cat’ing that file into your program.
For example, see my original solution to CHEFINSQ here; to generate a testcase and re-direct to a file, I just compile it so that the executable name is a.out and then do:
./a.out --test > testcase.txt
Then to run that testcase with my program, I just do:
cat testcase.txt | ./a.out
Or you can cut out the “middle-man”:
./a.out --test | ./a.out
You can easily generate much larger testcases by increasing the value of N on line 162.
Yes, of course - just get an IDE that supports it (I can’t advise which to use as I rarely use a debugger with small programs, and when I do I just use gdb from the command-line).
In Windows, \bulletg++ prog.cpp, produces a file named a.exe instead of the a.out Linux counterpart \bullet instead of cating, you TYPE \bullet don’t forget to replace \ with / \bullet you don’t care about letter cases
The rest is pretty much the same.
So, here’s a very basic demo:
C:\Users\HP\Desktop\temp>TYPE *
prog.cpp
#include <iostream>
int main (void) {
int t;
std::cin >> t;
while (t--) {
int a, b;
std::cin >> a >> b;
int c = a * b;
std::cout << c << std::endl;
}
return 0;
}
testcases.txt
3
1 2
2 3
4 5
C:\Users\HP\Desktop\temp>g++ prog.cpp
C:\Users\HP\Desktop\temp>TYPE testcases.txt | .\a.exe > output.txt
C:\Users\HP\Desktop\temp>TYPE output.txt
2
6
20
Also, here’s a C++14 test case generator. I’ve explained briefly what it does in comments.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
class Var_with_Constraints {
int LOWER_BOUND;
int UPPER_BOUND;
mutable int number;
public:
Var_with_Constraints(
int LOWER_BOUND,
int UPPER_BOUND
) : LOWER_BOUND (LOWER_BOUND),
UPPER_BOUND (UPPER_BOUND) {}
int generate (void) const { return number = rand() % UPPER_BOUND + LOWER_BOUND; }
int operator-- (int) { return number--; }
friend std::ostream& operator<< (std::ostream &out, const Var_with_Constraints& obj);
};
std::ostream& operator<< (std::ostream &out, const Var_with_Constraints& obj) {
out << obj.generate();
}
int main (void) {
// setting the seed
srand(time(0));
// tesfile creation
std::ofstream testfile;
testfile.open("testfile.txt");
// generators
Var_with_Constraints T(1, 10);
Var_with_Constraints a(1, 10);
Var_with_Constraints b(1, 10);
// inserting into tesfile
testfile << T << std::endl;
while (T--)
testfile << a << " " << b << std::endl;
// bye bye
testfile.close();
return 0;
}
This will search for findmax.exe in Desktop and will enter all those commands in input.txt (multiline too) instead of manually typing in the program and wil produce a output.txt where all the output which was supposed to appear on the screen will come.