double_linked_list 0.1.2

High-performance doubly-linked list with smart pointer support and memory pool optimization
Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
use double_linked_list::DoubleRinkedList;
use double_linked_list::double_linked_list::ListError;
use std::sync::{Arc, Mutex};
use std::time::Instant;

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

    fn create_test_list() -> DoubleRinkedList<i32> {
        let mut list = DoubleRinkedList::new();
        for i in 1..=5 {
            list.push(i).unwrap();
        }
        list
    }

    #[test]
    fn test_construction_and_capacity() {
        let list: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
        assert_eq!(list.pool_stats(), None);

        let list_with_pool = DoubleRinkedList::<i32>::with_capacity(10);
        assert_eq!(list_with_pool.len(), 0);
        assert!(list_with_pool.is_empty());
        assert_eq!(list_with_pool.pool_stats(), Some((10, 10)));

        let mut list_capacity = DoubleRinkedList::<i32>::with_capacity(5);
        list_capacity.add_capacity(5);
        assert_eq!(list_capacity.pool_stats(), Some((10, 10)));

        let mut list_set = DoubleRinkedList::<i32>::with_capacity(5);
        assert!(list_set.set_capacity(10).is_ok());
        assert_eq!(list_set.pool_stats(), Some((10, 10)));

        let mut list_fail = DoubleRinkedList::new();
        list_fail.push(1).unwrap();
        list_fail.push(2).unwrap();
        assert!(list_fail.set_capacity(1).is_err());
    }

    #[test]
    fn test_basic_stack_operations() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        assert_eq!(list.push(1).unwrap(), 1);
        assert_eq!(list.len(), 1);
        assert!(!list.is_empty());

        assert_eq!(list.push(2).unwrap(), 2);
        assert_eq!(list.len(), 2);

        assert_eq!(list.push(3).unwrap(), 3);
        assert_eq!(list.len(), 3);

        assert_eq!(list.pop(), Some(3));
        assert_eq!(list.len(), 2);
        
        assert_eq!(list.pop(), Some(2));
        assert_eq!(list.len(), 1);
        
        assert_eq!(list.pop(), Some(1));
        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
        
        assert_eq!(list.pop(), None);
    }

    #[test]
    fn test_queue_operations() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        assert_eq!(list.push_front(1).unwrap(), 1);
        assert_eq!(list.push_front(2).unwrap(), 2);
        assert_eq!(list.push_front(3).unwrap(), 3);
        assert_eq!(list.to_vec(), vec![3, 2, 1]);

        assert_eq!(list.pop_front(), Some(3));
        assert_eq!(list.pop_front(), Some(2));
        assert_eq!(list.pop_front(), Some(1));
        assert_eq!(list.pop_front(), None);
        assert!(list.is_empty());
    }

    #[test]
    fn test_element_access() {
        let list = create_test_list();

        assert_eq!(list.get_at_begin(), Some(1));
        assert_eq!(list.get_at_end(), Some(5));
        assert_eq!(list.get_at_index(0).unwrap(), 1);
        assert_eq!(list.get_at_index(2).unwrap(), 3);
        assert_eq!(list.get_at_index(4).unwrap(), 5);
        assert!(list.get_at_index(10).is_err());

        let empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert_eq!(empty.get_at_begin(), None);
        assert_eq!(empty.get_at_end(), None);
        assert!(empty.get_at_index(0).is_err());
    }

    #[test]
    fn test_insertion_operations() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        assert!(list.insert_at_begin(2).is_ok());
        assert!(list.insert_at_begin(1).is_ok());
        assert_eq!(list.to_vec(), vec![1, 2]);

        assert!(list.insert_at_end(3).is_ok());
        assert!(list.insert_at_end(4).is_ok());
        assert_eq!(list.to_vec(), vec![1, 2, 3, 4]);

        assert!(list.insert_at_index(2, 99).is_ok());
        assert_eq!(list.to_vec(), vec![1, 2, 99, 3, 4]);

        assert!(list.insert_at_index(0, 0).is_ok());
        assert_eq!(list.to_vec(), vec![0, 1, 2, 99, 3, 4]);

        assert!(list.insert_at_index(6, 100).is_ok());
        assert_eq!(list.to_vec(), vec![0, 1, 2, 99, 3, 4, 100]);

        assert!(list.insert_at_index(10, 999).is_err());
    }

    #[test]
    fn test_insert_many_at_index() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();
        
        // Start with some initial values
        list.push(1).unwrap();
        list.push(2).unwrap();
        list.push(5).unwrap();
        assert_eq!(list.to_vec(), vec![1, 2, 5]);
        
        // Insert multiple values at index 2 (after 2, before 5)
        let values = vec![3, 4];
        let count = list.insert_many_at_index(2, values).unwrap();
        assert_eq!(count, 2);
        assert_eq!(list.to_vec(), vec![1, 2, 3, 4, 5]);
        
        // Insert at beginning
        let more_values = vec![10, 11, 12];
        let count2 = list.insert_many_at_index(0, more_values).unwrap();
        assert_eq!(count2, 3);
        assert_eq!(list.to_vec(), vec![10, 11, 12, 1, 2, 3, 4, 5]);
        
        // Insert at end
        let end_values = vec![100, 101];
        let count3 = list.insert_many_at_index(8, end_values).unwrap();
        assert_eq!(count3, 2);
        assert_eq!(list.to_vec(), vec![10, 11, 12, 1, 2, 3, 4, 5, 100, 101]);
        
        // Test with iterator (range)
        let range_values = 50..53; // [50, 51, 52]
        let count4 = list.insert_many_at_index(4, range_values).unwrap();
        assert_eq!(count4, 3);
        assert_eq!(list.to_vec(), vec![10, 11, 12, 1, 50, 51, 52, 2, 3, 4, 5, 100, 101]);
        
        // Test empty iterator
        let empty_values: Vec<i32> = vec![];
        let count5 = list.insert_many_at_index(5, empty_values).unwrap();
        assert_eq!(count5, 0);
        assert_eq!(list.len(), 13); // Should remain unchanged
        
        // Test out of bounds
        let invalid_values = vec![999];
        assert!(list.insert_many_at_index(20, invalid_values).is_err());
        
        // Test with single element
        let single_value = vec![42];
        let count6 = list.insert_many_at_index(6, single_value).unwrap();
        assert_eq!(count6, 1);
        assert_eq!(list.get_at_index(6).unwrap(), 42);
    }

    #[test]
    fn test_removal_operations() {
        let mut list = create_test_list();

        assert_eq!(list.remove_at_end().unwrap(), 5);
        assert_eq!(list.to_vec(), vec![1, 2, 3, 4]);

        assert_eq!(list.remove_at_begin().unwrap(), 1);
        assert_eq!(list.to_vec(), vec![2, 3, 4]);

        assert_eq!(list.remove_at_index(1).unwrap(), 3);
        assert_eq!(list.to_vec(), vec![2, 4]);

        assert_eq!(list.remove_at_index(0).unwrap(), 2);
        assert_eq!(list.to_vec(), vec![4]);

        assert_eq!(list.remove_at_index(0).unwrap(), 4);
        assert!(list.is_empty());

        assert!(list.remove_at_end().is_err());
        assert!(list.remove_at_begin().is_err());
        assert!(list.remove_at_index(0).is_err());
    }

    #[test]
    fn test_search_operations() {
        let list = create_test_list();

        assert!(list.contains(&3));
        assert!(!list.contains(&10));
        assert!(list.includes(&4));
        assert!(!list.includes(&0));

        assert_eq!(list.index_of(&1), Some(0));
        assert_eq!(list.index_of(&3), Some(2));
        assert_eq!(list.index_of(&5), Some(4));
        assert_eq!(list.index_of(&10), None);

        assert_eq!(list.find_index(|x| *x > 3), Some(3));
        assert_eq!(list.find_index(|x| *x > 10), None);
        assert_eq!(list.find_index(|x| *x == 2), Some(1));

        let empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert!(!empty.contains(&1));
        assert_eq!(empty.index_of(&1), None);
        assert_eq!(empty.find_index(|x| *x == 1), None);
    }

    #[test]
    fn test_functional_programming() {
        let list = create_test_list();

        let doubled = list.map(|x| x * 2);
        assert_eq!(doubled.to_vec(), vec![2, 4, 6, 8, 10]);

        let evens = list.filter(|x| x % 2 == 0);
        assert_eq!(evens.to_vec(), vec![2, 4]);

        let odds = list.filter(|x| x % 2 == 1);
        assert_eq!(odds.to_vec(), vec![1, 3, 5]);

        let sum = list.reduce(|acc, x| acc + x, 0);
        assert_eq!(sum, 15);

        let product = list.reduce(|acc, x| acc * x, 1);
        assert_eq!(product, 120);

        assert!(list.every(|x| *x > 0));
        assert!(!list.every(|x| *x > 3));

        assert!(list.any(|x| *x > 3));
        assert!(!list.any(|x| *x > 10));
        assert!(list.some(|x| *x == 3));
        assert!(!list.some(|x| *x == 10));

        let empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert_eq!(empty.map(|x| x * 2).len(), 0);
        assert_eq!(empty.filter(|x| *x > 0).len(), 0);
        assert_eq!(empty.reduce(|acc, x| acc + x, 42), 42);
        assert!(empty.every(|x| *x > 0));
        assert!(!empty.any(|x| *x > 0));
    }

    #[test]
    fn test_cursor_positioning() {
        let mut list = create_test_list();

        assert!(list.move_cursor_at_begin().is_ok());
        assert!(list.move_cursor_at_end().is_ok());
        assert!(list.move_cursor_at_index(0).is_ok());
        assert!(list.move_cursor_at_index(2).is_ok());
        assert!(list.move_cursor_at_index(4).is_ok());
        assert!(list.move_cursor_at_index(5).is_ok());
        assert!(list.move_cursor_at_index(10).is_err());

        list.move_cursor_at_begin().unwrap();
        assert!(list.move_cursor_to_next().is_ok());
        assert!(list.move_cursor_to_next().is_ok());
        assert!(list.move_cursor_to_previous().is_ok());
        assert!(list.move_cursor_to_previous().is_ok());

        let mut empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert!(empty.move_cursor_at_begin().is_ok());
        assert!(empty.move_cursor_at_end().is_ok());
        assert!(empty.move_cursor_at_index(0).is_err());
        assert!(empty.move_cursor_to_next().is_err());
        assert!(empty.move_cursor_to_previous().is_err());
    }

    #[test]
    fn test_cursor_operations() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        assert!(list.insert_after_cursor(1).is_ok());
        assert_eq!(list.to_vec(), vec![1]);

        list.move_cursor_at_begin().unwrap();
        assert!(list.insert_after_cursor(2).is_ok());
        assert_eq!(list.to_vec(), vec![1, 2]);

        list.move_cursor_at_end().unwrap();
        assert!(list.insert_before_cursor(3).is_ok());
        assert_eq!(list.to_vec(), vec![1, 2, 3]);

        list.move_cursor_at_begin().unwrap();
        assert_eq!(list.remove_after_cursor().unwrap(), 2);
        assert_eq!(list.to_vec(), vec![1, 3]);

        list.move_cursor_at_end().unwrap();
        assert_eq!(list.remove_before_cursor().unwrap(), 1);
        assert_eq!(list.to_vec(), vec![3]);

        list.remove_after_cursor().ok();
        list.clear();

        assert!(list.remove_after_cursor().is_err());
        assert!(list.remove_before_cursor().is_err());
    }

    #[test]
    fn test_cursor_management() {
        let mut list = create_test_list();

        assert!(list.validate_cursor());

        list.move_cursor_at_index(2).unwrap();
        let cursor = list.get_cursor();
        
        list.move_cursor_at_begin().unwrap();
        assert!(list.set_cursor(cursor).is_ok());

        list.reset_cursor();
        assert!(list.validate_cursor());

        let mut empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert!(empty.validate_cursor());
        empty.reset_cursor();
        assert!(empty.validate_cursor());
    }

    #[test]
    fn test_display_methods() {
        let list = create_test_list();

        list.display(None);
        list.display(Some(", "));
        list.display(Some(" -> "));

        list.log(None);
        list.log(Some(", "));
        list.log(Some(" -> "));

        let empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        empty.display(None);
        empty.log(None);
    }

    #[test]
    fn test_utility_methods() {
        let list = create_test_list();

        assert_eq!(list.to_vec(), vec![1, 2, 3, 4, 5]);

        let mut list_to_clear = list.clone();
        list_to_clear.clear();
        assert!(list_to_clear.is_empty());
        assert_eq!(list_to_clear.len(), 0);

        let mut list_to_reverse = list.clone();
        list_to_reverse.reverse();
        assert_eq!(list_to_reverse.to_vec(), vec![5, 4, 3, 2, 1]);

        let mut empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        assert_eq!(empty.to_vec(), Vec::<i32>::new());
        empty.clear();
        empty.reverse();
        assert!(empty.is_empty());
    }

    #[test]
    fn test_splice_operation() {
        let mut list = create_test_list();

        let removed = list.splice(1, 2, vec![10, 20, 30]).unwrap();
        assert_eq!(removed, vec![2, 3]);
        assert_eq!(list.to_vec(), vec![1, 10, 20, 30, 4, 5]);

        let removed2 = list.splice(2, 2, vec![]).unwrap();
        assert_eq!(removed2, vec![20, 30]);
        assert_eq!(list.to_vec(), vec![1, 10, 4, 5]);

        let removed3 = list.splice(2, 0, vec![100]).unwrap();
        assert_eq!(removed3, Vec::<i32>::new());
        assert_eq!(list.to_vec(), vec![1, 10, 100, 4, 5]);

        assert!(list.splice(10, 1, vec![]).is_err());
    }

    #[test]
    fn test_iterator_implementation() {
        let list = create_test_list();

        let collected: Vec<i32> = list.clone().into_iter().collect();
        assert_eq!(collected, vec![1, 2, 3, 4, 5]);

        let collected_ref: Vec<i32> = (&list).into_iter().collect();
        assert_eq!(collected_ref, vec![1, 2, 3, 4, 5]);

        let empty: DoubleRinkedList<i32> = DoubleRinkedList::new();
        let empty_collected: Vec<i32> = empty.into_iter().collect();
        assert_eq!(empty_collected, Vec::<i32>::new());

        let sum: i32 = create_test_list().into_iter().sum();
        assert_eq!(sum, 15);

        let filtered: Vec<i32> = create_test_list().into_iter().filter(|&x| x % 2 == 0).collect();
        assert_eq!(filtered, vec![2, 4]);
    }

    #[test]
    fn test_from_iterator() {
        let vec = vec![1, 2, 3, 4, 5];
        let list: DoubleRinkedList<i32> = vec.into_iter().collect();
        assert_eq!(list.to_vec(), vec![1, 2, 3, 4, 5]);

        let range_list: DoubleRinkedList<i32> = (1..=5).collect();
        assert_eq!(range_list.to_vec(), vec![1, 2, 3, 4, 5]);

        let empty_vec: Vec<i32> = vec![];
        let empty_list: DoubleRinkedList<i32> = empty_vec.into_iter().collect();
        assert!(empty_list.is_empty());
    }

    #[test]
    fn test_default_trait() {
        let list: DoubleRinkedList<i32> = DoubleRinkedList::default();
        assert!(list.is_empty());
        assert_eq!(list.len(), 0);
    }

    #[test]
    fn test_clone_trait() {
        let list = create_test_list();
        let cloned = list.clone();
        
        assert_eq!(list.to_vec(), cloned.to_vec());
        assert_eq!(list.len(), cloned.len());
    }

    #[test]
    fn test_memory_pool_functionality() {
        let mut pooled_list = DoubleRinkedList::<i32>::with_capacity(5);
        
        assert_eq!(pooled_list.pool_stats(), Some((5, 5)));
        
        for i in 1..=3 {
            pooled_list.push(i).unwrap();
        }
        assert_eq!(pooled_list.pool_stats(), Some((2, 5)));
        
        pooled_list.pop();
        pooled_list.pop();
        pooled_list.clear();
        assert!(pooled_list.pool_stats().is_some());
    }

    #[test]
    fn test_thread_safety_ready() {
        let list = Arc::new(Mutex::new(DoubleRinkedList::<i32>::new()));
        
        {
            let mut guard = list.lock().unwrap();
            guard.push(1).unwrap();
            guard.push(2).unwrap();
            assert_eq!(guard.len(), 2);
        }
        
        let guard = list.lock().unwrap();
        assert_eq!(guard.len(), 2);
    }

    #[test]
    fn test_error_handling() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        match list.get_at_index(0) {
            Err(ListError::IndexOutOfBounds { index, length }) => {
                assert_eq!(index, 0);
                assert_eq!(length, 0);
            }
            _ => panic!("Expected IndexOutOfBounds error"),
        }

        match list.remove_at_end() {
            Err(ListError::EmptyList) => {},
            _ => panic!("Expected EmptyList error"),
        }

        list.push(1).unwrap();
        list.push(2).unwrap();
        match list.set_capacity(1) {
            Err(ListError::InsufficientCapacity { requested, current }) => {
                assert_eq!(requested, 1);
                assert_eq!(current, 2);
            }
            _ => panic!("Expected InsufficientCapacity error"),
        }

        let error = ListError::IndexOutOfBounds { index: 5, length: 3 };
        let error_string = format!("{}", error);
        assert!(error_string.contains("Index 5 out of bounds"));
        assert!(error_string.contains("length: 3"));
    }

    #[test]
    fn test_edge_cases() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
        assert_eq!(list.pop(), None);
        assert_eq!(list.pop_front(), None);
        assert_eq!(list.get_at_begin(), None);
        assert_eq!(list.get_at_end(), None);
        assert!(list.contains(&1) == false);
        assert_eq!(list.index_of(&1), None);
        assert_eq!(list.to_vec(), vec![]);

        list.push(42).unwrap();
        assert_eq!(list.len(), 1);
        assert!(!list.is_empty());
        assert_eq!(list.get_at_begin(), Some(42));
        assert_eq!(list.get_at_end(), Some(42));
        assert_eq!(list.get_at_index(0).unwrap(), 42);
        assert!(list.contains(&42));
        assert_eq!(list.index_of(&42), Some(0));
        assert_eq!(list.to_vec(), vec![42]);

        assert_eq!(list.pop(), Some(42));
        assert!(list.is_empty());

        list.push(-1).unwrap();
        list.push(-5).unwrap();
        assert_eq!(list.to_vec(), vec![-1, -5]);
        assert!(list.contains(&-1));
        assert!(list.contains(&-5));
    }

    #[test]
    fn test_large_list_operations() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();
        
        let size: usize = 1000;
        for i in 0..size as i32 {
            list.push(i).unwrap();
        }
        
        assert_eq!(list.len(), size);
        assert_eq!(list.get_at_begin(), Some(0));
        assert_eq!(list.get_at_end(), Some((size - 1) as i32));
        
        let sum: i32 = list.clone().into_iter().sum();
        let expected_sum: i32 = (0..size as i32).sum();
        assert_eq!(sum, expected_sum);
        
        assert!(list.contains(&500));
        assert_eq!(list.index_of(&500), Some(500));
        assert!(!list.contains(&2000));
        
        for _ in 0..size {
            list.pop();
        }
        assert!(list.is_empty());
    }

    #[test]
    fn test_debug_functionality() {
        let mut list = DoubleRinkedList::new();
        
        assert!(!list.is_debug_enabled());
        
        list.enable_debug();
        assert!(list.is_debug_enabled());
        
        list.push(1).unwrap();
        list.push(2).unwrap();
        list.push(3).unwrap();
        
        list.disable_debug();
        assert!(!list.is_debug_enabled());
        
        list.push(4).unwrap();
        assert_eq!(list.len(), 4);
    }

    #[test]
    fn test_simple_push_performance() {
        println!("Simple Push Performance Test");
        
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();
        
        let start = Instant::now();
        for i in 0..10 {
            match list.push(i) {
                Ok(_) => {},
                Err(e) => {
                    println!("Push failed at {}: {}", i, e);
                    break;
                }
            }
        }
        let duration = start.elapsed();
        println!("10 pushes took: {:?}", duration);
        println!("Final length: {}", list.len());
        
        let vec = list.to_vec();
        println!("Vector: {:?}", vec);
        
        let mut list100: DoubleRinkedList<i32> = DoubleRinkedList::new();
        let start100 = Instant::now();
        
        for i in 0..100 {
            match list100.push(i) {
                Ok(_) => {},
                Err(e) => {
                    println!("Push failed at {}: {}", i, e);
                    break;
                }
            }
        }
        let duration100 = start100.elapsed();
        println!("100 pushes took: {:?}", duration100);
        println!("Final length: {}", list100.len());
        
        println!("Testing to_vec on 100 elements...");
        let vec_start = Instant::now();
        let vec100 = list100.to_vec();
        let vec_duration = Instant::now() - vec_start;
        println!("to_vec took: {:?}, collected {} items", vec_duration, vec100.len());
        
        assert_eq!(vec100.len(), 100);
        assert_eq!(vec100[0], 0);
        assert_eq!(vec100[99], 99);
    }

    #[test]
    fn test_debug_enabled_performance() {
        println!("Debug Performance Test");
        
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();
        list.enable_debug();
        
        println!("Testing with debug enabled (first 3 elements):");
        let start = Instant::now();
        for i in 0..3 {
            list.push(i).unwrap();
        }
        let duration = start.elapsed();
        println!("3 pushes with debug took: {:?}", duration);
        
        list.disable_debug();
        println!("Debug disabled, testing 7 more elements:");
        let start2 = Instant::now();
        for i in 3..10 {
            list.push(i).unwrap();
        }
        let duration2 = start2.elapsed();
        println!("7 pushes without debug took: {:?}", duration2);
        
        let vec = list.to_vec();
        println!("Final vector: {:?}", vec);
    }

    #[test]
    fn test_production_ready_basic() {
        let mut list = DoubleRinkedList::new();
        
        assert!(list.push(1).is_ok());
        assert!(list.push(2).is_ok());
        assert_eq!(list.len(), 2);
        assert_eq!(list.to_vec(), vec![1, 2]);
        
        assert_eq!(list.pop(), Some(2));
        assert_eq!(list.pop_front(), Some(1));
        assert!(list.is_empty());
    }

    #[test]
    fn test_error_handling_production() {
        let mut list: DoubleRinkedList<i32> = DoubleRinkedList::new();

        match list.get_at_index(0) {
            Err(ListError::IndexOutOfBounds { index: 0, length: 0 }) => {},
            _ => panic!("Expected specific error"),
        }

        match list.remove_at_end() {
            Err(ListError::EmptyList) => {},
            _ => panic!("Expected EmptyList"),
        }

        list.push(1).unwrap();
        list.push(2).unwrap();
        match list.set_capacity(1) {
            Err(ListError::InsufficientCapacity { requested: 1, current: 2 }) => {},
            _ => panic!("Expected capacity error"),
        }
    }

    #[test]
    fn test_capacity_management() {
        let mut list = DoubleRinkedList::with_capacity(10);
        assert_eq!(list.pool_stats(), Some((10, 10)));

        for i in 0..5 {
            list.push(i).unwrap();
        }
        assert_eq!(list.pool_stats(), Some((5, 10)));

        list.add_capacity(5);
        assert_eq!(list.pool_stats(), Some((10, 15)));

        list.set_capacity(20).unwrap();
        assert_eq!(list.pool_stats(), Some((15, 20)));

        assert!(list.set_capacity(1).is_err());
    }

    #[test]
    fn test_cursor_sequential_navigation() {
        let mut list = DoubleRinkedList::new();
        for i in 0..5 {
            list.push(i).unwrap();
        }

        list.move_cursor_at_begin().unwrap();
        
        let mut values = Vec::new();
        for _ in 0..3 {
            if let Ok(()) = list.move_cursor_to_next() {
                values.push("moved");
            }
        }
        
        assert_eq!(values.len(), 3);
        assert!(list.validate_cursor());
    }

    #[test]
    fn test_comprehensive_functional_operations() {
        let list: DoubleRinkedList<i32> = (1..=10).collect();

        let doubled = list.map(|x| x * 2);
        assert_eq!(doubled.len(), 10);
        assert_eq!(doubled.get_at_index(0).unwrap(), 2);

        let evens = list.filter(|x| x % 2 == 0);
        assert_eq!(evens.len(), 5);
        assert_eq!(evens.to_vec(), vec![2, 4, 6, 8, 10]);

        let sum = list.reduce(|acc, x| acc + x, 0);
        assert_eq!(sum, 55);

        assert!(list.every(|x| *x > 0));
        assert!(!list.every(|x| *x > 5));
        assert!(list.any(|x| *x > 5));
        assert!(!list.any(|x| *x > 10));
    }

    #[test] 
    fn test_splice_comprehensive() {
        let mut list: DoubleRinkedList<i32> = (0..10).collect();

        let removed = list.splice(3, 4, vec![100, 200]).unwrap();
        assert_eq!(removed, vec![3, 4, 5, 6]);
        assert_eq!(list.len(), 8);

        let first_part: Vec<i32> = list.clone().into_iter().take(3).collect();
        assert_eq!(first_part, vec![0, 1, 2]);

        let spliced_part: Vec<i32> = list.clone().into_iter().skip(3).take(2).collect();
        assert_eq!(spliced_part, vec![100, 200]);

        let last_part: Vec<i32> = list.clone().into_iter().skip(5).collect();
        assert_eq!(last_part, vec![7, 8, 9]);
    }

    #[test]
    fn test_large_scale_operations() {
        let mut list = DoubleRinkedList::with_capacity(1000);
        
        for i in 0..1000 {
            list.push(i).unwrap();
        }
        assert_eq!(list.len(), 1000);
        
        let middle_values: Vec<i32> = list.clone().into_iter().skip(400).take(200).collect();
        assert_eq!(middle_values.len(), 200);
        assert_eq!(middle_values[0], 400);
        assert_eq!(middle_values[199], 599);
        
        for _ in 0..500 {
            list.pop();
        }
        assert_eq!(list.len(), 500);
        
        list.clear();
        assert!(list.is_empty());
        assert!(list.pool_stats().is_some());
    }
}