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
183
//! # Rusty Helloworld
//!
//! `rusty_helloworld` is a collection of code that I have written over my journey of learning Rust.

mod structure;
mod iters;

pub use self::kinds::PrimaryColor;
pub use self::kinds::SecondaryColor;
pub use self::utils::mix;


pub fn adds_two(a: i32) -> i32{
    println!("The value passed: {}", a);    // The print statement only shows up for failed tests. To sohow the print statement for passed tests also, pass "cargo test -- --show-output"
    a + 2
}

#[cfg(test)]
mod tests {
    use crate::{structure::Rectangle, iters::{Iterator, self}};

    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }

    // #[test]
    // fn failed_test() {
    //     panic!("Make this test fail");
    // }

    #[test]
    fn larger_can_fit_smaller(){
        let larger = Rectangle {
            width: 8,
            height: 7,
        };
        let smaller = Rectangle {
            width: 5,
            height: 1,
        };

        assert!(larger.can_hold(&smaller));
        assert!(!smaller.can_hold(&larger), "The first rectangle: {:#?} cannot store the other one: {:#?}", smaller, larger);
        // If one fails, the test fails
    }

    #[test]
    fn it_adds_two(){
        assert_eq!(4, super::adds_two(2), "Number given + 2 != 4"); // custom error message
    }

    #[test]
    #[should_panic(expected = "Make this test fail")]
    fn it_should_panic(){
        panic!("Make this test fail");
        // panic!("Make this fail");    the test will fail even after the "should panic", because the expected panic message is: "Make this test fail", any other panic message returned counts as a fail
    }

    // Run specific tests using "cargo test test_name"
    // If multiple functions run with same subname, use "cargo test common_subname"
    // Run tests from different modules using "cargo test module_name::"


    #[test]
    #[ignore]
    fn expensive_test(){
        // This test will run only when "cargo test -- --ignored" is run
        // To run only ignored ones, and run them sequentially, use "cargo test -- --ignored --test-threads=1"
        // To run only ignored ones, and run them sequentially, and show output, use "cargo test -- --ignored --test-threads=1 --show-output"
        assert!(true);
    }

    #[test]
    fn Iterator_demons(){
        let v = vec![1, 2, 3];

        let mut v_iter = v.iter();
        // let mut v_iter = v.iter_mut();  // To get mutable references
        // let mut v_into_iter = v.into_iter();    // To get ownership of the vector and return owned values, to execute this -> remove "&".

        assert_eq!(v_iter.next(), Some(&1));
        assert_eq!(v_iter.next(), Some(&2));
        assert_eq!(v_iter.next(), Some(&3));
        assert_eq!(v_iter.next(), None);    // None is returned when the iterator reaches the end of the collection
    }

    #[test]
    fn iterator_sum(){
        let v = vec![1, 2, 3];

        let v_iter = v.iter();

        let total: i32 = v_iter.sum();  // sum() is a method on the Iterator trait, and can be used only on iterators. Runs till the end.
        assert_eq!(total, 6);
    }

    #[test]
    fn filter_by_size(){
        use super::*;
        let shoes = vec![
            iters::Shoe{size: 10, style: String::from("sneaker")},
            iters::Shoe{size: 13, style: String::from("sandal")},
            iters::Shoe{size: 10, style: String::from("boot")}
        ];

        let in_my_size = iters::shoes_in_my_size(shoes, 10);
        assert_eq!(in_my_size, vec![
            iters::Shoe{size: 10, style: String::from("sneaker")},
            iters::Shoe{size: 10, style: String::from("boot")}
        ])
    }

    #[test]
    fn counter_iterator(){
        let mut counter = iters::Counter::new();

        assert_eq!(counter.next(), Some(1));
        assert_eq!(counter.next(), Some(2));
        assert_eq!(counter.next(), Some(3));
        assert_eq!(counter.next(), Some(4));
        assert_eq!(counter.next(), Some(5));

        assert_eq!(counter.next(), None);
    }

    // #[test]
    // fn counter_iterator_sum(){
    //     let sum: u32 = iters::Counter::new().zip(iters::Counter::new().skip(1))
    //         .map(|(a, b)| a * b)
    //         .filter(|x| x % 3 == 0)
    //         .sum();
        
    //     assert_eq!(18, sum);
    // }
    // Messed up test(to be fixed)
}


//* Documenting a function
/// Adds one to the given number
/// 
/// # Examples
/// 
/// ```
/// let arg = 5;
/// let ans = rusty_helloworld::add_one(arg);
/// 
/// assert_eq!(6, ans);
/// ```
pub fn add_one(x: i32) -> i32{
    x + 1
}


/*
//! # Art
//! 
//! A library for modelling artistic concepts.
*/

pub mod kinds{
    /// The primary colors according to the RYB color model
    pub enum PrimaryColor{
        Red, Yellow, Blue
    }
    
    /// The secondary colors accoding to the RYB color model
    pub enum SecondaryColor{
        Orange, Green, Purple
    }
}
pub mod utils{
    use crate::kinds::*;
    /// Combines two PrimaryColors into one in equal amounts to create a secondary color.
    
    pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor{
        //  ANCHOR_END: here
        SecondaryColor::Orange
        // ANCHOR: here
    }
}