]> git.lizzy.rs Git - rust.git/commitdiff
Share testing utilities with non-btree test cases
authorStein Somers <git@steinsomers.be>
Fri, 18 Feb 2022 14:21:56 +0000 (15:21 +0100)
committerStein Somers <git@steinsomers.be>
Mon, 2 May 2022 08:07:50 +0000 (10:07 +0200)
12 files changed:
library/alloc/src/collections/btree/map/tests.rs
library/alloc/src/collections/btree/mod.rs
library/alloc/src/collections/btree/set/tests.rs
library/alloc/src/collections/btree/testing/crash_test.rs [deleted file]
library/alloc/src/collections/btree/testing/mod.rs [deleted file]
library/alloc/src/collections/btree/testing/ord_chaos.rs [deleted file]
library/alloc/src/collections/btree/testing/rng.rs [deleted file]
library/alloc/src/lib.rs
library/alloc/src/testing/crash_test.rs [new file with mode: 0644]
library/alloc/src/testing/mod.rs [new file with mode: 0644]
library/alloc/src/testing/ord_chaos.rs [new file with mode: 0644]
library/alloc/src/testing/rng.rs [new file with mode: 0644]

index 47ba1777ae90327e6ead8404c4508a33332d7210..ee588282291eba1187e82c7ffd5679af4fc27041 100644 (file)
@@ -1,12 +1,12 @@
-use super::super::testing::crash_test::{CrashTestDummy, Panic};
-use super::super::testing::ord_chaos::{Cyclic3, Governed, Governor};
-use super::super::testing::rng::DeterministicRng;
 use super::Entry::{Occupied, Vacant};
 use super::*;
 use crate::boxed::Box;
 use crate::fmt::Debug;
 use crate::rc::Rc;
 use crate::string::{String, ToString};
+use crate::testing::crash_test::{CrashTestDummy, Panic};
+use crate::testing::ord_chaos::{Cyclic3, Governed, Governor};
+use crate::testing::rng::DeterministicRng;
 use crate::vec::Vec;
 use std::cmp::Ordering;
 use std::convert::TryFrom;
index 9571b3d594df8cf6c410a663388c6825d099c82d..9728c284315dcaf2d3fad742d40d8b1add2de043 100644 (file)
@@ -20,6 +20,3 @@ trait Recover<Q: ?Sized> {
     fn take(&mut self, key: &Q) -> Option<Self::Key>;
     fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
 }
-
-#[cfg(test)]
-mod testing;
index 429b1644976c71586589f49ed806ceb8cccb59a5..4d49d9e83de7ea993f1af223b9760e457d26ddf9 100644 (file)
@@ -1,6 +1,6 @@
-use super::super::testing::crash_test::{CrashTestDummy, Panic};
-use super::super::testing::rng::DeterministicRng;
 use super::*;
+use crate::testing::crash_test::{CrashTestDummy, Panic};
+use crate::testing::rng::DeterministicRng;
 use crate::vec::Vec;
 use std::cmp::Ordering;
 use std::hash::{Hash, Hasher};
diff --git a/library/alloc/src/collections/btree/testing/crash_test.rs b/library/alloc/src/collections/btree/testing/crash_test.rs
deleted file mode 100644 (file)
index bcf5f5f..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-// We avoid relying on anything else in the crate, apart from the `Debug` trait.
-use crate::fmt::Debug;
-use std::cmp::Ordering;
-use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
-
-/// A blueprint for crash test dummy instances that monitor particular events.
-/// Some instances may be configured to panic at some point.
-/// Events are `clone`, `drop` or some anonymous `query`.
-///
-/// Crash test dummies are identified and ordered by an id, so they can be used
-/// as keys in a BTreeMap.
-#[derive(Debug)]
-pub struct CrashTestDummy {
-    pub id: usize,
-    cloned: AtomicUsize,
-    dropped: AtomicUsize,
-    queried: AtomicUsize,
-}
-
-impl CrashTestDummy {
-    /// Creates a crash test dummy design. The `id` determines order and equality of instances.
-    pub fn new(id: usize) -> CrashTestDummy {
-        CrashTestDummy {
-            id,
-            cloned: AtomicUsize::new(0),
-            dropped: AtomicUsize::new(0),
-            queried: AtomicUsize::new(0),
-        }
-    }
-
-    /// Creates an instance of a crash test dummy that records what events it experiences
-    /// and optionally panics.
-    pub fn spawn(&self, panic: Panic) -> Instance<'_> {
-        Instance { origin: self, panic }
-    }
-
-    /// Returns how many times instances of the dummy have been cloned.
-    pub fn cloned(&self) -> usize {
-        self.cloned.load(SeqCst)
-    }
-
-    /// Returns how many times instances of the dummy have been dropped.
-    pub fn dropped(&self) -> usize {
-        self.dropped.load(SeqCst)
-    }
-
-    /// Returns how many times instances of the dummy have had their `query` member invoked.
-    pub fn queried(&self) -> usize {
-        self.queried.load(SeqCst)
-    }
-}
-
-#[derive(Debug)]
-pub struct Instance<'a> {
-    origin: &'a CrashTestDummy,
-    panic: Panic,
-}
-
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
-pub enum Panic {
-    Never,
-    InClone,
-    InDrop,
-    InQuery,
-}
-
-impl Instance<'_> {
-    pub fn id(&self) -> usize {
-        self.origin.id
-    }
-
-    /// Some anonymous query, the result of which is already given.
-    pub fn query<R>(&self, result: R) -> R {
-        self.origin.queried.fetch_add(1, SeqCst);
-        if self.panic == Panic::InQuery {
-            panic!("panic in `query`");
-        }
-        result
-    }
-}
-
-impl Clone for Instance<'_> {
-    fn clone(&self) -> Self {
-        self.origin.cloned.fetch_add(1, SeqCst);
-        if self.panic == Panic::InClone {
-            panic!("panic in `clone`");
-        }
-        Self { origin: self.origin, panic: Panic::Never }
-    }
-}
-
-impl Drop for Instance<'_> {
-    fn drop(&mut self) {
-        self.origin.dropped.fetch_add(1, SeqCst);
-        if self.panic == Panic::InDrop {
-            panic!("panic in `drop`");
-        }
-    }
-}
-
-impl PartialOrd for Instance<'_> {
-    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        self.id().partial_cmp(&other.id())
-    }
-}
-
-impl Ord for Instance<'_> {
-    fn cmp(&self, other: &Self) -> Ordering {
-        self.id().cmp(&other.id())
-    }
-}
-
-impl PartialEq for Instance<'_> {
-    fn eq(&self, other: &Self) -> bool {
-        self.id().eq(&other.id())
-    }
-}
-
-impl Eq for Instance<'_> {}
diff --git a/library/alloc/src/collections/btree/testing/mod.rs b/library/alloc/src/collections/btree/testing/mod.rs
deleted file mode 100644 (file)
index 7a094f8..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-pub mod crash_test;
-pub mod ord_chaos;
-pub mod rng;
diff --git a/library/alloc/src/collections/btree/testing/ord_chaos.rs b/library/alloc/src/collections/btree/testing/ord_chaos.rs
deleted file mode 100644 (file)
index 96ce7c1..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-use std::cell::Cell;
-use std::cmp::Ordering::{self, *};
-use std::ptr;
-
-// Minimal type with an `Ord` implementation violating transitivity.
-#[derive(Debug)]
-pub enum Cyclic3 {
-    A,
-    B,
-    C,
-}
-use Cyclic3::*;
-
-impl PartialOrd for Cyclic3 {
-    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        Some(self.cmp(other))
-    }
-}
-
-impl Ord for Cyclic3 {
-    fn cmp(&self, other: &Self) -> Ordering {
-        match (self, other) {
-            (A, A) | (B, B) | (C, C) => Equal,
-            (A, B) | (B, C) | (C, A) => Less,
-            (A, C) | (B, A) | (C, B) => Greater,
-        }
-    }
-}
-
-impl PartialEq for Cyclic3 {
-    fn eq(&self, other: &Self) -> bool {
-        self.cmp(&other) == Equal
-    }
-}
-
-impl Eq for Cyclic3 {}
-
-// Controls the ordering of values wrapped by `Governed`.
-#[derive(Debug)]
-pub struct Governor {
-    flipped: Cell<bool>,
-}
-
-impl Governor {
-    pub fn new() -> Self {
-        Governor { flipped: Cell::new(false) }
-    }
-
-    pub fn flip(&self) {
-        self.flipped.set(!self.flipped.get());
-    }
-}
-
-// Type with an `Ord` implementation that forms a total order at any moment
-// (assuming that `T` respects total order), but can suddenly be made to invert
-// that total order.
-#[derive(Debug)]
-pub struct Governed<'a, T>(pub T, pub &'a Governor);
-
-impl<T: Ord> PartialOrd for Governed<'_, T> {
-    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        Some(self.cmp(other))
-    }
-}
-
-impl<T: Ord> Ord for Governed<'_, T> {
-    fn cmp(&self, other: &Self) -> Ordering {
-        assert!(ptr::eq(self.1, other.1));
-        let ord = self.0.cmp(&other.0);
-        if self.1.flipped.get() { ord.reverse() } else { ord }
-    }
-}
-
-impl<T: PartialEq> PartialEq for Governed<'_, T> {
-    fn eq(&self, other: &Self) -> bool {
-        assert!(ptr::eq(self.1, other.1));
-        self.0.eq(&other.0)
-    }
-}
-
-impl<T: Eq> Eq for Governed<'_, T> {}
diff --git a/library/alloc/src/collections/btree/testing/rng.rs b/library/alloc/src/collections/btree/testing/rng.rs
deleted file mode 100644 (file)
index ecf543b..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-/// XorShiftRng
-pub struct DeterministicRng {
-    count: usize,
-    x: u32,
-    y: u32,
-    z: u32,
-    w: u32,
-}
-
-impl DeterministicRng {
-    pub fn new() -> Self {
-        DeterministicRng { count: 0, x: 0x193a6754, y: 0xa8a7d469, z: 0x97830e05, w: 0x113ba7bb }
-    }
-
-    /// Guarantees that each returned number is unique.
-    pub fn next(&mut self) -> u32 {
-        self.count += 1;
-        assert!(self.count <= 70029);
-        let x = self.x;
-        let t = x ^ (x << 11);
-        self.x = self.y;
-        self.y = self.z;
-        self.z = self.w;
-        let w_ = self.w;
-        self.w = w_ ^ (w_ >> 19) ^ (t ^ (t >> 8));
-        self.w
-    }
-}
index 4d2dc4ecee0b80f566a983750dd3731ce73afa7c..658f7b3bae797c80b4b3559f424eef341f792bf3 100644 (file)
 extern crate std;
 #[cfg(test)]
 extern crate test;
+#[cfg(test)]
+mod testing;
 
 // Module with internal macros used by other modules (needs to be included before other modules).
 #[macro_use]
diff --git a/library/alloc/src/testing/crash_test.rs b/library/alloc/src/testing/crash_test.rs
new file mode 100644 (file)
index 0000000..bcf5f5f
--- /dev/null
@@ -0,0 +1,119 @@
+// We avoid relying on anything else in the crate, apart from the `Debug` trait.
+use crate::fmt::Debug;
+use std::cmp::Ordering;
+use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
+
+/// A blueprint for crash test dummy instances that monitor particular events.
+/// Some instances may be configured to panic at some point.
+/// Events are `clone`, `drop` or some anonymous `query`.
+///
+/// Crash test dummies are identified and ordered by an id, so they can be used
+/// as keys in a BTreeMap.
+#[derive(Debug)]
+pub struct CrashTestDummy {
+    pub id: usize,
+    cloned: AtomicUsize,
+    dropped: AtomicUsize,
+    queried: AtomicUsize,
+}
+
+impl CrashTestDummy {
+    /// Creates a crash test dummy design. The `id` determines order and equality of instances.
+    pub fn new(id: usize) -> CrashTestDummy {
+        CrashTestDummy {
+            id,
+            cloned: AtomicUsize::new(0),
+            dropped: AtomicUsize::new(0),
+            queried: AtomicUsize::new(0),
+        }
+    }
+
+    /// Creates an instance of a crash test dummy that records what events it experiences
+    /// and optionally panics.
+    pub fn spawn(&self, panic: Panic) -> Instance<'_> {
+        Instance { origin: self, panic }
+    }
+
+    /// Returns how many times instances of the dummy have been cloned.
+    pub fn cloned(&self) -> usize {
+        self.cloned.load(SeqCst)
+    }
+
+    /// Returns how many times instances of the dummy have been dropped.
+    pub fn dropped(&self) -> usize {
+        self.dropped.load(SeqCst)
+    }
+
+    /// Returns how many times instances of the dummy have had their `query` member invoked.
+    pub fn queried(&self) -> usize {
+        self.queried.load(SeqCst)
+    }
+}
+
+#[derive(Debug)]
+pub struct Instance<'a> {
+    origin: &'a CrashTestDummy,
+    panic: Panic,
+}
+
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum Panic {
+    Never,
+    InClone,
+    InDrop,
+    InQuery,
+}
+
+impl Instance<'_> {
+    pub fn id(&self) -> usize {
+        self.origin.id
+    }
+
+    /// Some anonymous query, the result of which is already given.
+    pub fn query<R>(&self, result: R) -> R {
+        self.origin.queried.fetch_add(1, SeqCst);
+        if self.panic == Panic::InQuery {
+            panic!("panic in `query`");
+        }
+        result
+    }
+}
+
+impl Clone for Instance<'_> {
+    fn clone(&self) -> Self {
+        self.origin.cloned.fetch_add(1, SeqCst);
+        if self.panic == Panic::InClone {
+            panic!("panic in `clone`");
+        }
+        Self { origin: self.origin, panic: Panic::Never }
+    }
+}
+
+impl Drop for Instance<'_> {
+    fn drop(&mut self) {
+        self.origin.dropped.fetch_add(1, SeqCst);
+        if self.panic == Panic::InDrop {
+            panic!("panic in `drop`");
+        }
+    }
+}
+
+impl PartialOrd for Instance<'_> {
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        self.id().partial_cmp(&other.id())
+    }
+}
+
+impl Ord for Instance<'_> {
+    fn cmp(&self, other: &Self) -> Ordering {
+        self.id().cmp(&other.id())
+    }
+}
+
+impl PartialEq for Instance<'_> {
+    fn eq(&self, other: &Self) -> bool {
+        self.id().eq(&other.id())
+    }
+}
+
+impl Eq for Instance<'_> {}
diff --git a/library/alloc/src/testing/mod.rs b/library/alloc/src/testing/mod.rs
new file mode 100644 (file)
index 0000000..7a094f8
--- /dev/null
@@ -0,0 +1,3 @@
+pub mod crash_test;
+pub mod ord_chaos;
+pub mod rng;
diff --git a/library/alloc/src/testing/ord_chaos.rs b/library/alloc/src/testing/ord_chaos.rs
new file mode 100644 (file)
index 0000000..96ce7c1
--- /dev/null
@@ -0,0 +1,81 @@
+use std::cell::Cell;
+use std::cmp::Ordering::{self, *};
+use std::ptr;
+
+// Minimal type with an `Ord` implementation violating transitivity.
+#[derive(Debug)]
+pub enum Cyclic3 {
+    A,
+    B,
+    C,
+}
+use Cyclic3::*;
+
+impl PartialOrd for Cyclic3 {
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for Cyclic3 {
+    fn cmp(&self, other: &Self) -> Ordering {
+        match (self, other) {
+            (A, A) | (B, B) | (C, C) => Equal,
+            (A, B) | (B, C) | (C, A) => Less,
+            (A, C) | (B, A) | (C, B) => Greater,
+        }
+    }
+}
+
+impl PartialEq for Cyclic3 {
+    fn eq(&self, other: &Self) -> bool {
+        self.cmp(&other) == Equal
+    }
+}
+
+impl Eq for Cyclic3 {}
+
+// Controls the ordering of values wrapped by `Governed`.
+#[derive(Debug)]
+pub struct Governor {
+    flipped: Cell<bool>,
+}
+
+impl Governor {
+    pub fn new() -> Self {
+        Governor { flipped: Cell::new(false) }
+    }
+
+    pub fn flip(&self) {
+        self.flipped.set(!self.flipped.get());
+    }
+}
+
+// Type with an `Ord` implementation that forms a total order at any moment
+// (assuming that `T` respects total order), but can suddenly be made to invert
+// that total order.
+#[derive(Debug)]
+pub struct Governed<'a, T>(pub T, pub &'a Governor);
+
+impl<T: Ord> PartialOrd for Governed<'_, T> {
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl<T: Ord> Ord for Governed<'_, T> {
+    fn cmp(&self, other: &Self) -> Ordering {
+        assert!(ptr::eq(self.1, other.1));
+        let ord = self.0.cmp(&other.0);
+        if self.1.flipped.get() { ord.reverse() } else { ord }
+    }
+}
+
+impl<T: PartialEq> PartialEq for Governed<'_, T> {
+    fn eq(&self, other: &Self) -> bool {
+        assert!(ptr::eq(self.1, other.1));
+        self.0.eq(&other.0)
+    }
+}
+
+impl<T: Eq> Eq for Governed<'_, T> {}
diff --git a/library/alloc/src/testing/rng.rs b/library/alloc/src/testing/rng.rs
new file mode 100644 (file)
index 0000000..ecf543b
--- /dev/null
@@ -0,0 +1,28 @@
+/// XorShiftRng
+pub struct DeterministicRng {
+    count: usize,
+    x: u32,
+    y: u32,
+    z: u32,
+    w: u32,
+}
+
+impl DeterministicRng {
+    pub fn new() -> Self {
+        DeterministicRng { count: 0, x: 0x193a6754, y: 0xa8a7d469, z: 0x97830e05, w: 0x113ba7bb }
+    }
+
+    /// Guarantees that each returned number is unique.
+    pub fn next(&mut self) -> u32 {
+        self.count += 1;
+        assert!(self.count <= 70029);
+        let x = self.x;
+        let t = x ^ (x << 11);
+        self.x = self.y;
+        self.y = self.z;
+        self.z = self.w;
+        let w_ = self.w;
+        self.w = w_ ^ (w_ >> 19) ^ (t ^ (t >> 8));
+        self.w
+    }
+}