[core] Address clippy suggestions; clean up unit tests

In coordinates.rs:

- Add some [must_use] decorators to some getters
- Rewrite some unit tests to remove the .expect() and use ? instead
This commit is contained in:
Eryn Wells 2024-04-25 08:05:07 -07:00
parent 1b63f56042
commit a2d0c638d0

View file

@ -59,6 +59,7 @@ macro_rules! range_bound_struct {
}
impl $type {
#[must_use]
$vis fn new(x: $repr) -> Option<Self> {
if x < $max {
Some(Self(x))
@ -73,14 +74,21 @@ macro_rules! range_bound_struct {
///
/// This function does not perform any bounds checking. It should only be called when
/// the input is already known to be within bounds, i.e. when `x >= Self::FIRST && x < Self::LAST`.
#[must_use]
$vis unsafe fn new_unchecked(x: $repr) -> Self {
debug_assert!((Self::FIRST.0..=Self::LAST.0).contains(&x));
Self(x)
}
#[must_use]
$vis fn as_index(&self) -> &$repr {
&self.0
}
#[must_use]
$vis fn iter(&self) -> impl Iterator<Item = Self> {
(Self::FIRST.0..=Self::LAST.0).map(Self)
}
}
impl From<$type> for $repr {
@ -105,11 +113,13 @@ coordinate_enum!(
);
impl Direction {
#[must_use]
pub fn to_offset(&self) -> i8 {
const OFFSETS: [i8; 8] = [8, 9, 1, -7, -8, -9, -1, 7];
OFFSETS[*self as usize]
}
#[must_use]
pub fn opposite(&self) -> Direction {
const OPPOSITES: [Direction; 8] = [
Direction::South,
@ -228,7 +238,7 @@ impl Square {
#[inline]
pub fn file(self) -> File {
unsafe { File::new_unchecked((self as u8) & 0b000111) }
unsafe { File::new_unchecked((self as u8) & 0b000_00111) }
}
#[inline]
@ -374,39 +384,45 @@ mod tests {
}
#[test]
fn good_algebraic_input() {
let sq = Square::from_algebraic_str("a4").expect("Failed to parse 'a4' square");
fn good_algebraic_input() -> Result<(), ParseSquareError> {
let sq = Square::from_algebraic_str("a4")?;
assert_eq!(sq.file(), File(0));
assert_eq!(sq.rank(), Rank(3));
let sq = Square::from_algebraic_str("B8").expect("Failed to parse 'B8' square");
let sq = Square::from_algebraic_str("B8")?;
assert_eq!(sq.file(), File(1));
assert_eq!(sq.rank(), Rank(7));
let sq = Square::from_algebraic_str("e4").expect("Failed to parse 'B8' square");
let sq = Square::from_algebraic_str("e4")?;
assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(3));
Ok(())
}
#[test]
fn bad_algebraic_input() {
Square::from_algebraic_str("a0").expect_err("Got valid Square for 'a0'");
Square::from_algebraic_str("j3").expect_err("Got valid Square for 'j3'");
Square::from_algebraic_str("a11").expect_err("Got valid Square for 'a11'");
Square::from_algebraic_str("b-1").expect_err("Got valid Square for 'b-1'");
Square::from_algebraic_str("a 1").expect_err("Got valid Square for 'a 1'");
Square::from_algebraic_str("").expect_err("Got valid Square for ''");
fn bad_algebraic_input() -> Result<(), ParseSquareError> {
Square::from_algebraic_str("a0")?;
Square::from_algebraic_str("j3")?;
Square::from_algebraic_str("a11")?;
Square::from_algebraic_str("b-1")?;
Square::from_algebraic_str("a 1")?;
Square::from_algebraic_str("")?;
Ok(())
}
#[test]
fn from_index() {
let sq = Square::try_from(4u32).expect("Unable to get Square from index");
fn from_index() -> Result<(), ()> {
let sq = Square::try_from(4u32)?;
assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(0));
let sq = Square::try_from(28u32).expect("Unable to get Square from index");
let sq = Square::try_from(28u32)?;
assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(3));
Ok(())
}
#[test]