aboutsummaryrefslogtreecommitdiff
path: root/tests/raw.rs
blob: fe2970555408b33ccd4d4791e3e1975198b74ec8 (plain)
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
#![cfg(feature = "raw_os_str")]

use std::ffi::OsStr;

use os_str_bytes::EncodingError;
use os_str_bytes::OsStrBytes;
use os_str_bytes::RawOsStr;

mod common;
use common::RAW_WTF8_STRING;

fn from_raw_bytes(string: &[u8]) -> Result<&RawOsStr, EncodingError> {
    // SAFETY: The string is validated before conversion.
    OsStr::from_raw_bytes(string)
        .map(|_| unsafe { common::from_raw_bytes_unchecked(string) })
}

#[test]
fn test_ends_with() {
    test(true, b"");
    test(true, b"r");
    test(true, b"ar");
    test(true, b"bar");
    if cfg!(not(windows)) {
        test(true, b"\xA9bar");
        test(true, b"\x92\xA9bar");
        test(true, b"\x9F\x92\xA9bar");
    }
    test(cfg!(windows), b"\xED\xB2\xA9bar");
    test(true, b"\xF0\x9F\x92\xA9bar");
    test(true, b"\xED\xA0\xBD\xF0\x9F\x92\xA9bar");
    test(true, b"o\xED\xA0\xBD\xF0\x9F\x92\xA9bar");
    test(true, b"oo\xED\xA0\xBD\xF0\x9F\x92\xA9bar");
    test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92\xA9bar");

    test(false, b"\xED\xA0\xBDbar");
    test(false, b"\xED\xB2\xA9aar");

    fn test(result: bool, suffix: &[u8]) {
        let suffix = from_raw_bytes(suffix).unwrap();
        assert_eq!(result, RAW_WTF8_STRING.ends_with_os(suffix));
    }
}

#[test]
fn test_empty_ends_with() {
    macro_rules! test {
        ( $result:expr , $string:expr , $substring:expr ) => {
            #[allow(clippy::bool_assert_comparison)]
            {
                assert_eq!(
                    $result,
                    RawOsStr::from_str($string)
                        .ends_with_os(RawOsStr::from_str($substring)),
                );
            }
        };
    }
    test!(true, "", "");
    test!(false, "", "r");
    test!(false, "", "ar");
}

#[test]
fn test_starts_with() {
    test(true, b"");
    test(true, b"f");
    test(true, b"fo");
    test(true, b"foo");
    test(true, b"foo\xED\xA0\xBD");
    if cfg!(not(windows)) {
        test(true, b"foo\xED\xA0\xBD\xF0");
        test(true, b"foo\xED\xA0\xBD\xF0\x9F");
        test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92");
    }
    test(cfg!(windows), b"foo\xED\xA0\xBD\xED\xA0\xBD");
    test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92\xA9");
    test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92\xA9b");
    test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92\xA9ba");
    test(true, b"foo\xED\xA0\xBD\xF0\x9F\x92\xA9bar");

    test(false, b"foo\xED\xB2\xA9");
    test(false, b"fof\xED\xA0\xBD\xED\xA0\xBD");

    fn test(result: bool, prefix: &[u8]) {
        let prefix = from_raw_bytes(prefix).unwrap();
        assert_eq!(result, RAW_WTF8_STRING.starts_with_os(prefix));
    }
}

#[test]
fn test_empty_starts_with() {
    macro_rules! test {
        ( $result:expr , $string:expr , $substring:expr ) => {
            #[allow(clippy::bool_assert_comparison)]
            {
                assert_eq!(
                    $result,
                    RawOsStr::from_str($string)
                        .starts_with_os(RawOsStr::from_str($substring)),
                );
            }
        };
    }
    test!(true, "", "");
    test!(false, "", "f");
    test!(false, "", "fo");
}