|
| 1 | +use cheetah_string::CheetahString; |
| 2 | +use std::env; |
| 3 | +use std::fs; |
| 4 | +use std::mem::{align_of, size_of}; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +fn target_dir() -> PathBuf { |
| 8 | + env::var_os("CARGO_TARGET_DIR") |
| 9 | + .map(PathBuf::from) |
| 10 | + .unwrap_or_else(|| PathBuf::from("target")) |
| 11 | +} |
| 12 | + |
| 13 | +fn layout_entry<T>(name: &str) -> String { |
| 14 | + format!( |
| 15 | + r#"{{"type":"{}","size":{},"align":{}}}"#, |
| 16 | + name, |
| 17 | + size_of::<T>(), |
| 18 | + align_of::<T>() |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn layout_snapshot() { |
| 24 | + let layouts = [ |
| 25 | + layout_entry::<CheetahString>("CheetahString"), |
| 26 | + layout_entry::<Option<CheetahString>>("Option<CheetahString>"), |
| 27 | + layout_entry::<String>("String"), |
| 28 | + layout_entry::<Option<String>>("Option<String>"), |
| 29 | + layout_entry::<&str>("&str"), |
| 30 | + layout_entry::<Option<&str>>("Option<&str>"), |
| 31 | + layout_entry::<std::sync::Arc<str>>("Arc<str>"), |
| 32 | + layout_entry::<Option<std::sync::Arc<str>>>("Option<Arc<str>>"), |
| 33 | + ]; |
| 34 | + |
| 35 | + let snapshot = format!( |
| 36 | + concat!( |
| 37 | + "{{\n", |
| 38 | + " \"crate\":\"cheetah-string\",\n", |
| 39 | + " \"profile\":\"test\",\n", |
| 40 | + " \"target_arch\":\"{}\",\n", |
| 41 | + " \"target_os\":\"{}\",\n", |
| 42 | + " \"pointer_width\":\"{}\",\n", |
| 43 | + " \"layouts\":[\n {}\n ]\n", |
| 44 | + "}}\n" |
| 45 | + ), |
| 46 | + env::consts::ARCH, |
| 47 | + env::consts::OS, |
| 48 | + std::mem::size_of::<usize>() * 8, |
| 49 | + layouts.join(",\n ") |
| 50 | + ); |
| 51 | + |
| 52 | + let artifact_dir = target_dir().join("layout-artifacts"); |
| 53 | + fs::create_dir_all(&artifact_dir).expect("create layout artifact directory"); |
| 54 | + fs::write(artifact_dir.join("layout-snapshot.json"), &snapshot) |
| 55 | + .expect("write layout snapshot artifact"); |
| 56 | + |
| 57 | + println!("{snapshot}"); |
| 58 | + |
| 59 | + assert!(size_of::<CheetahString>() >= size_of::<usize>()); |
| 60 | + assert!(align_of::<CheetahString>() >= align_of::<usize>()); |
| 61 | + assert!(size_of::<Option<CheetahString>>() >= size_of::<CheetahString>()); |
| 62 | +} |
0 commit comments