Debian Codegraph @

debian/rust-vendor/zerocopy/tests/codegen.rs

raw
1 // Copyright 2026 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 #![cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
10
11 use std::{panic, path::PathBuf, process::Command, thread};
12
13 use regex::Regex;
14
15 enum Directive {
16 Asm,
17 Mca,
18 }
19
20 impl Directive {
21 fn arg(&self) -> &'static str {
22 match self {
23 Directive::Asm => "--asm",
24 Directive::Mca => "--mca",
25 }
26 }
27
28 fn ext(&self) -> &'static str {
29 match self {
30 Directive::Asm => "",
31 Directive::Mca => ".mca",
32 }
33 }
34 }
35
36 fn run_codegen_test(bench_name: &str, target_cpu: &str, bless: bool) {
37 println!("Testing {bench_name}.{target_cpu}");
38
39 let manifest_path = env!("CARGO_MANIFEST_PATH");
40 let target_dir = env!("CARGO_TARGET_DIR");
41
42 let cargo_asm = |directive: &Directive| {
43 Command::new("./cargo.sh")
44 .args([
45 "+nightly",
46 "asm",
47 "--quiet",
48 "-p",
49 "zerocopy",
50 "--manifest-path",
51 manifest_path,
52 "--target-dir",
53 target_dir,
54 "--all-features",
55 "--bench",
56 bench_name,
57 "--target-cpu",
58 target_cpu,
59 "--simplify",
60 directive.arg(),
61 &format!("bench_{bench_name}"),
62 ])
63 .output()
64 .expect("failed to execute process")
65 };
66
67 let re = Regex::new(r"(\.Lanon\.)[0-z]+(\.\d+)").unwrap();
68
69 let test_directive = |directive: Directive| {
70 let output = cargo_asm(&directive);
71 let actual_result = String::from_utf8_lossy(&output.stdout);
72 let actual_result = re.replace_all(&actual_result, "${1}HASH${2}");
73
74 if !(output.status.success()) {
75 panic!("{}\n{}", &actual_result, String::from_utf8_lossy(&output.stderr));
76 }
77
78 let expected_file_path = {
79 let ext = directive.ext();
80 let mut path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
81 path.push("benches");
82 let file_name = format!("{bench_name}.{target_cpu}{ext}",);
83 path.push(file_name);
84 path
85 };
86
87 if bless {
88 std::fs::write(expected_file_path, actual_result.as_bytes()).unwrap();
89 } else {
90 let expected_result = std::fs::read(expected_file_path).unwrap_or_default();
91 if actual_result.as_bytes() != expected_result {
92 let expected = String::from_utf8_lossy(&expected_result[..]);
93 panic!("Bless codegen tests with BLESS=1\nGot unexpected output:\n{}", expected);
94 }
95 }
96 };
97
98 test_directive(Directive::Asm);
99 test_directive(Directive::Mca);
100 }
101
102 #[test]
103 #[cfg_attr(miri, ignore)]
104 fn codegen() {
105 let bless = std::env::var("BLESS").is_ok();
106 let handles: Vec<_> = std::fs::read_dir("benches")
107 .unwrap()
108 .map(|entry| entry.unwrap().path())
109 .filter(|path| path.extension().is_some_and(|ext| ext == "rs"))
110 .map(|path| {
111 let bench_name = path.file_stem().unwrap().to_str().unwrap().to_owned();
112 thread::spawn(move || {
113 panic::catch_unwind(panic::AssertUnwindSafe(|| {
114 run_codegen_test(&bench_name, "x86-64", bless);
115 }))
116 })
117 })
118 .collect();
119
120 let failed = handles.into_iter().any(|handle| handle.join().unwrap().is_err());
121
122 if failed {
123 panic!("One or more codegen tests failed. See thread panics above for details.");
124 }
125 }