Complete these tasks to reinforce what you learned in this module.
The function double is defined for you in the editor. Replace the placeholder body of test_double with real assertions: double(5) == 10 and double(0) == 0.
assert_eq!(double(5), 10); and assert_eq!(double(0), 0); inside the #[test] fn test_double().// Task 1: write a unit test for `double`. Fill in the test below, then press
// "Check" — this task is graded by running `cargo test`. See tasks/TASKS.md.
pub fn double(n: i32) -> i32 {
n * 2
}
fn main() {
// `cargo test` ignores main; it's here only so this file also builds as a binary.
println!("double(5) = {}", double(5));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_double() {
// YOUR CODE HERE: assert that double(5) == 10 and double(0) == 0.
todo!("write the assertions with assert_eq!");
}
}
The function is_positive is defined for you. Replace the two placeholder tests: test_positive should assert!(is_positive(1)), and test_negative should assert!(!is_positive(-1)).
assert!(cond) fails if cond is false. For the negative case, assert the negation: assert!(!is_positive(-1));.// Task 2: write tests using assert! for `is_positive`. Fill in the two tests
// below, then press "Check" (graded by `cargo test`). See tasks/TASKS.md.
pub fn is_positive(n: i32) -> bool {
n > 0
}
fn main() {
println!("is_positive(1) = {}", is_positive(1));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_positive() {
// YOUR CODE HERE: assert!(is_positive(1))
todo!("assert that is_positive(1) is true");
}
#[test]
fn test_negative() {
// YOUR CODE HERE: assert!(!is_positive(-1))
todo!("assert that is_positive(-1) is false");
}
}