Testing Codes/Debugging help

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++?

Of course :slight_smile: What OS are you using?

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.

2 Likes

My bad luck, I use windows … Okay, can you tell me the solution of the second one?

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).

2 Likes

Re: your Edit - See above :slight_smile:

I’m sure Windows has similar functionality. In the absolute worst case, you could always install cygwin.

Perhaps a Windows user could weigh-in with their workflow?

2 Likes

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
follow these link

2 Likes

I envy you now. How the hell I didn’t know it earlier!!? It’d had definitely saved hundreds of hours of my life testing code.

I’m embarassed, I never knew it before…

Good Luck coders and continue this for other newbies to see.

Thanks again :slight_smile:

1 Like

In Windows,
\bullet g++ 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;
}

Hope this was helpful. :slightly_smiling_face:

2 Likes

@ssjgz Even better alternative is to use in windows is to use this < and >

Believe me, it took me hours of manual trial and error pain to find those.

Syntax:

C:/users/VAIO/Desktop> findmax.exe < input.txt >output.txt

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.

For example:
input.txt
10 15
20 22
22 23
0 5
1 7

will produce output.txt like : 15 22 23 5 7

:sunglasses::sunglasses::sunglasses: :wink:

2 Likes

Yeah that’s a valid method too. Actually CodeChef does it just like that. I found it here. :slightly_smiling_face:

1 Like