diff options
| author | Carol (Nichols || Goulding) <carol.nichols@gmail.com> | 2015-09-20 18:31:41 -0400 |
|---|---|---|
| committer | Carol (Nichols || Goulding) <carol.nichols@gmail.com> | 2015-09-20 18:31:41 -0400 |
| commit | 2a23eaced6a4b0f3241430fb8d9d177a482ca907 (patch) | |
| tree | fcbea514f2e19c3589480eb3aeb2651274ca008c /tests | |
| parent | 93869014f4c89d3fcd94e3455f39b14a297481a2 (diff) | |
Add some exercises about tests!
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/tests1.rs | 44 | ||||
| -rw-r--r-- | tests/tests2.rs | 43 | ||||
| -rw-r--r-- | tests/tests3.rs | 42 | ||||
| -rw-r--r-- | tests/tests4.rs | 18 |
4 files changed, 147 insertions, 0 deletions
diff --git a/tests/tests1.rs b/tests/tests1.rs new file mode 100644 index 0000000..15f810a --- /dev/null +++ b/tests/tests1.rs @@ -0,0 +1,44 @@ +// 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/tests/tests2.rs b/tests/tests2.rs new file mode 100644 index 0000000..0ae6ddb --- /dev/null +++ b/tests/tests2.rs @@ -0,0 +1,43 @@ +// 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/tests/tests3.rs b/tests/tests3.rs new file mode 100644 index 0000000..1ee0f29 --- /dev/null +++ b/tests/tests3.rs @@ -0,0 +1,42 @@ +// 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/tests/tests4.rs b/tests/tests4.rs new file mode 100644 index 0000000..278e29e --- /dev/null +++ b/tests/tests4.rs @@ -0,0 +1,18 @@ +// 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); + } +} |
