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
use crate::{
    list::List,
    variants::{doubly::Doubly, singly::Singly},
};
use orx_selfref_col::MemoryReclaimPolicy;
use std::fmt::Debug;

impl<'a, T, M> Debug for List<'a, Singly<M>, T>
where
    T: Debug,
    M: MemoryReclaimPolicy,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SinglyLinkedList")
            .field("len", &self.len())
            .field("front", &self.front())
            .field("forward", &self.iter().collect::<Vec<_>>())
            .finish()
    }
}

impl<'a, T, M> Debug for List<'a, Doubly<M>, T>
where
    T: Debug,
    M: 'a + MemoryReclaimPolicy,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DoublyLinkedList")
            .field("len", &self.len())
            .field("front", &self.front())
            .field("back", &self.back())
            .field("forward", &self.iter().collect::<Vec<_>>())
            .field("backward", &self.iter_from_back().collect::<Vec<_>>())
            .finish()
    }
}

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

    #[test]
    fn debug_singly_empty() {
        let list: List<Singly, usize> = List::new();

        assert_eq!(
            format!("{:?}", &list),
            "SinglyLinkedList { len: 0, front: None, forward: [] }"
        );
    }

    #[test]
    fn debug_singly_single() {
        let mut list: List<Singly, _> = List::new();
        list.push_front(42);

        assert_eq!(
            format!("{:?}", &list),
            "SinglyLinkedList { len: 1, front: Some(42), forward: [42] }"
        );
    }

    #[test]
    fn debug_singly_multi() {
        let mut list: List<Singly, _> = List::new();
        list.push_front(42);
        list.push_front(1);
        list.push_front(7);

        assert_eq!(
            format!("{:?}", &list),
            "SinglyLinkedList { len: 3, front: Some(7), forward: [7, 1, 42] }"
        );
    }

    #[test]
    fn debug_doubly_empty() {
        let list: List<Doubly, usize> = List::new();

        assert_eq!(
            format!("{:?}", &list),
            "DoublyLinkedList { len: 0, front: None, back: None, forward: [], backward: [] }"
        );
    }

    #[test]
    fn debug_doubly_single() {
        let mut list: List<Doubly, _> = List::new();
        list.push_front(42);

        assert_eq!(
            format!("{:?}", &list),
            "DoublyLinkedList { len: 1, front: Some(42), back: Some(42), forward: [42], backward: [42] }"
        );
    }

    #[test]
    fn debug_doubly_multi() {
        let mut list: List<Doubly, _> = List::new();
        list.push_front(42);
        list.push_front(1);
        list.push_front(7);

        assert_eq!(
            format!("{:?}", &list),
            "DoublyLinkedList { len: 3, front: Some(7), back: Some(42), forward: [7, 1, 42], backward: [42, 1, 7] }"
        );
    }
}