Defined ONLINE_JUDGE for testing and real Judge runs

Updated with simpler test code …
It would be most helpful to have a #defined flag that would indicate to the code being run whether the program is being run for testing (in the IDE) or for a real Online Judge run (via SUBMIT). I looked it up and found this CodeChef article

By having this, the code could have testing and performance measurements in it that would be taken out once the code is submitted for the Online Judge for the real test.

I have added this to my code to test the usage:

    int main() {
        #ifndef ONLINE_JUDGE
        Debug (printf("ONLINE_JUDGE not defined\n");)
        #else
        Debug (printf("ONLINE_JUDGE is defined\n");)
        #endif
/* ... rest of the code here ... */
    	return 0;
    }

When I run this in the IDE (CodeChef IDE) it shows the output with “ONLINE_JUDGE is defined” as the first output line (which it should not).

So, am I doing it incorrectly?

Or it is also being set in the IDE (which would be wrong behavior)?

Or what?

Thanks for the help.

1 Like

Most of C++ online-compiler will add this flag at the end of the complie-command: -DONLINE_JUDGE

Example: With C++11, Your code will be compile with this flag:

-std=c++0x -O2 -static -s -lm -DONLINE_JUDGE

So if you use

    #ifndef ONLINE_JUDGE
bool debug = false;
#else
bool debug = true;
#endif

“debug” variable will be true (because of flag above), so it will print “Debug Mode” :slight_smile:

I don’t know which code you used like that gave a correct answer, but when I set a problem on SPOJ or Codeforces, it automatically add that flag to complier command :slight_smile:

p.s: I found COCI also used a flag like that.

Admins are not reading the forum regularly, the best (and fastest) will be to write an e-mail to them if it is not working :wink:

I have updated the question to make it simpler. The IDE seems to have the “-DONLINE_JUDGE” which it should not.

Thanks for your info.

I sent an email to [email protected] … thanks.

I think “DONLINE_JUDGE” means “DON’T ONLINE_JUDGE”, so normally it outputed “ONLINE_JUDGE is defined” with else function :slight_smile:

-DONLINE_JUDGE is a compiler directive to add the defined word to the compile. It is the same as having a line of code that says “#define ONLINE_JUDGE” in your program file.

EDIT: This is done now!

The software behind the IDE and the actual SPOJ judge is the same. There may be some differences in the version of the languages but rest all is same. That is why you see that variable defined at both the places.

We understand that this needed. Right now I am not sure, if this could be done or how much time it will take us to change this, as this is controlled by the SPOJ team. We will ask them and get back to you.

Thank you.

@xyzzy529: This is done now. Please check and let us know if it works the way you want it to.

This is expected. Ideally while using local testing, you should write #ifndef ONLINE_JUDGE because you are sure that ONLINE_JUDGE is not define in your local machine, but it is define in codechef.