Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Rustのテスト機能をためす その2:色々やる

実際のところ

// lib.rs
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_positive_numbers() {
        assert_eq!(add(2, 3), 5);
    }

    #[test]
    fn test_add_negative_numbers() {
        assert_eq!(add(-2, -3), -5);
    }

    #[test]
    fn test_add_zero() {
        assert_eq!(add(0, 5), 5);
    }

    #[test]
    fn test_add_negative_and_positive() {
        assert_eq!(add(-2, 3), 1);
    }

    #[test]
    fn test_add_large_numbers() {
        assert_eq!(add(1000000, 2000000), 3000000);
    }
}

テスト実行

$ cargo test