circular-buffer 0.1.7

Efficient, fixed-size, overwriting circular buffer
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
// Copyright © 2023, 2024 Andrea Corbellini and contributors
// SPDX-License-Identifier: BSD-3-Clause

//! Compare the correctness of `CircularBuffer` against a reference implementation (that is assumed
//! to be fully correct).
//!
//! This module applies random actions (like `push_back`, `pop_front`, ...) to a `CircularBuffer`
//! and to a reference implementation at the same time, and compares their result after each
//! action. The reference implementation currently is based on top of `VecDeque`.

use circular_buffer::CircularBuffer;
use drop_tracker::DropItem;
use drop_tracker::DropTracker;
use rand::Rng;
use rand::distributions::Distribution;
use rand::distributions::Standard;
use rand::distributions::Uniform;
use std::collections::VecDeque;
use std::fmt;
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::RangeInclusive;
use std::rc::Rc;

#[cfg(not(miri))]
const ROUNDS: usize = 200_000;

#[cfg(miri)]
const ROUNDS: usize = 200;

#[derive(Clone, Debug)]
enum Action<T> {
    BackMut(T),
    FrontMut(T),
    GetMut(usize, T),
    PushBack(T),
    PushFront(T),
    PopBack,
    PopFront,
    Remove(usize),
    Swap(usize, usize),
    SwapRemoveBack(usize),
    SwapRemoveFront(usize),
    TruncateBack(usize),
    TruncateFront(usize),
    Clear,
    Extend(Vec<T>),
    ExtendFromSlice(Vec<T>),
    RangeMut(RangeInclusive<usize>, Vec<T>),
    Drain(RangeInclusive<usize>),
    MakeContiguous,
}

impl<T> Distribution<Action<T>> for Standard
    where Standard: Distribution<T>
{
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Action<T> {
        fn random_vec<T, R: Rng + ?Sized>(rng: &mut R) -> Vec<T>
            where Standard: Distribution<T>
        {
            let size = rng.gen_range(0..128);
            let mut vec = Vec::with_capacity(size);
            for _ in 0..size {
                vec.push(rng.gen());
            }
            vec
        }

        let action_num: u8 = rng.gen_range(0..=6);

        match action_num {
            0 => Action::PushBack(rng.gen()),
            1 => Action::PushFront(rng.gen()),
            2 => Action::PopBack,
            3 => Action::PopFront,
            4 => Action::Clear,
            5 => Action::Extend(random_vec(rng)),
            6 => Action::ExtendFromSlice(random_vec(rng)),
            _ => unreachable!(),
        }
    }
}

impl<T> Distribution<Action<T>> for Uniform<usize>
    where Standard: Distribution<T>
{
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Action<T> {
        fn random_vec<T, R: Rng + ?Sized>(rng: &mut R) -> Vec<T>
            where Standard: Distribution<T>
        {
            let size = rng.gen_range(0..128);
            let mut vec = Vec::with_capacity(size);
            for _ in 0..size {
                vec.push(rng.gen());
            }
            vec
        }

        fn random_range<D: Distribution<usize>, R: Rng + ?Sized>(dist: &D, rng: &mut R) -> RangeInclusive<usize> {
            let low = dist.sample(rng);
            let high = dist.sample(rng).max(low);
            low..=high
        }

        let action_num: u8 = rng.gen_range(0..=18);

        match action_num {
            0  => Action::BackMut(rng.gen()),
            1  => Action::FrontMut(rng.gen()),
            2  => Action::GetMut(self.sample(rng), rng.gen()),
            3  => Action::PushBack(rng.gen()),
            4  => Action::PushFront(rng.gen()),
            5  => Action::PopBack,
            6  => Action::PopFront,
            7  => Action::Remove(self.sample(rng)),
            8  => Action::Swap(self.sample(rng), self.sample(rng)),
            9  => Action::SwapRemoveBack(self.sample(rng)),
            10 => Action::SwapRemoveFront(self.sample(rng)),
            11 => Action::TruncateBack(self.sample(rng)),
            12 => Action::TruncateFront(self.sample(rng)),
            13 => Action::Clear,
            14 => Action::Extend(random_vec(rng)),
            15 => Action::ExtendFromSlice(random_vec(rng)),
            16 => Action::RangeMut(random_range(self, rng), random_vec(rng)),
            17 => Action::Drain(random_range(self, rng)),
            18 => Action::MakeContiguous,
            _ => unreachable!(),
        }
    }
}

#[derive(Copy, Clone, Debug)]
enum Direction {
    Back,
    Front,
}

#[derive(Debug)]
struct Reference<T> {
    inner: VecDeque<T>,
    max_len: usize,
}

impl<T> Reference<T> {
    fn new(max_len: usize) -> Self {
        Self {
            inner: VecDeque::new(),
            max_len,
        }
    }

    fn trim(&mut self, direction: Direction) {
        match direction {
            Direction::Back  => self.trim_back(),
            Direction::Front => self.trim_front(),
        }
    }

    fn trim_back(&mut self) {
        while self.len() > self.max_len {
            self.pop_back().unwrap();
        }
    }

    fn trim_front(&mut self) {
        while self.len() > self.max_len {
            self.pop_front().unwrap();
        }
    }
}

impl<T> Deref for Reference<T> {
    type Target = VecDeque<T>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for Reference<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
enum Result<T> {
    None,
    Val(T),
    Vec(Vec<T>),
}

impl<T> From<Option<T>> for Result<T> {
    fn from(opt: Option<T>) -> Self {
        match opt {
            Some(val) => Self::Val(val),
            None      => Self::None,
        }
    }
}

impl<T> FromIterator<T> for Result<T> {
    fn from_iter<I>(iter: I) -> Self
       where I: IntoIterator<Item = T>
    {
        let vec = Vec::from_iter(iter);
        Self::Vec(vec)
    }
}

trait Perform<T> {
    fn perform(&mut self, action: Action<T>) -> Result<T>;
}

impl<const N: usize, T> Perform<T> for CircularBuffer<N, T>
    where T: Clone
{
    fn perform(&mut self, action: Action<T>) -> Result<T> {
        match action {
            Action::BackMut(elem)          => { *self.back_mut().unwrap() = elem; Result::None },
            Action::FrontMut(elem)         => { *self.front_mut().unwrap() = elem; Result::None },
            Action::GetMut(index, elem)    => { *self.get_mut(index).unwrap() = elem; Result::None },
            Action::PushBack(elem)         => { self.push_back(elem); Result::None },
            Action::PushFront(elem)        => { self.push_front(elem); Result::None },
            Action::PopBack                => self.pop_back().into(),
            Action::PopFront               => self.pop_front().into(),
            Action::Remove(index)          => self.remove(index).into(),
            Action::Swap(x, y)             => { self.swap(x, y); Result::None },
            Action::SwapRemoveBack(index)  => { self.swap_remove_back(index); Result::None },
            Action::SwapRemoveFront(index) => { self.swap_remove_front(index); Result::None },
            Action::TruncateBack(index)    => { self.truncate_back(index); Result::None },
            Action::TruncateFront(index)   => { self.truncate_front(index); Result::None },
            Action::Clear                  => { self.clear(); Result::None },
            Action::Extend(elems)          => { self.extend(elems); Result::None },
            Action::ExtendFromSlice(elems) => { self.extend_from_slice(&elems[..]); Result::None },
            Action::RangeMut(range, elems) => {
                self.range_mut(range)
                    .zip(elems)
                    .map(|(elem, replacement)| *elem = replacement)
                    .count();
                Result::None
            },
            Action::Drain(range)           => { self.drain(range).collect() },
            Action::MakeContiguous         => { self.make_contiguous().iter().cloned().collect() },
        }
    }
}

impl<T> Perform<T> for VecDeque<T>
    where T: Clone
{
    fn perform(&mut self, action: Action<T>) -> Result<T> {
        match action {
            Action::BackMut(elem)          => { *self.back_mut().unwrap() = elem; Result::None },
            Action::FrontMut(elem)         => { *self.front_mut().unwrap() = elem; Result::None },
            Action::GetMut(index, elem)    => { *self.get_mut(index).unwrap() = elem; Result::None },
            Action::PushBack(elem)         => { self.push_back(elem); Result::None },
            Action::PushFront(elem)        => { self.push_front(elem); Result::None },
            Action::PopBack                => self.pop_back().into(),
            Action::PopFront               => self.pop_front().into(),
            Action::Remove(index)          => self.remove(index).into(),
            Action::Swap(x, y)             => { self.swap(x, y); Result::None },
            Action::SwapRemoveBack(index)  => { self.swap_remove_back(index); Result::None },
            Action::SwapRemoveFront(index) => { self.swap_remove_front(index); Result::None },
            Action::TruncateBack(size)     => {
                while self.len() > size { let _ = self.pop_back(); };
                Result::None
            },
            Action::TruncateFront(size)    => {
                while self.len() > size { let _ = self.pop_front(); };
                Result::None
            },
            Action::Clear                  => { self.clear(); Result::None },
            Action::Extend(elems)          => { self.extend(elems); Result::None },
            Action::ExtendFromSlice(elems) => { self.extend(elems); Result::None },
            Action::RangeMut(range, elems) => {
                self.range_mut(range)
                    .zip(elems)
                    .map(|(elem, replacement)| *elem = replacement)
                    .count();
                Result::None
            },
            Action::Drain(range)           => { self.drain(range).collect() },
            Action::MakeContiguous         => { self.make_contiguous().iter().cloned().collect() },
        }
    }
}

impl<T> Perform<T> for Reference<T>
    where T: Clone
{
    fn perform(&mut self, action: Action<T>) -> Result<T> {
        let trim_direction = match action {
            Action::PushBack(_)        => Some(Direction::Front),
            Action::PushFront(_)       => Some(Direction::Back),
            Action::Extend(_)          => Some(Direction::Front),
            Action::ExtendFromSlice(_) => Some(Direction::Front),
            _                          => None,
        };

        let result = self.inner.perform(action);

        if let Some(direction) = trim_direction {
            self.trim(direction);
        }

        result
    }
}

fn test<const N: usize, T>()
    where T: Clone + PartialEq + fmt::Debug,
          Standard: Distribution<T>
{
    let mut reference = Reference::<T>::new(N);
    let mut buffer = CircularBuffer::<N, T>::boxed();
    let mut rng = rand::thread_rng();

    for _ in 0..ROUNDS {
        // Generate a random action
        let action: Action<T> = if reference.is_empty() {
            <Standard as Distribution<Action<T>>>::sample(&Standard, &mut rng)
        } else {
            Uniform::from(0..reference.len()).sample(&mut rng)
        };

        println!("{action:?}");

        // Perform the action on both the reference implementation and the CircularBuffer
        let expected = reference.perform(action.clone());
        let actual = buffer.perform(action);

        // Compare the return value of both implementations
        assert_eq!(expected, actual);

        // Compare the state of both implementations
        let expected_items = reference.iter()
                                      .cloned()
                                      .collect::<Vec<T>>();
        #[allow(clippy::eq_op)]
        { assert_eq!(buffer, buffer); }
        assert_eq!(*buffer, &expected_items[..]);
        assert_eq!(buffer.to_vec(), expected_items);

        assert_eq!(reference.len(), buffer.len());
        assert_eq!(reference.is_empty(), buffer.is_empty());

        assert_eq!(reference.iter().collect::<Vec<&T>>(),
                   buffer.iter().collect::<Vec<&T>>());
        assert_eq!(reference.iter_mut().collect::<Vec<&mut T>>(),
                   buffer.iter_mut().collect::<Vec<&mut T>>());

        assert_eq!(reference.iter().rev().collect::<Vec<&T>>(),
                   buffer.iter().rev().collect::<Vec<&T>>());
        assert_eq!(reference.iter_mut().rev().collect::<Vec<&mut T>>(),
                   buffer.iter_mut().rev().collect::<Vec<&mut T>>());
    }
}

#[test]
fn zero() {
    test::<0, u64>();
}

#[test]
fn small() {
    test::<10, u64>();
}

#[test]
fn medium() {
    test::<1_000, u64>();
}

#[test]
fn large() {
    test::<1_000_000, u64>();
}

#[test]
fn largest_with_zero_sized_struct() {
    type Zst = ();
    assert_eq!(mem::size_of::<Zst>(), 0);
    test::<{ usize::MAX }, Zst>();
}

#[test]
fn drop() {
    static mut TRACKER: Option<DropTracker<u64>> = None;

    // SAFETY: the assumption is that this test function will be called only once
    unsafe { TRACKER.replace(DropTracker::new()); }

    fn tracker() -> &'static DropTracker<u64> {
        unsafe { TRACKER.as_ref().unwrap() }
    }

    fn tracker_mut() -> &'static mut DropTracker<u64> {
        unsafe { TRACKER.as_mut().unwrap() }
    }

    #[derive(Clone, PartialEq, Eq, Debug)]
    struct Item(Rc<DropItem<u64>>);

    impl Distribution<Item> for Standard {
        fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Item {
            let n = rng.gen();
            Item(Rc::new(tracker_mut().track(n)))
        }
    }

    test::<100, Item>();

    tracker().assert_fully_dropped();
}