ftui-widgets 0.4.0

Widget library built on FrankenTUI render and layout.
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
#![forbid(unsafe_code)]

//! Bounded circular buffer for log storage.
//!
//! [`LogRing`] provides memory-efficient storage for log lines that evicts
//! oldest entries when full. It supports absolute indexing across the entire
//! history (even for evicted items) and optional overflow file persistence.
//!
//! # Example
//!
//! ```
//! use ftui_widgets::LogRing;
//!
//! let mut ring = LogRing::new(3);
//! ring.push("line 1");
//! ring.push("line 2");
//! ring.push("line 3");
//! ring.push("line 4"); // evicts "line 1"
//!
//! assert_eq!(ring.len(), 3);
//! assert_eq!(ring.total_count(), 4);
//! assert_eq!(ring.get(3), Some(&"line 4"));
//! assert_eq!(ring.get(0), None); // evicted
//! ```

use std::collections::VecDeque;
use std::ops::Range;

/// Circular buffer for log storage with FIFO eviction.
///
/// Memory-efficient storage that maintains a sliding window of the most recent
/// items. Older items are evicted when capacity is reached.
#[derive(Debug, Clone)]
pub struct LogRing<T> {
    /// Circular buffer storage
    ring: VecDeque<T>,

    /// Maximum capacity
    capacity: usize,

    /// Total items ever added (for accurate absolute indexing)
    total_count: usize,
}

impl<T> LogRing<T> {
    /// Create a new LogRing with the specified capacity.
    ///
    /// # Panics
    ///
    /// Panics if capacity is 0.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0, "LogRing capacity must be greater than 0");
        Self {
            ring: VecDeque::with_capacity(capacity),
            capacity,
            total_count: 0,
        }
    }

    /// Add an item to the ring.
    ///
    /// If the ring is at capacity, the oldest item is evicted first.
    pub fn push(&mut self, item: T) {
        self.total_count = self.total_count.saturating_add(1);

        if self.ring.len() >= self.capacity {
            self.ring.pop_front();
        }

        self.ring.push_back(item);
    }

    /// Add multiple items efficiently.
    pub fn extend(&mut self, items: impl IntoIterator<Item = T>) {
        for item in items {
            self.push(item);
        }
    }

    /// Get item by absolute index (across entire history).
    ///
    /// Returns `None` if the index is out of range or the item has been evicted.
    #[must_use = "use the returned item (if any)"]
    pub fn get(&self, absolute_idx: usize) -> Option<&T> {
        let ring_start = self.first_index();

        if absolute_idx >= ring_start && absolute_idx < self.total_count {
            self.ring.get(absolute_idx - ring_start)
        } else {
            None
        }
    }

    /// Get mutable reference by absolute index.
    #[must_use = "use the returned item (if any)"]
    pub fn get_mut(&mut self, absolute_idx: usize) -> Option<&mut T> {
        let ring_start = self.first_index();

        if absolute_idx >= ring_start && absolute_idx < self.total_count {
            self.ring.get_mut(absolute_idx - ring_start)
        } else {
            None
        }
    }

    /// Get a range of items by absolute indices.
    ///
    /// Returns references to items that are still in memory within the range.
    /// Items that have been evicted are skipped.
    pub fn get_range(&self, range: Range<usize>) -> impl Iterator<Item = &T> {
        let ring_start = self.first_index();
        let ring_end = self.total_count;

        // Clamp range to what's in memory
        let start = range.start.max(ring_start);
        let end = range.end.min(ring_end);

        (start..end).filter_map(move |i| self.get(i))
    }

    /// Total items ever added (including evicted).
    #[must_use]
    pub const fn total_count(&self) -> usize {
        self.total_count
    }

    /// Number of items currently in memory.
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        self.ring.len()
    }

    /// Check if the ring is empty.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.ring.is_empty()
    }

    /// Maximum capacity of the ring.
    #[must_use]
    pub const fn capacity(&self) -> usize {
        self.capacity
    }

    /// First absolute index still in memory.
    #[must_use]
    pub fn first_index(&self) -> usize {
        self.total_count.saturating_sub(self.ring.len())
    }

    /// Last absolute index (most recent item).
    ///
    /// Returns `None` if the ring is empty.
    #[must_use = "use the returned index (if any)"]
    pub fn last_index(&self) -> Option<usize> {
        if self.total_count > 0 {
            Some(self.total_count - 1)
        } else {
            None
        }
    }

    /// Check if an absolute index is still in memory.
    #[must_use]
    pub fn is_in_memory(&self, absolute_idx: usize) -> bool {
        absolute_idx >= self.first_index() && absolute_idx < self.total_count
    }

    /// Number of items that have been evicted.
    #[must_use]
    pub fn evicted_count(&self) -> usize {
        self.first_index()
    }

    /// Clear all items.
    ///
    /// Note: `total_count` is preserved for consistency with absolute indexing.
    pub fn clear(&mut self) {
        self.ring.clear();
    }

    /// Clear all items and reset counters.
    pub fn reset(&mut self) {
        self.ring.clear();
        self.total_count = 0;
    }

    /// Get the most recent item.
    #[must_use = "use the returned item (if any)"]
    pub fn back(&self) -> Option<&T> {
        self.ring.back()
    }

    /// Get the oldest item still in memory.
    #[must_use = "use the returned item (if any)"]
    pub fn front(&self) -> Option<&T> {
        self.ring.front()
    }

    /// Iterate over items currently in memory (oldest to newest).
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
        self.ring.iter()
    }

    /// Iterate over items with their absolute indices.
    pub fn iter_indexed(&self) -> impl DoubleEndedIterator<Item = (usize, &T)> {
        let start = self.first_index();
        self.ring
            .iter()
            .enumerate()
            .map(move |(i, item)| (start + i, item))
    }

    /// Drain all items from the ring.
    pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
        self.ring.drain(..)
    }
}

impl<T> Default for LogRing<T> {
    fn default() -> Self {
        Self::new(1024) // Reasonable default capacity
    }
}

impl<T> Extend<T> for LogRing<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for item in iter {
            self.push(item);
        }
    }
}

impl<T> FromIterator<T> for LogRing<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let items: Vec<T> = iter.into_iter().collect();
        let capacity = items.len().max(1);
        let mut ring = Self::new(capacity);
        ring.extend(items);
        ring
    }
}

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

    #[test]
    fn new_creates_empty_ring() {
        let ring: LogRing<i32> = LogRing::new(10);
        assert!(ring.is_empty());
        assert_eq!(ring.len(), 0);
        assert_eq!(ring.total_count(), 0);
        assert_eq!(ring.capacity(), 10);
    }

    #[test]
    #[should_panic(expected = "capacity must be greater than 0")]
    fn new_panics_on_zero_capacity() {
        let _ring: LogRing<i32> = LogRing::new(0);
    }

    #[test]
    fn push_adds_items() {
        let mut ring = LogRing::new(5);
        ring.push("a");
        ring.push("b");
        ring.push("c");

        assert_eq!(ring.len(), 3);
        assert_eq!(ring.total_count(), 3);
        assert_eq!(ring.get(0), Some(&"a"));
        assert_eq!(ring.get(1), Some(&"b"));
        assert_eq!(ring.get(2), Some(&"c"));
    }

    #[test]
    fn push_evicts_oldest_when_full() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        ring.push(2);
        ring.push(3);
        ring.push(4); // evicts 1
        ring.push(5); // evicts 2

        assert_eq!(ring.len(), 3);
        assert_eq!(ring.total_count(), 5);
        assert_eq!(ring.get(0), None); // evicted
        assert_eq!(ring.get(1), None); // evicted
        assert_eq!(ring.get(2), Some(&3));
        assert_eq!(ring.get(3), Some(&4));
        assert_eq!(ring.get(4), Some(&5));
    }

    #[test]
    fn first_and_last_index() {
        let mut ring = LogRing::new(3);
        assert_eq!(ring.first_index(), 0);
        assert_eq!(ring.last_index(), None);

        ring.push("a");
        ring.push("b");
        assert_eq!(ring.first_index(), 0);
        assert_eq!(ring.last_index(), Some(1));

        ring.push("c");
        ring.push("d"); // evicts "a"
        assert_eq!(ring.first_index(), 1);
        assert_eq!(ring.last_index(), Some(3));
    }

    #[test]
    fn get_range_returns_available_items() {
        let mut ring = LogRing::new(3);
        ring.push("a");
        ring.push("b");
        ring.push("c");
        ring.push("d"); // evicts "a"

        let items: Vec<_> = ring.get_range(0..5).collect();
        assert_eq!(items, vec![&"b", &"c", &"d"]);

        let items: Vec<_> = ring.get_range(2..4).collect();
        assert_eq!(items, vec![&"c", &"d"]);
    }

    #[test]
    fn is_in_memory() {
        let mut ring = LogRing::new(2);
        ring.push(1);
        ring.push(2);
        ring.push(3); // evicts 1

        assert!(!ring.is_in_memory(0));
        assert!(ring.is_in_memory(1));
        assert!(ring.is_in_memory(2));
        assert!(!ring.is_in_memory(3));
    }

    #[test]
    fn evicted_count() {
        let mut ring = LogRing::new(2);
        assert_eq!(ring.evicted_count(), 0);

        ring.push(1);
        ring.push(2);
        assert_eq!(ring.evicted_count(), 0);

        ring.push(3); // evicts 1
        assert_eq!(ring.evicted_count(), 1);

        ring.push(4); // evicts 2
        assert_eq!(ring.evicted_count(), 2);
    }

    #[test]
    fn clear_preserves_total_count() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        ring.push(3);

        ring.clear();
        assert!(ring.is_empty());
        assert_eq!(ring.total_count(), 3);
        assert_eq!(ring.first_index(), 3);
    }

    #[test]
    fn reset_clears_everything() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        ring.push(3);

        ring.reset();
        assert!(ring.is_empty());
        assert_eq!(ring.total_count(), 0);
        assert_eq!(ring.first_index(), 0);
    }

    #[test]
    fn front_and_back() {
        let mut ring = LogRing::new(3);
        assert_eq!(ring.front(), None);
        assert_eq!(ring.back(), None);

        ring.push("first");
        ring.push("middle");
        ring.push("last");

        assert_eq!(ring.front(), Some(&"first"));
        assert_eq!(ring.back(), Some(&"last"));

        ring.push("newest"); // evicts "first"
        assert_eq!(ring.front(), Some(&"middle"));
        assert_eq!(ring.back(), Some(&"newest"));
    }

    #[test]
    fn iter_yields_oldest_to_newest() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        ring.push(2);
        ring.push(3);

        let items: Vec<_> = ring.iter().copied().collect();
        assert_eq!(items, vec![1, 2, 3]);
    }

    #[test]
    fn iter_indexed_includes_absolute_indices() {
        let mut ring = LogRing::new(2);
        ring.push("a");
        ring.push("b");
        ring.push("c"); // evicts "a"

        let indexed: Vec<_> = ring.iter_indexed().collect();
        assert_eq!(indexed, vec![(1, &"b"), (2, &"c")]);
    }

    #[test]
    fn extend_adds_multiple_items() {
        let mut ring = LogRing::new(5);
        ring.extend(vec![1, 2, 3]);

        assert_eq!(ring.len(), 3);
        assert_eq!(ring.total_count(), 3);
    }

    #[test]
    fn from_iter_creates_ring() {
        let ring: LogRing<i32> = vec![1, 2, 3, 4, 5].into_iter().collect();
        assert_eq!(ring.len(), 5);
        assert_eq!(ring.capacity(), 5);
    }

    #[test]
    fn default_has_reasonable_capacity() {
        let ring: LogRing<i32> = LogRing::default();
        assert_eq!(ring.capacity(), 1024);
    }

    #[test]
    fn get_mut_allows_modification() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        ring.push(2);

        if let Some(item) = ring.get_mut(0) {
            *item = 10;
        }

        assert_eq!(ring.get(0), Some(&10));
    }

    #[test]
    fn drain_removes_all_items() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        ring.push(3);

        let drained: Vec<_> = ring.drain().collect();
        assert_eq!(drained, vec![1, 2, 3]);
        assert!(ring.is_empty());
        assert_eq!(ring.total_count(), 3); // preserved
    }

    #[test]
    fn handles_large_total_count() {
        let mut ring = LogRing::new(2);
        for i in 0..1000 {
            ring.push(i);
        }

        assert_eq!(ring.len(), 2);
        assert_eq!(ring.total_count(), 1000);
        assert_eq!(ring.first_index(), 998);
        assert_eq!(ring.get(998), Some(&998));
        assert_eq!(ring.get(999), Some(&999));
    }

    // ── Edge-case tests (bd-2n2oo) ──────────────────────────

    #[test]
    fn capacity_one_ring() {
        let mut ring = LogRing::new(1);
        ring.push("a");
        assert_eq!(ring.len(), 1);
        assert_eq!(ring.get(0), Some(&"a"));

        ring.push("b"); // evicts "a"
        assert_eq!(ring.len(), 1);
        assert_eq!(ring.total_count(), 2);
        assert_eq!(ring.get(0), None);
        assert_eq!(ring.get(1), Some(&"b"));
        assert_eq!(ring.first_index(), 1);
    }

    #[test]
    fn get_mut_evicted_returns_none() {
        let mut ring = LogRing::new(2);
        ring.push(1);
        ring.push(2);
        ring.push(3); // evicts 1
        assert!(ring.get_mut(0).is_none());
    }

    #[test]
    fn get_mut_beyond_total_returns_none() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        assert!(ring.get_mut(1).is_none());
        assert!(ring.get_mut(100).is_none());
    }

    #[test]
    fn get_beyond_total_count() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        assert_eq!(ring.get(1), None); // total_count=1, idx 1 is out of range
        assert_eq!(ring.get(usize::MAX), None);
    }

    #[test]
    fn get_range_empty_range() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        let items: Vec<_> = ring.get_range(1..1).collect();
        assert!(items.is_empty());
    }

    #[test]
    fn get_range_inverted_start_gt_end() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        // Range start > end is empty. Use black_box to avoid clippy's
        // `reversed_empty_ranges` lint while still exercising the path.
        let start = std::hint::black_box(5usize);
        let end = std::hint::black_box(2usize);
        let items: Vec<_> = ring.get_range(start..end).collect();
        assert!(items.is_empty());
    }

    #[test]
    fn get_range_fully_evicted() {
        let mut ring = LogRing::new(2);
        ring.push(1);
        ring.push(2);
        ring.push(3);
        ring.push(4);
        // indices 0..2 are evicted, first_index=2
        let items: Vec<_> = ring.get_range(0..2).collect();
        assert!(items.is_empty());
    }

    #[test]
    fn get_range_fully_future() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        // total_count=1, range 5..10 is entirely future
        let items: Vec<_> = ring.get_range(5..10).collect();
        assert!(items.is_empty());
    }

    #[test]
    fn get_range_partial_overlap() {
        let mut ring = LogRing::new(3);
        ring.push(10);
        ring.push(20);
        ring.push(30);
        ring.push(40); // evicts 10, first_index=1
        // Request range 0..5, only 1..4 is in memory
        let items: Vec<_> = ring.get_range(0..5).collect();
        assert_eq!(items, vec![&20, &30, &40]);
    }

    #[test]
    fn iter_empty_ring() {
        let ring: LogRing<i32> = LogRing::new(5);
        assert_eq!(ring.iter().count(), 0);
    }

    #[test]
    fn iter_indexed_empty_ring() {
        let ring: LogRing<i32> = LogRing::new(5);
        assert_eq!(ring.iter_indexed().count(), 0);
    }

    #[test]
    fn iter_reverse() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        ring.push(2);
        ring.push(3);
        let rev: Vec<_> = ring.iter().rev().copied().collect();
        assert_eq!(rev, vec![3, 2, 1]);
    }

    #[test]
    fn iter_indexed_reverse() {
        let mut ring = LogRing::new(2);
        ring.push("a");
        ring.push("b");
        ring.push("c"); // evicts "a"
        let rev: Vec<_> = ring.iter_indexed().rev().collect();
        assert_eq!(rev, vec![(2, &"c"), (1, &"b")]);
    }

    #[test]
    fn extend_empty_iterator() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.extend(std::iter::empty::<i32>());
        assert_eq!(ring.len(), 1);
        assert_eq!(ring.total_count(), 1);
    }

    #[test]
    fn extend_trait_impl() {
        let mut ring = LogRing::new(5);
        Extend::extend(&mut ring, vec![1, 2, 3]);
        assert_eq!(ring.len(), 3);
        assert_eq!(ring.total_count(), 3);
    }

    #[test]
    fn from_iter_empty() {
        let ring: LogRing<i32> = std::iter::empty().collect();
        assert_eq!(ring.capacity(), 1); // max(0, 1)
        assert!(ring.is_empty());
    }

    #[test]
    fn from_iter_single() {
        let ring: LogRing<i32> = std::iter::once(42).collect();
        assert_eq!(ring.capacity(), 1);
        assert_eq!(ring.len(), 1);
        assert_eq!(ring.get(0), Some(&42));
    }

    #[test]
    fn clone_independence() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.push(2);
        let mut cloned = ring.clone();
        cloned.push(3);
        assert_eq!(ring.len(), 2);
        assert_eq!(cloned.len(), 3);
        assert_eq!(ring.total_count(), 2);
        assert_eq!(cloned.total_count(), 3);
    }

    #[test]
    fn debug_format() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        let dbg = format!("{:?}", ring);
        assert!(dbg.contains("LogRing"));
    }

    #[test]
    fn drain_empty_ring() {
        let mut ring: LogRing<i32> = LogRing::new(5);
        let drained: Vec<_> = ring.drain().collect();
        assert!(drained.is_empty());
        assert_eq!(ring.total_count(), 0);
    }

    #[test]
    fn clear_then_push_continues_absolute_index() {
        let mut ring = LogRing::new(5);
        ring.push("a");
        ring.push("b");
        ring.clear();
        assert_eq!(ring.total_count(), 2);
        assert_eq!(ring.first_index(), 2);

        ring.push("c");
        assert_eq!(ring.total_count(), 3);
        assert_eq!(ring.first_index(), 2);
        assert_eq!(ring.get(2), Some(&"c"));
        assert_eq!(ring.get(0), None); // old indices gone
        assert_eq!(ring.get(1), None);
    }

    #[test]
    fn reset_then_push_starts_fresh() {
        let mut ring = LogRing::new(5);
        ring.push("a");
        ring.push("b");
        ring.reset();
        ring.push("c");
        assert_eq!(ring.total_count(), 1);
        assert_eq!(ring.first_index(), 0);
        assert_eq!(ring.get(0), Some(&"c"));
    }

    #[test]
    fn last_index_after_clear() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.clear();
        assert_eq!(ring.last_index(), Some(0));
        // total_count is 1, so last_index = 0
    }

    #[test]
    fn last_index_after_reset() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.reset();
        assert_eq!(ring.last_index(), None);
    }

    #[test]
    fn front_back_after_clear() {
        let mut ring = LogRing::new(5);
        ring.push(1);
        ring.clear();
        assert_eq!(ring.front(), None);
        assert_eq!(ring.back(), None);
    }

    #[test]
    fn is_in_memory_at_exact_boundaries() {
        let mut ring = LogRing::new(3);
        ring.push(10);
        ring.push(20);
        ring.push(30);
        // first_index=0, total_count=3
        assert!(ring.is_in_memory(0));
        assert!(ring.is_in_memory(2));
        assert!(!ring.is_in_memory(3)); // == total_count, out of range
    }

    #[test]
    fn extend_causes_eviction() {
        let mut ring = LogRing::new(3);
        ring.extend(vec![1, 2, 3, 4, 5]);
        assert_eq!(ring.len(), 3);
        assert_eq!(ring.total_count(), 5);
        assert_eq!(ring.get(2), Some(&3));
        assert_eq!(ring.get(4), Some(&5));
        assert_eq!(ring.get(0), None); // evicted
    }

    #[test]
    fn get_range_exact_memory_window() {
        let mut ring = LogRing::new(3);
        ring.push(1);
        ring.push(2);
        ring.push(3);
        ring.push(4); // first_index=1
        let items: Vec<_> = ring.get_range(1..4).collect();
        assert_eq!(items, vec![&2, &3, &4]);
    }

    #[test]
    fn push_with_string_types() {
        let mut ring = LogRing::new(2);
        ring.push(String::from("hello"));
        ring.push(String::from("world"));
        ring.push(String::from("foo")); // evicts "hello"
        assert_eq!(ring.get(1), Some(&String::from("world")));
        assert_eq!(ring.get(2), Some(&String::from("foo")));
    }
}