ac-algorithm 0.2.0

Algorithm competition domain logic for ac
Documentation
//! Test cases: inputs, expected outputs, and comparison rules (A-02).

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::ids::{CaseId, TaskId};

/// Where a case came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CaseKind {
    /// Sample cases published with the problem statement.
    Sample,
    /// System test cases the contest publishes outside the statement
    /// (e.g. `AtCoder`'s Dropbox shares, #100).
    System,
    /// Cases created or imported by the user.
    Manual,
    /// Cases produced by the input generator (A-03, ac-test semantics).
    Generated,
}

/// How program output is compared against the expected output.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum OutputComparator {
    /// Byte-exact match after trailing-whitespace normalization.
    Exact,
    /// Token-wise comparison ignoring whitespace layout.
    Tokenwise,
    /// Numeric comparison with an absolute epsilon (floating-point output).
    Float { epsilon: f64 },
}

/// Expected output for a case. The file on disk is the source of truth;
/// the comparator records how it is judged.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ExpectedOutput {
    /// Path to the expected-output file.
    pub path: PathBuf,
    /// Comparison rule applied to program output.
    pub comparator: OutputComparator,
}

/// A single test case belonging to a task.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Case {
    /// Case identifier.
    pub id: CaseId,
    /// Task this case tests.
    pub task: TaskId,
    /// Human-readable case name (e.g. `sample_1`, `gen_seed_42`).
    pub name: String,
    /// Origin of the case.
    pub kind: CaseKind,
    /// Path to the input file.
    pub input_path: PathBuf,
    /// Expected output, when one exists. `None` limits the run to an
    /// execution check — it is never treated as a correctness pass
    /// (ADR-0005 invariant 1, AC-05).
    pub expected: Option<ExpectedOutput>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn case_without_expected_output_is_execution_only() {
        let case = Case {
            id: CaseId::new("case-1"),
            task: TaskId::new("abc351_c"),
            name: "manual".to_owned(),
            kind: CaseKind::Manual,
            input_path: PathBuf::from("in.txt"),
            expected: None,
        };
        assert!(case.expected.is_none());
    }

    #[test]
    fn comparator_serializes_with_epsilon() {
        let comparator = OutputComparator::Float { epsilon: 1e-6 };
        let json = serde_json::to_string(&comparator).expect("serialize");
        assert!(json.contains("1e-6") || json.contains("0.000001"));
    }
}