use safe_linked_list::LinkedList; #[test] fn test_new() { let list: LinkedList = LinkedList::new(); assert_eq!(list.len(), 0); } #[test] fn test_push_back() { let mut list = LinkedList::new(); list.push_back(3); list.push_back(7); list.push_back(8); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![3, 7, 8]); } #[test] fn test_from() { let list = LinkedList::from(&[3, 6, 9]); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![3, 6, 9]); } #[test] fn test_push_front() { let mut list = LinkedList::new(); list.push_front(3); list.push_front(7); list.push_front(8); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![8, 7, 3]); } #[test] fn test_len() { let list = LinkedList::from(&[4, 5, 6, 7]); assert_eq!(list.len(), 4); } #[test] fn test_is_empty() { let list1: LinkedList = LinkedList::from(&[]); assert!(list1.is_empty()); let mut list2 = LinkedList::new(); list2.push_back(8); list2.push_front(7); list2.pop_back(); list2.pop_front(); assert!(list2.is_empty()); } #[test] fn test_list_of_string() { let words = vec![ String::from("Hello"), String::from("my"), String::from("pigs"), ]; let list = LinkedList::from(&words); let retrieved_words: Vec<_> = list.into_iter().collect(); assert_eq!(retrieved_words, words); } #[test] fn test_push() { let mut list = LinkedList::new(); list.push_back(8); list.push_front(37); list.push_front(6); list.push_back(5); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![6, 37, 8, 5]); } #[test] fn test_pop_back() { let mut list = LinkedList::from(&[1, 2, 3, 4, 5]); assert_eq!(list.pop_back(), Some(5)); assert_eq!(list.pop_back(), Some(4)); assert_eq!(list.pop_back(), Some(3)); assert_eq!(list.pop_back(), Some(2)); assert_eq!(list.pop_back(), Some(1)); assert_eq!(list.pop_back(), None); } #[test] fn test_pop_front() { let mut list = LinkedList::from(&[1, 2, 3, 4, 5]); assert_eq!(list.pop_front(), Some(1)); assert_eq!(list.pop_front(), Some(2)); assert_eq!(list.pop_front(), Some(3)); assert_eq!(list.pop_front(), Some(4)); assert_eq!(list.pop_front(), Some(5)); assert_eq!(list.pop_front(), None); } #[test] fn test_pop() { let mut list = LinkedList::from(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); assert_eq!(list.pop_back(), Some(10)); assert_eq!(list.pop_back(), Some(9)); assert_eq!(list.pop_front(), Some(1)); assert_eq!(list.pop_front(), Some(2)); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![3, 4, 5, 6, 7, 8]); } #[test] fn test_push_pop() { let mut list = LinkedList::from(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); list.push_back(11); assert_eq!(list.pop_front(), Some(1)); assert_eq!(list.pop_back(), Some(11)); list.push_front(6); list.push_back(8); assert_eq!(list.pop_front(), Some(6)); assert_eq!(list.pop_back(), Some(8)); let vec: Vec<_> = list.into_iter().collect(); assert_eq!(vec, vec![2, 3, 4, 5, 6, 7, 8, 9, 10]); } #[test] fn test_iter() { let list1 = LinkedList::from(&[1, 2, 3, 4, 5, 6]); let mut vec1 = Vec::new(); for item in &list1 { vec1.push(*item.borrow()); } assert_eq!(vec1, vec![1, 2, 3, 4, 5, 6]); for item in &list1 { *(item.borrow_mut()) *= 2 } let vec2: Vec<_> = list1.into_iter().collect(); assert_eq!(vec2, vec![2, 4, 6, 8, 10, 12]); } #[test] fn test_iter_2() { let list = LinkedList::from(&[1, 7, 11, 2, 3]); for elem in &list { *(elem.borrow_mut()) += 1; } let mut vec = Vec::new(); for elem in &list { vec.push(*(elem.borrow())) } assert_eq!(vec, vec![2, 8, 12, 3, 4]); } #[test] fn test_display() { let list1: LinkedList = LinkedList::new(); assert_eq!(format!("{}", list1), "None"); let list2 = LinkedList::from(&[1, 2, 3, 4]); assert_eq!(format!("{}", list2), "1 -> 2 -> 3 -> 4 -> None"); } #[test] fn test_append() { let mut list1 = LinkedList::from(&[3, 6, 9]); let mut list2 = LinkedList::from(&[9, 7, 6, 5]); list1.append(&mut list2); assert_eq!(list2.len(), 0); assert_eq!( list1.into_iter().collect::>(), vec![3, 6, 9, 9, 7, 6, 5] ); let mut list3 = LinkedList::from(&[8, 7, 6]); let mut list4 = LinkedList::from(&[9]); list3.append(&mut list4); assert_eq!(list3.into_iter().collect::>(), vec![8, 7, 6, 9]) } #[test] fn test_clear() { let mut list = LinkedList::from(&[1, 2, 3]); list.clear(); assert_eq!(list.into_iter().collect::>(), vec![]); } #[test] fn test_contains() { let list = LinkedList::from(&[1, 2, 3, 4]); assert!(list.contains(&3)); assert!(!list.contains(&8)); } #[test] fn test_front_back() { let mut list = LinkedList::from(&[1, 2, 3, 4]); assert_eq!(*list.front().unwrap().borrow(), 1); assert_eq!(*list.back().unwrap().borrow(), 4); list.clear(); assert_eq!(list.front(), None); assert_eq!(list.back(), None); } #[test] fn test_clone() { let list = LinkedList::from(&[1, 2, 3, 4]); let list2 = list.clone(); assert_eq!(list2.into_iter().collect::>(), vec![1, 2, 3, 4]); } #[test] fn test_eq() { let list1 = LinkedList::from(&[4, 6, 8, 1]); let list2 = list1.clone(); let list3 = LinkedList::from(&[8, 7, 6, 5]); assert!(list1 == list2); assert!(list2 != list3); }