tests and iterators
This commit is contained in:
parent
4f4b6f5194
commit
0b44d9d739
@ -9,18 +9,17 @@
|
|||||||
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach"));// TODO: Step 3
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// Step 1.
|
// Step 1.
|
||||||
// Complete the `capitalize_first` function.
|
// Complete the `capitalize_first` function.
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
@ -15,7 +13,7 @@ pub fn capitalize_first(input: &str) -> String {
|
|||||||
let mut c = input.chars();
|
let mut c = input.chars();
|
||||||
match c.next() {
|
match c.next() {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
Some(first) => ???,
|
Some(first) => first.to_uppercase().to_string() + &input[1..],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +22,7 @@ pub fn capitalize_first(input: &str) -> String {
|
|||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
words.iter().map(|w| capitalize_first(w)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// Step 3.
|
||||||
@ -32,7 +30,7 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
|||||||
// Return a single string.
|
// Return a single string.
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
pub fn capitalize_words_string(words: &[&str]) -> String {
|
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
capitalize_words_vector(words).join("")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -9,8 +9,6 @@
|
|||||||
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum DivisionError {
|
pub enum DivisionError {
|
||||||
NotDivisible(NotDivisibleError),
|
NotDivisible(NotDivisibleError),
|
||||||
@ -26,23 +24,35 @@ pub struct NotDivisibleError {
|
|||||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||||
// Otherwise, return a suitable error.
|
// Otherwise, return a suitable error.
|
||||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||||
todo!();
|
if b == 0 {
|
||||||
|
Err(DivisionError::DivideByZero)
|
||||||
|
}
|
||||||
|
else if a % b == 0 {
|
||||||
|
Ok(a / b)
|
||||||
|
} else {
|
||||||
|
Err(DivisionError::NotDivisible(NotDivisibleError {
|
||||||
|
dividend: a,
|
||||||
|
divisor: b
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// Complete the function and return a value of the correct type so the test
|
||||||
// passes.
|
// passes.
|
||||||
// Desired output: Ok([1, 11, 1426, 3])
|
// Desired output: Ok([1, 11, 1426, 3])
|
||||||
fn result_with_list() -> () {
|
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results: Result<Vec<_>, DivisionError> = numbers.into_iter().map(|n| divide(n, 27)).collect();
|
||||||
|
division_results
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// Complete the function and return a value of the correct type so the test
|
||||||
// passes.
|
// passes.
|
||||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||||
fn list_of_results() -> () {
|
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results: Vec<Result<_, DivisionError>> = numbers.into_iter().map(|n| divide(n, 27)).collect();
|
||||||
|
division_results
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn factorial(num: u64) -> u64 {
|
pub fn factorial(num: u64) -> u64 {
|
||||||
// Complete this function to return the factorial of num
|
// Complete this function to return the factorial of num
|
||||||
// Do not use:
|
// Do not use:
|
||||||
@ -15,6 +13,7 @@ pub fn factorial(num: u64) -> u64 {
|
|||||||
// For an extra challenge, don't use:
|
// For an extra challenge, don't use:
|
||||||
// - recursion
|
// - recursion
|
||||||
// Execute `rustlings hint iterators4` for hints.
|
// Execute `rustlings hint iterators4` for hints.
|
||||||
|
if num == 0 { 1 } else { (1..=num).fold(1, |acc, v| acc * v) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -10,12 +10,10 @@
|
|||||||
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn you_can_assert() {
|
fn you_can_assert() {
|
||||||
assert!();
|
assert!(1 == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,10 @@
|
|||||||
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn you_can_assert_eq() {
|
fn you_can_assert_eq() {
|
||||||
assert_eq!();
|
assert_eq!(1, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn is_even(num: i32) -> bool {
|
pub fn is_even(num: i32) -> bool {
|
||||||
num % 2 == 0
|
num % 2 == 0
|
||||||
}
|
}
|
||||||
@ -19,11 +17,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn is_true_when_even() {
|
fn is_true_when_even() {
|
||||||
assert!();
|
assert!(is_even(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn is_false_when_odd() {
|
fn is_false_when_odd() {
|
||||||
assert!();
|
assert!(!is_even(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
width: i32,
|
width: i32,
|
||||||
height: i32
|
height: i32
|
||||||
@ -30,17 +28,19 @@ mod tests {
|
|||||||
fn correct_width_and_height() {
|
fn correct_width_and_height() {
|
||||||
// This test should check if the rectangle is the size that we pass into its constructor
|
// This test should check if the rectangle is the size that we pass into its constructor
|
||||||
let rect = Rectangle::new(10, 20);
|
let rect = Rectangle::new(10, 20);
|
||||||
assert_eq!(???, 10); // check width
|
assert_eq!(rect.width, 10); // check width
|
||||||
assert_eq!(???, 20); // check height
|
assert_eq!(rect.height, 20); // check height
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn negative_width() {
|
fn negative_width() {
|
||||||
// This test should check if program panics when we try to create rectangle with negative width
|
// This test should check if program panics when we try to create rectangle with negative width
|
||||||
let _rect = Rectangle::new(-10, 10);
|
let _rect = Rectangle::new(-10, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn negative_height() {
|
fn negative_height() {
|
||||||
// This test should check if program panics when we try to create rectangle with negative height
|
// This test should check if program panics when we try to create rectangle with negative height
|
||||||
let _rect = Rectangle::new(10, -10);
|
let _rect = Rectangle::new(10, -10);
|
||||||
|
Loading…
Reference in New Issue
Block a user