Add example files
This commit is contained in:
14
examples/hello/Cargo.toml
Normal file
14
examples/hello/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "hello"
|
||||
version = "0.1.0"
|
||||
authors = ["Nathan Stocks <nathan@agileperception.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
|
||||
[[bench]]
|
||||
name = "snuggle_speed"
|
||||
harness = false
|
||||
9
examples/hello/benches/snuggle_speed.rs
Normal file
9
examples/hello/benches/snuggle_speed.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use hello::snuggle;
|
||||
|
||||
pub fn snuggle_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("snuggle 2", |b| b.iter(|| snuggle(black_box(2))));
|
||||
}
|
||||
|
||||
criterion_group!(benches, snuggle_benchmark);
|
||||
criterion_main!(benches);
|
||||
35
examples/hello/src/lib.rs
Normal file
35
examples/hello/src/lib.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use hello::snuggle;
|
||||
/// let bunnies = snuggle(5);
|
||||
/// assert_eq!(bunnies, 40);
|
||||
/// ```
|
||||
pub fn snuggle(bunnies: u128) -> u128 {
|
||||
bunnies << 3
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::num::ParseIntError;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn snuggling_bunnies_multiply() {
|
||||
assert_eq!(snuggle(2), 16);
|
||||
}
|
||||
|
||||
#[should_panic]
|
||||
#[test]
|
||||
fn scared_bunny() {
|
||||
panic!("Hop hoppity hop!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bunny_result() -> Result<(), ParseIntError> {
|
||||
let num_bunnies: u64 = "4".parse()?;
|
||||
assert_eq!(num_bunnies, 4);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
10
examples/hello/src/main.rs
Normal file
10
examples/hello/src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn main() {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
#[test]
|
||||
fn the_bin_test() {
|
||||
assert_eq!(1 + 1, 2);
|
||||
}
|
||||
}
|
||||
6
examples/hello/tests/anything.rs
Normal file
6
examples/hello/tests/anything.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use hello::snuggle;
|
||||
|
||||
#[test]
|
||||
fn it_works_from_outside() {
|
||||
assert!(snuggle(4) == 32);
|
||||
}
|
||||
Reference in New Issue
Block a user