Skip to main content

ac_algorithm/
verdict.rs

1//! Verdict vocabulary for the Algorithm domain.
2//!
3//! Two distinct verdict families exist and must never be conflated
4//! (ADR-0005 invariant 1): [`JudgeVerdict`] mirrors judge/official result
5//! codes, while [`LocalVerdict`] describes local test runs only. A local
6//! pass is never an official acceptance.
7
8use serde::{Deserialize, Serialize};
9
10/// Judge-side verdict codes, mirroring `AtCoder`'s result notation.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum JudgeVerdict {
13    /// Accepted.
14    Accepted,
15    /// Wrong answer.
16    WrongAnswer,
17    /// Time limit exceeded.
18    TimeLimitExceeded,
19    /// Memory limit exceeded.
20    MemoryLimitExceeded,
21    /// Runtime error (including crashes and forbidden syscalls).
22    RuntimeError,
23    /// Compilation error.
24    CompilationError,
25    /// Internal error on the judge side.
26    InternalError,
27}
28
29impl JudgeVerdict {
30    /// Returns the canonical two-letter code used by `AtCoder`.
31    #[must_use]
32    pub const fn as_code(self) -> &'static str {
33        match self {
34            Self::Accepted => "AC",
35            Self::WrongAnswer => "WA",
36            Self::TimeLimitExceeded => "TLE",
37            Self::MemoryLimitExceeded => "MLE",
38            Self::RuntimeError => "RE",
39            Self::CompilationError => "CE",
40            Self::InternalError => "IE",
41        }
42    }
43
44    /// Parses a verdict code. Unknown or empty codes return `None`; callers
45    /// decide how to surface unrecognized official values.
46    #[must_use]
47    pub fn from_code(code: &str) -> Option<Self> {
48        match code {
49            "AC" => Some(Self::Accepted),
50            "WA" => Some(Self::WrongAnswer),
51            "TLE" => Some(Self::TimeLimitExceeded),
52            "MLE" => Some(Self::MemoryLimitExceeded),
53            "RE" => Some(Self::RuntimeError),
54            "CE" => Some(Self::CompilationError),
55            "IE" => Some(Self::InternalError),
56            _ => None,
57        }
58    }
59}
60
61/// Why a local run could not produce a pass/fail verdict. Carried next to
62/// [`LocalVerdict::Error`] so execution-check failures keep their RE/TLE/
63/// MLE/CE distinction without ever borrowing judge verdict codes
64/// (ADR-0005 invariant 1).
65#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
66pub enum LocalFailure {
67    /// The solution failed to compile.
68    Compile,
69    /// The process exited non-zero or died on a signal.
70    Runtime,
71    /// The wall-clock limit was exceeded.
72    TimeLimit,
73    /// Peak memory exceeded the problem's memory limit.
74    MemoryLimit,
75    /// The combined output cap was exceeded.
76    OutputLimit,
77    /// The run was cancelled.
78    Cancelled,
79    /// Harness or infrastructure failure (spawn, I/O, lost job).
80    Harness,
81}
82
83/// Outcome of a local test run. Deliberately separate from
84/// [`JudgeVerdict`]: these describe what the local harness observed, not
85/// what the official judge decided.
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
87pub enum LocalVerdict {
88    /// Output matched the expected output ("local case pass").
89    CasePass,
90    /// The program ran to completion but no expected output exists, so only
91    /// execution was verified ("execution check pass").
92    ExecutionPass,
93    /// Output differed from the expected output or a checker rejected it.
94    CaseFail,
95    /// Compilation, runtime, or harness failure prevented a verdict.
96    Error,
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn judge_verdict_codes_roundtrip() {
105        for verdict in [
106            JudgeVerdict::Accepted,
107            JudgeVerdict::WrongAnswer,
108            JudgeVerdict::TimeLimitExceeded,
109            JudgeVerdict::MemoryLimitExceeded,
110            JudgeVerdict::RuntimeError,
111            JudgeVerdict::CompilationError,
112            JudgeVerdict::InternalError,
113        ] {
114            assert_eq!(JudgeVerdict::from_code(verdict.as_code()), Some(verdict));
115        }
116    }
117
118    #[test]
119    fn judge_verdict_rejects_unknown_codes() {
120        assert_eq!(JudgeVerdict::from_code(""), None);
121        assert_eq!(JudgeVerdict::from_code("AC "), None);
122        assert_eq!(JudgeVerdict::from_code("OK"), None);
123    }
124}