• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "derive")]
2 
3 use arbitrary::{Arbitrary, Unstructured};
4 
arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T5 fn arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T {
6     let mut buf = Unstructured::new(input);
7     T::arbitrary(&mut buf).expect("can create arbitrary instance OK")
8 }
9 
10 /// This wrapper trait *implies* `Arbitrary`, but the compiler isn't smart enough to work that out
11 /// so when using this wrapper we *must* opt-out of the auto-generated `T: Arbitrary` bounds.
12 pub trait WrapperTrait: for<'a> Arbitrary<'a> {}
13 
14 impl WrapperTrait for u32 {}
15 
16 #[derive(Arbitrary)]
17 #[arbitrary(bound = "T: WrapperTrait")]
18 struct GenericSingleBound<T: WrapperTrait> {
19     t: T,
20 }
21 
22 #[test]
single_bound()23 fn single_bound() {
24     let v: GenericSingleBound<u32> = arbitrary_from(&[0, 0, 0, 0]);
25     assert_eq!(v.t, 0);
26 }
27 
28 #[derive(Arbitrary)]
29 #[arbitrary(bound = "T: WrapperTrait, U: WrapperTrait")]
30 struct GenericMultipleBoundsSingleAttribute<T: WrapperTrait, U: WrapperTrait> {
31     t: T,
32     u: U,
33 }
34 
35 #[test]
multiple_bounds_single_attribute()36 fn multiple_bounds_single_attribute() {
37     let v: GenericMultipleBoundsSingleAttribute<u32, u32> =
38         arbitrary_from(&[1, 0, 0, 0, 2, 0, 0, 0]);
39     assert_eq!(v.t, 1);
40     assert_eq!(v.u, 2);
41 }
42 
43 #[derive(Arbitrary)]
44 #[arbitrary(bound = "T: WrapperTrait")]
45 #[arbitrary(bound = "U: Default")]
46 struct GenericMultipleArbitraryAttributes<T: WrapperTrait, U: Default> {
47     t: T,
48     #[arbitrary(default)]
49     u: U,
50 }
51 
52 #[test]
multiple_arbitrary_attributes()53 fn multiple_arbitrary_attributes() {
54     let v: GenericMultipleArbitraryAttributes<u32, u32> = arbitrary_from(&[1, 0, 0, 0]);
55     assert_eq!(v.t, 1);
56     assert_eq!(v.u, 0);
57 }
58 
59 #[derive(Arbitrary)]
60 #[arbitrary(bound = "T: WrapperTrait", bound = "U: Default")]
61 struct GenericMultipleBoundAttributes<T: WrapperTrait, U: Default> {
62     t: T,
63     #[arbitrary(default)]
64     u: U,
65 }
66 
67 #[test]
multiple_bound_attributes()68 fn multiple_bound_attributes() {
69     let v: GenericMultipleBoundAttributes<u32, u32> = arbitrary_from(&[1, 0, 0, 0]);
70     assert_eq!(v.t, 1);
71     assert_eq!(v.u, 0);
72 }
73 
74 #[derive(Arbitrary)]
75 #[arbitrary(bound = "T: WrapperTrait", bound = "U: Default")]
76 #[arbitrary(bound = "V: WrapperTrait, W: Default")]
77 struct GenericMultipleArbitraryAndBoundAttributes<
78     T: WrapperTrait,
79     U: Default,
80     V: WrapperTrait,
81     W: Default,
82 > {
83     t: T,
84     #[arbitrary(default)]
85     u: U,
86     v: V,
87     #[arbitrary(default)]
88     w: W,
89 }
90 
91 #[test]
multiple_arbitrary_and_bound_attributes()92 fn multiple_arbitrary_and_bound_attributes() {
93     let v: GenericMultipleArbitraryAndBoundAttributes<u32, u32, u32, u32> =
94         arbitrary_from(&[1, 0, 0, 0, 2, 0, 0, 0]);
95     assert_eq!(v.t, 1);
96     assert_eq!(v.u, 0);
97     assert_eq!(v.v, 2);
98     assert_eq!(v.w, 0);
99 }
100 
101 #[derive(Arbitrary)]
102 #[arbitrary(bound = "T: Default")]
103 struct GenericDefault<T: Default> {
104     #[arbitrary(default)]
105     x: T,
106 }
107 
108 #[test]
default_bound()109 fn default_bound() {
110     // We can write a generic func without any `Arbitrary` bound.
111     fn generic_default<T: Default>() -> GenericDefault<T> {
112         arbitrary_from(&[])
113     }
114 
115     assert_eq!(generic_default::<u64>().x, 0);
116     assert_eq!(generic_default::<String>().x, String::new());
117     assert_eq!(generic_default::<Vec<u8>>().x, Vec::new());
118 }
119 
120 #[derive(Arbitrary)]
121 #[arbitrary()]
122 struct EmptyArbitraryAttribute {
123     t: u32,
124 }
125 
126 #[test]
empty_arbitrary_attribute()127 fn empty_arbitrary_attribute() {
128     let v: EmptyArbitraryAttribute = arbitrary_from(&[1, 0, 0, 0]);
129     assert_eq!(v.t, 1);
130 }
131 
132 #[derive(Arbitrary)]
133 #[arbitrary(bound = "")]
134 struct EmptyBoundAttribute {
135     t: u32,
136 }
137 
138 #[test]
empty_bound_attribute()139 fn empty_bound_attribute() {
140     let v: EmptyBoundAttribute = arbitrary_from(&[1, 0, 0, 0]);
141     assert_eq!(v.t, 1);
142 }
143