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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::*;
use std::string::{String, FromUtf8Error, FromUtf16Error, ParseError};
use std::iter;
use std::slice;
use std::rc::Rc;
use std::sync::Arc;
#[derive(Copy, Clone, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StringParameter<'a>(&'a str);
impl<'a> Default for StringParameter<'a> {
fn default() -> Self {
StringParameter("\\PC*")
}
}
impl<'a> Arbitrary<'a> for String {
valuetree!();
type Parameters = StringParameter<'a>;
type Strategy = &'a str;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
args.into()
}
}
macro_rules! dst_wrapped {
($($w: ident),*) => {
$(arbitrary!($w<str>, FMapped<'a, String, Self>, StringParameter<'a>;
a => any_with_sinto::<String, _>(a)
);)*
};
}
dst_wrapped!(Box, Rc, Arc);
generator!(FromUtf16Error, || String::from_utf16(&[0xD800]).unwrap_err());
generator!(ParseError, || panic!());
arbitrary!(FromUtf8Error, SFnPtrMap<BoxedStrategy<Vec<u8>>, Self>;
static_map(not_utf8_bytes(), |bytes| String::from_utf8(bytes).unwrap_err())
);
pub(crate) fn not_utf8_bytes() -> BoxedStrategy<Vec<u8>> {
(any::<u16>(), gen_el_bytes()).prop_flat_map(|(valid_up_to, el_bytes)| {
let bounds: SizeBounds = (valid_up_to as usize).into();
any_with_map(product_pack![bounds, default()], move |p: Vec<char>| {
let mut bytes = p.iter().collect::<String>().into_bytes();
bytes.extend(el_bytes.into_iter());
bytes
})
}).boxed()
}
#[derive(Debug)]
enum ELBytes {
B1([u8; 1]),
B2([u8; 2]),
B3([u8; 3]),
B4([u8; 4])
}
impl<'a> IntoIterator for &'a ELBytes {
type Item = u8;
type IntoIter = iter::Map<slice::Iter<'a, u8>, fn(&u8) -> u8>;
fn into_iter(self) -> Self::IntoIter {
use self::ELBytes::*;
(match *self {
B1(ref a) => a.iter(),
B2(ref a) => a.iter(),
B3(ref a) => a.iter(),
B4(ref a) => a.iter(),
}).map(|x| *x)
}
}
fn b1(a: u8) -> ELBytes { ELBytes::B1([a]) }
fn b2(a: (u8, u8)) -> ELBytes { ELBytes::B2([a.0, a.1]) }
fn b3(a: ((u8, u8), u8)) -> ELBytes { ELBytes::B3([(a.0).0, (a.0).1, a.1]) }
fn b4(a: ((u8, u8), u8, u8)) -> ELBytes {
ELBytes::B4([(a.0).0, (a.0).1, a.1, a.2])
}
fn gen_el_bytes() -> BoxedStrategy<ELBytes> {
let succ_byte = 0x80u8..0xC0u8;
let fail_byte = prop_oneof![0x00u8..0x7Fu8, 0xC1u8..];
let byte0_w0 = prop_oneof![0x80u8..0xC0u8, 0xF5u8..];
let byte0_w2 = 0xC2u8..0xE0u8;
let byte0_w3 = 0xE0u8..0xF0u8;
let byte0_w4 = 0xF0u8..0xF5u8;
let byte01_w3 = byte0_w3.clone().prop_flat_map(|x| (Just(x), match x {
0xE0u8 => 0xA0u8..0xC0u8,
0xE1u8...0xECu8 => 0x80u8..0xC0u8,
0xEDu8 => 0x80u8..0xA0u8,
0xEEu8...0xEFu8 => 0x80u8..0xA0u8,
_ => panic!(),
}));
let byte01_w3_e1 = byte0_w3.clone().prop_flat_map(|x| (Just(x), match x {
0xE0u8 => prop_oneof![..0xA0u8, 0xC0u8..],
0xE1u8...0xECu8 => prop_oneof![..0x80u8, 0xC0u8..],
0xEDu8 => prop_oneof![..0x80u8, 0xA0u8..],
0xEEu8...0xEFu8 => prop_oneof![..0x80u8, 0xA0u8..],
_ => panic!(),
}));
let byte01_w4_e1 = byte0_w4.clone().prop_flat_map(|x| (Just(x), match x {
0xF0u8 => prop_oneof![..0x90u8, 0xA0u8..],
0xF1u8...0xF3u8 => prop_oneof![..0x80u8, 0xA0u8..],
0xF4u8 => prop_oneof![..0x80u8, 0x90u8..],
_ => panic!()
}));
let byte01_w4 = byte0_w4.clone().prop_flat_map(|x| (Just(x), match x {
0xF0u8 => 0x90u8..0xA0u8,
0xF1u8...0xF3u8 => 0x80u8..0xA0u8,
0xF4u8 => 0x80u8..0x90u8,
_ => panic!()
}));
prop_oneof![
prop_oneof![
static_map(byte0_w2.clone(), b1),
static_map(byte0_w3, b1),
static_map(byte01_w3.clone(), b2),
static_map(byte0_w4, b1),
static_map(byte01_w4.clone(), b2),
static_map((byte01_w4.clone(), succ_byte.clone()), b3),
],
prop_oneof![
static_map(byte0_w0, b1),
static_map((byte0_w2, fail_byte.clone()), b2),
static_map(byte01_w3_e1, b2),
static_map(byte01_w4_e1, b2),
],
static_map(prop_oneof![
(byte01_w3, fail_byte.clone()),
(byte01_w4.clone(), fail_byte.clone())
], b3),
static_map((byte01_w4, succ_byte, fail_byte), b4),
].boxed()
}