Skip to content

Commit

Permalink
Support testing constants which are structs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Sep 17, 2015
1 parent 6ac333e commit a2bea04
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,25 @@ impl<'a> Generator<'a> {
let cty = self.rust_ty_to_c_ty(rust_ty);

t!(writeln!(self.c, r#"
{cty} __test_const_{name}(void) {{ return {name}; }}
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}() -> {ty};
fn __test_const_{name}() -> *const {ty};
}}
let val = {name};
unsafe {{
same({name}, __test_const_{name}(), "{name} value");
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));
Expand Down
2 changes: 1 addition & 1 deletion testcrate/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn t2() {
"bad field offset b of T2Baz",
"bad field type b of T2Baz",
"bad T2a function pointer",
"bad T2C value",
"bad T2C value at byte 0",
];
let mut errors = errors.iter().cloned().collect::<HashSet<_>>();

Expand Down

0 comments on commit a2bea04

Please sign in to comment.