1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use core::fmt::{Debug, Formatter};
15 use core::marker::PhantomData;
16 use core::ptr::null;
17 
18 // todo: Considers deleting PhantomData.
19 
20 /// Linked list implementation, provides two sets of methods for getting nodes and members.
21 /// Only tail insertion, reading, and eject are supported.
22 pub(crate) struct LinkedList<T> {
23     head: *const Node<T>,
24     tail: *const Node<T>,
25     len: usize,
26     marker: PhantomData<Box<Node<T>>>,
27 }
28 
29 impl<T> LinkedList<T> {
30     /// Creates LinkedList.
new() -> Self31     pub(crate) const fn new() -> Self {
32         LinkedList {
33             head: null(),
34             tail: null(),
35             len: 0,
36             marker: PhantomData,
37         }
38     }
39 
40     /// Gets length of the list.
41     #[inline]
len(&self) -> usize42     pub(crate) fn len(&self) -> usize {
43         self.len
44     }
45 
46     /// Determines whether the linked list is empty.
47     #[inline]
is_empty(&self) -> bool48     pub(crate) fn is_empty(&self) -> bool {
49         self.len == 0
50     }
51 
52     /// Inserts an element at the end of the list
push_back(&mut self, value: T)53     pub(crate) fn push_back(&mut self, value: T) {
54         let mut node = Box::new(Node::new(value));
55         unsafe {
56             // Sets prev to LinkedList.tail
57             node.prev = self.tail;
58             // Gets an internal element pointer.
59             let node = Box::leak(node) as *const Node<T>;
60 
61             if self.tail.is_null() {
62                 self.head = node;
63             } else {
64                 (*(self.tail as *mut Node<T>)).next = node;
65             }
66 
67             self.tail = node;
68             self.len += 1;
69         }
70     }
71 
72     /// Pops an element from the end of the list.
pop_back(&mut self) -> Option<T>73     pub(crate) fn pop_back(&mut self) -> Option<T> {
74         if self.tail.is_null() {
75             None
76         } else {
77             unsafe {
78                 let node = Box::from_raw(self.tail as *mut Node<T>);
79                 self.tail = node.prev;
80 
81                 if self.tail.is_null() {
82                     self.head = null();
83                 } else {
84                     (*(self.tail as *mut Node<T>)).next = null();
85                 }
86 
87                 self.len -= 1;
88                 Some(node.into_element())
89             }
90         }
91     }
92 
93     /// Gets an ordinary iterator for a linked list.
94     #[inline]
iter(&self) -> Iter<'_, T>95     pub(crate) fn iter(&self) -> Iter<'_, T> {
96         Iter {
97             head: self.head,
98             tail: self.tail,
99             len: self.len,
100             marker: PhantomData,
101         }
102     }
103 
104     /// Gets a mutable iterator for the linked list.
105     #[inline]
iter_mut(&mut self) -> IterMut<'_, T>106     pub(crate) fn iter_mut(&mut self) -> IterMut<'_, T> {
107         IterMut {
108             head: self.head,
109             tail: self.tail,
110             len: self.len,
111             marker: PhantomData,
112         }
113     }
114 
115     /// Gets the normal cursor of the list and sets the starting point to the list header.
116     #[inline]
cursor_front(&self) -> Cursor<'_, T>117     pub(crate) fn cursor_front(&self) -> Cursor<'_, T> {
118         Cursor {
119             index: 0,
120             current: self.head,
121             list: self,
122         }
123     }
124 
125     /// Gets the variable cursor of the list and sets the starting point to the list header.
126     #[inline]
cursor_front_mut(&mut self) -> CursorMut<'_, T>127     pub(crate) fn cursor_front_mut(&mut self) -> CursorMut<'_, T> {
128         CursorMut {
129             index: 0,
130             current: self.head,
131             list: self,
132         }
133     }
134 
135     /// Gets the normal cursor of the list and sets the starting point to the end of the list.
136     #[cfg(feature = "list_array")]
137     #[inline]
cursor_back(&self) -> Cursor<'_, T>138     pub(crate) fn cursor_back(&self) -> Cursor<'_, T> {
139         Cursor {
140             index: self.len.saturating_sub(1),
141             current: self.tail,
142             list: self,
143         }
144     }
145 
146     /// Gets the variable cursor of the list and sets the start to the end of the list.
147     #[cfg(feature = "list_array")]
148     #[inline]
cursor_back_mut(&mut self) -> CursorMut<'_, T>149     pub(crate) fn cursor_back_mut(&mut self) -> CursorMut<'_, T> {
150         CursorMut {
151             index: self.len.saturating_sub(1),
152             current: self.tail,
153             list: self,
154         }
155     }
156 
157     /// Gets a mutable reference to the tail element of the list.
158     #[cfg(feature = "list_array")]
159     #[inline]
back(&self) -> Option<&T>160     pub(crate) fn back(&self) -> Option<&T> {
161         if self.tail.is_null() {
162             None
163         } else {
164             unsafe { Some(&(*self.tail).element) }
165         }
166     }
167 
168     /// Gets a mutable reference to the tail element of the list.
169     #[inline]
back_mut(&mut self) -> Option<&mut T>170     pub(crate) fn back_mut(&mut self) -> Option<&mut T> {
171         if self.tail.is_null() {
172             None
173         } else {
174             unsafe { Some(&mut (*(self.tail as *mut Node<T>)).element) }
175         }
176     }
177 
178     /// Gets a common reference to the node at the end of the linked list.
179     #[cfg(feature = "list_array")]
180     #[inline]
back_node(&self) -> Option<&Node<T>>181     pub(crate) fn back_node(&self) -> Option<&Node<T>> {
182         if self.tail.is_null() {
183             None
184         } else {
185             unsafe { Some(&(*self.tail)) }
186         }
187     }
188 
189     /// Gets a mutable reference to the node at the end of the linked list.
190     #[cfg(feature = "list_array")]
191     #[inline]
back_node_mut(&mut self) -> Option<&mut Node<T>>192     pub(crate) fn back_node_mut(&mut self) -> Option<&mut Node<T>> {
193         if self.tail.is_null() {
194             None
195         } else {
196             unsafe {
197                 // Sets node.parent to the current linked_list in order to delete node.
198                 let node = &mut *(self.tail as *mut Node<T>);
199                 node.parent = self as *const LinkedList<T>;
200                 Some(node)
201             }
202         }
203     }
204 
205     /// Removes a node from the linked list.
unlink_node(&mut self, node: *const Node<T>)206     pub(crate) unsafe fn unlink_node(&mut self, node: *const Node<T>) {
207         let node = &mut (*(node as *mut Node<T>));
208 
209         if node.prev.is_null() {
210             self.head = node.next;
211         } else {
212             (*(node.prev as *mut Node<T>)).next = node.next;
213         }
214 
215         if node.next.is_null() {
216             self.tail = node.prev;
217         } else {
218             (*(node.next as *mut Node<T>)).prev = node.prev;
219         }
220 
221         self.len -= 1;
222     }
223 }
224 
225 impl<T> Default for LinkedList<T> {
default() -> Self226     fn default() -> Self {
227         Self::new()
228     }
229 }
230 
231 impl<T: Debug> Debug for LinkedList<T> {
fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result232     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
233         for (n, item) in self.iter().enumerate() {
234             if n != 0 {
235                 write!(f, ",")?;
236             }
237             write!(f, "{item:?}")?;
238         }
239         Ok(())
240     }
241 }
242 
243 impl<T: PartialEq> PartialEq for LinkedList<T> {
eq(&self, other: &Self) -> bool244     fn eq(&self, other: &Self) -> bool {
245         if self.len != other.len {
246             return false;
247         }
248         for (a, b) in self.iter().zip(other.iter()) {
249             if a.ne(b) {
250                 return false;
251             }
252         }
253         true
254     }
255 }
256 
257 impl<T: Clone> Clone for LinkedList<T> {
clone(&self) -> Self258     fn clone(&self) -> Self {
259         let mut new_list = LinkedList::new();
260         for item in self.iter() {
261             new_list.push_back(item.clone());
262         }
263         new_list
264     }
265 }
266 
267 impl<T> Drop for LinkedList<T> {
drop(&mut self)268     fn drop(&mut self) {
269         while self.len != 0 {
270             let _ = self.pop_back();
271         }
272     }
273 }
274 
275 // We need to use static to store the JsonValue, so we need to make the LinkedList implement Send and Sync.
276 // However, when using this list, locking is still required under concurrent conditions.
277 unsafe impl<T: Send> Send for LinkedList<T> {}
278 unsafe impl<T: Sync> Sync for LinkedList<T> {}
279 
280 /// Linked list node, only through a linked list cursor to get the node.
281 pub struct Node<T> {
282     next: *const Node<T>,
283     prev: *const Node<T>,
284     parent: *const LinkedList<T>,
285     element: T,
286 }
287 
288 impl<T> Node<T> {
289     /// Creates a linked list node.
new(element: T) -> Self290     pub(crate) fn new(element: T) -> Self {
291         Node {
292             next: null(),
293             prev: null(),
294             parent: null(),
295             element,
296         }
297     }
298 
299     /// Retrieves the member inside the list node.
into_element(self) -> T300     pub(crate) fn into_element(self) -> T {
301         self.element
302     }
303 
304     /// Gets a common reference to an internal member of a linked list node.
get_element_mut(&mut self) -> &mut T305     pub(crate) fn get_element_mut(&mut self) -> &mut T {
306         &mut self.element
307     }
308 
309     /// Removes the node itself from the linked list and returns the member below.
310     #[cfg(feature = "c_adapter")]
remove_self(&mut self) -> Option<T>311     pub(crate) fn remove_self(&mut self) -> Option<T> {
312         let list = unsafe { &mut *(self.parent as *mut LinkedList<T>) };
313         let mut cursor = CursorMut {
314             index: 0,
315             current: self as *const Node<T>,
316             list,
317         };
318         cursor.remove_current()
319     }
320 }
321 
322 /// A common iterator of a linked list.
323 pub struct Iter<'a, T: 'a> {
324     head: *const Node<T>,
325     tail: *const Node<T>,
326     len: usize,
327     marker: PhantomData<&'a Node<T>>,
328 }
329 
330 impl<'a, T> Iterator for Iter<'a, T> {
331     type Item = &'a T;
332 
333     #[inline]
next(&mut self) -> Option<&'a T>334     fn next(&mut self) -> Option<&'a T> {
335         if self.len == 0 || self.head.is_null() {
336             None
337         } else {
338             let node = unsafe { &*(self.head as *mut Node<T>) };
339             self.len -= 1;
340             self.head = node.next;
341             Some(&node.element)
342         }
343     }
344 
345     #[inline]
size_hint(&self) -> (usize, Option<usize>)346     fn size_hint(&self) -> (usize, Option<usize>) {
347         // Returns a tuple representing the remaining range of iterators.
348         (self.len, Some(self.len))
349     }
350 
351     #[inline]
last(mut self) -> Option<&'a T>352     fn last(mut self) -> Option<&'a T> {
353         // Uses the iterator traversal and returns the last element.
354         self.next_back()
355     }
356 }
357 
358 impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
next_back(&mut self) -> Option<&'a T>359     fn next_back(&mut self) -> Option<&'a T> {
360         if self.len == 0 || self.tail.is_null() {
361             None
362         } else {
363             let node = unsafe { &*(self.tail as *mut Node<T>) };
364             self.len -= 1;
365             self.tail = node.prev;
366             Some(&node.element)
367         }
368     }
369 }
370 
371 /// A variable iterator of a linked list.
372 pub struct IterMut<'a, T: 'a> {
373     head: *const Node<T>,
374     tail: *const Node<T>,
375     len: usize,
376     marker: PhantomData<&'a mut Node<T>>,
377 }
378 
379 impl<'a, T> Iterator for IterMut<'a, T> {
380     type Item = &'a mut T;
381 
382     #[inline]
next(&mut self) -> Option<&'a mut T>383     fn next(&mut self) -> Option<&'a mut T> {
384         if self.len == 0 || self.head.is_null() {
385             None
386         } else {
387             let node = unsafe { &mut *(self.head as *mut Node<T>) };
388             self.len -= 1;
389             self.head = node.next;
390             Some(&mut node.element)
391         }
392     }
393 
394     #[inline]
size_hint(&self) -> (usize, Option<usize>)395     fn size_hint(&self) -> (usize, Option<usize>) {
396         // Returns a tuple representing the remaining range of iterators.
397         (self.len, Some(self.len))
398     }
399 
400     #[inline]
last(mut self) -> Option<&'a mut T>401     fn last(mut self) -> Option<&'a mut T> {
402         // Uses the iterator traversal and returns the last element.
403         self.next_back()
404     }
405 }
406 
407 impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
408     #[inline]
next_back(&mut self) -> Option<&'a mut T>409     fn next_back(&mut self) -> Option<&'a mut T> {
410         if self.len == 0 || self.tail.is_null() {
411             None
412         } else {
413             let node = unsafe { &mut *(self.tail as *mut Node<T>) };
414             self.len -= 1;
415             self.tail = node.prev;
416             Some(&mut node.element)
417         }
418     }
419 }
420 
421 /// A common cursor for a linked list. When the list is empty,
422 /// it points to a virtual location (pointing to a node that does not actually exist).
423 pub(crate) struct Cursor<'a, T: 'a> {
424     index: usize,
425     current: *const Node<T>,
426     list: &'a LinkedList<T>,
427 }
428 
429 impl<'a, T> Cursor<'a, T> {
430     /// Gets the position the cursor is pointing to.
431     /// If the cursor points to a virtual position, return None.
432     #[inline]
index(&self) -> Option<usize>433     pub(crate) fn index(&self) -> Option<usize> {
434         if self.current.is_null() {
435             return None;
436         }
437         Some(self.index)
438     }
439 
440     /// The cursor moves back.
441     #[inline]
move_next(&mut self)442     pub(crate) fn move_next(&mut self) {
443         if self.current.is_null() {
444             self.current = self.list.head;
445             self.index = 0;
446         } else {
447             self.current = unsafe { (*self.current).next };
448             self.index += 1;
449         }
450     }
451 
452     /// The cursor moves forward.
453     #[cfg(feature = "list_array")]
454     #[inline]
move_prev(&mut self)455     pub(crate) fn move_prev(&mut self) {
456         if self.current.is_null() {
457             self.current = self.list.tail;
458             self.index = self.list.len().saturating_sub(1);
459         } else {
460             self.current = unsafe { (*self.current).prev };
461             self.index = self.index.checked_sub(1).unwrap_or_else(|| self.list.len());
462         }
463     }
464 
465     /// Gets the cursor.
466     #[inline]
current(&self) -> Option<&'a T>467     pub(crate) fn current(&self) -> Option<&'a T> {
468         if self.current.is_null() {
469             None
470         } else {
471             unsafe { Some(&(*self.current).element) }
472         }
473     }
474 
475     /// Gets a reference to the current node.
476     #[inline]
current_node(&self) -> Option<&'a Node<T>>477     pub(crate) fn current_node(&self) -> Option<&'a Node<T>> {
478         if self.current.is_null() {
479             None
480         } else {
481             unsafe { Some(&*(self.current)) }
482         }
483     }
484 
485     #[cfg(feature = "list_object")]
486     #[inline]
current_node_ptr(&self) -> *const Node<T>487     pub(crate) fn current_node_ptr(&self) -> *const Node<T> {
488         self.current
489     }
490 }
491 
492 pub(crate) struct CursorMut<'a, T: 'a> {
493     index: usize,
494     current: *const Node<T>,
495     list: &'a mut LinkedList<T>,
496 }
497 
498 impl<'a, T> CursorMut<'a, T> {
499     /// Gets the index.
500     #[inline]
index(&self) -> Option<usize>501     pub(crate) fn index(&self) -> Option<usize> {
502         if self.current.is_null() {
503             return None;
504         }
505         Some(self.index)
506     }
507 
508     /// The cursor moves beck.
509     #[inline]
move_next(&mut self)510     pub(crate) fn move_next(&mut self) {
511         if self.current.is_null() {
512             self.current = self.list.head;
513             self.index = 0;
514         } else {
515             self.current = unsafe { (*self.current).next };
516             self.index += 1;
517         }
518     }
519 
520     /// The cursor moves forward.
521     #[cfg(feature = "list_array")]
522     #[inline]
move_prev(&mut self)523     pub(crate) fn move_prev(&mut self) {
524         if self.current.is_null() {
525             self.current = self.list.tail;
526             self.index = self.list.len().saturating_sub(1);
527         } else {
528             self.current = unsafe { (*self.current).prev };
529             self.index = self.index.checked_sub(1).unwrap_or_else(|| self.list.len());
530         }
531     }
532 
533     /// Gets a mutable reference to the current element.
534     #[cfg(feature = "list_object")]
535     #[inline]
current(&mut self) -> Option<&mut T>536     pub(crate) fn current(&mut self) -> Option<&mut T> {
537         if self.current.is_null() {
538             None
539         } else {
540             unsafe { Some(&mut (*(self.current as *mut Node<T>)).element) }
541         }
542     }
543 
544     /// Gets a mutable reference to the current node.
545     #[inline]
current_node(&mut self) -> Option<&'a mut Node<T>>546     pub(crate) fn current_node(&mut self) -> Option<&'a mut Node<T>> {
547         if self.current.is_null() {
548             None
549         } else {
550             unsafe {
551                 let node = &mut *(self.current as *mut Node<T>);
552                 node.parent = self.list as *mut LinkedList<T>;
553                 Some(node)
554             }
555         }
556     }
557 
558     /// Deletes the node to which the cursor is pointing.
559     #[inline]
remove_current(&mut self) -> Option<T>560     pub(crate) fn remove_current(&mut self) -> Option<T> {
561         if self.current.is_null() {
562             return None;
563         }
564 
565         let unlinked_node = self.current;
566         unsafe {
567             self.current = (*unlinked_node).next;
568             self.list.unlink_node(unlinked_node);
569             let unlinked_node = Box::from_raw(unlinked_node as *mut Node<T>);
570             Some(unlinked_node.element)
571         }
572     }
573 }
574 
575 #[cfg(test)]
576 mod ut_linked_list {
577     use crate::LinkedList;
578 
579     /// UT test for `LinkedList::pop_back`.
580     ///
581     /// # Title
582     /// ut_linked_list_pop_back
583     ///
584     /// # Brief
585     /// 1. Creates a `LinkedList`.
586     /// 2. Calls `LinkedList::pop_back` on it.
587     /// 3. Checks if the test results are correct.
588     #[test]
ut_linked_list_pop_back()589     fn ut_linked_list_pop_back() {
590         let mut list = LinkedList::new();
591         assert_eq!(list.pop_back(), None);
592 
593         list.push_back(1i32);
594         assert_eq!(list.pop_back(), Some(1));
595     }
596 
597     /// UT test for `LinkedList::iter_mut`.
598     ///
599     /// # Title
600     /// ut_linked_list_iter_mut
601     ///
602     /// # Brief
603     /// 1. Creates a `LinkedList`.
604     /// 2. Calls `LinkedList::iter_mut` on it.
605     /// 3. Checks if the test results are correct.
606     #[test]
ut_linked_list_iter_mut()607     fn ut_linked_list_iter_mut() {
608         let mut list = LinkedList::new();
609         list.push_back(1i32);
610         list.push_back(2i32);
611 
612         let mut iter = list.iter_mut();
613         assert_eq!(iter.next(), Some(&mut 1));
614         assert_eq!(iter.next(), Some(&mut 2));
615         assert_eq!(iter.next(), None);
616     }
617 
618     /// UT test for `LinkedList::back`.
619     ///
620     /// # Title
621     /// ut_linked_list_back
622     ///
623     /// # Brief
624     /// 1. Creates a `LinkedList`.
625     /// 2. Calls `LinkedList::back` on it.
626     /// 3. Checks if the test results are correct.
627     #[cfg(feature = "list_array")]
628     #[test]
ut_linked_list_back()629     fn ut_linked_list_back() {
630         let mut list = LinkedList::new();
631         assert_eq!(list.back(), None);
632 
633         list.push_back(1i32);
634         assert_eq!(list.back(), Some(&1));
635     }
636 
637     /// UT test for `LinkedList::back_mut`.
638     ///
639     /// # Title
640     /// ut_linked_list_back_mut
641     ///
642     /// # Brief
643     /// 1. Creates a `LinkedList`.
644     /// 2. Calls `LinkedList::back_mut` on it.
645     /// 3. Checks if the test results are correct.
646     #[test]
ut_linked_list_back_mut()647     fn ut_linked_list_back_mut() {
648         let mut list = LinkedList::new();
649         assert_eq!(list.back_mut(), None);
650 
651         list.push_back(1i32);
652         assert_eq!(list.back_mut(), Some(&mut 1));
653     }
654 
655     /// UT test for `LinkedList::back_node`.
656     ///
657     /// # Title
658     /// ut_linked_list_back_node
659     ///
660     /// # Brief
661     /// 1. Creates a `LinkedList`.
662     /// 2. Calls `LinkedList::back_node` on it.
663     /// 3. Checks if the test results are correct.
664     #[cfg(feature = "list_array")]
665     #[test]
ut_linked_list_back_node()666     fn ut_linked_list_back_node() {
667         let mut list = LinkedList::new();
668         assert!(list.back_node().is_none());
669 
670         list.push_back(1i32);
671         assert!(list.back_node().is_some());
672     }
673 
674     /// UT test for `LinkedList::back_node_mut`.
675     ///
676     /// # Title
677     /// ut_linked_list_back_node_mut
678     ///
679     /// # Brief
680     /// 1. Creates a `LinkedList`.
681     /// 2. Calls `LinkedList::back_node_mut` on it.
682     /// 3. Checks if the test results are correct.
683     #[cfg(feature = "list_array")]
684     #[test]
ut_linked_list_back_node_mut()685     fn ut_linked_list_back_node_mut() {
686         let mut list = LinkedList::new();
687         assert!(list.back_node_mut().is_none());
688 
689         list.push_back(1i32);
690         assert!(list.back_node_mut().is_some());
691     }
692 
693     /// UT test for `LinkedList::default`.
694     ///
695     /// # Title
696     /// ut_linked_list_default
697     ///
698     /// # Brief
699     /// 1. Calls `LinkedList::default` to create a `LinkedList`.
700     /// 2. Checks if the test results are correct.
701     #[test]
ut_linked_list_default()702     fn ut_linked_list_default() {
703         assert_eq!(LinkedList::<i32>::default(), LinkedList::<i32>::new());
704     }
705 
706     /// UT test for `LinkedList::eq`.
707     ///
708     /// # Title
709     /// ut_linked_list_eq
710     ///
711     /// # Brief
712     /// 1. Creates some `LinkedList`s.
713     /// 2. Calls `LinkedList::eq` on them.
714     /// 3. Checks if the test results are correct.
715     #[test]
ut_linked_list_eq()716     fn ut_linked_list_eq() {
717         let mut list1 = LinkedList::new();
718         list1.push_back(1i32);
719 
720         let mut list2 = LinkedList::new();
721         list2.push_back(1i32);
722         list2.push_back(2i32);
723 
724         let mut list3 = LinkedList::new();
725         list3.push_back(1i32);
726         list3.push_back(3i32);
727 
728         assert_eq!(list1, list1);
729         assert_ne!(list1, list2);
730         assert_ne!(list2, list3);
731     }
732 
733     /// UT test for `LinkedList::clone`.
734     ///
735     /// # Title
736     /// ut_linked_list_clone
737     ///
738     /// # Brief
739     /// 1. Creates a `LinkedList`.
740     /// 2. Calls `LinkedList::clone` to create a copy of it.
741     /// 3. Checks if the test results are correct.
742     #[test]
ut_linked_list_clone()743     fn ut_linked_list_clone() {
744         let mut list1 = LinkedList::new();
745         list1.push_back(1i32);
746 
747         let list2 = list1.clone();
748         assert_eq!(list1, list2);
749     }
750 
751     /// UT test for `LinkedList::fmt`.
752     ///
753     /// # Title
754     /// ut_linked_list_fmt
755     ///
756     /// # Brief
757     /// 1. Creates a `LinkedList`.
758     /// 2. Calls `LinkedList::fmt` on it.
759     /// 3. Checks if the test results are correct.
760     #[test]
ut_linked_list_fmt()761     fn ut_linked_list_fmt() {
762         let mut list = LinkedList::new();
763         list.push_back(1i32);
764         list.push_back(2i32);
765         assert_eq!(format!("{list:?}"), "1,2");
766     }
767 
768     /// UT test for `Cursor::index`.
769     ///
770     /// # Title
771     /// ut_cursor_index
772     ///
773     /// # Brief
774     /// 1. Creates a `LinkedList` and a `Cursor`.
775     /// 2. Calls `Cursor::index`.
776     /// 3. Checks if the test results are correct.
777     #[test]
ut_cursor_index()778     fn ut_cursor_index() {
779         let mut list = LinkedList::new();
780         list.push_back(1i32);
781 
782         let mut cursor = list.cursor_front();
783         assert_eq!(cursor.index(), Some(0));
784 
785         cursor.move_next();
786         assert_eq!(cursor.index(), None);
787     }
788 
789     /// UT test for `Cursor::move_next`.
790     ///
791     /// # Title
792     /// ut_cursor_move_next
793     ///
794     /// # Brief
795     /// 1. Creates a `LinkedList` and a `Cursor`.
796     /// 2. Calls `Cursor::move_next`.
797     /// 3. Checks if the test results are correct.
798     #[test]
ut_cursor_move_next()799     fn ut_cursor_move_next() {
800         let mut list = LinkedList::new();
801         list.push_back(1i32);
802         list.push_back(2i32);
803 
804         let mut cursor = list.cursor_front();
805         assert_eq!(cursor.current(), Some(&1));
806 
807         cursor.move_next();
808         assert_eq!(cursor.current(), Some(&2));
809 
810         cursor.move_next();
811         assert_eq!(cursor.current(), None);
812 
813         cursor.move_next();
814         assert_eq!(cursor.current(), Some(&1));
815     }
816 
817     /// UT test for `Cursor::move_prev`.
818     ///
819     /// # Title
820     /// ut_cursor_move_prev
821     ///
822     /// # Brief
823     /// 1. Creates a `LinkedList` and a `Cursor`.
824     /// 2. Calls `Cursor::move_prev`.
825     /// 3. Checks if the test results are correct.
826     #[cfg(feature = "list_array")]
827     #[test]
ut_cursor_move_prev()828     fn ut_cursor_move_prev() {
829         let mut list = LinkedList::new();
830         list.push_back(1i32);
831         list.push_back(2i32);
832 
833         let mut cursor = list.cursor_front();
834         assert_eq!(cursor.current(), Some(&1));
835 
836         cursor.move_prev();
837         assert_eq!(cursor.current(), None);
838 
839         cursor.move_prev();
840         assert_eq!(cursor.current(), Some(&2));
841 
842         cursor.move_prev();
843         assert_eq!(cursor.current(), Some(&1));
844     }
845 
846     /// UT test for `Cursor::current_node`.
847     ///
848     /// # Title
849     /// ut_cursor_current_node
850     ///
851     /// # Brief
852     /// 1. Creates a `LinkedList` and a `Cursor`.
853     /// 2. Calls `Cursor::current_node`.
854     /// 3. Checks if the test results are correct.
855     #[test]
ut_cursor_current_node()856     fn ut_cursor_current_node() {
857         let mut list = LinkedList::new();
858         list.push_back(1i32);
859 
860         let mut cursor = list.cursor_front();
861         assert!(cursor.current_node().is_some());
862 
863         cursor.move_next();
864         assert!(cursor.current_node().is_none());
865     }
866 
867     /// UT test for `CursorMut::index`.
868     ///
869     /// # Title
870     /// ut_cursor_mut_index
871     ///
872     /// # Brief
873     /// 1. Creates a `LinkedList` and a `CursorMut`.
874     /// 2. Calls `CursorMut::index`.
875     /// 3. Checks if the test results are correct.
876     #[test]
ut_cursor_mut_index()877     fn ut_cursor_mut_index() {
878         let mut list = LinkedList::new();
879         list.push_back(1i32);
880 
881         let mut cursor = list.cursor_front_mut();
882         assert_eq!(cursor.index(), Some(0));
883 
884         cursor.move_next();
885         assert_eq!(cursor.index(), None);
886     }
887 
888     /// UT test for `CursorMut::move_next`.
889     ///
890     /// # Title
891     /// ut_cursor_mut_move_next
892     ///
893     /// # Brief
894     /// 1. Creates a `LinkedList` and a `CursorMut`.
895     /// 2. Calls `CursorMut::move_next`.
896     /// 3. Checks if the test results are correct.
897     #[test]
ut_cursor_mut_move_next()898     fn ut_cursor_mut_move_next() {
899         let mut list = LinkedList::new();
900         list.push_back(1i32);
901 
902         let mut cursor = list.cursor_front_mut();
903         assert!(cursor.current_node().is_some());
904 
905         cursor.move_next();
906         assert!(cursor.current_node().is_none());
907 
908         cursor.move_next();
909         assert!(cursor.current_node().is_some());
910     }
911 
912     /// UT test for `CursorMut::move_prev`.
913     ///
914     /// # Title
915     /// ut_cursor_mut_move_prev
916     ///
917     /// # Brief
918     /// 1. Creates a `LinkedList` and a `CursorMut`.
919     /// 2. Calls `CursorMut::move_prev`.
920     /// 3. Checks if the test results are correct.
921     #[cfg(feature = "list_array")]
922     #[test]
ut_cursor_mut_move_prev()923     fn ut_cursor_mut_move_prev() {
924         let mut list = LinkedList::new();
925         list.push_back(1i32);
926 
927         let mut cursor = list.cursor_front_mut();
928         assert!(cursor.current_node().is_some());
929 
930         cursor.move_prev();
931         assert!(cursor.current_node().is_none());
932 
933         cursor.move_prev();
934         assert!(cursor.current_node().is_some());
935     }
936 
937     /// UT test for `CursorMut::current`.
938     ///
939     /// # Title
940     /// ut_cursor_mut_current
941     ///
942     /// # Brief
943     /// 1. Creates a `LinkedList` and a `CursorMut`.
944     /// 2. Calls `CursorMut::current`.
945     /// 3. Checks if the test results are correct.
946     #[cfg(feature = "list_object")]
947     #[test]
ut_cursor_mut_current()948     fn ut_cursor_mut_current() {
949         let mut list = LinkedList::new();
950         list.push_back(1i32);
951 
952         let mut cursor = list.cursor_front_mut();
953         assert_eq!(cursor.current(), Some(&mut 1));
954 
955         cursor.move_next();
956         assert_eq!(cursor.current(), None);
957     }
958 
959     /// UT test for `CursorMut::current`.
960     ///
961     /// # Title
962     /// ut_cursor_mut_current
963     ///
964     /// # Brief
965     /// 1. Creates a `LinkedList` and a `CursorMut`.
966     /// 2. Calls `CursorMut::current`.
967     /// 3. Checks if the test results are correct.
968     #[test]
ut_cursor_mut_remove_current()969     fn ut_cursor_mut_remove_current() {
970         let mut list = LinkedList::new();
971         list.push_back(1i32);
972 
973         let mut cursor = list.cursor_front_mut();
974         assert_eq!(cursor.remove_current(), Some(1));
975         assert_eq!(cursor.remove_current(), None);
976     }
977 }
978