1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
7 //! A simple fuzz tester for the library.
14 use std
::borrow
::ToOwned
;
16 use rand
::distributions
::{IndependentSample, Range}
;
18 use tendril
::StrTendril
;
21 let mut rng
= rand
::thread_rng();
22 let capacity
= Range
::new(0u32, 1 << 14).ind_sample(&mut rng
);
23 let mut buf_string
= String
::with_capacity(capacity
as usize);
24 let mut buf_tendril
= StrTendril
::with_capacity(capacity
);
25 let mut string_slices
= vec
![];
26 let mut tendril_slices
= vec
![];
29 if buf_string
.len() > (1 << 30) {
30 buf_string
.truncate(0);
34 let dist_action
= Range
::new(0, 100);
35 match dist_action
.ind_sample(&mut rng
) {
37 let (start
, end
) = random_slice(&mut rng
, TEXT
);
38 let snip
= &TEXT
[start
..end
];
39 buf_string
.push_str(snip
);
40 buf_tendril
.push_slice(snip
);
41 assert_eq
!(&*buf_string
, &*buf_tendril
);
45 let (start
, end
) = random_slice(&mut rng
, &buf_string
);
46 let snip
= &buf_string
[start
..end
].to_owned();
47 buf_string
.push_str(&snip
);
48 buf_tendril
.push_slice(&snip
);
49 assert_eq
!(&*buf_string
, &*buf_tendril
);
53 let lenstr
= format
!("[length = {}]", buf_tendril
.len());
54 buf_string
.push_str(&lenstr
);
55 buf_tendril
.push_slice(&lenstr
);
56 assert_eq
!(&*buf_string
, &*buf_tendril
);
60 let n
= random_boundary(&mut rng
, &buf_string
);
61 buf_tendril
.pop_front(n
as u32);
62 buf_string
= buf_string
[n
..].to_owned();
63 assert_eq
!(&*buf_string
, &*buf_tendril
);
67 let new_len
= random_boundary(&mut rng
, &buf_string
);
68 let n
= buf_string
.len() - new_len
;
69 buf_string
.truncate(new_len
);
70 buf_tendril
.pop_back(n
as u32);
71 assert_eq
!(&*buf_string
, &*buf_tendril
);
75 let (start
, end
) = random_slice(&mut rng
, &buf_string
);
76 buf_string
= buf_string
[start
..end
].to_owned();
77 buf_tendril
= buf_tendril
.subtendril(start
as u32, (end
- start
) as u32);
78 assert_eq
!(&*buf_string
, &*buf_tendril
);
84 assert
!(buf_tendril
.try_push_char(c
).is_ok());
85 assert_eq
!(&*buf_string
, &*buf_tendril
);
89 buf_string
.truncate(0);
91 assert_eq
!(&*buf_string
, &*buf_tendril
);
95 let (start
, end
) = random_slice(&mut rng
, &buf_string
);
96 string_slices
.push(buf_string
[start
..end
].to_owned());
97 tendril_slices
.push(buf_tendril
.subtendril(start
as u32, (end
- start
) as u32));
98 assert_eq
!(string_slices
.len(), tendril_slices
.len());
101 .zip(tendril_slices
.iter())
102 .all(|(s
, t
)| **s
== **t
));
108 fn random_boundary
<R
: Rng
>(rng
: &mut R
, text
: &str) -> usize {
110 let i
= Range
::new(0, text
.len() + 1).ind_sample(rng
);
111 if text
.is_char_boundary(i
) {
117 fn random_slice
<R
: Rng
>(rng
: &mut R
, text
: &str) -> (usize, usize) {
119 let start
= Range
::new(0, text
.len() + 1).ind_sample(rng
);
120 let end
= Range
::new(start
, text
.len() + 1).ind_sample(rng
);
121 if !text
.is_char_boundary(start
) {
124 if end
< text
.len() && !text
.is_char_boundary(end
) {
131 static TEXT
: &'
static str =
132 "It was from the artists and poets that the pertinent answers came, and I \
133 know that panic would have broken loose had they been able to compare notes. \
134 As it was, lacking their original letters, I half suspected the compiler of \
135 having asked leading questions, or of having edited the correspondence in \
136 corroboration of what he had latently resolved to see.\
138 ˙ǝǝs oʇ pǝʌʃosǝɹ ʎʃʇuǝʇɐʃ pɐɥ ǝɥ ʇɐɥʍ ɟo uoıʇɐɹoqoɹɹoɔ uı ǝɔuǝpuodsǝɹɹoɔ ǝɥʇ \
139 pǝʇıpǝ ƃuıʌɐɥ ɟo ɹo 'suoıʇsǝnb ƃuıpɐǝʃ pǝʞsɐ ƃuıʌɐɥ ɟo ɹǝʃıdɯoɔ ǝɥʇ pǝʇɔǝdsns \
140 ɟʃɐɥ I 'sɹǝʇʇǝʃ ʃɐuıƃıɹo ɹıǝɥʇ ƃuıʞɔɐʃ 'sɐʍ ʇı s∀ ˙sǝʇou ǝɹɐdɯoɔ oʇ ǝʃqɐ uǝǝq \
141 ʎǝɥʇ pɐɥ ǝsooʃ uǝʞoɹq ǝʌɐɥ pʃnoʍ ɔıuɐd ʇɐɥʇ ʍouʞ I puɐ 'ǝɯɐɔ sɹǝʍsuɐ ʇuǝuıʇɹǝd \
142 ǝɥʇ ʇɐɥʇ sʇǝod puɐ sʇsıʇɹɐ ǝɥʇ ɯoɹɟ sɐʍ ʇI";