]> git.proxmox.com Git - rustc.git/blob - vendor/crossbeam-queue-0.1.2/tests/seg_queue.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / crossbeam-queue-0.1.2 / tests / seg_queue.rs
1 extern crate crossbeam_queue;
2 extern crate crossbeam_utils;
3 extern crate rand;
4
5 use std::sync::atomic::{AtomicUsize, Ordering};
6
7 use crossbeam_queue::SegQueue;
8 use crossbeam_utils::thread::scope;
9 use rand::{thread_rng, Rng};
10
11 #[test]
12 fn smoke() {
13 let q = SegQueue::new();
14 q.push(7);
15 assert_eq!(q.pop(), Ok(7));
16
17 q.push(8);
18 assert_eq!(q.pop(), Ok(8));
19 assert!(q.pop().is_err());
20 }
21
22 #[test]
23 fn len_empty_full() {
24 let q = SegQueue::new();
25
26 assert_eq!(q.len(), 0);
27 assert_eq!(q.is_empty(), true);
28
29 q.push(());
30
31 assert_eq!(q.len(), 1);
32 assert_eq!(q.is_empty(), false);
33
34 q.pop().unwrap();
35
36 assert_eq!(q.len(), 0);
37 assert_eq!(q.is_empty(), true);
38 }
39
40 #[test]
41 fn len() {
42 let q = SegQueue::new();
43
44 assert_eq!(q.len(), 0);
45
46 for i in 0..50 {
47 q.push(i);
48 assert_eq!(q.len(), i + 1);
49 }
50
51 for i in 0..50 {
52 q.pop().unwrap();
53 assert_eq!(q.len(), 50 - i - 1);
54 }
55
56 assert_eq!(q.len(), 0);
57 }
58
59 #[test]
60 fn spsc() {
61 const COUNT: usize = 100_000;
62
63 let q = SegQueue::new();
64
65 scope(|scope| {
66 scope.spawn(|_| {
67 for i in 0..COUNT {
68 loop {
69 if let Ok(x) = q.pop() {
70 assert_eq!(x, i);
71 break;
72 }
73 }
74 }
75 assert!(q.pop().is_err());
76 });
77 scope.spawn(|_| {
78 for i in 0..COUNT {
79 q.push(i);
80 }
81 });
82 }).unwrap();
83 }
84
85 #[test]
86 fn mpmc() {
87 const COUNT: usize = 25_000;
88 const THREADS: usize = 4;
89
90 let q = SegQueue::<usize>::new();
91 let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>();
92
93 scope(|scope| {
94 for _ in 0..THREADS {
95 scope.spawn(|_| {
96 for _ in 0..COUNT {
97 let n = loop {
98 if let Ok(x) = q.pop() {
99 break x;
100 }
101 };
102 v[n].fetch_add(1, Ordering::SeqCst);
103 }
104 });
105 }
106 for _ in 0..THREADS {
107 scope.spawn(|_| {
108 for i in 0..COUNT {
109 q.push(i);
110 }
111 });
112 }
113 }).unwrap();
114
115 for c in v {
116 assert_eq!(c.load(Ordering::SeqCst), THREADS);
117 }
118 }
119
120 #[test]
121 fn drops() {
122 static DROPS: AtomicUsize = AtomicUsize::new(0);
123
124 #[derive(Debug, PartialEq)]
125 struct DropCounter;
126
127 impl Drop for DropCounter {
128 fn drop(&mut self) {
129 DROPS.fetch_add(1, Ordering::SeqCst);
130 }
131 }
132
133 let mut rng = thread_rng();
134
135 for _ in 0..100 {
136 let steps = rng.gen_range(0, 10_000);
137 let additional = rng.gen_range(0, 1000);
138
139 DROPS.store(0, Ordering::SeqCst);
140 let q = SegQueue::new();
141
142 scope(|scope| {
143 scope.spawn(|_| {
144 for _ in 0..steps {
145 while q.pop().is_err() {}
146 }
147 });
148
149 scope.spawn(|_| {
150 for _ in 0..steps {
151 q.push(DropCounter);
152 }
153 });
154 }).unwrap();
155
156 for _ in 0..additional {
157 q.push(DropCounter);
158 }
159
160 assert_eq!(DROPS.load(Ordering::SeqCst), steps);
161 drop(q);
162 assert_eq!(DROPS.load(Ordering::SeqCst), steps + additional);
163 }
164 }