summaryrefslogtreecommitdiffstats
path: root/vendor/arbitrary/tests
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/arbitrary/tests')
-rw-r--r--vendor/arbitrary/tests/bound.rs142
-rw-r--r--vendor/arbitrary/tests/derive.rs278
-rw-r--r--vendor/arbitrary/tests/path.rs32
3 files changed, 452 insertions, 0 deletions
diff --git a/vendor/arbitrary/tests/bound.rs b/vendor/arbitrary/tests/bound.rs
new file mode 100644
index 000000000..7a772ac71
--- /dev/null
+++ b/vendor/arbitrary/tests/bound.rs
@@ -0,0 +1,142 @@
+#![cfg(feature = "derive")]
+
+use arbitrary::{Arbitrary, Unstructured};
+
+fn arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T {
+ let mut buf = Unstructured::new(input);
+ T::arbitrary(&mut buf).expect("can create arbitrary instance OK")
+}
+
+/// This wrapper trait *implies* `Arbitrary`, but the compiler isn't smart enough to work that out
+/// so when using this wrapper we *must* opt-out of the auto-generated `T: Arbitrary` bounds.
+pub trait WrapperTrait: for<'a> Arbitrary<'a> {}
+
+impl WrapperTrait for u32 {}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: WrapperTrait")]
+struct GenericSingleBound<T: WrapperTrait> {
+ t: T,
+}
+
+#[test]
+fn single_bound() {
+ let v: GenericSingleBound<u32> = arbitrary_from(&[0, 0, 0, 0]);
+ assert_eq!(v.t, 0);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: WrapperTrait, U: WrapperTrait")]
+struct GenericMultipleBoundsSingleAttribute<T: WrapperTrait, U: WrapperTrait> {
+ t: T,
+ u: U,
+}
+
+#[test]
+fn multiple_bounds_single_attribute() {
+ let v: GenericMultipleBoundsSingleAttribute<u32, u32> =
+ arbitrary_from(&[1, 0, 0, 0, 2, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+ assert_eq!(v.u, 2);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: WrapperTrait")]
+#[arbitrary(bound = "U: Default")]
+struct GenericMultipleArbitraryAttributes<T: WrapperTrait, U: Default> {
+ t: T,
+ #[arbitrary(default)]
+ u: U,
+}
+
+#[test]
+fn multiple_arbitrary_attributes() {
+ let v: GenericMultipleArbitraryAttributes<u32, u32> = arbitrary_from(&[1, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+ assert_eq!(v.u, 0);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: WrapperTrait", bound = "U: Default")]
+struct GenericMultipleBoundAttributes<T: WrapperTrait, U: Default> {
+ t: T,
+ #[arbitrary(default)]
+ u: U,
+}
+
+#[test]
+fn multiple_bound_attributes() {
+ let v: GenericMultipleBoundAttributes<u32, u32> = arbitrary_from(&[1, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+ assert_eq!(v.u, 0);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: WrapperTrait", bound = "U: Default")]
+#[arbitrary(bound = "V: WrapperTrait, W: Default")]
+struct GenericMultipleArbitraryAndBoundAttributes<
+ T: WrapperTrait,
+ U: Default,
+ V: WrapperTrait,
+ W: Default,
+> {
+ t: T,
+ #[arbitrary(default)]
+ u: U,
+ v: V,
+ #[arbitrary(default)]
+ w: W,
+}
+
+#[test]
+fn multiple_arbitrary_and_bound_attributes() {
+ let v: GenericMultipleArbitraryAndBoundAttributes<u32, u32, u32, u32> =
+ arbitrary_from(&[1, 0, 0, 0, 2, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+ assert_eq!(v.u, 0);
+ assert_eq!(v.v, 2);
+ assert_eq!(v.w, 0);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "T: Default")]
+struct GenericDefault<T: Default> {
+ #[arbitrary(default)]
+ x: T,
+}
+
+#[test]
+fn default_bound() {
+ // We can write a generic func without any `Arbitrary` bound.
+ fn generic_default<T: Default>() -> GenericDefault<T> {
+ arbitrary_from(&[])
+ }
+
+ assert_eq!(generic_default::<u64>().x, 0);
+ assert_eq!(generic_default::<String>().x, String::new());
+ assert_eq!(generic_default::<Vec<u8>>().x, Vec::new());
+}
+
+#[derive(Arbitrary)]
+#[arbitrary()]
+struct EmptyArbitraryAttribute {
+ t: u32,
+}
+
+#[test]
+fn empty_arbitrary_attribute() {
+ let v: EmptyArbitraryAttribute = arbitrary_from(&[1, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+}
+
+#[derive(Arbitrary)]
+#[arbitrary(bound = "")]
+struct EmptyBoundAttribute {
+ t: u32,
+}
+
+#[test]
+fn empty_bound_attribute() {
+ let v: EmptyBoundAttribute = arbitrary_from(&[1, 0, 0, 0]);
+ assert_eq!(v.t, 1);
+}
diff --git a/vendor/arbitrary/tests/derive.rs b/vendor/arbitrary/tests/derive.rs
new file mode 100644
index 000000000..a84e4dc6c
--- /dev/null
+++ b/vendor/arbitrary/tests/derive.rs
@@ -0,0 +1,278 @@
+#![cfg(feature = "derive")]
+// Various structs/fields that we are deriving `Arbitrary` for aren't actually
+// used except to exercise the derive.
+#![allow(dead_code)]
+
+use arbitrary::*;
+
+fn arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T {
+ let mut buf = Unstructured::new(input);
+ T::arbitrary(&mut buf).expect("can create arbitrary instance OK")
+}
+
+#[derive(Copy, Clone, Debug, Eq, PartialEq, Arbitrary)]
+pub struct Rgb {
+ pub r: u8,
+ pub g: u8,
+ pub b: u8,
+}
+
+#[test]
+fn struct_with_named_fields() {
+ let rgb: Rgb = arbitrary_from(&[4, 5, 6]);
+ assert_eq!(rgb.r, 4);
+ assert_eq!(rgb.g, 5);
+ assert_eq!(rgb.b, 6);
+
+ assert_eq!((3, Some(3)), <Rgb as Arbitrary>::size_hint(0));
+}
+
+#[derive(Copy, Clone, Debug, Arbitrary)]
+struct MyTupleStruct(u8, bool);
+
+#[test]
+fn tuple_struct() {
+ let s: MyTupleStruct = arbitrary_from(&[43, 42]);
+ assert_eq!(s.0, 43);
+ assert_eq!(s.1, false);
+
+ let s: MyTupleStruct = arbitrary_from(&[42, 43]);
+ assert_eq!(s.0, 42);
+ assert_eq!(s.1, true);
+
+ assert_eq!((2, Some(2)), <MyTupleStruct as Arbitrary>::size_hint(0));
+}
+
+#[derive(Clone, Debug, Arbitrary)]
+struct EndingInVec(u8, bool, u32, Vec<u16>);
+#[derive(Clone, Debug, Arbitrary)]
+struct EndingInString(u8, bool, u32, String);
+
+#[test]
+fn test_take_rest() {
+ let bytes = [1, 1, 1, 2, 3, 4, 5, 6, 7, 8];
+ let s1 = EndingInVec::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
+ let s2 = EndingInString::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
+ assert_eq!(s1.0, 1);
+ assert_eq!(s2.0, 1);
+ assert_eq!(s1.1, true);
+ assert_eq!(s2.1, true);
+ assert_eq!(s1.2, 0x4030201);
+ assert_eq!(s2.2, 0x4030201);
+ assert_eq!(s1.3, vec![0x0706]);
+ assert_eq!(s2.3, "\x05\x06\x07\x08");
+}
+
+#[derive(Copy, Clone, Debug, Arbitrary)]
+enum MyEnum {
+ Unit,
+ Tuple(u8, u16),
+ Struct { a: u32, b: (bool, u64) },
+}
+
+#[test]
+fn derive_enum() {
+ let mut raw = vec![
+ // The choice of which enum variant takes 4 bytes.
+ 1, 2, 3, 4,
+ // And then we need up to 13 bytes for creating `MyEnum::Struct`, the
+ // largest variant.
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+ ];
+
+ let mut saw_unit = false;
+ let mut saw_tuple = false;
+ let mut saw_struct = false;
+
+ for i in 0..=255 {
+ // Choose different variants each iteration.
+ for el in &mut raw[..4] {
+ *el = i;
+ }
+
+ let e: MyEnum = arbitrary_from(&raw);
+
+ match e {
+ MyEnum::Unit => {
+ saw_unit = true;
+ }
+ MyEnum::Tuple(a, b) => {
+ saw_tuple = true;
+ assert_eq!(a, arbitrary_from(&raw[4..5]));
+ assert_eq!(b, arbitrary_from(&raw[5..]));
+ }
+ MyEnum::Struct { a, b } => {
+ saw_struct = true;
+ assert_eq!(a, arbitrary_from(&raw[4..8]));
+ assert_eq!(b, arbitrary_from(&raw[8..]));
+ }
+ }
+ }
+
+ assert!(saw_unit);
+ assert!(saw_tuple);
+ assert!(saw_struct);
+
+ assert_eq!((4, Some(17)), <MyEnum as Arbitrary>::size_hint(0));
+}
+
+#[derive(Arbitrary, Debug)]
+enum RecursiveTree {
+ Leaf,
+ Node {
+ left: Box<RecursiveTree>,
+ right: Box<RecursiveTree>,
+ },
+}
+
+#[test]
+fn recursive() {
+ let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
+ let _rec: RecursiveTree = arbitrary_from(&raw);
+
+ let (lower, upper) = <RecursiveTree as Arbitrary>::size_hint(0);
+ assert_eq!(lower, 4, "need a u32 for the discriminant at minimum");
+ assert!(
+ upper.is_none(),
+ "potentially infinitely recursive, so no upper bound"
+ );
+}
+
+#[derive(Arbitrary, Debug)]
+struct Generic<T> {
+ inner: T,
+}
+
+#[test]
+fn generics() {
+ let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
+ let gen: Generic<bool> = arbitrary_from(&raw);
+ assert!(gen.inner);
+
+ let (lower, upper) = <Generic<u32> as Arbitrary>::size_hint(0);
+ assert_eq!(lower, 4);
+ assert_eq!(upper, Some(4));
+}
+
+#[derive(Arbitrary, Debug)]
+struct OneLifetime<'a> {
+ alpha: &'a str,
+}
+
+#[test]
+fn one_lifetime() {
+ // Last byte is used for length
+ let raw: Vec<u8> = vec![97, 98, 99, 100, 3];
+ let lifetime: OneLifetime = arbitrary_from(&raw);
+ assert_eq!("abc", lifetime.alpha);
+
+ let (lower, upper) = <OneLifetime as Arbitrary>::size_hint(0);
+ assert_eq!(lower, 0);
+ assert_eq!(upper, None);
+}
+
+#[derive(Arbitrary, Debug)]
+struct TwoLifetimes<'a, 'b> {
+ alpha: &'a str,
+ beta: &'b str,
+}
+
+#[test]
+fn two_lifetimes() {
+ // Last byte is used for length
+ let raw: Vec<u8> = vec![97, 98, 99, 100, 101, 102, 103, 3];
+ let lifetime: TwoLifetimes = arbitrary_from(&raw);
+ assert_eq!("abc", lifetime.alpha);
+ assert_eq!("def", lifetime.beta);
+
+ let (lower, upper) = <TwoLifetimes as Arbitrary>::size_hint(0);
+ assert_eq!(lower, 0);
+ assert_eq!(upper, None);
+}
+
+#[test]
+fn recursive_and_empty_input() {
+ // None of the following derives should result in a stack overflow. See
+ // https://github.com/rust-fuzz/arbitrary/issues/107 for details.
+
+ #[derive(Debug, Arbitrary)]
+ enum Nat {
+ Succ(Box<Nat>),
+ Zero,
+ }
+
+ let _ = Nat::arbitrary(&mut Unstructured::new(&[]));
+
+ #[derive(Debug, Arbitrary)]
+ enum Nat2 {
+ Zero,
+ Succ(Box<Nat2>),
+ }
+
+ let _ = Nat2::arbitrary(&mut Unstructured::new(&[]));
+
+ #[derive(Debug, Arbitrary)]
+ struct Nat3 {
+ f: Option<Box<Nat3>>,
+ }
+
+ let _ = Nat3::arbitrary(&mut Unstructured::new(&[]));
+
+ #[derive(Debug, Arbitrary)]
+ struct Nat4(Option<Box<Nat4>>);
+
+ let _ = Nat4::arbitrary(&mut Unstructured::new(&[]));
+
+ #[derive(Debug, Arbitrary)]
+ enum Nat5 {
+ Zero,
+ Succ { f: Box<Nat5> },
+ }
+
+ let _ = Nat5::arbitrary(&mut Unstructured::new(&[]));
+}
+
+#[test]
+fn test_field_attributes() {
+ // A type that DOES NOT implement Arbitrary
+ #[derive(Debug)]
+ struct Weight(u8);
+
+ #[derive(Debug, Arbitrary)]
+ struct Parcel {
+ #[arbitrary(with = arbitrary_weight)]
+ weight: Weight,
+
+ #[arbitrary(default)]
+ width: u8,
+
+ #[arbitrary(value = 2 + 2)]
+ length: u8,
+
+ height: u8,
+
+ #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=100))]
+ price: u8,
+ }
+
+ fn arbitrary_weight(u: &mut Unstructured) -> arbitrary::Result<Weight> {
+ u.int_in_range(45..=56).map(Weight)
+ }
+
+ let parcel: Parcel = arbitrary_from(&[6, 199, 17]);
+
+ // 45 + 6 = 51
+ assert_eq!(parcel.weight.0, 51);
+
+ // u8::default()
+ assert_eq!(parcel.width, 0);
+
+ // 2 + 2 = 4
+ assert_eq!(parcel.length, 4);
+
+ // 199 is the 2nd byte used by arbitrary
+ assert_eq!(parcel.height, 199);
+
+ // 17 is the 3rd byte used by arbitrary
+ assert_eq!(parcel.price, 17);
+}
diff --git a/vendor/arbitrary/tests/path.rs b/vendor/arbitrary/tests/path.rs
new file mode 100644
index 000000000..c42ec0a11
--- /dev/null
+++ b/vendor/arbitrary/tests/path.rs
@@ -0,0 +1,32 @@
+#![cfg(feature = "derive")]
+// Various structs/fields that we are deriving `Arbitrary` for aren't actually
+// used except to show off the derive.
+#![allow(dead_code)]
+
+// Regression test for ensuring the derives work without Arbitrary being imported
+
+#[derive(arbitrary::Arbitrary, Clone, Debug)]
+pub struct Struct {
+ x: u8,
+ y: u8,
+}
+
+#[derive(arbitrary::Arbitrary, Clone, Debug)]
+pub struct Tuple(u8);
+
+#[derive(arbitrary::Arbitrary, Clone, Debug)]
+pub struct Unit(u8);
+
+#[derive(arbitrary::Arbitrary, Clone, Debug)]
+pub enum Enum {
+ X(u8),
+ Y(u8),
+}
+
+#[derive(arbitrary::Arbitrary, Clone, Debug)]
+struct EndingInVec(u8, bool, u32, Vec<u16>);
+
+#[derive(arbitrary::Arbitrary, Debug)]
+struct Generic<T> {
+ inner: T,
+}