File: test_ascii_strings.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-backports, trixie-proposed-updates
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (53 lines) | stat: -rw-r--r-- 1,724 bytes parent folder | download | duplicates (15)
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
use borsh::{from_slice, to_vec};

use alloc::{
    string::String,
    vec::Vec,
};

/// Verifies serialisation and deserialisation of an ASCII string `value`.
fn check_ascii(value: &str) -> Vec<u8> {
    // Caller promises value is ASCII.
    let ascii_str = ascii::AsciiStr::from_ascii(&value).unwrap();
    let buf = to_vec(ascii_str).unwrap();
    // AsciiStr and AsciiString serialise the same way String does.
    assert_eq!(buf, to_vec(&ascii::AsciiString::from(ascii_str)).unwrap());
    // Check round trip.
    let got = from_slice::<ascii::AsciiString>(&buf).unwrap();
    assert_eq!(ascii_str, got);
    buf
}

macro_rules! test_ascii_string {
    ($test_name: ident, $str: expr, $snap:expr) => {
        #[test]
        fn $test_name() {
            let value = String::from($str);
            let _buf = check_ascii(&value);
            #[cfg(feature = "std")]
            if $snap {
                insta::assert_debug_snapshot!(_buf);
            }
        }
    };
}

test_ascii_string!(test_empty_string, "", true);
test_ascii_string!(test_a, "a", true);
test_ascii_string!(test_hello_world, "hello world", true);
test_ascii_string!(test_x_1024, "x".repeat(1024), true);
test_ascii_string!(test_x_4096, "x".repeat(4096), false);
test_ascii_string!(test_x_65535, "x".repeat(65535), false);
test_ascii_string!(test_hello_10, "hello world!".repeat(30), true);
test_ascii_string!(test_hello_1000, "hello Achilles!".repeat(1000), false);

#[test]
fn test_ascii_char() {
    use ascii::AsciiChar;

    let buf = to_vec(&AsciiChar::Dot).unwrap();
    assert_eq!(".".as_bytes(), buf);
    assert_eq!(AsciiChar::Dot, from_slice::<AsciiChar>(&buf).unwrap());

    from_slice::<AsciiChar>(&[b'\x80']).unwrap_err();
}