summaryrefslogtreecommitdiffstats
path: root/openpgp/src/lib.rs
diff options
context:
space:
mode:
authorNora Widdecke <nora@sequoia-pgp.org>2021-07-26 12:28:29 +0200
committerNora Widdecke <nora@sequoia-pgp.org>2021-12-13 19:01:14 +0100
commit321b0cc381f3c44f81cfc4a9cf502be169262d14 (patch)
tree20c5ee6c7a551872a5037d89331b642e6190f4a8 /openpgp/src/lib.rs
parenta79a35952cb2cf92bd6ba60fa7df057fb2eae1d2 (diff)
ipc, openpgp: Bump quickcheck to 1.0.3.
- Adapt to the new API: - Gen is now a struct, not a Trait, and replaces StdThreadGen. - The rand re-export has been removed. As a consequence, we need our own function to generate an arbitrary value from a range.
Diffstat (limited to 'openpgp/src/lib.rs')
-rw-r--r--openpgp/src/lib.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs
index a8280d75..4cc96884 100644
--- a/openpgp/src/lib.rs
+++ b/openpgp/src/lib.rs
@@ -329,3 +329,36 @@ pub enum Error {
}
assert_send_and_sync!(Error);
+
+/// Provide a helper function that generates an arbitrary value from a given
+/// range. Quickcheck > 1 does not re-export rand so we need to implement this
+/// ourselves.
+#[cfg(test)]
+mod arbitrary_helper {
+ use quickcheck::{Arbitrary, Gen};
+
+ pub(crate) fn gen_arbitrary_from_range<T>(
+ range: std::ops::Range<T>,
+ g: &mut Gen,
+ ) -> T
+ where
+ T: Arbitrary
+ + std::cmp::PartialOrd
+ + std::ops::Sub<Output = T>
+ + std::ops::Rem<Output = T>
+ + std::ops::Add<Output = T>
+ + Copy,
+ {
+ if !range.is_empty() {
+ let m = range.end - range.start;
+ // The % operator calculates the remainder, which is negative for
+ // negative inputs, not the modulus. This actually calculates the
+ // modulus by making sure the result is positive. The primitive
+ // integer types implement .rem_euclid for that, but there is no way
+ // to constrain this function to primitive types.
+ range.start + (T::arbitrary(g) % m + m) % m
+ } else {
+ panic!()
+ }
+ }
+}