aboutsummaryrefslogtreecommitdiff
path: root/src/rust/tor_util/tests/rust_string.rs
blob: 1ff605a43cd256125a38e1e8438a4dc77c12eeb3 (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
extern crate tor_util;
extern crate libc;

use std::ffi::CString;
use tor_util::RustString;

#[test]
fn rust_string_conversions_preserve_c_string() {
    let s = CString::new("asdf foo").unwrap();
    let r = RustString::from(s.clone());
    let r2 = RustString::from(s.clone());
    let c = r2.as_ptr();
    assert_eq!(unsafe { libc::strlen(c) }, 8);
    let c_str = r.into();
    assert_eq!(s, c_str);
}

#[test]
fn empty_string() {
    let s = CString::new("").unwrap();
    let r = RustString::from(s.clone());
    let c = r.as_ptr();
    assert_eq!(unsafe { libc::strlen(c) }, 0);
    let c_str = r.into();
    assert_eq!(s, c_str);
}

#[test]
fn c_string_with_unicode() {
    // The euro sign is three bytes
    let s = CString::new("asd€asd").unwrap();
    let r = RustString::from(s.clone());
    let c = r.as_ptr();
    assert_eq!(unsafe { libc::strlen(c) }, 9);
    let c_str = r.into();
    assert_eq!(s, c_str);
}