proptest/arbitrary/_alloc/
char.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Arbitrary implementations for `std::char`.
11
12use crate::std_facade::Vec;
13use core::char::*;
14use core::iter::once;
15use core::ops::Range;
16
17use crate::collection::vec;
18
19multiplex_alloc! {
20    core::char::DecodeUtf16, std::char::DecodeUtf16,
21    core::char::DecodeUtf16Error, std::char::DecodeUtf16Error,
22    core::char::decode_utf16, std::char::decode_utf16
23}
24
25const VEC_MAX: usize = ::core::u16::MAX as usize;
26
27use crate::arbitrary::*;
28use crate::strategy::statics::static_map;
29use crate::strategy::*;
30
31macro_rules! impl_wrap_char {
32    ($type: ty, $mapper: expr) => {
33        arbitrary!($type, SMapped<char, Self>;
34            static_map(any::<char>(), $mapper));
35    };
36}
37
38impl_wrap_char!(EscapeDebug, char::escape_debug);
39impl_wrap_char!(EscapeDefault, char::escape_default);
40impl_wrap_char!(EscapeUnicode, char::escape_unicode);
41#[cfg(feature = "unstable")]
42impl_wrap_char!(ToLowercase, char::to_lowercase);
43#[cfg(feature = "unstable")]
44impl_wrap_char!(ToUppercase, char::to_uppercase);
45
46#[cfg(feature = "break-dead-code")]
47arbitrary!(DecodeUtf16<<Vec<u16> as IntoIterator>::IntoIter>,
48    SMapped<Vec<u16>, Self>;
49    static_map(vec(any::<u16>(), ..VEC_MAX), decode_utf16)
50);
51
52arbitrary!(ParseCharError, IndFlatten<Mapped<bool, Just<Self>>>;
53    any::<bool>().prop_ind_flat_map(|is_two|
54        Just((if is_two { "__" } else { "" }).parse::<char>().unwrap_err()))
55);
56
57#[cfg(feature = "unstable")]
58arbitrary!(CharTryFromError; {
59    use core::convert::TryFrom;
60    char::try_from(0xD800 as u32).unwrap_err()
61});
62
63arbitrary!(DecodeUtf16Error, SFnPtrMap<Range<u16>, Self>;
64    static_map(0xD800..0xE000, |x|
65        decode_utf16(once(x)).next().unwrap().unwrap_err())
66);
67
68#[cfg(test)]
69mod test {
70    no_panic_test!(
71        escape_debug => EscapeDebug,
72        escape_default => EscapeDefault,
73        escape_unicode => EscapeUnicode,
74        parse_char_error => ParseCharError,
75        decode_utf16_error => DecodeUtf16Error
76    );
77
78    #[cfg(feature = "break-dead-code")]
79    no_panic_test!(
80        decode_utf16 => DecodeUtf16<<Vec<u16> as IntoIterator>::IntoIter>
81    );
82
83    #[cfg(feature = "unstable")]
84    no_panic_test!(
85        to_lowercase => ToLowercase,
86        to_uppercase => ToUppercase,
87        char_try_from_error => CharTryFromError
88    );
89}