Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #293 from pbor/master
Browse files Browse the repository at this point in the history
regen
  • Loading branch information
sdroege authored Feb 7, 2021
2 parents a16a17d + 3f634e6 commit c396778
Show file tree
Hide file tree
Showing 88 changed files with 7,896 additions and 1,259 deletions.
1 change: 0 additions & 1 deletion atk/src/auto/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use glib::translate::*;
use std::fmt;

glib::wrapper! {
Expand Down
2 changes: 1 addition & 1 deletion atk/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ 21213ed)
Generated by gir (https://github.com/gtk-rs/gir @ 1c7b41a)
from gir-files (https://github.com/gtk-rs/gir-files @ 21f7670)
193 changes: 77 additions & 116 deletions atk/sys/tests/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use atk_sys::*;
use std::env;
use std::error::Error;
use std::ffi::OsString;
use std::mem::{align_of, size_of};
use std::path::Path;
use std::process::Command;
Expand All @@ -22,6 +23,8 @@ impl Compiler {
pub fn new() -> Result<Compiler, Box<dyn Error>> {
let mut args = get_var("CC", "cc")?;
args.push("-Wno-deprecated-declarations".to_owned());
// For _Generic
args.push("-std=c11".to_owned());
// For %z support in printf when using MinGW.
args.push("-D__USE_MINGW_ANSI_STDIO".to_owned());
args.extend(get_var("CFLAGS", "")?);
Expand All @@ -30,14 +33,6 @@ impl Compiler {
Ok(Compiler { args })
}

pub fn define<'a, V: Into<Option<&'a str>>>(&mut self, var: &str, val: V) {
let arg = match val.into() {
None => format!("-D{}", var),
Some(val) => format!("-D{}={}", var, val),
};
self.args.push(arg);
}

pub fn compile(&self, src: &Path, out: &Path) -> Result<(), Box<dyn Error>> {
let mut cmd = self.to_command();
cmd.arg(src);
Expand Down Expand Up @@ -69,7 +64,8 @@ fn pkg_config_cflags(packages: &[&str]) -> Result<Vec<String>, Box<dyn Error>> {
if packages.is_empty() {
return Ok(Vec::new());
}
let mut cmd = Command::new("pkg-config");
let pkg_config = env::var_os("PKG_CONFIG").unwrap_or_else(|| OsString::from("pkg-config"));
let mut cmd = Command::new(pkg_config);
cmd.arg("--cflags");
cmd.args(packages);
let out = cmd.output()?;
Expand All @@ -92,8 +88,6 @@ struct Results {
passed: usize,
/// Total number of failed tests (including those that failed to compile).
failed: usize,
/// Number of tests that failed to compile.
failed_to_compile: usize,
}

impl Results {
Expand All @@ -103,15 +97,8 @@ impl Results {
fn record_failed(&mut self) {
self.failed += 1;
}
fn record_failed_to_compile(&mut self) {
self.failed += 1;
self.failed_to_compile += 1;
}
fn summary(&self) -> String {
format!(
"{} passed; {} failed (compilation errors: {})",
self.passed, self.failed, self.failed_to_compile
)
format!("{} passed; {} failed", self.passed, self.failed)
}
fn expect_total_success(&self) {
if self.failed == 0 {
Expand All @@ -124,128 +111,102 @@ impl Results {

#[test]
fn cross_validate_constants_with_c() {
let tmpdir = Builder::new()
.prefix("abi")
.tempdir()
.expect("temporary directory");
let cc = Compiler::new().expect("configured compiler");
let mut c_constants: Vec<(String, String)> = Vec::new();

assert_eq!(
"1",
get_c_value(tmpdir.path(), &cc, "1").expect("C constant"),
"failed to obtain correct constant value for 1"
);
for l in get_c_output("constant").unwrap().lines() {
let mut words = l.trim().split(";");
let name = words.next().expect("Failed to parse name").to_owned();
let value = words
.next()
.and_then(|s| s.parse().ok())
.expect("Failed to parse value");
c_constants.push((name, value));
}

let mut results: Results = Default::default();
for (i, &(name, rust_value)) in RUST_CONSTANTS.iter().enumerate() {
match get_c_value(tmpdir.path(), &cc, name) {
Err(e) => {
results.record_failed_to_compile();
eprintln!("{}", e);
}
Ok(ref c_value) => {
if rust_value == c_value {
results.record_passed();
} else {
results.record_failed();
eprintln!(
"Constant value mismatch for {}\nRust: {:?}\nC: {:?}",
name, rust_value, c_value
);
}
}
};
if (i + 1) % 25 == 0 {
println!("constants ... {}", results.summary());
let mut results = Results::default();

for ((rust_name, rust_value), (c_name, c_value)) in
RUST_CONSTANTS.iter().zip(c_constants.iter())
{
if rust_name != c_name {
results.record_failed();
eprintln!("Name mismatch:\nRust: {:?}\nC: {:?}", rust_name, c_name,);
continue;
}

if rust_value != c_value {
results.record_failed();
eprintln!(
"Constant value mismatch for {}\nRust: {:?}\nC: {:?}",
rust_name, rust_value, &c_value
);
continue;
}

results.record_passed();
}

results.expect_total_success();
}

#[test]
fn cross_validate_layout_with_c() {
let tmpdir = Builder::new()
.prefix("abi")
.tempdir()
.expect("temporary directory");
let cc = Compiler::new().expect("configured compiler");
let mut c_layouts = Vec::new();

assert_eq!(
Layout {
size: 1,
alignment: 1
},
get_c_layout(tmpdir.path(), &cc, "char").expect("C layout"),
"failed to obtain correct layout for char type"
);
for l in get_c_output("layout").unwrap().lines() {
let mut words = l.trim().split(";");
let name = words.next().expect("Failed to parse name").to_owned();
let size = words
.next()
.and_then(|s| s.parse().ok())
.expect("Failed to parse size");
let alignment = words
.next()
.and_then(|s| s.parse().ok())
.expect("Failed to parse alignment");
c_layouts.push((name, Layout { size, alignment }));
}

let mut results: Results = Default::default();
for (i, &(name, rust_layout)) in RUST_LAYOUTS.iter().enumerate() {
match get_c_layout(tmpdir.path(), &cc, name) {
Err(e) => {
results.record_failed_to_compile();
eprintln!("{}", e);
}
Ok(c_layout) => {
if rust_layout == c_layout {
results.record_passed();
} else {
results.record_failed();
eprintln!(
"Layout mismatch for {}\nRust: {:?}\nC: {:?}",
name, rust_layout, &c_layout
);
}
}
};
if (i + 1) % 25 == 0 {
println!("layout ... {}", results.summary());
let mut results = Results::default();

for ((rust_name, rust_layout), (c_name, c_layout)) in RUST_LAYOUTS.iter().zip(c_layouts.iter())
{
if rust_name != c_name {
results.record_failed();
eprintln!("Name mismatch:\nRust: {:?}\nC: {:?}", rust_name, c_name,);
continue;
}
}
results.expect_total_success();
}

fn get_c_layout(dir: &Path, cc: &Compiler, name: &str) -> Result<Layout, Box<dyn Error>> {
let exe = dir.join("layout");
let mut cc = cc.clone();
cc.define("ABI_TYPE_NAME", name);
cc.compile(Path::new("tests/layout.c"), &exe)?;
if rust_layout != c_layout {
results.record_failed();
eprintln!(
"Layout mismatch for {}\nRust: {:?}\nC: {:?}",
rust_name, rust_layout, &c_layout
);
continue;
}

let mut abi_cmd = Command::new(exe);
let output = abi_cmd.output()?;
if !output.status.success() {
return Err(format!("command {:?} failed, {:?}", &abi_cmd, &output).into());
results.record_passed();
}

let stdout = str::from_utf8(&output.stdout)?;
let mut words = stdout.trim().split_whitespace();
let size = words.next().unwrap().parse().unwrap();
let alignment = words.next().unwrap().parse().unwrap();
Ok(Layout { size, alignment })
results.expect_total_success();
}

fn get_c_value(dir: &Path, cc: &Compiler, name: &str) -> Result<String, Box<dyn Error>> {
let exe = dir.join("constant");
let mut cc = cc.clone();
cc.define("ABI_CONSTANT_NAME", name);
cc.compile(Path::new("tests/constant.c"), &exe)?;
fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
let tmpdir = Builder::new().prefix("abi").tempdir()?;
let exe = tmpdir.path().join(name);
let c_file = Path::new("tests").join(name).with_extension("c");

let cc = Compiler::new().expect("configured compiler");
cc.compile(&c_file, &exe)?;

let mut abi_cmd = Command::new(exe);
let output = abi_cmd.output()?;
if !output.status.success() {
return Err(format!("command {:?} failed, {:?}", &abi_cmd, &output).into());
}

let output = str::from_utf8(&output.stdout)?.trim();
if !output.starts_with("###gir test###") || !output.ends_with("###gir test###") {
return Err(format!(
"command {:?} return invalid output, {:?}",
&abi_cmd, &output
)
.into());
}

Ok(String::from(&output[14..(output.len() - 14)]))
Ok(String::from_utf8(output.stdout)?)
}

const RUST_LAYOUTS: &[(&str, Layout)] = &[
Expand Down
Loading

0 comments on commit c396778

Please sign in to comment.