#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PivotType {
OneByOne,
TwoByTwo {
partner: usize,
},
Delayed,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Block2x2 {
pub first_col: usize,
pub a: f64,
pub b: f64,
pub c: f64,
}
impl Block2x2 {
pub fn determinant(&self) -> f64 {
self.a * self.c - self.b * self.b
}
pub fn trace(&self) -> f64 {
self.a + self.c
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pivot_type_one_by_one() {
let p = PivotType::OneByOne;
assert_eq!(p, PivotType::OneByOne);
}
#[test]
fn pivot_type_two_by_two_with_partner() {
let p = PivotType::TwoByTwo { partner: 5 };
assert_eq!(p, PivotType::TwoByTwo { partner: 5 });
assert_ne!(p, PivotType::TwoByTwo { partner: 3 });
}
#[test]
fn pivot_type_delayed() {
let p = PivotType::Delayed;
assert_eq!(p, PivotType::Delayed);
assert_ne!(p, PivotType::OneByOne);
}
#[test]
fn pivot_type_copy_clone() {
let p = PivotType::TwoByTwo { partner: 2 };
let p2 = p; #[allow(clippy::clone_on_copy)]
let p3 = p.clone(); assert_eq!(p, p2);
assert_eq!(p, p3);
}
#[test]
fn pivot_type_debug_formatting() {
let one = PivotType::OneByOne;
let two = PivotType::TwoByTwo { partner: 7 };
let delayed = PivotType::Delayed;
assert!(!format!("{:?}", one).is_empty());
assert!(format!("{:?}", two).contains("7"));
assert!(!format!("{:?}", delayed).is_empty());
}
#[test]
fn pivot_type_eq_is_structural() {
assert_eq!(PivotType::OneByOne, PivotType::OneByOne);
assert_eq!(PivotType::Delayed, PivotType::Delayed);
assert_ne!(PivotType::OneByOne, PivotType::Delayed);
assert_ne!(
PivotType::TwoByTwo { partner: 1 },
PivotType::TwoByTwo { partner: 2 }
);
}
#[test]
fn block2x2_field_storage() {
let block = Block2x2 {
first_col: 3,
a: 2.0,
b: 0.5,
c: -3.0,
};
assert_eq!(block.first_col, 3);
assert_eq!(block.a, 2.0);
assert_eq!(block.b, 0.5);
assert_eq!(block.c, -3.0);
}
#[test]
fn block2x2_determinant() {
let block = Block2x2 {
first_col: 0,
a: 2.0,
b: 0.5,
c: -3.0,
};
assert_eq!(block.determinant(), -6.25);
}
#[test]
fn block2x2_determinant_identity() {
let block = Block2x2 {
first_col: 0,
a: 1.0,
b: 0.0,
c: 1.0,
};
assert_eq!(block.determinant(), 1.0);
}
#[test]
fn block2x2_trace() {
let block = Block2x2 {
first_col: 0,
a: 2.0,
b: 0.5,
c: -3.0,
};
assert_eq!(block.trace(), -1.0);
}
#[test]
fn block2x2_trace_positive() {
let block = Block2x2 {
first_col: 0,
a: 5.0,
b: 1.0,
c: 3.0,
};
assert_eq!(block.trace(), 8.0);
}
#[test]
fn block2x2_copy_clone() {
let block = Block2x2 {
first_col: 0,
a: 1.0,
b: 2.0,
c: 3.0,
};
let b2 = block; #[allow(clippy::clone_on_copy)]
let b3 = block.clone(); assert_eq!(block, b2);
assert_eq!(block, b3);
}
#[test]
fn block2x2_partial_eq() {
let b1 = Block2x2 {
first_col: 0,
a: 1.0,
b: 2.0,
c: 3.0,
};
let b2 = Block2x2 {
first_col: 0,
a: 1.0,
b: 2.0,
c: 3.0,
};
let b3 = Block2x2 {
first_col: 1,
a: 1.0,
b: 2.0,
c: 3.0,
};
assert_eq!(b1, b2);
assert_ne!(b1, b3);
}
}