dgunther2001_linked_list/
lib.rs

1
2pub mod linked_list;
3use crate::linked_list::LinkedList;
4
5#[cfg(test)]
6mod tests {
7    use super::*;
8
9    #[test]
10    fn test_init_list() {
11        let list: LinkedList<i32> = LinkedList::init_list();
12        assert!(list.is_empty(), "Newly initialized list should be empty");
13    }
14
15    #[test]
16    fn test_append_front() {
17        let mut list = LinkedList::init_list();
18        list.append_front(1000);
19        list.append_front(55);
20        assert_eq!(list.get_head(), Some(&55), "Front of list should be the most recently appended element");
21        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
22    }
23
24
25    #[test]
26    fn test_append_back() {
27        let mut list = LinkedList::init_list();
28        list.append_back(37);
29        list.append_back(33);
30        assert_eq!(list.get_at_index(1), Some(&33), "End of list should be most recently appeneded element");
31        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
32    }
33
34    #[test]
35    fn test_insert_at_index() {
36        let mut list = LinkedList::init_list();
37        list.insert_at_index(22, 0);
38        list.insert_at_index(10, 1);
39        list.insert_at_index(23, 1);
40        list.insert_at_index(47000, 6500);
41        assert_eq!(list.get_at_index(1), Some(&23), "Indexing should adjust as we insert new elements in the middle");
42        assert_eq!(list.get_length(), 4, "Length should be adjusted properly");
43    }
44
45    #[test]
46    fn test_delete_front() {
47        let mut list = LinkedList::init_list();
48        list.append_front(22);
49        list.append_front(34);
50        list.delete_front();
51        assert_eq!(list.get_head(), Some(&22), "Deletion should occur at the front of the list");
52        assert_eq!(list.get_length(), 1, "Length should be adjusted properly");
53    }
54
55    #[test]
56    fn test_delete_back() {
57        let mut list = LinkedList::init_list();
58        list.append_back(1000);
59        list.append_back(100054);
60        list.delete_back();
61        assert_eq!(list.get_at_index(1), None, "Index should no longer exist if deletion done properly");
62        assert_eq!(list.get_length(), 1, "Length should be adjusted properly");
63    }
64
65    #[test]
66    fn test_delete_at_index() {
67        let mut list = LinkedList::init_list();
68        list.append_front(10);
69        list.append_back(20);
70        list.append_back(30);
71        list.delete_at_index(1);
72        assert_eq!(list.get_at_index(1), Some(&30), "Deletion not occuring properly");
73        assert_eq!(list.get_length(), 2, "Length should be adjusted properly");
74    }
75
76    #[test]
77    fn test_get_head() {
78        let mut list = LinkedList::init_list();
79        list.append_front(33);
80        list.append_back(57);
81        assert_eq!(list.get_head(), Some(&33), "Returned head is incorrect");
82
83    }
84
85    #[test]
86    fn test_get_head_as_mut() {
87        let mut list = LinkedList::init_list();
88        list.append_front(33);
89        list.append_back(57);
90        if let Some(val) = list.get_head_as_mut() {
91            *val = 10000;
92        }
93        assert_eq!(list.get_head(), Some(&10000), "Should be able to mutate the element at head");
94    }
95
96    #[test]
97    fn test_get_tail_ref() {
98        let mut list: LinkedList<i32> = LinkedList::init_list();
99        list.append_back(33);
100        list.append_back(57);
101        list.append_front(31);
102        assert_eq!(list.get_tail_ref(), Some(&57), "Unable to retrieve correct tail reference");
103        list.delete_back();
104        assert_eq!(list.get_tail_ref(), Some(&33), "Unable to retrieve correct tail reference");
105
106    }
107
108    #[test]
109    fn test_get_tail_ref_mut() {
110        let mut list: LinkedList<i32> = LinkedList::init_list();
111        list.append_back(99);
112        list.append_front(-31);
113        list.append_front(-33);
114        if let Some(my_var) = list.get_tail_ref_mut() {
115            *my_var = -10000;
116        }
117        assert_eq!(list.get_tail_ref(), Some(&-10000), "Mutable reference to tail not updating properly");
118    }
119
120    #[test]
121    fn test_get_at_index() {
122        let mut list = LinkedList::init_list();
123        list.append_front("Hello");
124        list.append_front("Goodbye");
125        assert_eq!(list.get_at_index(1), Some(&"Hello"), "Not returning the correct element");
126        assert_eq!(list.get_length(), 2, "Incorrect length");
127    }
128
129    #[test]
130    fn test_get_mutable_ref_at_index() {
131        let mut list = LinkedList::init_list();
132        list.append_front(0xffff);
133        list.append_front(0xcec1);
134        if let Some(test_var) = list.get_mutable_ref_at_index(1) {
135            *test_var = 0x0001;
136        }
137        assert_eq!(list.get_at_index(1), Some(&0x0001), "Mutable reference not being returned properly")
138    }
139
140    #[test]
141    fn test_get_length() {
142        let mut list = LinkedList::init_list();
143        list.append_front(33);
144        list.append_front(54);
145        list.append_front(65);
146        assert_eq!(list.get_length(), 3, "Length incorrect");
147        list.delete_back();
148        assert_eq!(list.get_length(), 2, "Length incorrect");
149    }
150
151    #[test]
152    fn test_is_empty() {
153        let mut list = LinkedList::init_list();
154        list.append_front(65);
155        assert_eq!(list.is_empty(), false, "List reported as empty when non-empty");
156        list.delete_front();
157        assert_eq!(list.is_empty(), true, "List reported as non-empty when empty")
158    }
159
160    #[test]
161    fn test_clear() {
162        let mut list = LinkedList::init_list();
163        list.insert_at_index(22, 0);
164        list.insert_at_index(10, 1);
165        list.clear();
166        assert!(list.is_empty(), "List should be empty after clearing");
167    }
168
169    #[test]
170    fn test_contains() {
171        let mut list = LinkedList::init_list();
172        list.append_back(33);
173        list.append_back(37);
174        assert_eq!(list.contains(33), true, "Not reporting entry in list");
175        assert_eq!(list.contains(10000), false, "Falsely reporting an entry in the list");
176    }
177
178    #[test]
179    fn test_find() {
180        let mut list = LinkedList::init_list();
181        list.append_back("Daniel");
182        list.append_back("George");
183        list.append_back("Larry");
184        assert_eq!(list.find("George"), Some(1), "Not reporting the proper index in which an item currently exists");
185        assert_eq!(list.find("John"), None, "Incorrectly reporting an index for an item that should not be there");
186    }
187    
188}