Skip to content

Commit

Permalink
Assert cube vector invariant and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Sep 22, 2023
1 parent e6d1c0f commit 7b0690c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/game/cube_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl CubeVec {
/// Creates a new vector from the given cube components.
#[inline]
pub const fn new(q: i32, r: i32, s: i32) -> Self {
assert!(q + r + s == 0);
Self { q, r, s }
}

Expand Down Expand Up @@ -237,11 +238,11 @@ impl TryFrom<&Element> for CubeVec {
type Error = Error;

fn try_from(elem: &Element) -> Result<Self> {
Ok(CubeVec {
q: elem.attribute("q")?.parse()?,
r: elem.attribute("r")?.parse()?,
s: elem.attribute("s")?.parse()?,
})
Ok(CubeVec::new(
elem.attribute("q")?.parse()?,
elem.attribute("r")?.parse()?,
elem.attribute("s")?.parse()?,
))
}
}

Expand All @@ -252,8 +253,8 @@ mod tests {
#[test]
fn test_xml_parses() {
assert_xml_parse!(
r#"<position q="23" r="0" s="-2" />"#,
CubeVec::new(23, 0, -2)
r#"<position q="23" r="0" s="-23" />"#,
CubeVec::new(23, 0, -23)
);
}
}
4 changes: 2 additions & 2 deletions src/game/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ mod tests {
},
Segment {
direction: CubeDir::Right,
center: CubeVec::new(0, 4, -4),
center: CubeVec::new(4, 0, -4),
fields: vec![
vec![
Field::Water,
Expand Down Expand Up @@ -833,7 +833,7 @@ mod tests {
},
Ship {
team: Team::Two,
position: CubeVec::new(1, -2, 1),
position: CubeVec::new(-2, 1, 1),
direction: CubeDir::Right,
speed: 1,
free_turns: 1,
Expand Down

0 comments on commit 7b0690c

Please sign in to comment.