diff options
Diffstat (limited to 'ex4')
| -rw-r--r-- | ex4/Cargo.lock | 7 | ||||
| -rw-r--r-- | ex4/Cargo.toml | 6 | ||||
| -rw-r--r-- | ex4/src/main.rs | 35 |
3 files changed, 48 insertions, 0 deletions
diff --git a/ex4/Cargo.lock b/ex4/Cargo.lock new file mode 100644 index 0000000..fb485de --- /dev/null +++ b/ex4/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ex4" +version = "0.1.0" diff --git a/ex4/Cargo.toml b/ex4/Cargo.toml new file mode 100644 index 0000000..0aff052 --- /dev/null +++ b/ex4/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ex4" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ex4/src/main.rs b/ex4/src/main.rs new file mode 100644 index 0000000..3c2e63f --- /dev/null +++ b/ex4/src/main.rs @@ -0,0 +1,35 @@ +fn velocity(start: u32, end: u32, time_elapsed: u32) -> u32 { + if time_elapsed == 0 { + panic!("time_elapsed is zero"); + } + if end < start { + panic!("end < start"); + } + end - start / time_elapsed +} + +fn main() { + println!("{}", velocity(1,2,1)); +} + +#[cfg(test)] +mod tests { + use crate::velocity; + + #[test] + #[should_panic(expected="time_elapsed is zero")] + fn t0 () { + velocity(1,2,0); + } + + #[test] + #[should_panic(expected="end < start")] + fn t1 () { + velocity(2,1,1); + } + + #[test] + fn t2 () { + assert_eq!(velocity(1,1,1), 0); + } +} |
