villa01-data-structures 0.2.0

Personal crate of me trying to implement data structures and algorithms in Rust.
Documentation
use super::{DoublyLinkedList, ListNode};
impl<T> DoublyLinkedList<T> {
    /// Inserts an item of type `T` at the end of the `DoublyLinkedList`
    ///
    /// # Parameters
    /// - `data`: The new value to be inserted.
    ///
    /// # Examples
    /// ```
    /// use villa01_data_structures::doubly_linked_list::DoublyLinkedList;
    ///
    /// let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
    /// list.insert_at_end(1);
    /// assert_eq!(list.len(), 1);
    /// ```
    pub fn insert_at_end(&mut self, data: T) {
        let new_node = Box::into_raw(Box::new(ListNode::new(data)));
        match self.tail {
            Some(tail) => {
                unsafe {
                    (*tail).next = Some(new_node);
                    (*new_node).prev = Some(tail);
                }
                self.tail = Some(new_node);
            }
            None => {
                // The list is empty and should be initialized

                self.head = Some(new_node);
                self.tail = Some(new_node);
            }
        }
        self.length += 1;
    }
}

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

    #[test]
    fn test_empty_list() {
        let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
        list.insert_at_end(1);
        assert!(!list.is_empty());
        assert_eq!(list.len(), 1);
    }

    #[test]
    fn test_existing_head() {
        let mut list: DoublyLinkedList<i32> = DoublyLinkedList::with_value(2);
        list.insert_at_end(1);
        assert_eq!(list.len(), 2);
    }

    #[test]
    fn test_with_several_nodes() {
        struct T {}
        let mut list: DoublyLinkedList<T> = DoublyLinkedList::new();

        let list_length: usize = 100;

        for _ in 1..list_length + 1 {
            list.insert_at_end(T {});
        }

        assert_eq!(list.len(), list_length);
    }
}