summaryrefslogtreecommitdiff
path: root/exercises/17_tests
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/17_tests')
-rw-r--r--exercises/17_tests/tests1.rs24
-rw-r--r--exercises/17_tests/tests2.rs22
-rw-r--r--exercises/17_tests/tests3.rs50
-rw-r--r--exercises/17_tests/tests4.rs48
4 files changed, 62 insertions, 82 deletions
diff --git a/exercises/17_tests/tests1.rs b/exercises/17_tests/tests1.rs
index 810277a..7529f9f 100644
--- a/exercises/17_tests/tests1.rs
+++ b/exercises/17_tests/tests1.rs
@@ -1,21 +1,23 @@
-// tests1.rs
-//
// Tests are important to ensure that your code does what you think it should
-// do. Tests can be run on this file with the following command: rustlings run
-// tests1
-//
-// This test has a problem with it -- make the test compile! Make the test pass!
-// Make the test fail!
-//
-// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
-// hint.
+// do.
-// I AM NOT DONE
+fn is_even(n: i64) -> bool {
+ n % 2 == 0
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
#[cfg(test)]
mod tests {
+ // TODO: Import `is_even`. You can use a wildcard to import everything in
+ // the outer module.
+
#[test]
fn you_can_assert() {
+ // TODO: Test the function `is_even` with some values.
+ assert!();
assert!();
}
}
diff --git a/exercises/17_tests/tests2.rs b/exercises/17_tests/tests2.rs
index f8024e9..0c6573e 100644
--- a/exercises/17_tests/tests2.rs
+++ b/exercises/17_tests/tests2.rs
@@ -1,17 +1,23 @@
-// tests2.rs
-//
-// This test has a problem with it -- make the test compile! Make the test pass!
-// Make the test fail!
-//
-// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
-// hint.
+// Calculates the power of 2 using a bit shift.
+// `1 << n` is equivalent to "2 to the power of n".
+fn power_of_2(n: u8) -> u64 {
+ 1 << n
+}
-// I AM NOT DONE
+fn main() {
+ // You can optionally experiment here.
+}
#[cfg(test)]
mod tests {
+ use super::*;
+
#[test]
fn you_can_assert_eq() {
+ // TODO: Test the function `power_of_2` with some values.
+ assert_eq!();
+ assert_eq!();
+ assert_eq!();
assert_eq!();
}
}
diff --git a/exercises/17_tests/tests3.rs b/exercises/17_tests/tests3.rs
index 4013e38..ec99479 100644
--- a/exercises/17_tests/tests3.rs
+++ b/exercises/17_tests/tests3.rs
@@ -1,16 +1,23 @@
-// tests3.rs
-//
-// This test isn't testing our function -- make it do that in such a way that
-// the test passes. Then write a second test that tests whether we get the
-// result we expect to get when we call `is_even(5)`.
-//
-// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
-// hint.
+struct Rectangle {
+ width: i32,
+ height: i32,
+}
+
+impl Rectangle {
+ // Don't change this function.
+ fn new(width: i32, height: i32) -> Self {
+ if width <= 0 || height <= 0 {
+ // Returning a `Result` would be better here. But we want to learn
+ // how to test functions that can panic.
+ panic!("Rectangle width and height can't be negative");
+ }
-// I AM NOT DONE
+ Rectangle { width, height }
+ }
+}
-pub fn is_even(num: i32) -> bool {
- num % 2 == 0
+fn main() {
+ // You can optionally experiment here.
}
#[cfg(test)]
@@ -18,12 +25,25 @@ mod tests {
use super::*;
#[test]
- fn is_true_when_even() {
- assert!();
+ fn correct_width_and_height() {
+ // TODO: This test should check if the rectangle has the size that we
+ // pass to its constructor.
+ let rect = Rectangle::new(10, 20);
+ assert_eq!(todo!(), 10); // Check width
+ assert_eq!(todo!(), 20); // Check height
+ }
+
+ // TODO: This test should check if the program panics when we try to create
+ // a rectangle with negative width.
+ #[test]
+ fn negative_width() {
+ let _rect = Rectangle::new(-10, 10);
}
+ // TODO: This test should check if the program panics when we try to create
+ // a rectangle with negative height.
#[test]
- fn is_false_when_odd() {
- assert!();
+ fn negative_height() {
+ let _rect = Rectangle::new(10, -10);
}
}
diff --git a/exercises/17_tests/tests4.rs b/exercises/17_tests/tests4.rs
deleted file mode 100644
index 935d0db..0000000
--- a/exercises/17_tests/tests4.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-// tests4.rs
-//
-// Make sure that we're testing for the correct conditions!
-//
-// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
-
-struct Rectangle {
- width: i32,
- height: i32
-}
-
-impl Rectangle {
- // Only change the test functions themselves
- pub fn new(width: i32, height: i32) -> Self {
- if width <= 0 || height <= 0 {
- panic!("Rectangle width and height cannot be negative!")
- }
- Rectangle {width, height}
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn correct_width_and_height() {
- // This test should check if the rectangle is the size that we pass into its constructor
- let rect = Rectangle::new(10, 20);
- assert_eq!(???, 10); // check width
- assert_eq!(???, 20); // check height
- }
-
- #[test]
- fn negative_width() {
- // This test should check if program panics when we try to create rectangle with negative width
- let _rect = Rectangle::new(-10, 10);
- }
-
- #[test]
- fn negative_height() {
- // This test should check if program panics when we try to create rectangle with negative height
- let _rect = Rectangle::new(10, -10);
- }
-}