diff --git a/crates/ascend/Cargo.toml b/crates/ascend/Cargo.toml index bf4bcb6..0eb68d5 100644 --- a/crates/ascend/Cargo.toml +++ b/crates/ascend/Cargo.toml @@ -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 = [ diff --git a/crates/ascend/src/models.rs b/crates/ascend/src/models.rs index 272969b..d3ee4ee 100644 --- a/crates/ascend/src/models.rs +++ b/crates/ascend/src/models.rs @@ -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, diff --git a/crates/ascend/src/models/semantics.rs b/crates/ascend/src/models/semantics.rs index da42f99..8989489 100644 --- a/crates/ascend/src/models/semantics.rs +++ b/crates/ascend/src/models/semantics.rs @@ -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); + } +}