()
| 28 | |
| 29 | #[test] |
| 30 | fn t_has_cycle() { |
| 31 | let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 32 | let mut ll = LinkedList::default(); |
| 33 | for v in data { |
| 34 | ll.push_back(v); |
| 35 | } |
| 36 | |
| 37 | //no cycle |
| 38 | assert!(!has_cycle(ll.head)); |
| 39 | |
| 40 | //create cycle by hand |
| 41 | let mut tail = ll.tail.unwrap(); |
| 42 | unsafe { |
| 43 | tail.as_mut().next = ll.head; |
| 44 | } |
| 45 | assert!(has_cycle(ll.head)); |
| 46 | |
| 47 | //eliminate cycle, otherwise LinkedList drop failed |
| 48 | unsafe { |
| 49 | tail.as_mut().next = None; |
| 50 | } |
| 51 | } |