|
| 1 | +#![doc(html_root_url = "https://docs.rs/wasm-bindgen-test-shared/0.2")] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +extern crate alloc; |
| 5 | + |
| 6 | +use alloc::string::{String, ToString}; |
| 7 | + |
| 8 | +pub fn coverage_path(env: Option<&str>, pid: u32, tmpdir: &str, module_signature: u64) -> String { |
| 9 | + let env = env.unwrap_or("default_%m_%p.profraw"); |
| 10 | + |
| 11 | + let mut path = String::new(); |
| 12 | + let mut chars = env.chars().enumerate().peekable(); |
| 13 | + |
| 14 | + while let Some((index, char)) = chars.next() { |
| 15 | + if char != '%' { |
| 16 | + path.push(char); |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + if chars.next_if(|(_, c)| *c == 'p').is_some() { |
| 21 | + path.push_str(&pid.to_string()) |
| 22 | + } else if chars.next_if(|(_, c)| *c == 'h').is_some() { |
| 23 | + path.push_str("wbgt") |
| 24 | + } else if chars.next_if(|(_, c)| *c == 't').is_some() { |
| 25 | + path.push_str(tmpdir) |
| 26 | + } else { |
| 27 | + let mut last_index = index; |
| 28 | + |
| 29 | + loop { |
| 30 | + if let Some((index, _)) = chars.next_if(|(_, c)| c.is_ascii_digit()) { |
| 31 | + last_index = index; |
| 32 | + } else if chars.next_if(|(_, c)| *c == 'm').is_some() { |
| 33 | + path.push_str(&module_signature.to_string()); |
| 34 | + path.push_str("_0"); |
| 35 | + break; |
| 36 | + } else { |
| 37 | + path.push_str(&env[index..=last_index]); |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + path |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn test() { |
| 49 | + fn asssert<'a>(env: impl Into<Option<&'a str>>, result: &str) { |
| 50 | + assert_eq!(coverage_path(env.into(), 123, "tmp", 456), result); |
| 51 | + } |
| 52 | + |
| 53 | + asssert(None, "default_456_0_123.profraw"); |
| 54 | + asssert("", ""); |
| 55 | + asssert("%p", "123"); |
| 56 | + asssert("%h", "wbgt"); |
| 57 | + asssert("%t", "tmp"); |
| 58 | + asssert("%m", "456_0"); |
| 59 | + asssert("%0123456789m", "456_0"); |
| 60 | + asssert("%", "%"); |
| 61 | + asssert("%%", "%%"); |
| 62 | + asssert("%a", "%a"); |
| 63 | + asssert("%0123456789", "%0123456789"); |
| 64 | + asssert("%0123456789p", "%0123456789p"); |
| 65 | + asssert("%%p", "%123"); |
| 66 | + asssert("%%%p", "%%123"); |
| 67 | + asssert("%a%p", "%a123"); |
| 68 | + asssert("%0123456789%p", "%0123456789123"); |
| 69 | + asssert("%p%", "123%"); |
| 70 | + asssert("%p%%", "123%%"); |
| 71 | + asssert("%p%a", "123%a"); |
| 72 | + asssert("%p%0123456789", "123%0123456789"); |
| 73 | + asssert("%p%0123456789p", "123%0123456789p"); |
| 74 | + asssert("%m%a", "456_0%a"); |
| 75 | + asssert("%m%0123456789", "456_0%0123456789"); |
| 76 | + asssert("%m%0123456789p", "456_0%0123456789p"); |
| 77 | + asssert("%0123456789m%a", "456_0%a"); |
| 78 | + asssert("%0123456789m%0123456789", "456_0%0123456789"); |
| 79 | + asssert("%0123456789m%0123456789p", "456_0%0123456789p"); |
| 80 | +} |
0 commit comments