Skip to main content

adder_rmt/
lib.rs

1//! # Adder
2//!
3//! `adder` is a collection of utilities to make performing certain
4//! calculations more convenient.
5#[cfg(test)]
6mod tests {
7    use super::*;
8    #[test]
9    fn exploration() {
10        assert_eq!(2 + 2, 4);
11    }
12
13    #[test]
14    fn it_works() -> Result<(), String> {
15        if 2 + 2 == 4 {
16            Ok(())
17        } else {
18            Err(String::from("two plus two does not equal four"))
19        }
20    }
21
22    #[test]
23    #[ignore]
24    fn another() {
25        panic!("Make this test fail");
26    }
27
28    #[test]
29    fn larger_can_hold_smaller() {
30        let larger = Rectangle {
31            width: 8,
32            height: 7,
33        };
34        let smaller = Rectangle {
35            width: 5,
36            height: 1,
37        };
38
39        assert!(larger.can_hold(&smaller));
40    }
41
42    #[test]
43    fn smaller_cannot_hold_larger() {
44        let larger = Rectangle {
45            width: 8,
46            height: 7,
47        };
48        let smaller = Rectangle {
49            width: 5,
50            height: 1,
51        };
52
53        assert!(!smaller.can_hold(&larger));
54    }
55
56    #[test]
57    fn it_adds_two() {
58        assert_eq!(4, add_two(2));
59    }
60
61    #[test]
62    fn greeting_contains_name() {
63        let result = greeting("Carol");
64        assert!(
65            result.contains("Carol"),
66            "Greeting did not contain name, value was '{}'",
67            result
68        );
69    }
70
71    #[test]
72    #[should_panic(expected = "Guess value must be less than or equal to")]
73    fn greater_than_100() {
74        Guess::new(200);
75    }
76}
77
78#[derive(Debug)]
79pub struct Rectangle {
80    width: u32,
81    height: u32,
82}
83
84impl Rectangle {
85    pub fn can_hold(&self, other: &Rectangle) -> bool {
86        self.width > other.width && self.height > other.height
87    }
88}
89
90/// Adds two to the number given.
91///
92///  # Examples
93///
94/// ```
95/// let arg = 5;
96/// let answer = adder_rmt::add_two(arg);
97///
98/// assert_eq!(7, answer);
99/// ```
100pub fn add_two(a: i32) -> i32 {
101    a + 2
102}
103
104pub fn greeting(name: &str) -> String {
105    format!("Hello {}!", name)
106}
107
108pub struct Guess {
109    value: i32,
110}
111
112impl Guess {
113    pub fn new(value: i32) -> Guess {
114        if value < 1 {
115            panic!(
116                "Guess value must be greater than or equal to 1, got {}.",
117                value
118            );
119        } else if value > 100 {
120            panic!(
121                "Guess value must be less than or equal to 100, got {}.",
122                value
123            );
124        }
125
126        Guess { value }
127    }
128}