pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}
pub fn divide(a: i32, b: i32) -> Option<i32> {
if b == 0 {
None
} else {
Some(a / b)
}
}
pub fn is_positive(n: i32) -> bool {
n > 0
}
pub fn is_even(n: i32) -> bool {
n % 2 == 0
}
pub fn max(a: i32, b: i32) -> i32 {
if a > b {
a
} else {
b
}
}
pub fn are_equal(a: i32, b: i32) -> bool {
a == b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(0, 0), 0);
assert_eq!(add(-1, 1), 0);
assert_eq!(add(100, 200), 300);
}
#[test]
fn test_subtract() {
assert_eq!(subtract(5, 3), 2);
assert_eq!(subtract(0, 0), 0);
}
#[test]
fn test_multiply() {
assert_eq!(multiply(2, 3), 6);
assert_eq!(multiply(0, 5), 0);
}
#[test]
fn test_divide() {
assert_eq!(divide(6, 2), Some(3));
assert_eq!(divide(5, 0), None);
}
#[test]
fn test_is_positive() {
assert!(is_positive(1));
assert!(!is_positive(0));
}
#[test]
fn test_is_even() {
assert!(is_even(2));
assert!(is_even(0));
assert!(!is_even(1));
}
#[test]
fn test_max() {
assert_eq!(max(5, 3), 5);
assert_eq!(max(3, 5), 5);
}
}