Going through rustlings for the first time

ex(17): tests

Changed files
+100 -16
exercises
solutions
+5 -2
.rustlings-state.txt
··· 1 1 DON'T EDIT THIS FILE! 2 2 3 - tests1 3 + iterators1 4 4 5 5 intro1 6 6 intro2 ··· 68 68 quiz3 69 69 lifetimes1 70 70 lifetimes2 71 - lifetimes3 71 + lifetimes3 72 + tests1 73 + tests2 74 + tests3
+3 -2
exercises/17_tests/tests1.rs
··· 13 13 mod tests { 14 14 // TODO: Import `is_even`. You can use a wildcard to import everything in 15 15 // the outer module. 16 + use super::*; 16 17 17 18 #[test] 18 19 fn you_can_assert() { 19 20 // TODO: Test the function `is_even` with some values. 20 - assert!(); 21 - assert!(); 21 + assert!(is_even(2)); 22 + assert!(!is_even(3)); 22 23 } 23 24 }
+3 -4
exercises/17_tests/tests2.rs
··· 15 15 #[test] 16 16 fn you_can_assert_eq() { 17 17 // TODO: Test the function `power_of_2` with some values. 18 - assert_eq!(); 19 - assert_eq!(); 20 - assert_eq!(); 21 - assert_eq!(); 18 + assert_eq!(power_of_2(0), 1); 19 + assert_eq!(power_of_2(1), 2); 20 + assert_eq!(power_of_2(2), 4); 22 21 } 23 22 }
+4 -2
exercises/17_tests/tests3.rs
··· 29 29 // TODO: This test should check if the rectangle has the size that we 30 30 // pass to its constructor. 31 31 let rect = Rectangle::new(10, 20); 32 - assert_eq!(todo!(), 10); // Check width 33 - assert_eq!(todo!(), 20); // Check height 32 + assert_eq!(rect.width, 10); // Check width 33 + assert_eq!(rect.height, 20); // Check height 34 34 } 35 35 36 36 // TODO: This test should check if the program panics when we try to create 37 37 // a rectangle with negative width. 38 38 #[test] 39 + #[should_panic] 39 40 fn negative_width() { 40 41 let _rect = Rectangle::new(-10, 10); 41 42 } ··· 43 44 // TODO: This test should check if the program panics when we try to create 44 45 // a rectangle with negative height. 45 46 #[test] 47 + #[should_panic] 46 48 fn negative_height() { 47 49 let _rect = Rectangle::new(10, -10); 48 50 }
+22 -2
solutions/17_tests/tests1.rs
··· 1 + // Tests are important to ensure that your code does what you think it should 2 + // do. 3 + 4 + fn is_even(n: i64) -> bool { 5 + n % 2 == 0 6 + } 7 + 1 8 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 9 + // You can optionally experiment here. 10 + } 11 + 12 + #[cfg(test)] 13 + mod tests { 14 + // When writing unit tests, it is common to import everything from the outer 15 + // module (`super`) using a wildcard. 16 + use super::*; 17 + 18 + #[test] 19 + fn you_can_assert() { 20 + assert!(is_even(0)); 21 + assert!(!is_even(-1)); 22 + // ^ You can assert `false` using the negation operator `!`. 23 + } 4 24 }
+20 -2
solutions/17_tests/tests2.rs
··· 1 + // Calculates the power of 2 using a bit shift. 2 + // `1 << n` is equivalent to "2 to the power of n". 3 + fn power_of_2(n: u8) -> u64 { 4 + 1 << n 5 + } 6 + 1 7 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 8 + // You can optionally experiment here. 9 + } 10 + 11 + #[cfg(test)] 12 + mod tests { 13 + use super::*; 14 + 15 + #[test] 16 + fn you_can_assert_eq() { 17 + assert_eq!(power_of_2(0), 1); 18 + assert_eq!(power_of_2(1), 2); 19 + assert_eq!(power_of_2(2), 4); 20 + assert_eq!(power_of_2(3), 8); 21 + } 4 22 }
+43 -2
solutions/17_tests/tests3.rs
··· 1 + struct Rectangle { 2 + width: i32, 3 + height: i32, 4 + } 5 + 6 + impl Rectangle { 7 + // Don't change this function. 8 + fn new(width: i32, height: i32) -> Self { 9 + if width <= 0 || height <= 0 { 10 + // Returning a `Result` would be better here. But we want to learn 11 + // how to test functions that can panic. 12 + panic!("Rectangle width and height must be positive"); 13 + } 14 + 15 + Rectangle { width, height } 16 + } 17 + } 18 + 1 19 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 20 + // You can optionally experiment here. 21 + } 22 + 23 + #[cfg(test)] 24 + mod tests { 25 + use super::*; 26 + 27 + #[test] 28 + fn correct_width_and_height() { 29 + let rect = Rectangle::new(10, 20); 30 + assert_eq!(rect.width, 10); // Check width 31 + assert_eq!(rect.height, 20); // Check height 32 + } 33 + 34 + #[test] 35 + #[should_panic] // Added this attribute to check that the test panics. 36 + fn negative_width() { 37 + let _rect = Rectangle::new(-10, 10); 38 + } 39 + 40 + #[test] 41 + #[should_panic] // Added this attribute to check that the test panics. 42 + fn negative_height() { 43 + let _rect = Rectangle::new(10, -10); 44 + } 4 45 }