-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcommon.rb
More file actions
124 lines (105 loc) · 3.04 KB
/
common.rb
File metadata and controls
124 lines (105 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
$sample_count = 0
$test_count = 0
$targets = {}
file 'Rakefile'
directory 'aux'
directory 'samples'
directory 'tests'
def find(symbol)
Dir["#{symbol}.{cpp,java,py}"].first.split('.')
end
def command(symbol)
b, e = find(symbol)
case e
when 'cpp'
"./#{b}.exe"
when 'java'
"java #{b}"
when 'py'
"python #{b}.py"
end
end
def make_target(symbol)
b, e = find(symbol)
case e
when 'cpp'
target = "#{b}.exe"
file target => "#{b}.cpp" do
sh "c++ -I../.. ${CXXFLAGS} #{b}.cpp -o#{b}.exe"
end
when 'java'
target = "#{b}.class"
file target => "#{b}.java" do
sh "javac #{b}.java"
end
when 'py'
target = "#{b}.py"
file target
end
target
end
def target(symbol)
$targets[symbol] ||= make_target(symbol)
end
def generate(generator, test_input, test_output, *params, **options)
common_deps = %w(Rakefile aux samples tests)
if generator.is_a? String
file test_input => [*common_deps, generator] do
cp generator, test_input
end
else
file test_input => [*common_deps, target(generator)] do
sh "#{command generator} #{params.join(' ')} #{options[:seed] || test_input} > #{test_input}"
end
end
file test_output => [*common_deps, test_input, target(:check), target(:solution), target(:validator)] do
sh "#{command :validator} < #{test_input}"
sh "#{command :solution} < #{test_input} > #{test_output}"
sh "#{command :check} #{test_input} #{test_output} #{test_output}"
end
end
def sample(generator, *params, **options)
$sample_count += 1
test_input = 'samples/%03d' % [$sample_count]
test_output = 'samples/%03d.a' % [$sample_count]
generate generator, test_input, test_output, *params, **options
task :default => test_output
end
def test(generator, *params, **options)
$test_count += 1
test_input = 'tests/%03d' % [$test_count]
test_output = 'tests/%03d.a' % [$test_count]
generate generator, test_input, test_output, *params, **options
task :default => test_output
end
def multitest(*tests)
$test_count += 1
aux_inputs, aux_outputs = [], []
tests.each_with_index do |test, test_id|
generator, *params = test
aux_input = 'aux/%03d.%03d' % [$test_count, test_id]
aux_output = 'aux/%03d.%03d.a' % [$test_count, test_id]
generate generator, aux_input, aux_output, *params
aux_inputs << aux_input
aux_outputs << aux_output
end
test_input = 'tests/%03d' % [$test_count]
test_output = 'tests/%03d.a' % [$test_count]
file test_input => aux_inputs do
sh "cat #{aux_inputs.join(' ')} > #{test_input}"
end
file test_output => [test_input, *aux_outputs] do
sh "#{command :validator} < #{test_input}"
sh "#{command :solution} < #{test_input} > #{test_output}"
sh "bash -c '#{command :check} #{test_input} #{test_output} <(cat #{aux_outputs.join(' ')})'"
end
task :default => test_output
end
task :clean do
sh 'rm -rf *.a *.class *.exe input output answer out ans aux samples tests'
if /darwin/ =~ RUBY_PLATFORM
sh 'find . -perm +u+x -type f -delete'
else
sh 'find . -executable -delete'
end
end