From 52851af20aa17e7d16dc44e289531cd64ab0a484 Mon Sep 17 00:00:00 2001 From: Augusto Date: Fri, 25 Dec 2020 20:12:23 +0000 Subject: [PATCH] Add exercise `ownership`'s subject and test --- rust/tests/ownership_test/Cargo.lock | 12 +++++ rust/tests/ownership_test/Cargo.toml | 10 +++++ rust/tests/ownership_test/src/main.rs | 64 +++++++++++++++++++++++++++ subjects/ownership/README.md | 43 ++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 rust/tests/ownership_test/Cargo.lock create mode 100644 rust/tests/ownership_test/Cargo.toml create mode 100644 rust/tests/ownership_test/src/main.rs create mode 100644 subjects/ownership/README.md diff --git a/rust/tests/ownership_test/Cargo.lock b/rust/tests/ownership_test/Cargo.lock new file mode 100644 index 00000000..9cf48260 --- /dev/null +++ b/rust/tests/ownership_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ownership" +version = "0.1.0" + +[[package]] +name = "ownership_test" +version = "0.1.0" +dependencies = [ + "ownership", +] diff --git a/rust/tests/ownership_test/Cargo.toml b/rust/tests/ownership_test/Cargo.toml new file mode 100644 index 00000000..a724362b --- /dev/null +++ b/rust/tests/ownership_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ownership_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ownership = { path = "../../../../rust-piscine-solutions/ownership"} diff --git a/rust/tests/ownership_test/src/main.rs b/rust/tests/ownership_test/src/main.rs new file mode 100644 index 00000000..ebaeef61 --- /dev/null +++ b/rust/tests/ownership_test/src/main.rs @@ -0,0 +1,64 @@ +/* +## ownership + +### Instruction + +Create a function that takes ownership of a string and returns the +first sub-word in it. It should work for camelCase as well as snake_case +first_subword(camelCase) returns camel +first_subword(snake_case) returns snake + +And fix the printing expression so the code works + +### +*/ + +use ownership::*; + +fn main() { + let s1 = String::from("helloWorld"); + let s2 = String::from("snake_case"); + let s3 = String::from("CamelCase"); + let s4 = String::from("just"); + + println!("first_subword({}) = {}", s1.clone(), first_subword(s1)); // Must print first_subword(helloWorld) = hello + println!("first_subword({}) = {}", s2.clone(), first_subword(s2)); // Must print first_subword(snake_case) = snake + println!("first_subword({}) = {}", s3.clone(), first_subword(s3)); // Must print first_subword(CamelCase) = Camel + println!("first_subword({}) = {}", s4.clone(), first_subword(s4)); // Must print first_subword(just) = just +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn first_subword_test() { + struct TstString<'a> { + str: String, + l: &'a str, + } + + let o_tsts = vec![ + TstString { + str: "helloWorld".to_string(), + l: "hello", + }, + TstString { + str: "how_you".to_string(), + l: "how", + }, + TstString { + str: "Changeyou".to_string(), + l: "Changeyou", + }, + TstString { + str: "CamelCase".to_string(), + l: "Camel", + }, + ]; + + for t in o_tsts.iter() { + assert_eq!(t.l.to_string(), first_subword(t.str.clone())); + } + } +} diff --git a/subjects/ownership/README.md b/subjects/ownership/README.md new file mode 100644 index 00000000..6a5eda1f --- /dev/null +++ b/subjects/ownership/README.md @@ -0,0 +1,43 @@ +## ownership + +### Instruction + +- Create a function that takes ownership of a string and returns the first sub-word in it + +- It should work for `camelCase` as well as `snake_case` + +### Expected Function + +```rust +pub fn first_subword(mut s: String) -> String { +} +``` + +### Usage + +Here is a program to test your function + +```rust +fn main() { + let s1 = String::from("helloWorld"); + let s2 = String::from("snake_case"); + let s3 = String::from("CamelCase"); + let s4 = String::from("just"); + + println!("first_subword({}) = {}", s1.clone(), first_subword(s1)); + println!("first_subword({}) = {}", s2.clone(), first_subword(s2)); + println!("first_subword({}) = {}", s3.clone(), first_subword(s3)); + println!("first_subword({}) = {}", s4.clone(), first_subword(s4)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +first_subword(helloWorld) = hello +first_subword(snake_case) = snake +first_subword(CamelCase) = Camel +first_subword(just) = just +student@ubuntu:~/[[ROOT]]/test$ +```