about summary refs log tree commit diff
path: root/library/std/src/sync/mpsc/spsc_queue
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2020-08-27 13:45:01 +0000
committerLzu Tao <taolzu@gmail.com>2020-08-31 02:56:59 +0000
commita4e926daeeaedc9178846711daf1f4cb6ce505fb (patch)
tree0c830f716f6f5ad17736d459f5de9b9199006d54 /library/std/src/sync/mpsc/spsc_queue
parentdb6cbfc49ca655739ba8caae43ebd7c77c8a1179 (diff)
downloadrust-a4e926daeeaedc9178846711daf1f4cb6ce505fb.tar.gz
rust-a4e926daeeaedc9178846711daf1f4cb6ce505fb.zip
std: move "mod tests/benches" to separate files
Also doing fmt inplace as requested.
Diffstat (limited to 'library/std/src/sync/mpsc/spsc_queue')
-rw-r--r--library/std/src/sync/mpsc/spsc_queue/tests.rs101
1 files changed, 101 insertions, 0 deletions
diff --git a/library/std/src/sync/mpsc/spsc_queue/tests.rs b/library/std/src/sync/mpsc/spsc_queue/tests.rs
new file mode 100644
index 00000000000..e4fd15cbbde
--- /dev/null
+++ b/library/std/src/sync/mpsc/spsc_queue/tests.rs
@@ -0,0 +1,101 @@
+use super::Queue;
+use crate::sync::mpsc::channel;
+use crate::sync::Arc;
+use crate::thread;
+
+#[test]
+fn smoke() {
+    unsafe {
+        let queue = Queue::with_additions(0, (), ());
+        queue.push(1);
+        queue.push(2);
+        assert_eq!(queue.pop(), Some(1));
+        assert_eq!(queue.pop(), Some(2));
+        assert_eq!(queue.pop(), None);
+        queue.push(3);
+        queue.push(4);
+        assert_eq!(queue.pop(), Some(3));
+        assert_eq!(queue.pop(), Some(4));
+        assert_eq!(queue.pop(), None);
+    }
+}
+
+#[test]
+fn peek() {
+    unsafe {
+        let queue = Queue::with_additions(0, (), ());
+        queue.push(vec![1]);
+
+        // Ensure the borrowchecker works
+        match queue.peek() {
+            Some(vec) => {
+                assert_eq!(&*vec, &[1]);
+            }
+            None => unreachable!(),
+        }
+
+        match queue.pop() {
+            Some(vec) => {
+                assert_eq!(&*vec, &[1]);
+            }
+            None => unreachable!(),
+        }
+    }
+}
+
+#[test]
+fn drop_full() {
+    unsafe {
+        let q: Queue<Box<_>> = Queue::with_additions(0, (), ());
+        q.push(box 1);
+        q.push(box 2);
+    }
+}
+
+#[test]
+fn smoke_bound() {
+    unsafe {
+        let q = Queue::with_additions(0, (), ());
+        q.push(1);
+        q.push(2);
+        assert_eq!(q.pop(), Some(1));
+        assert_eq!(q.pop(), Some(2));
+        assert_eq!(q.pop(), None);
+        q.push(3);
+        q.push(4);
+        assert_eq!(q.pop(), Some(3));
+        assert_eq!(q.pop(), Some(4));
+        assert_eq!(q.pop(), None);
+    }
+}
+
+#[test]
+fn stress() {
+    unsafe {
+        stress_bound(0);
+        stress_bound(1);
+    }
+
+    unsafe fn stress_bound(bound: usize) {
+        let q = Arc::new(Queue::with_additions(bound, (), ()));
+
+        let (tx, rx) = channel();
+        let q2 = q.clone();
+        let _t = thread::spawn(move || {
+            for _ in 0..100000 {
+                loop {
+                    match q2.pop() {
+                        Some(1) => break,
+                        Some(_) => panic!(),
+                        None => {}
+                    }
+                }
+            }
+            tx.send(()).unwrap();
+        });
+        for _ in 0..100000 {
+            q.push(1);
+        }
+        rx.recv().unwrap();
+    }
+}