Skip to main content

ac_algorithm/
case.rs

1//! Test cases: inputs, expected outputs, and comparison rules (A-02).
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::ids::{CaseId, TaskId};
8
9/// Where a case came from.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum CaseKind {
12    /// Sample cases published with the problem statement.
13    Sample,
14    /// System test cases the contest publishes outside the statement
15    /// (e.g. `AtCoder`'s Dropbox shares, #100).
16    System,
17    /// Cases created or imported by the user.
18    Manual,
19    /// Cases produced by the input generator (A-03, ac-test semantics).
20    Generated,
21}
22
23/// How program output is compared against the expected output.
24#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
25pub enum OutputComparator {
26    /// Byte-exact match after trailing-whitespace normalization.
27    Exact,
28    /// Token-wise comparison ignoring whitespace layout.
29    Tokenwise,
30    /// Numeric comparison with an absolute epsilon (floating-point output).
31    Float { epsilon: f64 },
32}
33
34/// Expected output for a case. The file on disk is the source of truth;
35/// the comparator records how it is judged.
36#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
37pub struct ExpectedOutput {
38    /// Path to the expected-output file.
39    pub path: PathBuf,
40    /// Comparison rule applied to program output.
41    pub comparator: OutputComparator,
42}
43
44/// A single test case belonging to a task.
45#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
46pub struct Case {
47    /// Case identifier.
48    pub id: CaseId,
49    /// Task this case tests.
50    pub task: TaskId,
51    /// Human-readable case name (e.g. `sample_1`, `gen_seed_42`).
52    pub name: String,
53    /// Origin of the case.
54    pub kind: CaseKind,
55    /// Path to the input file.
56    pub input_path: PathBuf,
57    /// Expected output, when one exists. `None` limits the run to an
58    /// execution check — it is never treated as a correctness pass
59    /// (ADR-0005 invariant 1, AC-05).
60    pub expected: Option<ExpectedOutput>,
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn case_without_expected_output_is_execution_only() {
69        let case = Case {
70            id: CaseId::new("case-1"),
71            task: TaskId::new("abc351_c"),
72            name: "manual".to_owned(),
73            kind: CaseKind::Manual,
74            input_path: PathBuf::from("in.txt"),
75            expected: None,
76        };
77        assert!(case.expected.is_none());
78    }
79
80    #[test]
81    fn comparator_serializes_with_epsilon() {
82        let comparator = OutputComparator::Float { epsilon: 1e-6 };
83        let json = serde_json::to_string(&comparator).expect("serialize");
84        assert!(json.contains("1e-6") || json.contains("0.000001"));
85    }
86}