A CLI runner to test your solutions


#1

Hi coders.

I have a lot of fun with CodingGame, but sometimes it’s hard to figure out why your solution can’t be validated. So I made cg-runner, a simple command line tool, able to run a given solution against various test cases.

You need to provide input and output files, and copy-past your solution from CodingGame IDE into a file, then run the tool !

Basically, it spawns an external executable with your solution as source file, and for each test case found, sends the .in file content to standard input, and awaits on the standard output for stuff specified in the .out file.

If it differs, cg-runner will fail, and displays output and error streams to help you understand why (It should reminds you something).

Obviously, you won’t be able to simulate complex challenges that includes bots, because a static input file is not really adapted for that. But it’s perfect for optimization challenges, and you can use it with IDE to make step-by-step debugging.

You can get it from NPM (node.js package manager), and the code is on github.

At the moment, only Python and JavaScript language are supported, but it should be easy to add more if someone needs it. Just open an issue on github and ask for it !

I’ve just spent few hours on it, the code is not 100% documented nor unit-tested, so be indulgent.
— Merry Xmas !


#2

I have an arena for my multiplayers codes. I have also a cli runner for solo puzzles. To handle every languages, my arenas use the compiled code directly and take the command line to run it. For example i run my multiplayers arena for tron like this :

cg-arena -e tron -p1 "node tron.js" -p2 "tron.exe" -p3 "php tron.php"

#3

Great !
I’ll be curious to have more information on how you achieve running compile code. Especially for interpreted langages, like Php and JS in your example.

How do you manage to simulate a puzzles input in a reactive way ?


#4

For interpreted languages there’s no compilation so i just take the whole command line to run the code like in my example.

php tron.php will run some php code and node tron.js will run some javascript code.

It’s pretty easy in Java with the Process class. You can read/write the output/input of each spawned processes by the given command lines. I just have to code the arena engine. At the moment i only have an engine for Tron. I can simulate 1000 games in ten minutes by using all my cores.

But coding an engine is not that hard. Since the next contest is a multiplayer game, i think the first thing i will do is to code the engine for this new contest to simulation a maximum of games localy between my differents codes.


#5

Pretty smart indeed.
Just be warned that in JS, the readline(), print() and printErr() functions used inside CodingGame are specifics to SpiderMonkey and node supported by Node.js.

So you won’t be able to copy-paste from CodinGame IDE.
In the same way, SpiderMonkey and Node have different supports of EcmaScript 2015 functionalities.

You should download a SpiderMonkey build instead, and use the jsshell command.


#6

I have a NodeJS wrapper code to add this 3 functions.

But for multiplayers arena i don’t really use javascript anyway. Performances are too importants for that puzzles.


#7

Is there any way to batch download all the test cases (input and output) automatically, instead of copy pasting one by one?
Would be very useful for puzzles that can be solved locally like “Music Scores”!


#8

I made a greasemonkey script (+ a daemon) to create netbeans project with test cases. I don’t know javascript so I did what I could. I modified it a little bit for your purpose. You can found it here

This script add a “Download test” button on top right (next to language selection) which create a json file with puzzle title, test cases (name, input + output), description and code template.

Original script come from here


Downloading test inputs. This used to be possible
Codingame Offline
#9

The script does not seems to work now unless you reload the puzzle page with debug info.

if (typeof angular.element(document.body).scope() == 'undefined') {
    angular.reloadWithDebugInfo();
}

Put this check before any other code inside the main function.Line 8 in the original script should be a perfect place.
Mind that this will cause reload of any visited page on codingame.com. To avoid that, you can be more specific on line 3. For example match only pages like this: ://www.codingame.com/ide/puzzle/.