Complete exercise: testing

This commit is contained in:
2026-05-25 16:27:58 +04:00
parent b25fe11bc7
commit d745026c1a
4 changed files with 37 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ pub fn sploosh(x: i32, y: i32, z: i32) -> i32 {
(x, _, _) if x < 0 => 99,
(1, 2, 3) => 4,
(5, 6, 7) => 3,
(8, 9, 10) => 7,
(x, y, z) => x + y - z,
}
}
@@ -13,9 +14,11 @@ pub fn splish(a: i32, b: i32) -> i32 {
// 1. Use the `cfg` attribute to mark the `test` module below as a test module
#[cfg(test)]
mod test {
// 2. Bring all the library items into scope with a `use` statement
// Hint: It's okay to use `*` here.
use super::*;
// 3. Write a test function that verifies the following condition using the `assert_eq!` or
// `assert_ne!` macros
@@ -26,10 +29,24 @@ mod test {
// `cargo test` should run your tests and pass
// Hint: Don't forget the `#[test]` attribute for your test function!
#[test]
fn test_sploosh() {
assert_eq!(sploosh(1, 2, 3), 4);
assert_ne!(sploosh(5, 6, 7), 4);
assert_eq!(sploosh(-1, 2, 3), 99);
}
// 4. Write a test function that verifies the following conditions using the `assert!` macro
// - splish(100, 10) is negative
// - splish(40, 20) is positive
// - splish(9, 3) is 0
#[test]
fn test_splish() {
assert!(splish(100, 10) < 0);
assert!(splish(40, 20) > 0);
assert!(splish(9, 3) == 0);
}
}
// 5. Create a `tests/` directory and an integration test file `tests/more_tests.rs`