• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "checked_conversions")]
2 
3 use std::str;
4 
5 mod common;
6 use common::Result;
7 use common::WTF8_STRING;
8 
assert_string_is_invalid_utf8(string: &[u8])9 fn assert_string_is_invalid_utf8(string: &[u8]) {
10     assert!(str::from_utf8(string).is_err());
11 }
12 
assert_invalid_result(result: &Result<()>)13 fn assert_invalid_result(result: &Result<()>) {
14     if cfg!(windows) {
15         assert!(result.is_err());
16     } else {
17         assert_eq!(&Ok(()), result);
18     }
19 }
20 
21 #[test]
test_empty()22 fn test_empty() {
23     common::test_utf8_bytes("");
24     common::test_utf8_vec("");
25 }
26 
27 #[test]
test_nonempty_utf8()28 fn test_nonempty_utf8() {
29     const UTF8_STRING: &str = "string";
30 
31     common::test_utf8_bytes(UTF8_STRING);
32     common::test_utf8_vec(UTF8_STRING);
33 }
34 
35 #[test]
test_invalid()36 fn test_invalid() {
37     const INVALID_STRING: &[u8] = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
38     assert_string_is_invalid_utf8(INVALID_STRING);
39 
40     assert_invalid_result(&common::test_bytes(INVALID_STRING));
41     assert_invalid_result(&common::test_vec(INVALID_STRING));
42 }
43 
44 #[test]
test_wtf8()45 fn test_wtf8() {
46     const HIGH_SURROGATE: &[u8] = b"\xED\xA0\x80";
47     const LOW_SURROGATE: &[u8] = b"\xED\xB0\x80";
48 
49     for string in [WTF8_STRING, HIGH_SURROGATE, LOW_SURROGATE] {
50         assert_string_is_invalid_utf8(string);
51 
52         assert_eq!(Ok(()), common::test_bytes(string));
53         assert_eq!(Ok(()), common::test_vec(string));
54     }
55 }
56