ch11.1 done
This commit is contained in:
parent
b571a7be8b
commit
b433eb19d2
@ -1,15 +1,141 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
|
||||
pub fn add_two(a: usize) -> usize {
|
||||
a + 2
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// #[test] indicates this is a test function
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
fn exploration() {
|
||||
// let result = add(2, 2);
|
||||
assert_eq!(add(2, 2), 4);
|
||||
}
|
||||
|
||||
// comment this out for now. There is a way to
|
||||
// specify which tests are wanted
|
||||
// #[test]
|
||||
// fn another() {
|
||||
// panic!("Make this test fail!");
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn larger_can_hold_smaller() {
|
||||
let larger = Rectangle {
|
||||
width: 8,
|
||||
height: 7,
|
||||
};
|
||||
let smaller = Rectangle {
|
||||
width: 5,
|
||||
height: 1,
|
||||
};
|
||||
|
||||
assert!(larger.can_hold(&smaller));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smaller_can_hold_larger() {
|
||||
let larger = Rectangle {
|
||||
width: 8,
|
||||
height: 7,
|
||||
};
|
||||
let smaller = Rectangle {
|
||||
width: 5,
|
||||
height: 1,
|
||||
};
|
||||
|
||||
assert!(!smaller.can_hold(&larger));
|
||||
}
|
||||
|
||||
// rust has assert_eq!() and assert_ne!() for
|
||||
// testing
|
||||
#[test]
|
||||
fn it_adds_two() {
|
||||
let result = add_two(2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_contains_name() {
|
||||
// any String without Carol will fail here
|
||||
let string = String::from("Carol");
|
||||
assert!(
|
||||
string.contains("Carol"),
|
||||
"Greeting did not contain name, value was `{string}`"
|
||||
);
|
||||
}
|
||||
|
||||
// important to check code handles error conditions properly
|
||||
// possible with the should_panic attribute
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn greater_than_100() {
|
||||
Guess::new(200);
|
||||
}
|
||||
|
||||
// Test for a should panic with an explicit message
|
||||
// Tests to see if the panic message contains the
|
||||
// string inside of the expected parameter
|
||||
#[test]
|
||||
#[should_panic(expected = "less than or equal to 100")]
|
||||
fn greater_than_100_precise() {
|
||||
Guess::new_precise(200);
|
||||
}
|
||||
|
||||
// Can use the Result type in tests
|
||||
#[test]
|
||||
fn it_works() -> Result<(), String> {
|
||||
let result = add(2, 2);
|
||||
|
||||
if result == 4 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(String::from("two plus two does not equal four"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pulled from chapter 5
|
||||
#[derive(Debug)]
|
||||
struct Rectangle {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl Rectangle {
|
||||
// purposefully introduce a bug into the code. Swapped the first < with >
|
||||
fn can_hold(&self, other: &Rectangle) -> bool {
|
||||
self.width > other.width && self.height > other.height
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Guess {
|
||||
value: i32,
|
||||
}
|
||||
|
||||
impl Guess {
|
||||
pub fn new(value: i32) -> Guess {
|
||||
// introduce a bug into the code where remove the `|| value > 100`
|
||||
// meaning this code will not panic
|
||||
if value < 1 || value > 100 {
|
||||
panic!("Guess value must be between 1 and 100, got {value}.");
|
||||
}
|
||||
|
||||
Guess { value }
|
||||
}
|
||||
|
||||
pub fn new_precise(value: i32) -> Guess {
|
||||
if value < 1 {
|
||||
panic!("Guess value must be greater than or equal to 1, got {value}.");
|
||||
} else if value > 100 {
|
||||
panic!("Guess value must be less than or equal to 100, got {value}.");
|
||||
}
|
||||
|
||||
Guess { value }
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user