Add example files

This commit is contained in:
Nathan Stocks
2021-06-12 22:36:24 -06:00
commit 26d8ef8451
20 changed files with 448 additions and 0 deletions

14
examples/hello/Cargo.toml Normal file
View 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

View 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
View 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(())
}
}

View File

@@ -0,0 +1,10 @@
fn main() {}
#[cfg(test)]
mod test {
#[test]
fn the_bin_test() {
assert_eq!(1 + 1, 2);
}
}

View File

@@ -0,0 +1,6 @@
use hello::snuggle;
#[test]
fn it_works_from_outside() {
assert!(snuggle(4) == 32);
}