This commit is contained in:
Asger Juul Brunshøj 2025-03-31 22:43:19 +02:00
parent d11f8510b4
commit ed6aa4b9c9
3 changed files with 79 additions and 13 deletions

View File

@ -51,12 +51,12 @@ codee = { version = "0.3" }
error_reporter = { version = "1" }
getrandom = { version = "0.3.1" }
[dev-dependencies]
test-try= "0.1"
[dev-dependencies.serde_json]
version = "1"
[dev-dependencies.test-try]
version = "0.1"
[features]
hydrate = ["leptos/hydrate", "getrandom/wasm_js", "uuid/js"]
ssr = [

View File

@ -18,6 +18,7 @@ pub use v3::UserInteraction;
pub use v4::DatedAttempt;
pub use v4::Problem;
pub use v4::ProblemHolds;
pub use v4::Transformation;
mod semantics;
@ -47,7 +48,7 @@ pub mod v4 {
pub transformation: Transformation,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Default)]
pub struct Transformation {
pub horizontal_shift: u64,
pub mirror: bool,

View File

@ -11,8 +11,10 @@ impl Problem {
transformation,
} = self;
let min_col = holds.0.iter().map(|(hold_position, _)| hold_position.col).min().unwrap_or(0);
let mut transformation = *transformation;
let min_col = holds.0.iter().map(|(hold_position, _)| hold_position.col).min().unwrap_or(0);
transformation.horizontal_shift += min_col;
let holds = {
let holds = holds
.0
@ -28,17 +30,17 @@ impl Problem {
ProblemHolds(holds)
};
let transformation = {
let mut transformation = *transformation;
transformation.horizontal_shift += min_col;
transformation
};
Self {
let mut new = Self {
holds,
method: *method,
transformation,
};
if new.transformation.mirror {
new = new.mirror();
}
new
}
pub fn mirror(&self) -> Self {
@ -78,7 +80,6 @@ impl Problem {
method: *method,
transformation,
}
.normalize()
}
pub fn shift(&self, shift: i64) -> Self {
@ -186,3 +187,67 @@ impl Attempt {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test_try::test_try]
fn normalize_empty_problem() {
let problem = Problem {
holds: ProblemHolds([].into_iter().collect()),
method: Method::FeetFollowHands,
transformation: Transformation::default(),
};
let normalized = problem.normalize();
assert_eq!(problem, normalized);
let mirrored = problem.mirror();
assert_eq!(problem, mirrored);
}
#[test_try::test_try]
fn normalize_problem() {
let problem = Problem {
holds: ProblemHolds(
[
(HoldPosition { row: 0, col: 1 }, HoldRole::End),
(HoldPosition { row: 7, col: 6 }, HoldRole::Start),
]
.into_iter()
.collect(),
),
method: Method::FeetFollowHands,
transformation: Transformation::default(),
};
let normalized = problem.normalize();
assert_eq!(normalized.holds.0[&HoldPosition { row: 0, col: 0 }], HoldRole::End);
assert_eq!(normalized.holds.0[&HoldPosition { row: 7, col: 5 }], HoldRole::Start);
assert_eq!(normalized.transformation.horizontal_shift, 1);
assert_eq!(normalized.transformation.mirror, false);
}
#[test_try::test_try]
fn mirror_problem() {
let problem = Problem {
holds: ProblemHolds(
[
(HoldPosition { row: 0, col: 1 }, HoldRole::End),
(HoldPosition { row: 7, col: 6 }, HoldRole::Start),
]
.into_iter()
.collect(),
),
method: Method::FeetFollowHands,
transformation: Transformation::default(),
};
let normalized = problem.normalize();
assert_eq!(normalized.holds.0[&HoldPosition { row: 0, col: 0 }], HoldRole::End);
assert_eq!(normalized.holds.0[&HoldPosition { row: 7, col: 5 }], HoldRole::Start);
assert_eq!(normalized.transformation.horizontal_shift, 1);
assert_eq!(normalized.transformation.mirror, false);
}
}