···13mod tests {
14 // TODO: Import `is_even`. You can use a wildcard to import everything in
15 // the outer module.
01617 #[test]
18 fn you_can_assert() {
19 // TODO: Test the function `is_even` with some values.
20- assert!();
21- assert!();
22 }
23}
···13mod tests {
14 // TODO: Import `is_even`. You can use a wildcard to import everything in
15 // the outer module.
16+ use super::*;
1718 #[test]
19 fn you_can_assert() {
20 // TODO: Test the function `is_even` with some values.
21+ assert!(is_even(2));
22+ assert!(!is_even(3));
23 }
24}
+3-4
exercises/17_tests/tests2.rs
···15 #[test]
16 fn you_can_assert_eq() {
17 // TODO: Test the function `power_of_2` with some values.
18- assert_eq!();
19- assert_eq!();
20- assert_eq!();
21- assert_eq!();
22 }
23}
···15 #[test]
16 fn you_can_assert_eq() {
17 // TODO: Test the function `power_of_2` with some values.
18+ assert_eq!(power_of_2(0), 1);
19+ assert_eq!(power_of_2(1), 2);
20+ assert_eq!(power_of_2(2), 4);
021 }
22}
+4-2
exercises/17_tests/tests3.rs
···29 // TODO: This test should check if the rectangle has the size that we
30 // pass to its constructor.
31 let rect = Rectangle::new(10, 20);
32- assert_eq!(todo!(), 10); // Check width
33- assert_eq!(todo!(), 20); // Check height
34 }
3536 // TODO: This test should check if the program panics when we try to create
37 // a rectangle with negative width.
38 #[test]
039 fn negative_width() {
40 let _rect = Rectangle::new(-10, 10);
41 }
···43 // TODO: This test should check if the program panics when we try to create
44 // a rectangle with negative height.
45 #[test]
046 fn negative_height() {
47 let _rect = Rectangle::new(10, -10);
48 }
···29 // TODO: This test should check if the rectangle has the size that we
30 // pass to its constructor.
31 let rect = Rectangle::new(10, 20);
32+ assert_eq!(rect.width, 10); // Check width
33+ assert_eq!(rect.height, 20); // Check height
34 }
3536 // TODO: This test should check if the program panics when we try to create
37 // a rectangle with negative width.
38 #[test]
39+ #[should_panic]
40 fn negative_width() {
41 let _rect = Rectangle::new(-10, 10);
42 }
···44 // TODO: This test should check if the program panics when we try to create
45 // a rectangle with negative height.
46 #[test]
47+ #[should_panic]
48 fn negative_height() {
49 let _rect = Rectangle::new(10, -10);
50 }
+22-2
solutions/17_tests/tests1.rs
···00000001fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
00000000000004}
···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+8fn main() {
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+ }
24}
+20-2
solutions/17_tests/tests2.rs
···0000001fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
0000000000004}
···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+7fn main() {
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+ }
22}
+43-2
solutions/17_tests/tests3.rs
···0000000000000000001fn main() {
2- // DON'T EDIT THIS SOLUTION FILE!
3- // It will be automatically filled after you finish the exercise.
000000000000000000000004}
···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+19fn main() {
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+ }
45}