Skip to content

Instantly share code, notes, and snippets.

@patilarpith
Last active October 10, 2016 19:10
Show Gist options
  • Save patilarpith/0937638ad4dcb49673e2 to your computer and use it in GitHub Desktop.
Save patilarpith/0937638ad4dcb49673e2 to your computer and use it in GitHub Desktop.
// Start of HEAD
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
// End of HEAD
// Start of BODY
/**
* TestStruct::
* testcase_id [long] ID of the test-case
* testcase_input_path [String] File path to test-case input
* testcase_output_path [String] File path to test-case output generated by the problem solver
* testcase_expected_output_path [String] File path to test-case expected output to be matched with
* metadata_file_paths [ArrayList<String>] File paths to Question metadata (Extra files usually used for defining traning sets)
* submission_code_path [String] File path to submission source code
* testcase_result [boolean] Set to true if test-case output matches test-case expected output. Matching is done line by line
* testcase_signal [long] Exit code of the test-case process
* testcase_time [double] Time taken by the test-case process in seconds
* testcase_memory [long] Peak memory of the test-case process determined in bytes
* data [String] <Future use>
*
*
* ResultStruct::
* result [boolean] Assign test-case result. true determines success. false determines failure
* score [double] Assing test-case score. Normalized between 0 to 1
* message [String] Assing test-case message. This message is visible to the problem solver
**/
public class CustomChecker {
static void run_custom_checker(final TestStruct t_obj, ResultStruct r_obj) {
System.out.println("testcase_id: " + t_obj.testcase_id);
System.out.println("testcase_input_path: " + t_obj.testcase_input_path);
System.out.println("testcase_output_path: " + t_obj.testcase_output_path);
System.out.println("testcase_expected_output_path: " + t_obj.testcase_expected_output_path);
for(String path : t_obj.metadata_file_paths)
System.out.println("metadata_file_paths: " + path);
System.out.println("submission_code_path: " + t_obj.submission_code_path);
System.out.println("testcase_result: " + t_obj.testcase_result);
System.out.println("testcase_signal: " + t_obj.testcase_signal);
System.out.println("testcase_time: " + t_obj.testcase_time);
System.out.println("testcase_memory: " + t_obj.testcase_memory);
System.out.println("data: " + t_obj.data);
r_obj.result = true;
r_obj.score = 1.1d;
r_obj.message = "Success";
}
// End of BODY
// Start of TAIL
static int read_input_json(final String json_file_path, TestStruct t_obj) {
try {
JSONParser parser = new JSONParser();
JSONObject json_obj = (JSONObject)parser.parse(new FileReader(json_file_path));
// Read values
t_obj.testcase_id = (long) json_obj.get("testcase_id");
t_obj.testcase_input_path = (String) json_obj.get("input_file_path");
t_obj.testcase_output_path = (String) json_obj.get("output_file_path");
t_obj.testcase_expected_output_path = (String) json_obj.get("expected_output_file_path");
JSONArray metadata_file_path_node = (JSONArray) json_obj.get("metadata_file_paths");
if(metadata_file_path_node != null && !metadata_file_path_node.isEmpty()) {
Iterator<String> metadata_file_paths_it = metadata_file_path_node.iterator();
while (metadata_file_paths_it.hasNext())
t_obj.metadata_file_paths.add(metadata_file_paths_it.next());
}
t_obj.submission_code_path = (String) json_obj.get("submission_code_path");
t_obj.testcase_result = (boolean) json_obj.get("testcase_result");
t_obj.testcase_signal = (long) json_obj.get("testcase_signal");
t_obj.testcase_time = ((Number) json_obj.get("testcase_time")).doubleValue();
t_obj.testcase_memory = (long) json_obj.get("testcase_memory");
t_obj.data = (String) json_obj.get("data");
}
catch(Exception err) {
return 1;
}
return 0;
}
static void write_result_json(final ResultStruct r_obj) {
JSONObject json_obj = new JSONObject();
json_obj.put("custom_result", new Integer((r_obj.result)? 1 : 0));
json_obj.put("custom_score", new Double( Math.max(((r_obj.score > 1.0d)? 1.0d : r_obj.score), 0.0d)));
json_obj.put("custom_message", (r_obj.message.length() > 4096)? r_obj.message.substring(0, 4095) : r_obj.message);
System.out.println(json_obj);
}
public static void main(String args[]) {
// Input parameters
TestStruct t_obj = new TestStruct();
// Out parameters
ResultStruct r_obj = new ResultStruct();
if(args.length < 1)
{
write_result_json(r_obj);
System.exit(1);
}
// Decode input JSON
int failure = read_input_json((String)args[0], t_obj);
// Incase input JSON was malformed or not existent
if(failure != 0)
{
r_obj.message = "Unable to read input json";
write_result_json(r_obj);
System.exit(2);
}
// Run the custom checker evaluator
run_custom_checker(t_obj, r_obj);
// Encode result JSON
write_result_json(r_obj);
System.exit(0);
}
}
final class TestStruct {
long testcase_id = 0;
String testcase_input_path;
String testcase_output_path;
String testcase_expected_output_path;
ArrayList<String> metadata_file_paths = new ArrayList<String>();
String submission_code_path;
boolean testcase_result = false;
long testcase_signal = 0;
double testcase_time = 0.0f;
long testcase_memory = 0;
String data;
}
final class ResultStruct {
boolean result = false;
double score = 0.0f;
String message = "Uninitialized";
}
// End of TAIL
@patilarpith
Copy link
Author

Testing:
Input file data. Path to be passed as command line argument

{"testcase_id":0,"input_file_path":"/run-XX/input00.txt","output_file_path":"/run-XX/output00.out","expected_output_file_path":"/run-XX/output_expected00.out","metadata_file_paths":["/run-XX/training.txt","/run-XX/training2.txt"],"submission_code_path":"/run-XX/solution.cpp", "testcase_result":true,"testcase_signal":0,"testcase_time":1.2,"testcase_memory":312322,"data":"Data"}

Expected output:

testcase_id: 0
testcase_input_path: /run-XX/input00.txt
testcase_output_path: /run-XX/output00.out
testcase_expected_output_path: /run-XX/output_expected00.out
metadata_file_paths: /run-XX/training.txt
metadata_file_paths: /run-XX/training2.txt
submission_code_path: /run-XX/solution.cpp
testcase_result: true
testcase_signal: 0
testcase_time: 1.2
testcase_memory: 312322
data: Data
{"custom_message":"Success","custom_score":1.0,"custom_result":1}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment