From 06355b050b3960c6b279ed1d5dc85eabd9f3a629 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 26 Dec 2020 21:07:27 +0000 Subject: [PATCH] Add subject and test for the exercise `name_initials` --- rust/tests/name_initials_test/Cargo.lock | 93 ++++++++++++++++ rust/tests/name_initials_test/Cargo.toml | 12 ++ rust/tests/name_initials_test/src/main.rs | 128 ++++++++++++++++++++++ subjects/name_initials/README.md | 41 +++++++ 4 files changed, 274 insertions(+) create mode 100644 rust/tests/name_initials_test/Cargo.lock create mode 100644 rust/tests/name_initials_test/Cargo.toml create mode 100644 rust/tests/name_initials_test/src/main.rs create mode 100644 subjects/name_initials/README.md diff --git a/rust/tests/name_initials_test/Cargo.lock b/rust/tests/name_initials_test/Cargo.lock new file mode 100644 index 00000000..75c4b369 --- /dev/null +++ b/rust/tests/name_initials_test/Cargo.lock @@ -0,0 +1,93 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cc" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15" + +[[package]] +name = "fs_extra" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" + +[[package]] +name = "jemalloc-ctl" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c502a5ff9dd2924f1ed32ba96e3b65735d837b4bfd978d3161b1702e66aca4b7" +dependencies = [ + "jemalloc-sys", + "libc", + "paste", +] + +[[package]] +name = "jemalloc-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45" +dependencies = [ + "cc", + "fs_extra", + "libc", +] + +[[package]] +name = "jemallocator" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69" +dependencies = [ + "jemalloc-sys", + "libc", +] + +[[package]] +name = "libc" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" + +[[package]] +name = "name_initials" +version = "0.1.0" +dependencies = [ + "jemalloc-ctl", + "jemallocator", +] + +[[package]] +name = "name_initials_test" +version = "0.1.0" +dependencies = [ + "jemalloc-ctl", + "jemallocator", + "name_initials", +] + +[[package]] +name = "paste" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" +dependencies = [ + "paste-impl", + "proc-macro-hack", +] + +[[package]] +name = "paste-impl" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" +dependencies = [ + "proc-macro-hack", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" diff --git a/rust/tests/name_initials_test/Cargo.toml b/rust/tests/name_initials_test/Cargo.toml new file mode 100644 index 00000000..4f40145b --- /dev/null +++ b/rust/tests/name_initials_test/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "name_initials_test" +version = "0.1.0" +authors = ["lee "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +jemalloc-ctl = "0.3.3" +jemallocator = "0.3.2" +name_initials = { path = "../../../../rust-piscine-solutions/name_initials"} \ No newline at end of file diff --git a/rust/tests/name_initials_test/src/main.rs b/rust/tests/name_initials_test/src/main.rs new file mode 100644 index 00000000..1fe75427 --- /dev/null +++ b/rust/tests/name_initials_test/src/main.rs @@ -0,0 +1,128 @@ +/* +## name_initials + +### Instructions + +Create a function called `initials`, this function will receive a vector of string literals +with names and return a vector of Strings with the initials of each name. + +### Example: + +```rust +``` + +> This exercise will test the **heap allocation** of your function! +> So try your best to allocate the minimum data on the heap! + +### Notions + +- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html + +*/ + +#[global_allocator] +static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + +#[allow(unused_imports)] +use name_initials::*; + +#[allow(dead_code)] +fn main() { + let mut names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"]; + println!("{:?}", initials(&mut names)); + // output: ["H. P.", "S. E.", "J. L.", "B. O."] +} + +#[allow(unused_imports)] +use jemalloc_ctl::{epoch, stats}; + +#[allow(dead_code)] +struct Test<'a> { + names: Vec<&'a str>, + result: Vec<&'a str>, +} + +// solution that will run against the students solution +// this function uses the less heap allocation +#[allow(dead_code)] +fn initials_sol(arr: &mut Vec<&str>) -> Vec { + arr.iter() + .map(|ele| { + let mut names = ele.split_whitespace(); + let mut a = names.next().unwrap().chars().nth(0).unwrap().to_string(); + a.push_str(". "); + let mut b = names.next().unwrap().chars().nth(0).unwrap().to_string(); + b.push_str("."); + a.push_str(&b); + a + }) + .collect() +} + +#[test] +fn test_memory_allocation() { + // the statistics tracked by jemalloc are cached + // The epoch controls when they are refreshed + let e = epoch::mib().unwrap(); + // allocated: number of bytes allocated by the application + let allocated = stats::allocated::mib().unwrap(); + let mut test_value = vec![ + "Lee Silva", + "Harry Potter", + "Someone Else", + "J. L.", + "Barack Obama", + ]; + + initials_sol(&mut test_value); + // this will advance with the epoch giving the its old value + // where we read the updated heap allocation using the `allocated.read()` + e.advance().unwrap(); + let solution = allocated.read().unwrap(); + + initials(&mut test_value); + e.advance().unwrap(); + let student = allocated.read().unwrap(); + + assert!( + student <= solution, + format!( + "your heap allocation is {}, and it must be less or equal to {}", + student, solution + ) + ); +} + +#[test] +fn test_function() { + let cases = vec![ + Test { + names: vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"], + result: vec!["H. P.", "S. E.", "J. L.", "B. O."], + }, + Test { + names: vec![ + "James John", + "David Joseph", + "Matthew Brian", + "Jacob Sousa", + "Bruce Banner", + "Scarlett Johansson", + "Graydon Hoare", + ], + result: vec![ + "J. J.", "D. J.", "M. B.", "J. S.", "B. B.", "S. J.", "G. H.", + ], + }, + ]; + + for mut v in cases { + assert_eq!( + initials(&mut v.names), + v.result + .iter() + .map(|ele| ele.to_string()) + .collect::>() + ); + } +} diff --git a/subjects/name_initials/README.md b/subjects/name_initials/README.md new file mode 100644 index 00000000..e52fb636 --- /dev/null +++ b/subjects/name_initials/README.md @@ -0,0 +1,41 @@ +## name_initials + +### Instructions + +Create a function called `initials`, this function will receive a vector of string literals +with names and return a vector of Strings with the initials of each name. + +> This exercise will test the **heap allocation** of your function! +> So try your best to allocate the minimum data on the heap! + +### Notions + +- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html + +### Expected Function + +```rust +fn initials(names: &mut Vec<&str>) -> Vec { +} +``` + +### Usage + +Here is a program to test your function + +```rust +use name_initials::initials; + +fn main() { + let names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"] + println!("{:?}", initials(names)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +["H. P.", "S. E.", "J. L.", "B. O."] +student@ubuntu:~/[[ROOT]]/test$ +```