1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
pub fn add(left: usize, right: usize) -> usize { left + right }
pub fn add_two(a: i32) -> i32 { a + 2 }
#[derive(Debug)] pub struct Rectangle { width: u32, height: u32, }
impl Rectangle { pub fn can_hold(&self, other: &Rectangle) -> bool { self.width < other.width && self.height > other.height } }
pub fn greeting(name: &str) -> String { format!("Hello {}!", name) }
pub struct Guess { value: u32, }
impl Guess { pub fn new(value: u32) -> 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 } } }
fn prints_and_returns_10(a: i32) -> i32 { println!("I got the value {}", a); 10 }
pub fn add_three(a: i32) -> i32 { internal_adder(a, 3) }
fn internal_adder(a: i32, b: i32) -> i32 { a + b }
#[cfg(test)] mod tests { use super::*;
#[test] fn it_works() { let result = add(2, 2); assert_eq!(result, 4); assert_ne!(result, 5); }
#[test] fn it_works2() -> Result<(), String> { if add(2, 2) == 4 { Ok(()) } else { Err(String::from("two plus two does not equal four")) } }
#[test] fn larger_can_hold_smaller() { let larger = Rectangle { width: 10, height: 8, }; let smaller = Rectangle { width: 5, height: 4, }; assert!(!larger.can_hold(&smaller), "smaller can hold larger"); }
#[test] fn greeting_contains_name() { let result = greeting("Carol"); assert!( result.contains("Carol"), "Greeting did not contain name, value was `{}`", result ); }
#[test] #[should_panic(expected = "Guess value must be less than or equal to 100")] fn greater_than_100() { Guess::new(200); }
#[test] fn this_test_will_pass(){ let value = prints_and_returns_10(4); assert_eq!(10, value); }
#[test]
#[test] fn add_two_and_two(){ assert_eq!(4, add_two(2)); }
#[test] fn add_three_and_two(){ assert_eq!(5, add_two(3)); }
#[test] fn one_hundred(){ assert_eq!(102, add_two(100)); }
#[test] #[ignore] fn expensive_test(){ assert_eq!(1+1+1+1+1+1+1+1+1+1, 10); }
#[test] fn internal(){ assert_eq!(5, internal_adder(2, 3)); }
}
|