about summary refs log tree commit diff
path: root/library/coretests/tests/ffi/cstr.rs
blob: 7d669cc1c3fff0f014641ffe5e70e051604ae231 (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
use core::ffi::CStr;

#[test]
fn compares_as_u8s() {
    let a: &CStr = c"Hello!"; // Starts with ascii
    let a_bytes: &[u8] = a.to_bytes();
    assert!((..0b1000_0000).contains(&a_bytes[0]));

    let b: &CStr = c"こんにちは!"; // Starts with non ascii
    let b_bytes: &[u8] = b.to_bytes();
    assert!((0b1000_0000..).contains(&b_bytes[0]));

    assert_eq!(Ord::cmp(a, b), Ord::cmp(a_bytes, b_bytes));
    assert_eq!(PartialOrd::partial_cmp(a, b), PartialOrd::partial_cmp(a_bytes, b_bytes));
}

#[test]
fn debug() {
    let s = c"abc\x01\x02\n\xE2\x80\xA6\xFF";
    assert_eq!(format!("{s:?}"), r#""abc\x01\x02\n…\xff""#);
}

#[test]
fn display() {
    let s = c"\xf0\x28\x8c\xbc";
    assert_eq!(format!("{}", s.display()), "�(��");
}