1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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);
}
|