From 61612a20504025a2e460f9885f752490b2a3b13f Mon Sep 17 00:00:00 2001 From: Nathan Stocks Date: Tue, 15 Jun 2021 22:51:21 -0600 Subject: [PATCH] Add testing exercise --- Cargo.toml | 1 + exercise/errors/src/lib.rs | 2 +- exercise/testing/Cargo.toml | 25 ++++++++++++++++++++++ exercise/testing/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 exercise/testing/Cargo.toml create mode 100644 exercise/testing/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 54c4002..00df047 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ members = [ "exercise/closures_iterators", "exercise/traits", "exercise/errors", + "exercise/testing", ] diff --git a/exercise/errors/src/lib.rs b/exercise/errors/src/lib.rs index 21fc80f..ceea6b4 100644 --- a/exercise/errors/src/lib.rs +++ b/exercise/errors/src/lib.rs @@ -3,7 +3,7 @@ // - TooYoung - An dolphin is too young // - LongName - The dolphin's name is too long and annoying to say // -// As a reminder, here are the 5 Guidelines for creating an error type +// As a reminder, here are the 5 Guidelines for creating an error type: // (1) Use an `enum` for your error type // (2) Your error conditions should be enum variants grouped in as few enums as makes sense // (3) Don't expose error types other than your own (you don't have to do anything for this one) diff --git a/exercise/testing/Cargo.toml b/exercise/testing/Cargo.toml new file mode 100644 index 0000000..5314d0d --- /dev/null +++ b/exercise/testing/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "testing" +version = "0.1.0" +authors = ["Nathan Stocks "] +edition = "2018" + +[dependencies] + + +# 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"] } + +# Challenge Help 2: Each benchmark needs a `[[bench]]` section with a name and disabling the harness. +# The name will correspond with a file benches/somename.rs + +# [[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()`! +# https://bheisler.github.io/criterion.rs/book/getting_started.html#step-2---add-benchmark diff --git a/exercise/testing/src/lib.rs b/exercise/testing/src/lib.rs new file mode 100644 index 0000000..b4d1001 --- /dev/null +++ b/exercise/testing/src/lib.rs @@ -0,0 +1,41 @@ +pub fn sploosh(x: i32, y: i32, z: i32) -> i32 { + match (x, y, z) { + (x, _, _) if x < 0 => 99, + (1, 2, 3) => 4, + (5, 6, 7) => 3, + (x, y, z) => x + y - z, + } +} + +pub fn splish(a: i32, b: i32) -> i32 { + -a + 3 * b +} + +// 1. Use the `cfg` attribute to mark the `test` module below as a test module + +mod test { + // 2. Bring all the library items into scope with a `use` statement + + // 3. Write a test function that verifies the following condition using assert_eq! or assert_ne! + // - sploosh(1, 2, 3) returns 4 + // - sploosh(5, 6, 7) does not return 4 + // - If you pass sploosh a negative number for the first argument, 99 is returned + // + // `cargo test` should run your tests AND pass + // Hint: Don't forget the `test` attribute for your test function! + + // 4. Write a test function that verifies the following conditions using assert! + // - splish(100, 10) is negative + // - splish(40, 20) is positive + // - splish(9, 3) is 0 +} + +// 4. Create a tests/ directory and an integration test file tests/more_tests.rs +// Inside that file, create a test function that verifies: +// - sploosh(splish(-1, 0), splish(1, 1), splish(3, 2)) returns 4 +// +// `cargo test` should run your more_tests.rs file and pass + +// Challenge: Create a benchmark that measures the speed of splish(8, 9, 10) +// - Speed up the implementation of splish(8, 9, 10) without breaking the other tests. +// - Hint: See Cargo.toml to get you started