Simple Online Judge Program: Run C/C++ Code with Input Test Case Data Set

We are going to make a simple Online judge program

a problem in any online judge has huge data set. Usually these are stored in input files. There are also corresponding output files. When you submit your program in online judge site, then your program are executed against the input files and produces, your program generated output files. Now there are two sets of output files. One is the correct output file set and other is the output file set produced by your program. Then the output files are compared and if they match exactly then your program is correct otherwise wrong.

Guess you have the input data set files. For example, there are 10 input files. Each one contains one test case. Now very likely you have to run your code 10 times and with each time you need to change the input file name and run the code, then match the two set of output files manually, this is troublesome.

Once i was trying to solve the CROATIAN OPEN COMPETITION IN INFORMATICS past contest problems. You will find the problem set along with judgeData and sample solutions.

I try to solve the problem first and run my code against data set and if my code get accepted then i see the solution for a better idea.
As you can see they do not have any robot online judge, so i had to manually check the input data set files

finally i made a simple online judge program, which do the troublesome task for me

I just need to paste my code and change the input file name and bang! the rest of the task is gonna done by the simple online judge program.

this code is applicable for Contest 1 Task name: Modulo

you have 10 input files named:

and 10 output files named:

my generated output files will be named:

How to run:
paste the input files and output files in folder named test_data/ inside your execution directory

#37: change the name with the first part of the file name, in this case modulo
#38: enter the number of files
#47: use the function callOut() as if it is main() function

now run this program


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <unistd.h>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

#define FILENAME "modulo"   // input data file name
#define CASES 10            // number of input files 

using namespace std;

    This is the code function
    Use this function as if it is the "main()" function  

void callOut()
    int ret = 0;
    bool vis [42];
    int num;

    Set (vis, false);

    for ( int i = 0; i < 10; i++ ) {
        scanf ("%d", &num);

        vis [num % 42] = true;

    for ( int i = 0; i < 42; i++ ) {
        if ( vis [i] ) {

	printf ("%d\n", ret);

bool startCase(int caseNum)
    string pathName = "test_data/";
    pathName += FILENAME;

    string inStream = ".in.";
    string outStream = ".out.";

    string caseNumber = static_cast<ostringstream*>( &(ostringstream() << caseNum) )->str();

    string inputPath = pathName + ".in." + caseNumber;
    string outputPath = pathName + ".out." + caseNumber;
    string thisOutputPath = pathName + ".thisOut." + caseNumber;

    //cout << "Input: " << inputPath << endl;
    //cout << "Output: " << outputPath << endl;
    //cout << "thisOutput: " << thisOutputPath << endl;

    int    fd;
    fpos_t pos;

    fgetpos(stdout, &pos);
    fd = dup(fileno(stdout));

    freopen (inputPath.c_str(), "r", stdin);
    freopen (thisOutputPath.c_str(), "w", stdout);



    dup2(fd, fileno(stdout));
    fsetpos(stdout, &pos);

	char bufferIn [N];
	char bufferOut [N];

	FILE *inFile = fopen (outputPath.c_str() , "r");
	FILE *outFile = fopen (thisOutputPath.c_str(), "r");

	do {
        size_t fpIn = fread (bufferIn, 1, N, inFile);
        size_t fpOut = fread (bufferOut, 1, N, outFile);

        if (fpIn != fpOut || memcmp (bufferIn, bufferOut, fpIn)) {
            return false;
	} while (!feof (inFile) && !feof(outFile));

	return feof(inFile) && feof(outFile);

int main ()
    for ( int i = 1; i <= CASES; i++ ) {
        printf ("Case %d: ", i);
        if (startCase(i)) {
            printf ("*Yes*\n");
        } else {
            printf ("Wrong ans!\n");

	return 0;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s