]> git.proxmox.com Git - rustc.git/blame - library/std/src/sync/mpsc/spsc_queue/tests.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / library / std / src / sync / mpsc / spsc_queue / tests.rs
CommitLineData
1b1a35ee
XL
1use super::Queue;
2use crate::sync::mpsc::channel;
3use crate::sync::Arc;
4use crate::thread;
5
6#[test]
7fn smoke() {
8 unsafe {
9 let queue = Queue::with_additions(0, (), ());
10 queue.push(1);
11 queue.push(2);
12 assert_eq!(queue.pop(), Some(1));
13 assert_eq!(queue.pop(), Some(2));
14 assert_eq!(queue.pop(), None);
15 queue.push(3);
16 queue.push(4);
17 assert_eq!(queue.pop(), Some(3));
18 assert_eq!(queue.pop(), Some(4));
19 assert_eq!(queue.pop(), None);
20 }
21}
22
23#[test]
24fn peek() {
25 unsafe {
26 let queue = Queue::with_additions(0, (), ());
27 queue.push(vec![1]);
28
29 // Ensure the borrowchecker works
30 match queue.peek() {
31 Some(vec) => {
32 assert_eq!(&*vec, &[1]);
33 }
34 None => unreachable!(),
35 }
36
37 match queue.pop() {
38 Some(vec) => {
39 assert_eq!(&*vec, &[1]);
40 }
41 None => unreachable!(),
42 }
43 }
44}
45
46#[test]
47fn drop_full() {
48 unsafe {
49 let q: Queue<Box<_>> = Queue::with_additions(0, (), ());
923072b8
FG
50 q.push(Box::new(1));
51 q.push(Box::new(2));
1b1a35ee
XL
52 }
53}
54
55#[test]
56fn smoke_bound() {
57 unsafe {
58 let q = Queue::with_additions(0, (), ());
59 q.push(1);
60 q.push(2);
61 assert_eq!(q.pop(), Some(1));
62 assert_eq!(q.pop(), Some(2));
63 assert_eq!(q.pop(), None);
64 q.push(3);
65 q.push(4);
66 assert_eq!(q.pop(), Some(3));
67 assert_eq!(q.pop(), Some(4));
68 assert_eq!(q.pop(), None);
69 }
70}
71
72#[test]
73fn stress() {
74 unsafe {
75 stress_bound(0);
76 stress_bound(1);
77 }
78
79 unsafe fn stress_bound(bound: usize) {
80 let q = Arc::new(Queue::with_additions(bound, (), ()));
81
82 let (tx, rx) = channel();
83 let q2 = q.clone();
84 let _t = thread::spawn(move || {
85 for _ in 0..100000 {
86 loop {
87 match q2.pop() {
88 Some(1) => break,
89 Some(_) => panic!(),
90 None => {}
91 }
92 }
93 }
94 tx.send(()).unwrap();
95 });
96 for _ in 0..100000 {
97 q.push(1);
98 }
99 rx.recv().unwrap();
100 }
101}