Skip to content

Commit 46a3c13

Browse files
committed
Fix default scoring
1 parent 9ac782b commit 46a3c13

File tree

7 files changed

+111
-11
lines changed

7 files changed

+111
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.o
22
*.swp
3+
*.swo
34
build/
4-
.vscode/
5+
.vscode/

src/server_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55
#include "tioj/submission.h"
66

7-
const std::string kVersionCode = "2.0.1";
7+
const std::string kVersionCode = "2.0.2";
88
extern std::string kTIOJUrl;
99
extern std::string kTIOJKey;
1010
extern size_t kMaxQueue;

test/default_scoring_test.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <array>
2+
#include <gtest/gtest-matchers.h>
3+
#include "example_problem.h"
4+
#include "utils.h"
5+
6+
namespace {
7+
8+
constexpr long kTime = 1655000000;
9+
10+
struct ScoringParam {
11+
std::vector<std::string> default_scoring_args;
12+
std::array<Verdict, 10> verdicts;
13+
};
14+
15+
std::string ParamName(const ::testing::TestParamInfo<ScoringParam>& info) {
16+
std::string ret;
17+
for (auto& i : info.param.default_scoring_args) ret += i + '_';
18+
for (auto& i : ret) {
19+
if (i == '-') i = '_';
20+
}
21+
if (ret.empty()) {
22+
ret = "empty";
23+
} else {
24+
ret.pop_back();
25+
}
26+
return ret;
27+
}
28+
29+
}
30+
31+
class ExampleProblemDefaultScoring : public ExampleProblem, public testing::WithParamInterface<ScoringParam> {};
32+
TEST_P(ExampleProblemDefaultScoring, Mode) {
33+
auto& param = GetParam();
34+
SetUp(2, {
35+
{"123\n234\n", "123\n234\n"}, // match: AC for all
36+
{"123\n234", "123 \n234 \n\n"}, // trailing whitespaces / blank lines: AC except strict
37+
{" 123 234\n", "123 234\n"}, // preceding whitespaces: AC for white-diff/float-diff
38+
{"123 234\n", "123 234\n"}, // additional whitespaces: AC for white-diff/float-diff
39+
{" 1.0000001 1.0000001 ", "1.0 1.0\n"}, // AC for float-diff >1e-7
40+
{" 1.0000001 1.0000001 \n a 1.0000001 -1", "1.0 1.0\na 1.0 -1\n"}, // AC for float-diff >1e-7
41+
{" 1.00001 1.00001 ", "1.0 1.0\n"}, // AC for float-diff >1e-5
42+
{" 100.00001 100.00001 ", "100.0 100.0\n"}, // AC for float-diff (absolute>1e-5) (absolute-relative/relative>1e-7)
43+
{" 100.00001 0.0100001\n", "100.0 0.01\n"}, // AC for float-diff (absolute-relative>1e-7) (absolute/relative>1e-5)
44+
{"1.0000001 1.0000001\n", "1.0 1\n"}, // unmatched on non-float: WA for all
45+
}, 2);
46+
AssertVerdictReporter reporter(Verdict::WA, true, false);
47+
sub.reporter = reporter.GetReporter();
48+
auto orig_score = sub.reporter.ReportScoringResult;
49+
sub.reporter.ReportScoringResult = [&](const Submission& sub, const SubmissionResult& res, int subtask, int stage) {
50+
orig_score(sub, res, subtask, stage);
51+
ASSERT_EQ(res.td_results[subtask].verdict, param.verdicts[subtask]) << "Subtask " << subtask;
52+
};
53+
sub.default_scoring_args = param.default_scoring_args;
54+
long id = SetupSubmission(sub, 5, Compiler::GCC_CPP_17, kTime, true, R"(#include <unistd.h>
55+
char buf[256];
56+
int main(){ write(1, buf, read(0, buf, 256)); })", SpecjudgeType::NORMAL);
57+
RunAndTeardownSubmission(id);
58+
}
59+
60+
constexpr Verdict AC = Verdict::AC;
61+
constexpr Verdict WA = Verdict::WA;
62+
63+
INSTANTIATE_TEST_SUITE_P(OneSubmission, ExampleProblemDefaultScoring,
64+
testing::Values(
65+
(ScoringParam){{"strict"},
66+
{{AC, WA, WA, WA, WA, WA, WA, WA, WA, WA}}},
67+
(ScoringParam){{"line"},
68+
{{AC, AC, WA, WA, WA, WA, WA, WA, WA, WA}}},
69+
(ScoringParam){{},
70+
{{AC, AC, WA, WA, WA, WA, WA, WA, WA, WA}}},
71+
(ScoringParam){{"white-diff"},
72+
{{AC, AC, AC, AC, WA, WA, WA, WA, WA, WA}}},
73+
(ScoringParam){{"float-diff", "absolute-relative", "1e-9"},
74+
{{AC, AC, AC, AC, WA, WA, WA, WA, WA, WA}}},
75+
(ScoringParam){{"float-diff", "absolute", "1e-6"},
76+
{{AC, AC, AC, AC, AC, AC, WA, WA, WA, WA}}},
77+
(ScoringParam){{"float-diff", "absolute"},
78+
{{AC, AC, AC, AC, AC, AC, WA, WA, WA, WA}}},
79+
(ScoringParam){{"float-diff", "relative"},
80+
{{AC, AC, AC, AC, AC, AC, WA, AC, WA, WA}}},
81+
(ScoringParam){{"float-diff", "absolute-relative"},
82+
{{AC, AC, AC, AC, AC, AC, WA, AC, AC, WA}}},
83+
(ScoringParam){{"float-diff"},
84+
{{AC, AC, AC, AC, AC, AC, WA, AC, AC, WA}}},
85+
(ScoringParam){{"float-diff", "relative", "1e-4"},
86+
{{AC, AC, AC, AC, AC, AC, AC, AC, AC, WA}}}
87+
),
88+
ParamName);

test/example_problem.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#include "utils.h"
99

1010
void ExampleProblem::SetUp(int problem_id_, int td_num, int max_parallel) {
11+
std::vector<std::pair<std::string, std::string>> tds;
12+
for (int i = 0; i < td_num; i++) tds.emplace_back(std::to_string(i) + '\n', std::to_string(i) + '\n');
13+
SetUp(problem_id_, tds, max_parallel);
14+
}
15+
16+
void ExampleProblem::SetUp(int problem_id_, const std::vector<std::pair<std::string, std::string>>& tds, int max_parallel) {
1117
kMaxParallel = max_parallel;
1218
{
1319
char td_path_tmp[256] = "/tmp/td_test_XXXXXX";
@@ -16,12 +22,12 @@ void ExampleProblem::SetUp(int problem_id_, int td_num, int max_parallel) {
1622
}
1723
problem_id = problem_id_;
1824
fs::create_directories(td_path);
19-
for (int i = 0; i < td_num; i++) {
20-
std::filesystem::path in_path = td_path / (std::to_string(td_num) + ".in");
21-
std::filesystem::path out_path = td_path / (std::to_string(td_num) + ".out");
25+
for (int i = 0; i < (int)tds.size(); i++) {
26+
std::filesystem::path in_path = td_path / (std::to_string(i) + ".in");
27+
std::filesystem::path out_path = td_path / (std::to_string(i) + ".out");
2228
std::ofstream f1(in_path), f2(out_path);
23-
f1 << i << std::endl;
24-
f2 << i << std::endl;
29+
f1 << tds[i].first;
30+
f2 << tds[i].second;
2531
sub.testdata.push_back({in_path, out_path, 65536, 65536, 65536, 1'000'000, false});
2632
}
2733
sub.problem_id = problem_id;

test/example_problem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class ExampleProblem : public ::testing::Test {
1010
protected:
1111
void SetUp(int problem_id_, int td_num, int max_parallel = 1);
12+
void SetUp(int problem_id_, const std::vector<std::pair<std::string, std::string>>& tds, int max_parallel = 1);
1213
void TearDown() override;
1314

1415
void RunAndTeardownSubmission(long id);

test/utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ class AssertVerdictReporter {
99
bool has_scoring_result_;
1010
bool has_overall_result_;
1111
bool require_scoring_;
12+
bool assert_scoring_verdict_;
1213

1314
void AssertResult_() {
1415
ASSERT_TRUE(has_overall_result_ && (!require_scoring_ || has_scoring_result_));
1516
}
1617
public:
1718
Verdict expect_verdict;
1819

19-
AssertVerdictReporter(Verdict ver, bool require_scoring = true) :
20+
AssertVerdictReporter(Verdict ver, bool require_scoring = true, bool assert_scoring_verdict = true) :
2021
has_scoring_result_(false),
2122
has_overall_result_(false),
2223
require_scoring_(require_scoring),
24+
assert_scoring_verdict_(assert_scoring_verdict),
2325
expect_verdict(ver) {}
2426
~AssertVerdictReporter() { AssertResult_(); }
2527

@@ -28,7 +30,9 @@ class AssertVerdictReporter {
2830
has_overall_result_ = true;
2931
}
3032
void ReportScoringResult(const SubmissionResult& res, int subtask) {
31-
ASSERT_EQ(res.td_results[subtask].verdict, expect_verdict);
33+
if (assert_scoring_verdict_) {
34+
ASSERT_EQ(res.td_results[subtask].verdict, expect_verdict);
35+
}
3236
has_scoring_result_ = true;
3337
}
3438

tools/default-scoring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int main(int argc, char** argv) {
170170
}
171171

172172
// parse arguments
173-
std::string type = "line", subtype;
173+
std::string type = "line", subtype = "absolute-relative";
174174
double threshold = 1e-6;
175175
{
176176
for (int i = 2; i < argc; i++) {
@@ -184,7 +184,7 @@ int main(int argc, char** argv) {
184184
subtype = argv[++i];
185185
if (i + 1 < argc) {
186186
try {
187-
threshold = std::stold(std::string(argv[i]));
187+
threshold = std::stold(std::string(argv[i + 1]));
188188
i++;
189189
} catch (...) {}
190190
}

0 commit comments

Comments
 (0)