Skip to content

Commit

Permalink
Add test for all possible error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Sep 16, 2015
1 parent 63ff2b4 commit 3e7a6a9
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<'a> Generator<'a> {
t!(writeln!(self.rust, r#"
fn fn_{name}() {{
extern {{
fn __test_fn_{name}() -> size_t;
fn __test_fn_{name}() -> *mut ();
}}
unsafe {{
if !{skip} {{
Expand Down
16 changes: 16 additions & 0 deletions testcrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@ build = "build.rs"
[build-dependencies]
ctest = { path = ".." }
gcc = "0.3"

[dependencies]
libc = "0.1"

[lib]
name = "testcrate"
test = false
doctest = false

[[bin]]
name = "t1"
test = false

[[bin]]
name = "t2"
test = false
7 changes: 7 additions & 0 deletions testcrate/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ extern crate ctest;
extern crate gcc;

fn main() {
gcc::Config::new()
.include("src")
.file("src/t1.c")
.compile("libt1.a");
gcc::Config::new()
.file("src/t2.c")
.compile("libt2.a");
ctest::TestGenerator::new()
.header("t1.h")
.include("src")
Expand Down
2 changes: 2 additions & 0 deletions testcrate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
extern crate libc;

pub mod t1;
pub mod t2;
9 changes: 9 additions & 0 deletions testcrate/src/t1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdlib.h>
#include "t1.h"

void T1a(void) {}
void* T1b(void) { return NULL; }
void* T1c(void* a) { return NULL; }
int32_t T1d(unsigned a ) { return 0; }
void T1e(unsigned a, const struct T1Bar* b) { }
void T1f(void) {}
23 changes: 22 additions & 1 deletion testcrate/src/t1.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#include <stdint.h>

typedef int32_t Foo;
typedef int32_t T1Foo;

struct T1Bar {
int32_t a;
uint32_t b;
T1Foo c;
uint8_t d;
};

struct T1Baz {
uint64_t a;
struct T1Bar b;
};

void T1a(void);
void* T1b(void);
void* T1c(void*);
int32_t T1d(unsigned);
void T1e(unsigned, const struct T1Bar*);
void T1f(void);

#define T1C 4
31 changes: 30 additions & 1 deletion testcrate/src/t1.rs
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
pub type Foo = i32;
use libc::*;

pub type T1Foo = i32;

#[repr(C)]
pub struct T1Bar {
pub a: i32,
pub b: u32,
pub c: T1Foo,
pub d: u8,
}

#[repr(C)]
pub struct T1Baz {
pub a: u64,
pub b: T1Bar,
}

pub const T1C: u32 = 4;

extern {
pub fn T1a();
pub fn T1b() -> *mut c_void;
pub fn T1c(a: *mut c_void) -> *mut c_void;
pub fn T1d(a: c_uint) -> i32;
pub fn T1e(a: c_uint, b: *const T1Bar);

#[link_name = "T1f"]
pub fn f();
}
2 changes: 2 additions & 0 deletions testcrate/src/t2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

void T2a() {}
12 changes: 11 additions & 1 deletion testcrate/src/t2.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#include <stdint.h>

typedef int32_t Foo;
typedef int32_t T2Foo;
typedef int8_t T2Bar;

struct T2Baz {
int64_t a;
uint32_t b;
};

static void T2a(void) {}

#define T2C 4
15 changes: 14 additions & 1 deletion testcrate/src/t2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
pub type Foo = u32;
pub type T2Foo = u32;
pub type T2Bar = u32;

#[repr(C)]
pub struct T2Baz {
pub a: u8,
pub b: i32,
}

pub const T2C: i32 = 5;

extern {
pub fn T2a();
}
52 changes: 45 additions & 7 deletions testcrate/tests/all.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::process::Command;
use std::process::{Command, ExitStatus};
use std::collections::HashSet;
use std::env;

fn cmd(name: &str) -> Command {
Expand All @@ -10,14 +11,51 @@ fn cmd(name: &str) -> Command {

#[test]
fn t1() {
let mut c = cmd("t1");
let output = c.output().unwrap();
assert!(output.status.success());
let (o, status) = output(&mut cmd("t1"));
assert!(status.success(), o);
assert!(!o.contains("bad "), o);
}

#[test]
fn t2() {
let mut c = cmd("t2");
let output = c.output().unwrap();
assert!(!output.status.success());
let (o, status) = output(&mut cmd("t2"));
assert!(!status.success(), o);
let errors = [
"bad T2Foo signed",
"bad T2Bar size",
"bad T2Bar align",
"bad T2Bar signed",
"bad T2Baz size",
"bad T2Baz align",
"bad field size a of T2Baz",
"bad field offset b of T2Baz",
"bad T2a function pointer",
"bad T2C value",
];
let mut errors = errors.iter().cloned().collect::<HashSet<_>>();

let mut bad = false;
for line in o.lines().filter(|l| l.starts_with("bad ")) {
let msg = &line[..line.find(":").unwrap()];
if !errors.remove(&msg) {
println!("unknown error: {}", msg);
bad = true;
}
}

for error in errors {
println!("didn't find error: {}", error);
bad = true;
}
if bad {
panic!();
}
}

fn output(cmd: &mut Command) -> (String, ExitStatus) {
let output = cmd.output().unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
let stderr = String::from_utf8(output.stderr).unwrap();

(stdout + &stderr, output.status)
}

0 comments on commit 3e7a6a9

Please sign in to comment.