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
184
185
186
187
188

pub mod linked_list;
use crate::linked_list::LinkedList;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_init_list() {
        let list: LinkedList<i32> = LinkedList::init_list();
        assert!(list.is_empty(), "Newly initialized list should be empty");
    }

    #[test]
    fn test_append_front() {
        let mut list = LinkedList::init_list();
        list.append_front(1000);
        list.append_front(55);
        assert_eq!(list.get_head(), Some(&55), "Front of list should be the most recently appended element");
        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
    }


    #[test]
    fn test_append_back() {
        let mut list = LinkedList::init_list();
        list.append_back(37);
        list.append_back(33);
        assert_eq!(list.get_at_index(1), Some(&33), "End of list should be most recently appeneded element");
        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
    }

    #[test]
    fn test_insert_at_index() {
        let mut list = LinkedList::init_list();
        list.insert_at_index(22, 0);
        list.insert_at_index(10, 1);
        list.insert_at_index(23, 1);
        list.insert_at_index(47000, 6500);
        assert_eq!(list.get_at_index(1), Some(&23), "Indexing should adjust as we insert new elements in the middle");
        assert_eq!(list.get_length(), 4, "Length should be adjusted properly");
    }

    #[test]
    fn test_delete_front() {
        let mut list = LinkedList::init_list();
        list.append_front(22);
        list.append_front(34);
        list.delete_front();
        assert_eq!(list.get_head(), Some(&22), "Deletion should occur at the front of the list");
        assert_eq!(list.get_length(), 1, "Length should be adjusted properly");
    }

    #[test]
    fn test_delete_back() {
        let mut list = LinkedList::init_list();
        list.append_back(1000);
        list.append_back(100054);
        list.delete_back();
        assert_eq!(list.get_at_index(1), None, "Index should no longer exist if deletion done properly");
        assert_eq!(list.get_length(), 1, "Length should be adjusted properly");
    }

    #[test]
    fn test_delete_at_index() {
        let mut list = LinkedList::init_list();
        list.append_front(10);
        list.append_back(20);
        list.append_back(30);
        list.delete_at_index(1);
        assert_eq!(list.get_at_index(1), Some(&30), "Deletion not occuring properly");
        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
    }

    #[test]
    fn test_get_head() {
        let mut list = LinkedList::init_list();
        list.append_front(33);
        list.append_back(57);
        assert_eq!(list.get_head(), Some(&33), "Returned head is incorrect");

    }

    #[test]
    fn test_get_head_as_mut() {
        let mut list = LinkedList::init_list();
        list.append_front(33);
        list.append_back(57);
        if let Some(val) = list.get_head_as_mut() {
            *val = 10000;
        }
        assert_eq!(list.get_head(), Some(&10000), "Should be able to mutate the element at head");
    }

    #[test]
    fn test_get_tail_ref() {
        let mut list: LinkedList<i32> = LinkedList::init_list();
        list.append_back(33);
        list.append_back(57);
        list.append_front(31);
        assert_eq!(list.get_tail_ref(), Some(&57), "Unable to retrieve correct tail reference");
        list.delete_back();
        assert_eq!(list.get_tail_ref(), Some(&33), "Unable to retrieve correct tail reference");

    }

    #[test]
    fn test_get_tail_ref_mut() {
        let mut list: LinkedList<i32> = LinkedList::init_list();
        list.append_back(99);
        list.append_front(-31);
        list.append_front(-33);
        if let Some(my_var) = list.get_tail_ref_mut() {
            *my_var = -10000;
        }
        assert_eq!(list.get_tail_ref(), Some(&-10000), "Mutable reference to tail not updating properly");
    }

    #[test]
    fn test_get_at_index() {
        let mut list = LinkedList::init_list();
        list.append_front("Hello");
        list.append_front("Goodbye");
        assert_eq!(list.get_at_index(1), Some(&"Hello"), "Not returning the correct element");
        assert_eq!(list.get_length(), 2, "Incorrect length");
    }

    #[test]
    fn test_get_mutable_ref_at_index() {
        let mut list = LinkedList::init_list();
        list.append_front(0xffff);
        list.append_front(0xcec1);
        if let Some(test_var) = list.get_mutable_ref_at_index(1) {
            *test_var = 0x0001;
        }
        assert_eq!(list.get_at_index(1), Some(&0x0001), "Mutable reference not being returned properly")
    }

    #[test]
    fn test_get_length() {
        let mut list = LinkedList::init_list();
        list.append_front(33);
        list.append_front(54);
        list.append_front(65);
        assert_eq!(list.get_length(), 3, "Length incorrect");
        list.delete_back();
        assert_eq!(list.get_length(), 2, "Length incorrect");
    }

    #[test]
    fn test_is_empty() {
        let mut list = LinkedList::init_list();
        list.append_front(65);
        assert_eq!(list.is_empty(), false, "List reported as empty when non-empty");
        list.delete_front();
        assert_eq!(list.is_empty(), true, "List reported as non-empty when empty")
    }

    #[test]
    fn test_clear() {
        let mut list = LinkedList::init_list();
        list.insert_at_index(22, 0);
        list.insert_at_index(10, 1);
        list.clear();
        assert!(list.is_empty(), "List should be empty after clearing");
    }

    #[test]
    fn test_contains() {
        let mut list = LinkedList::init_list();
        list.append_back(33);
        list.append_back(37);
        assert_eq!(list.contains(33), true, "Not reporting entry in list");
        assert_eq!(list.contains(10000), false, "Falsely reporting an entry in the list");
    }

    #[test]
    fn test_find() {
        let mut list = LinkedList::init_list();
        list.append_back("Daniel");
        list.append_back("George");
        list.append_back("Larry");
        assert_eq!(list.find("George"), Some(1), "Not reporting the proper index in which an item currently exists");
        assert_eq!(list.find("John"), None, "Incorrectly reporting an index for an item that should not be there");
    }
    
}