|
| 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::{path::PathBuf, process::Command}; |
| 12 | + |
| 13 | +fn run_codegen_test(bench_name: &str, target_cpu: &str, bless: bool) { |
| 14 | + let manifest_path = env!("CARGO_MANIFEST_PATH"); |
| 15 | + let target_dir = env!("CARGO_TARGET_DIR"); |
| 16 | + |
| 17 | + let output = Command::new("cargo") |
| 18 | + .args([ |
| 19 | + "asm", |
| 20 | + "-p", |
| 21 | + "zerocopy", |
| 22 | + "--manifest-path", |
| 23 | + manifest_path, |
| 24 | + "--target-dir", |
| 25 | + target_dir, |
| 26 | + "--bench", |
| 27 | + bench_name, |
| 28 | + /*"--target-cpu", |
| 29 | + target_cpu,*/ |
| 30 | + "--mca", |
| 31 | + "codegen_test", |
| 32 | + ]) |
| 33 | + .output() |
| 34 | + .expect("failed to execute process"); |
| 35 | + |
| 36 | + let actual_result = output.stdout; |
| 37 | + |
| 38 | + if !(output.status.success()) { |
| 39 | + panic!("{}", String::from_utf8_lossy(&output.stderr)); |
| 40 | + } |
| 41 | + |
| 42 | + let expected_file_path = { |
| 43 | + let mut path: PathBuf = env!("CARGO_MANIFEST_DIR").into(); |
| 44 | + path.push("benches"); |
| 45 | + let file_name = format!("{bench_name}.{target_cpu}.mca"); |
| 46 | + path.push(file_name); |
| 47 | + path |
| 48 | + }; |
| 49 | + |
| 50 | + if bless { |
| 51 | + std::fs::write(expected_file_path, &actual_result).unwrap(); |
| 52 | + } else { |
| 53 | + let expected_result = std::fs::read(expected_file_path).unwrap_or_default(); |
| 54 | + if actual_result != expected_result { |
| 55 | + let expected = String::from_utf8_lossy(&expected_result[..]); |
| 56 | + panic!("Bless codegen tests with BLESS=1\nGot unexpected output:\n{}", expected); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +#[cfg_attr(miri, ignore)] |
| 63 | +fn codegen() { |
| 64 | + let bless = std::env::var("BLESS").ok().filter(|s| s == "1").is_some(); |
| 65 | + let paths = std::fs::read_dir("benches").unwrap(); |
| 66 | + for path in paths { |
| 67 | + let path = path.unwrap().path(); |
| 68 | + if !path.extension().map(|s| s == "rs").unwrap_or(false) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + let path = path.file_stem().unwrap().to_str().unwrap(); |
| 72 | + run_codegen_test(path, "icelake-server", bless); |
| 73 | + } |
| 74 | +} |
0 commit comments