Skip to content

Commit

Permalink
a-plus-b updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyanxing committed Feb 22, 2015
1 parent 9f0eba3 commit 1a6c536
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 182 deletions.
2 changes: 2 additions & 0 deletions app/problems/a-plus-b.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"code_ruby": "def plus_num(a, b)\nend",
"code_python": "def plus_num(a, b):",
"code_lua": "function plus_num(a, b)\nend",
"code_scala": "object Solution {\n\tdef plus_num(a: Int, b: Int): Int = {\n\t}\n}",
"in_type_cpp": [
"int",
"int"
Expand All @@ -29,5 +30,6 @@
"judge_type_ruby": "equal",
"judge_type_python": "equal",
"judge_type_lua": "equal",
"judge_type_scala": "equal",
"judge_call": "plus_num(@)"
}
9 changes: 6 additions & 3 deletions app/script/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ var modes = {
"Java" : "ace/mode/java",
"Ruby" : "ace/mode/ruby",
"Python" : "ace/mode/python",
"Lua" : "ace/mode/lua"
"Lua" : "ace/mode/lua",
"Scala" : "ace/mode/scala"
};

var codes = {
"C++" : "code_cpp",
"Java" : "code_java",
"Ruby" : "code_ruby",
"Python" : "code_python",
"Lua" : "code_lua"
"Lua" : "code_lua",
"Scala" : "code_scala"
};

var langs = [
'C++',
'Java',
'Ruby',
'Python',
'Lua'
'Lua',
'Scala'
];

var fgdsbControllers = angular.module('fgdsbControllers', ['ui.bootstrap']);
Expand Down
102 changes: 99 additions & 3 deletions app/script/judge.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ var generate_java = function(problem) {
return out_file;
};

var generate_scala = function(problem) {
var test_class = problem['id'].replace(new RegExp('-', 'g'), '_');

var out_file = "package judge;\n";
out_file += "import tests." + test_class + ";\n\n";

out_file += "object src {\n";

out_file += " def main(args: Array[String]) {\n";
out_file += " " + test_class + ".judge();\n";
out_file += " }\n"

out_file += "}\n";
return out_file;
};

var generate_ruby = function(problem) {
var out_file = "require './judge/ruby/common'\n";
out_file += "require './judge/ruby/solution'\n";
Expand Down Expand Up @@ -123,7 +139,7 @@ var judge_cpp = function($scope, callback, msg) {
);
});
});
}
};

var judge_java = function($scope, callback, msg) {
var java_out = generate_java($scope.problem);
Expand Down Expand Up @@ -203,7 +219,7 @@ var judge_java = function($scope, callback, msg) {
);
});
});
}
};

var judge_ruby = function($scope, callback, msg) {
var ruby_out = generate_ruby($scope.problem).
Expand Down Expand Up @@ -376,4 +392,84 @@ var judge_lua = function($scope, callback, msg) {
);
});
});
}
};

var judge_scala = function($scope, callback, msg) {
var scala_out = generate_scala($scope.problem);

var class_name = $scope.problem['id'].replace(new RegExp('-', 'g'), '_');

var solution_file = $scope.problem['class_name_java'];
if (!solution_file) solution_file = 'Solution';
solution_file += '.scala';

var solution = "package judge; " + $scope.$editor.getValue();

// write source
fs.writeFile('judge/scala/' + solution_file, solution, function (err) {
if (err) {
ret = {"result": "Internal Error", "details": err};
callback(ret);
return;
}

fs.writeFile('judge/scala/src.scala', scala_out, function (err) {
if (err) {
ret = {"result": "Internal Error", "details": err};
callback(ret);
return;
}

// compile
var test_file_name = class_name + '.scala';
msg('Compiling');
var cmd = 'scalac -cp judge/scala -d judge/scala/bin judge/scala/common.scala judge/scala/' + solution_file + ' judge/scala/src.scala ';
cmd += "judge/scala/tests/" + test_file_name;
exec(cmd,
function (error, stdout, stderr) {
if (stderr != undefined && stderr != "") {
// Compile error
var errors = stderr.replace(new RegExp('judge/scala/src.scala:', 'g'), '');
var ret = {"result": "Compile Error", "details": errors};
callback(ret);
return;
}

if (error !== null) {
// Internal error
var ret = {"result": "Internal Error", "details": error};
callback(ret);
return;
}

msg('Judging');
// execute and judge
exec('scala -cp judge/scala/bin judge.src',
function (error, stdout, stderr) {
if (error !== null) {
var ret = {"result": "Runtime Error", "details": error};
callback(ret);
return;
}
var results = stdout.trim();
if (beginsWith('Accepted', results)) {
var ret = {"result": "Accepted", "runtime": results.split(";")[1]};
callback(ret);
} else {
var res = results.split(";");
var ret = {
"result": "Wrong Answer",
"tests": res[0],
"input": res[1],
"output": res[2],
"expected": res[3]
};
callback(ret);
}
}
);
}
);
});
});
};
149 changes: 149 additions & 0 deletions judge/build_tests/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(name)
gen_ruby_test
gen_python_test
gen_lua_test
gen_scala_test
end
end

Expand Down Expand Up @@ -824,6 +825,154 @@ def gen_lua_test
file.close
end

def gen_scala_test
scala_funcs = {
'bool' => 'common.read_bool_array',
'int' => 'common.read_int_array',
'double' => 'common.read_double_array',
'char' => 'common.read_char_array',
'string' => 'common.read_string_array',
'Interval' => 'common.read_interval_array',
'Point' => 'common.read_point_array',
'TreeNode*' => 'common.read_tree_array',
'TreeNodeWithParent*' => 'common.read_tree_with_p_array',
'vector<bool>' => 'common.read_bool_matrix',
'vector<int>' => 'common.read_int_matrix',
'vector<char>' => 'common.read_char_matrix',
'vector<double>' => 'common.read_double_matrix',
'vector<string>' => 'common.read_string_matrix',
'vector<Interval>' => 'common.read_interval_matrix',
'vector<Point>' => 'common.read_point_matrix',
'vector<TreeNode*>' => 'common.read_tree_matrix',
'vector<TreeNodeWithParent*>' => 'common.read_tree_with_p_matrix',
'vector<vector<bool>>' => 'common.read_bool_matrix_arr',
'vector<vector<char>>' => 'common.read_char_matrix_arr',
'vector<vector<int>>' => 'common.read_int_matrix_arr',
'vector<vector<double>>' => 'common.read_double_matrix_arr',
'vector<vector<string>>' => 'common.read_string_matrix_arr'
}

type_map = {
'int' => 'Int',
'double' => 'Double',
'char' => 'Char',
'boolean' => 'Bool'
}

class_name = @name.gsub(/-/, '_')
file = File.open("../scala/tests/#{class_name}.scala", 'w')

file.puts 'package test'
file.puts 'import scala.io.Source'
file.puts 'import judge.common'
file.puts 'import judge.Solution'
file.puts

file.puts "object #{class_name} {"
file.puts indent(1) + "val num_test = #{@test_in[0].length};"

@problem['in_type_java'].each_with_index do |in_type, i|
type = type_map.has_key?(in_type) ? type_map[in_type] : in_type
file.puts indent(1) + "var in_#{i} = List[#{type}]();"
end
out_type = @problem['out_type_java']
if type_map.has_key? out_type
out_type = type_map[out_type]
end
file.print indent(1) + "var out = List[#{out_type}]();";

file.puts
file.puts @extra_test_code_java
file.puts

file.puts ' def load_test() = {'
file.puts " val in = Source.fromFile(\"judge/tests/a-plus-b.txt\").getLines;"

@problem['in_type_cpp'].each_with_index do |in_type, i|
file.puts " in_#{i} = #{scala_funcs[in_type]}(in);"
end
file.puts " out = #{scala_funcs[@problem['out_type_cpp']]}(in);"
file.puts ' }'

solution_class = @problem['class_name_java']
solution_class = 'Solution' if solution_class.nil?

file.puts
file.puts ' def judge(): Int = {'
file.puts ' load_test();'
file.puts ' common.capture_stdout();'

file.puts
file.puts ' val startTime = System.currentTimeMillis();'
file.puts ' var i = 0;'
file.puts
file.puts ' while(i < num_test) {'
file.puts ' printf("Testing case #%d\n", i+1);'

caller = @problem['judge_call_java']
caller = 'Solution.' + @problem['judge_call'] if caller.nil?
judge_call = ' '
judge_inputs = ''
if(@problem['ret_type_java'] != 'void')
judge_call += " val answer = #{caller}"
else
judge_call += caller
end
judge_call += ';'

@problem['in_type_java'].each_with_index do |in_type, i|
judge_inputs += ', ' if i != 0
judge_inputs += "in_#{i}(i)"
end

file.puts judge_call.gsub(/@/, judge_inputs)

if @problem['ret_type_java'] == 'void'
file.puts ' val answer = in_0(i);'
end

if @problem['judge_type_java'] == 'equal'
file.puts ' if(answer != out(i)) {'
else
file.puts " if(#{@problem['judge_type_java']}) {"
end

file.puts ' common.release_stdout();'
file.puts ' printf("%d / %d;", i+1, num_test);'

file.print ' var outs = '
@problem['in_type_java'].each_with_index do |in_type, i|
file.print ' + ", " + ' if i != 0
file.print "#{class_name}.in_#{i}(i).toString"
end
file.puts ';'
file.puts ' print(outs + ";");'

vis_answer = @problem['vis_answer_java']
vis_answer = 'answer.toString' if vis_answer.nil?

vis_out = @problem['vis_out_java']
vis_out = 'out(i).toString' if vis_out.nil?

file.puts " print(#{vis_answer} + \";\");"
file.puts " println(#{vis_out});"
file.puts ' return 1;'
file.puts ' }'
file.puts ' i += 1;'
file.puts ' }'
file.puts
file.puts ' common.release_stdout();'
file.puts ' val estimatedTime = System.currentTimeMillis - startTime;'
file.puts ' print("Accepted;");'
file.puts ' println(estimatedTime);'
file.puts ' return 0;'
file.puts ' }'

file.puts '}'

file.close
end

def gen_tests
end

Expand Down
Loading

0 comments on commit 1a6c536

Please sign in to comment.