Complete exercise: testing
This commit is contained in:
@@ -9,15 +9,15 @@ edition = "2021"
|
||||
# Challenge Help 1: If you choose to take on the challenge, you'll need to add `criterion` as a
|
||||
# development dependency. Here is one way to do it:
|
||||
|
||||
# [dev-dependencies]
|
||||
# criterion = { version = "0.3", features = ["html_reports"] }
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
|
||||
# Challenge Help 2: Each benchmark needs a `[[bench]]` section with a name and disabling the harness.
|
||||
# A name "somename" will correspond with a file "benches/somename.rs"
|
||||
|
||||
# [[bench]]
|
||||
# name = "somename"
|
||||
# harness = false
|
||||
[[bench]]
|
||||
name = "somename"
|
||||
harness = false
|
||||
|
||||
# Challenge Help 3: The Criterion documentation has a great tutorial for how to actually write your
|
||||
# benchmark. Don't skip the part about `black_box()`!
|
||||
|
||||
9
exercise/testing/benches/somename.rs
Normal file
9
exercise/testing/benches/somename.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use testing::sploosh;
|
||||
|
||||
pub fn sploosh_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("sploosh", |b| b.iter(|| sploosh(black_box(8), black_box(9), black_box(10))));
|
||||
}
|
||||
|
||||
criterion_group!(benches, sploosh_benchmark);
|
||||
criterion_main!(benches);
|
||||
@@ -3,6 +3,7 @@ pub fn sploosh(x: i32, y: i32, z: i32) -> i32 {
|
||||
(x, _, _) if x < 0 => 99,
|
||||
(1, 2, 3) => 4,
|
||||
(5, 6, 7) => 3,
|
||||
(8, 9, 10) => 7,
|
||||
(x, y, z) => x + y - z,
|
||||
}
|
||||
}
|
||||
@@ -13,9 +14,11 @@ pub fn splish(a: i32, b: i32) -> i32 {
|
||||
|
||||
// 1. Use the `cfg` attribute to mark the `test` module below as a test module
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
// 2. Bring all the library items into scope with a `use` statement
|
||||
// Hint: It's okay to use `*` here.
|
||||
use super::*;
|
||||
|
||||
// 3. Write a test function that verifies the following condition using the `assert_eq!` or
|
||||
// `assert_ne!` macros
|
||||
@@ -26,10 +29,24 @@ mod test {
|
||||
// `cargo test` should run your tests and pass
|
||||
// Hint: Don't forget the `#[test]` attribute for your test function!
|
||||
|
||||
#[test]
|
||||
fn test_sploosh() {
|
||||
assert_eq!(sploosh(1, 2, 3), 4);
|
||||
assert_ne!(sploosh(5, 6, 7), 4);
|
||||
assert_eq!(sploosh(-1, 2, 3), 99);
|
||||
}
|
||||
|
||||
// 4. Write a test function that verifies the following conditions using the `assert!` macro
|
||||
// - splish(100, 10) is negative
|
||||
// - splish(40, 20) is positive
|
||||
// - splish(9, 3) is 0
|
||||
|
||||
#[test]
|
||||
fn test_splish() {
|
||||
assert!(splish(100, 10) < 0);
|
||||
assert!(splish(40, 20) > 0);
|
||||
assert!(splish(9, 3) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Create a `tests/` directory and an integration test file `tests/more_tests.rs`
|
||||
|
||||
6
exercise/testing/tests/more_tests.rs
Normal file
6
exercise/testing/tests/more_tests.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use testing::{sploosh, splish};
|
||||
|
||||
#[test]
|
||||
pub fn test_sploosh_splish() {
|
||||
assert_eq!(sploosh(splish(-1, 0), splish(1, 1), splish(3, 2)), 4);
|
||||
}
|
||||
Reference in New Issue
Block a user