Debian Codegraph @

debian/missing-sources/gio-unix-sys/tests/abi.rs

raw
1 // This file was generated by gir (https://github.com/gtk-rs/gir)
2 // from gir-files (https://github.com/gtk-rs/gir-files)
3 // DO NOT EDIT
4
5 #![cfg(unix)]
6
7 use gio_unix_sys::*;
8 use std::env;
9 use std::error::Error;
10 use std::ffi::OsString;
11 use std::mem::{align_of, size_of};
12 use std::path::Path;
13 use std::process::{Command, Stdio};
14 use std::str;
15 use tempfile::Builder;
16
17 static PACKAGES: &[&str] = &["gio-unix-2.0"];
18
19 #[derive(Clone, Debug)]
20 struct Compiler {
21 pub args: Vec<String>,
22 }
23
24 impl Compiler {
25 pub fn new() -> Result<Self, Box<dyn Error>> {
26 let mut args = get_var("CC", "cc")?;
27 args.push("-Wno-deprecated-declarations".to_owned());
28 // For _Generic
29 args.push("-std=c11".to_owned());
30 // For %z support in printf when using MinGW.
31 args.push("-D__USE_MINGW_ANSI_STDIO".to_owned());
32 args.extend(get_var("CFLAGS", "")?);
33 args.extend(get_var("CPPFLAGS", "")?);
34 args.extend(pkg_config_cflags(PACKAGES)?);
35 Ok(Self { args })
36 }
37
38 pub fn compile(&self, src: &Path, out: &Path) -> Result<(), Box<dyn Error>> {
39 let mut cmd = self.to_command();
40 cmd.arg(src);
41 cmd.arg("-o");
42 cmd.arg(out);
43 let status = cmd.spawn()?.wait()?;
44 if !status.success() {
45 return Err(format!("compilation command {cmd:?} failed, {status}").into());
46 }
47 Ok(())
48 }
49
50 fn to_command(&self) -> Command {
51 let mut cmd = Command::new(&self.args[0]);
52 cmd.args(&self.args[1..]);
53 cmd
54 }
55 }
56
57 fn get_var(name: &str, default: &str) -> Result<Vec<String>, Box<dyn Error>> {
58 match env::var(name) {
59 Ok(value) => Ok(shell_words::split(&value)?),
60 Err(env::VarError::NotPresent) => Ok(shell_words::split(default)?),
61 Err(err) => Err(format!("{name} {err}").into()),
62 }
63 }
64
65 fn pkg_config_cflags(packages: &[&str]) -> Result<Vec<String>, Box<dyn Error>> {
66 if packages.is_empty() {
67 return Ok(Vec::new());
68 }
69 let pkg_config = env::var_os("PKG_CONFIG").unwrap_or_else(|| OsString::from("pkg-config"));
70 let mut cmd = Command::new(pkg_config);
71 cmd.arg("--cflags");
72 cmd.args(packages);
73 cmd.stderr(Stdio::inherit());
74 let out = cmd.output()?;
75 if !out.status.success() {
76 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
77 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
78 }
79 let stdout = str::from_utf8(&out.stdout)?;
80 Ok(shell_words::split(stdout.trim())?)
81 }
82
83 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
84 struct Layout {
85 size: usize,
86 alignment: usize,
87 }
88
89 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
90 struct Results {
91 /// Number of successfully completed tests.
92 passed: usize,
93 /// Total number of failed tests (including those that failed to compile).
94 failed: usize,
95 }
96
97 impl Results {
98 fn record_passed(&mut self) {
99 self.passed += 1;
100 }
101 fn record_failed(&mut self) {
102 self.failed += 1;
103 }
104 fn summary(&self) -> String {
105 format!("{} passed; {} failed", self.passed, self.failed)
106 }
107 fn expect_total_success(&self) {
108 if self.failed == 0 {
109 println!("OK: {}", self.summary());
110 } else {
111 panic!("FAILED: {}", self.summary());
112 };
113 }
114 }
115
116 #[test]
117 fn cross_validate_constants_with_c() {
118 let mut c_constants: Vec<(String, String)> = Vec::new();
119
120 for l in get_c_output("constant").unwrap().lines() {
121 let (name, value) = l.split_once(';').expect("Missing ';' separator");
122 c_constants.push((name.to_owned(), value.to_owned()));
123 }
124
125 let mut results = Results::default();
126
127 for ((rust_name, rust_value), (c_name, c_value)) in
128 RUST_CONSTANTS.iter().zip(c_constants.iter())
129 {
130 if rust_name != c_name {
131 results.record_failed();
132 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
133 continue;
134 }
135
136 if rust_value != c_value {
137 results.record_failed();
138 eprintln!(
139 "Constant value mismatch for {rust_name}\nRust: {rust_value:?}\nC: {c_value:?}",
140 );
141 continue;
142 }
143
144 results.record_passed();
145 }
146
147 results.expect_total_success();
148 }
149
150 #[test]
151 fn cross_validate_layout_with_c() {
152 let mut c_layouts = Vec::new();
153
154 for l in get_c_output("layout").unwrap().lines() {
155 let (name, value) = l.split_once(';').expect("Missing first ';' separator");
156 let (size, alignment) = value.split_once(';').expect("Missing second ';' separator");
157 let size = size.parse().expect("Failed to parse size");
158 let alignment = alignment.parse().expect("Failed to parse alignment");
159 c_layouts.push((name.to_owned(), Layout { size, alignment }));
160 }
161
162 let mut results = Results::default();
163
164 for ((rust_name, rust_layout), (c_name, c_layout)) in RUST_LAYOUTS.iter().zip(c_layouts.iter())
165 {
166 if rust_name != c_name {
167 results.record_failed();
168 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
169 continue;
170 }
171
172 if rust_layout != c_layout {
173 results.record_failed();
174 eprintln!("Layout mismatch for {rust_name}\nRust: {rust_layout:?}\nC: {c_layout:?}",);
175 continue;
176 }
177
178 results.record_passed();
179 }
180
181 results.expect_total_success();
182 }
183
184 fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
185 let tmpdir = Builder::new().prefix("abi").tempdir()?;
186 let exe = tmpdir.path().join(name);
187 let c_file = Path::new("tests").join(name).with_extension("c");
188
189 let cc = Compiler::new().expect("configured compiler");
190 cc.compile(&c_file, &exe)?;
191
192 let mut cmd = Command::new(exe);
193 cmd.stderr(Stdio::inherit());
194 let out = cmd.output()?;
195 if !out.status.success() {
196 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
197 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
198 }
199
200 Ok(String::from_utf8(out.stdout)?)
201 }
202
203 const RUST_LAYOUTS: &[(&str, Layout)] = &[
204 (
205 "GDesktopAppInfoClass",
206 Layout {
207 size: size_of::<GDesktopAppInfoClass>(),
208 alignment: align_of::<GDesktopAppInfoClass>(),
209 },
210 ),
211 (
212 "GDesktopAppInfoLookupIface",
213 Layout {
214 size: size_of::<GDesktopAppInfoLookupIface>(),
215 alignment: align_of::<GDesktopAppInfoLookupIface>(),
216 },
217 ),
218 (
219 "GFileDescriptorBasedIface",
220 Layout {
221 size: size_of::<GFileDescriptorBasedIface>(),
222 alignment: align_of::<GFileDescriptorBasedIface>(),
223 },
224 ),
225 (
226 "GUnixFDMessage",
227 Layout {
228 size: size_of::<GUnixFDMessage>(),
229 alignment: align_of::<GUnixFDMessage>(),
230 },
231 ),
232 (
233 "GUnixFDMessageClass",
234 Layout {
235 size: size_of::<GUnixFDMessageClass>(),
236 alignment: align_of::<GUnixFDMessageClass>(),
237 },
238 ),
239 (
240 "GUnixInputStream",
241 Layout {
242 size: size_of::<GUnixInputStream>(),
243 alignment: align_of::<GUnixInputStream>(),
244 },
245 ),
246 (
247 "GUnixInputStreamClass",
248 Layout {
249 size: size_of::<GUnixInputStreamClass>(),
250 alignment: align_of::<GUnixInputStreamClass>(),
251 },
252 ),
253 (
254 "GUnixOutputStream",
255 Layout {
256 size: size_of::<GUnixOutputStream>(),
257 alignment: align_of::<GUnixOutputStream>(),
258 },
259 ),
260 (
261 "GUnixOutputStreamClass",
262 Layout {
263 size: size_of::<GUnixOutputStreamClass>(),
264 alignment: align_of::<GUnixOutputStreamClass>(),
265 },
266 ),
267 ];
268
269 const RUST_CONSTANTS: &[(&str, &str)] = &[(
270 "G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME",
271 "gio-desktop-app-info-lookup",
272 )];