Fix tests and add Rectangle struct

This commit is contained in:
Rock070 2024-01-08 23:51:45 +08:00
parent 4ad24e4e34
commit d6c5788761
3 changed files with 7 additions and 11 deletions

View File

@ -6,12 +6,10 @@
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert_eq() { fn you_can_assert_eq() {
assert_eq!(); assert_eq!(1, 1);
} }
} }

View File

@ -7,8 +7,6 @@
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a // Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
pub fn is_even(num: i32) -> bool { pub fn is_even(num: i32) -> bool {
num % 2 == 0 num % 2 == 0
} }
@ -19,11 +17,11 @@ mod tests {
#[test] #[test]
fn is_true_when_even() { fn is_true_when_even() {
assert!(); assert!(is_even(2));
} }
#[test] #[test]
fn is_false_when_odd() { fn is_false_when_odd() {
assert!(); assert!(!is_even(1));
} }
} }

View File

@ -5,8 +5,6 @@
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a // Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
struct Rectangle { struct Rectangle {
width: i32, width: i32,
height: i32 height: i32
@ -30,17 +28,19 @@ mod tests {
fn correct_width_and_height() { fn correct_width_and_height() {
// This test should check if the rectangle is the size that we pass into its constructor // This test should check if the rectangle is the size that we pass into its constructor
let rect = Rectangle::new(10, 20); let rect = Rectangle::new(10, 20);
assert_eq!(???, 10); // check width assert_eq!(rect.width, 10); // check width
assert_eq!(???, 20); // check height assert_eq!(rect.height, 20); // check height
} }
#[test] #[test]
#[should_panic(expected="Rectangle width and height cannot be negative!")]
fn negative_width() { fn negative_width() {
// This test should check if program panics when we try to create rectangle with negative width // This test should check if program panics when we try to create rectangle with negative width
let _rect = Rectangle::new(-10, 10); let _rect = Rectangle::new(-10, 10);
} }
#[test] #[test]
#[should_panic(expected="Rectangle width and height cannot be negative!")]
fn negative_height() { fn negative_height() {
// This test should check if program panics when we try to create rectangle with negative height // This test should check if program panics when we try to create rectangle with negative height
let _rect = Rectangle::new(10, -10); let _rect = Rectangle::new(10, -10);