test: new
This commit is contained in:
30
src/lib.rs
30
src/lib.rs
@@ -1,11 +1,12 @@
|
||||
use std::{fmt, ops};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Fraction {
|
||||
num: i64,
|
||||
den: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum FractionError {
|
||||
DivisionByZero,
|
||||
ZeroDenominator,
|
||||
@@ -220,4 +221,31 @@ impl TryInto<i64> for Fraction {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_new_valid_fraction() {
|
||||
let f = Fraction::new(2, 4).unwrap();
|
||||
assert_eq!(f.num, 1);
|
||||
assert_eq!(f.den, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_zero_denominator_error() {
|
||||
let f = Fraction::new(2, 0);
|
||||
assert!(matches!(f, Err(FractionError::ZeroDenominator)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_reduces_fraction() {
|
||||
let f = Fraction::new(256, 512).unwrap();
|
||||
assert_eq!(f.num, 1);
|
||||
assert_eq!(f.den, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_normalizes_signs() {
|
||||
let f = Fraction::new(1, -2).unwrap();
|
||||
assert_eq!(f.num, -1);
|
||||
assert_eq!(f.den, 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user