diff options
Diffstat (limited to 'exercises/tests')
| -rwxr-xr-x | exercises/tests/tests1.rs | 49 | ||||
| -rwxr-xr-x | exercises/tests/tests2.rs | 44 | ||||
| -rwxr-xr-x | exercises/tests/tests3.rs | 43 | ||||
| -rwxr-xr-x | exercises/tests/tests4.rs | 19 |
4 files changed, 155 insertions, 0 deletions
diff --git a/exercises/tests/tests1.rs b/exercises/tests/tests1.rs new file mode 100755 index 0000000..959ed85 --- /dev/null +++ b/exercises/tests/tests1.rs @@ -0,0 +1,49 @@ +// 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: +// rustc --test tests1.rs + +// This test has a problem with it -- make the test compile! Make the test +// pass! Make the test fail! Scroll down for hints :) + +#[cfg(test)] +mod tests { + #[test] + fn you_can_assert() { + assert!(); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// You don't even need to write any code to test -- you can just test values and run that, even +// though you wouldn't do that in real life :) `assert!` is a macro that needs an argument. +// Depending on the value of the argument, `assert!` will do nothing (in which case the test will +// pass) or `assert!` will panic (in which case the test will fail). So try giving different values +// to `assert!` and see which ones compile, which ones pass, and which ones fail :) diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs new file mode 100755 index 0000000..6775d61 --- /dev/null +++ b/exercises/tests/tests2.rs @@ -0,0 +1,44 @@ +// tests2.rs +// This test has a problem with it -- make the test compile! Make the test +// pass! Make the test fail! Scroll down for hints :) + +#[cfg(test)] +mod tests { + #[test] + fn you_can_assert_eq() { + assert_eq!(); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Like the previous exercise, you don't need to write any code to get this test to compile and +// run. `assert_eq!` is a macro that takes two arguments and compares them. Try giving it two +// values that are equal! Try giving it two arguments that are different! Try giving it two values +// that are of different types! Try switching which argument comes first and which comes second! diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs new file mode 100755 index 0000000..e041f38 --- /dev/null +++ b/exercises/tests/tests3.rs @@ -0,0 +1,43 @@ +// 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 that we get the result +// we expect to get when we call `is_even(5)`. Scroll down for hints! + +pub fn is_even(num: i32) -> bool { + num % 2 == 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_true_when_even() { + assert!(false); + } +} + + + + + + + + + + + + + + + + + + + + + + +// You can call a function right where you're passing arguments to `assert!` -- so you could do +// something like `assert!(having_fun())`. If you want to check that you indeed get false, you +// can negate the result of what you're doing using `!`, like `assert!(!having_fun())`. diff --git a/exercises/tests/tests4.rs b/exercises/tests/tests4.rs new file mode 100755 index 0000000..23d444a --- /dev/null +++ b/exercises/tests/tests4.rs @@ -0,0 +1,19 @@ +// tests4.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 that we get the result +// we expect to get when we call `times_two` with a negative number. +// No hints, you can do this :) + +pub fn times_two(num: i32) -> i32 { + num * 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn returns_twice_of_positive_numbers() { + assert_eq!(4, 4); + } +} |
