Skip to content

Commit

Permalink
Add support for testing str consts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jul 5, 2017
1 parent de371b4 commit 9771b45
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
73 changes: 58 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ impl TestGenerator {
fn pretty(&self) -> String;
}
impl<'a> Pretty for &'a str {
fn pretty(&self) -> String { format!("{:?}", self) }
}
impl<T> Pretty for *const T {
fn pretty(&self) -> String { format!("{:?}", self) }
}
Expand Down Expand Up @@ -994,6 +997,9 @@ impl<'a> Generator<'a> {
}

fn rust_ty_to_c_ty(&self, mut rust_ty: &str) -> String {
if rust_ty == "&str" {
return "char*".to_string();
}
let mut cty = self.rust2c(&rust_ty.replace("*mut ", "")
.replace("*const ", ""));
while rust_ty.starts_with("*") {
Expand All @@ -1014,30 +1020,47 @@ impl<'a> Generator<'a> {
}

let cty = self.rust_ty_to_c_ty(rust_ty);

t!(writeln!(self.c, r#"
static {cty} __test_const_{name}_val = {name};
{cty}* __test_const_{name}(void) {{
return &__test_const_{name}_val;
}}
"#, name = name, cty = cty));
t!(writeln!(self.rust, r#"
fn const_{name}() {{
extern {{
fn __test_const_{name}() -> *const {ty};

if rust_ty == "&str" {
t!(writeln!(self.rust, r#"
fn const_{name}() {{
extern {{
fn __test_const_{name}() -> *const *const u8;
}}
let val = {name};
unsafe {{
let ptr = *__test_const_{name}();
let c = ::std::ffi::CStr::from_ptr(ptr as *const _);
let c = c.to_str().expect("const {name} not utf8");
same(val, c, "{name} string");
}}
}}
let val = {name};
unsafe {{
let ptr1 = &val as *const _ as *const u8;
let ptr2 = __test_const_{name}() as *const u8;
for i in 0..mem::size_of::<{ty}>() {{
let i = i as isize;
same(*ptr1.offset(i), *ptr2.offset(i),
&format!("{name} value at byte {{}}", i));
"#, name = name));
} else {
t!(writeln!(self.rust, r#"
fn const_{name}() {{
extern {{
fn __test_const_{name}() -> *const {ty};
}}
let val = {name};
unsafe {{
let ptr1 = &val as *const _ as *const u8;
let ptr2 = __test_const_{name}() as *const u8;
for i in 0..mem::size_of::<{ty}>() {{
let i = i as isize;
same(*ptr1.offset(i), *ptr2.offset(i),
&format!("{name} value at byte {{}}", i));
}}
}}
}}
}}
"#, ty = rust_ty, name = name));
"#, ty = rust_ty, name = name));
}
self.tests.push(format!("const_{}", name));
}

Expand Down Expand Up @@ -1149,6 +1172,26 @@ impl<'a> Generator<'a> {
assert!(rust);
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
}
ast::TyKind::Rptr(_, ast::MutTy {
ref ty,
mutbl: ast::Mutability::Immutable,
}) => {
let path = match ty.node {
ast::TyKind::Path(_, ref p) => p,
_ => panic!("unknown ty {:?}", ty),
};
if path.segments.len() != 1 {
panic!("unknown ty {:?}", ty)
}
if &*path.segments[0].identifier.name.as_str() != "str" {
panic!("unknown ty {:?}", ty)
}
if rust {
format!("&str")
} else {
format!("char*")
}
}
_ => panic!("unknown ty {:?}", ty),
}
}
Expand Down
1 change: 1 addition & 0 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
typedef int32_t T1Foo;

#define T1N 5
#define T1S "foo"

struct T1Bar {
int32_t a;
Expand Down
1 change: 1 addition & 0 deletions testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use libc::*;

pub type T1Foo = i32;
pub const T1S: &'static str = "foo";

pub const T1N: i32 = 5;

Expand Down
1 change: 1 addition & 0 deletions testcrate/src/t2.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ struct T2Baz {
static void T2a(void) {}

#define T2C 4
#define T2S "a"
1 change: 1 addition & 0 deletions testcrate/src/t2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct T2Baz {
}

pub const T2C: i32 = 5;
pub const T2S: &'static str = "b";

extern {
pub fn T2a();
Expand Down
1 change: 1 addition & 0 deletions testcrate/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn t2() {
"bad field type b of T2Baz",
"bad T2a function pointer",
"bad T2C value at byte 0",
"bad T2S string",
];
let mut errors = errors.iter().cloned().collect::<HashSet<_>>();

Expand Down

0 comments on commit 9771b45

Please sign in to comment.