summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ringbuf/src/tests/access.rs
blob: 17481b38bce1c5440b060e886db1cf9244beeb61 (plain)
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
use crate::RingBuffer;
use alloc::rc::Rc;
use core::mem::MaybeUninit;

#[test]
fn push() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (mut prod, mut cons) = buf.split();

    let vs_20 = (123, 456);
    let push_fn_20 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 2);
        assert_eq!(right.len(), 0);
        left[0] = MaybeUninit::new(vs_20.0);
        left[1] = MaybeUninit::new(vs_20.1);
        2
    };

    assert_eq!(unsafe { prod.push_access(push_fn_20) }, 2);

    assert_eq!(cons.pop().unwrap(), vs_20.0);
    assert_eq!(cons.pop().unwrap(), vs_20.1);
    assert_eq!(cons.pop(), None);

    let vs_11 = (123, 456);
    let push_fn_11 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 1);
        assert_eq!(right.len(), 1);
        left[0] = MaybeUninit::new(vs_11.0);
        right[0] = MaybeUninit::new(vs_11.1);
        2
    };

    assert_eq!(unsafe { prod.push_access(push_fn_11) }, 2);

    assert_eq!(cons.pop().unwrap(), vs_11.0);
    assert_eq!(cons.pop().unwrap(), vs_11.1);
    assert_eq!(cons.pop(), None);
}

#[test]
fn pop_full() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (_, mut cons) = buf.split();

    let dummy_fn = |_l: &mut [MaybeUninit<i32>], _r: &mut [MaybeUninit<i32>]| -> usize { 0 };
    assert_eq!(unsafe { cons.pop_access(dummy_fn) }, 0);
}

#[test]
fn pop_empty() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (_, mut cons) = buf.split();

    let dummy_fn = |_l: &mut [MaybeUninit<i32>], _r: &mut [MaybeUninit<i32>]| -> usize { 0 };
    assert_eq!(unsafe { cons.pop_access(dummy_fn) }, 0);
}

#[test]
fn pop() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (mut prod, mut cons) = buf.split();

    let vs_20 = (123, 456);

    assert_eq!(prod.push(vs_20.0), Ok(()));
    assert_eq!(prod.push(vs_20.1), Ok(()));
    assert_eq!(prod.push(0), Err(0));

    let pop_fn_20 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 2);
            assert_eq!(right.len(), 0);
            assert_eq!(left[0].assume_init(), vs_20.0);
            assert_eq!(left[1].assume_init(), vs_20.1);
            2
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_20) }, 2);

    let vs_11 = (123, 456);

    assert_eq!(prod.push(vs_11.0), Ok(()));
    assert_eq!(prod.push(vs_11.1), Ok(()));
    assert_eq!(prod.push(0), Err(0));

    let pop_fn_11 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 1);
            assert_eq!(right.len(), 1);
            assert_eq!(left[0].assume_init(), vs_11.0);
            assert_eq!(right[0].assume_init(), vs_11.1);
            2
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_11) }, 2);
}

#[test]
fn push_return() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (mut prod, mut cons) = buf.split();

    let push_fn_0 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 2);
        assert_eq!(right.len(), 0);
        0
    };

    assert_eq!(unsafe { prod.push_access(push_fn_0) }, 0);

    let push_fn_1 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 2);
        assert_eq!(right.len(), 0);
        left[0] = MaybeUninit::new(12);
        1
    };

    assert_eq!(unsafe { prod.push_access(push_fn_1) }, 1);

    let push_fn_2 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 1);
        assert_eq!(right.len(), 0);
        left[0] = MaybeUninit::new(34);
        1
    };

    assert_eq!(unsafe { prod.push_access(push_fn_2) }, 1);

    assert_eq!(cons.pop().unwrap(), 12);
    assert_eq!(cons.pop().unwrap(), 34);
    assert_eq!(cons.pop(), None);
}

#[test]
fn pop_return() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (mut prod, mut cons) = buf.split();

    assert_eq!(prod.push(12), Ok(()));
    assert_eq!(prod.push(34), Ok(()));
    assert_eq!(prod.push(0), Err(0));

    let pop_fn_0 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 2);
        assert_eq!(right.len(), 0);
        0
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_0) }, 0);

    let pop_fn_1 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 2);
            assert_eq!(right.len(), 0);
            assert_eq!(left[0].assume_init(), 12);
            1
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_1) }, 1);

    let pop_fn_2 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 1);
            assert_eq!(right.len(), 0);
            assert_eq!(left[0].assume_init(), 34);
            1
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_2) }, 1);
}

#[test]
fn push_pop() {
    let cap = 2;
    let buf = RingBuffer::<i32>::new(cap);
    let (mut prod, mut cons) = buf.split();

    let vs_20 = (123, 456);
    let push_fn_20 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 2);
        assert_eq!(right.len(), 0);
        left[0] = MaybeUninit::new(vs_20.0);
        left[1] = MaybeUninit::new(vs_20.1);
        2
    };

    assert_eq!(unsafe { prod.push_access(push_fn_20) }, 2);

    let pop_fn_20 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 2);
            assert_eq!(right.len(), 0);
            assert_eq!(left[0].assume_init(), vs_20.0);
            assert_eq!(left[1].assume_init(), vs_20.1);
            2
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_20) }, 2);

    let vs_11 = (123, 456);
    let push_fn_11 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        assert_eq!(left.len(), 1);
        assert_eq!(right.len(), 1);
        left[0] = MaybeUninit::new(vs_11.0);
        right[0] = MaybeUninit::new(vs_11.1);
        2
    };

    assert_eq!(unsafe { prod.push_access(push_fn_11) }, 2);

    let pop_fn_11 = |left: &mut [MaybeUninit<i32>], right: &mut [MaybeUninit<i32>]| -> usize {
        unsafe {
            assert_eq!(left.len(), 1);
            assert_eq!(right.len(), 1);
            assert_eq!(left[0].assume_init(), vs_11.0);
            assert_eq!(right[0].assume_init(), vs_11.1);
            2
        }
    };

    assert_eq!(unsafe { cons.pop_access(pop_fn_11) }, 2);
}

#[test]
fn discard() {
    // Initialize ringbuffer, prod and cons
    let rb = RingBuffer::<i8>::new(10);
    let (mut prod, mut cons) = rb.split();
    let mut i = 0;

    // Fill the buffer
    for _ in 0..10 {
        prod.push(i).unwrap();
        i += 1;
    }

    // Pop in the middle of the buffer
    assert_eq!(cons.discard(5), 5);

    // Make sure changes are taken into account
    assert_eq!(cons.pop().unwrap(), 5);

    // Fill the buffer again
    for _ in 0..5 {
        prod.push(i).unwrap();
        i += 1;
    }

    assert_eq!(cons.discard(6), 6);
    assert_eq!(cons.pop().unwrap(), 12);

    // Fill the buffer again
    for _ in 0..7 {
        prod.push(i).unwrap();
        i += 1;
    }

    // Ask too much, delete the max number of elements
    assert_eq!(cons.discard(10), 9);

    // Try to remove more than possible
    assert_eq!(cons.discard(1), 0);

    // Make sure it is still usable
    assert_eq!(cons.pop(), None);
    assert_eq!(prod.push(0), Ok(()));
    assert_eq!(cons.pop(), Some(0));
}

#[test]
fn discard_drop() {
    let rc = Rc::<()>::new(());

    static N: usize = 10;

    let rb = RingBuffer::<Rc<()>>::new(N);
    let (mut prod, mut cons) = rb.split();

    for _ in 0..N {
        prod.push(rc.clone()).unwrap();
    }

    assert_eq!(cons.len(), N);
    assert_eq!(Rc::strong_count(&rc), N + 1);

    assert_eq!(cons.discard(N), N);

    // Check ring buffer is empty
    assert_eq!(cons.len(), 0);
    // Check that items are dropped
    assert_eq!(Rc::strong_count(&rc), 1);
}